QuestionTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Question;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Exception\InvalidArgumentException;
  13. use Symfony\Component\Console\Question\Question;
  14. class QuestionTest extends TestCase
  15. {
  16. private $question;
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $this->question = new Question('Test question');
  21. }
  22. public function providerTrueFalse()
  23. {
  24. return [[true], [false]];
  25. }
  26. public function testGetQuestion()
  27. {
  28. self::assertSame('Test question', $this->question->getQuestion());
  29. }
  30. public function testGetDefault()
  31. {
  32. $question = new Question('Test question', 'Default value');
  33. self::assertSame('Default value', $question->getDefault());
  34. }
  35. public function testGetDefaultDefault()
  36. {
  37. self::assertNull($this->question->getDefault());
  38. }
  39. /**
  40. * @dataProvider providerTrueFalse
  41. */
  42. public function testIsSetHidden(bool $hidden)
  43. {
  44. $this->question->setHidden($hidden);
  45. self::assertSame($hidden, $this->question->isHidden());
  46. }
  47. public function testIsHiddenDefault()
  48. {
  49. self::assertFalse($this->question->isHidden());
  50. }
  51. public function testSetHiddenWithAutocompleterCallback()
  52. {
  53. $this->question->setAutocompleterCallback(
  54. function (string $input): array { return []; }
  55. );
  56. $this->expectException(\LogicException::class);
  57. $this->expectExceptionMessage(
  58. 'A hidden question cannot use the autocompleter.'
  59. );
  60. $this->question->setHidden(true);
  61. }
  62. public function testSetHiddenWithNoAutocompleterCallback()
  63. {
  64. $this->question->setAutocompleterCallback(
  65. function (string $input): array { return []; }
  66. );
  67. $this->question->setAutocompleterCallback(null);
  68. $exception = null;
  69. try {
  70. $this->question->setHidden(true);
  71. } catch (\Exception $exception) {
  72. // Do nothing
  73. }
  74. $this->assertNull($exception);
  75. }
  76. /**
  77. * @dataProvider providerTrueFalse
  78. */
  79. public function testIsSetHiddenFallback(bool $hidden)
  80. {
  81. $this->question->setHiddenFallback($hidden);
  82. self::assertSame($hidden, $this->question->isHiddenFallback());
  83. }
  84. public function testIsHiddenFallbackDefault()
  85. {
  86. self::assertTrue($this->question->isHiddenFallback());
  87. }
  88. public function providerGetSetAutocompleterValues()
  89. {
  90. return [
  91. 'array' => [
  92. ['a', 'b', 'c', 'd'],
  93. ['a', 'b', 'c', 'd'],
  94. ],
  95. 'associative array' => [
  96. ['a' => 'c', 'b' => 'd'],
  97. ['a', 'b', 'c', 'd'],
  98. ],
  99. 'iterator' => [
  100. new \ArrayIterator(['a', 'b', 'c', 'd']),
  101. ['a', 'b', 'c', 'd'],
  102. ],
  103. 'null' => [null, null],
  104. ];
  105. }
  106. /**
  107. * @dataProvider providerGetSetAutocompleterValues
  108. */
  109. public function testGetSetAutocompleterValues($values, $expectValues)
  110. {
  111. $this->question->setAutocompleterValues($values);
  112. self::assertSame(
  113. $expectValues,
  114. $this->question->getAutocompleterValues()
  115. );
  116. }
  117. public function providerSetAutocompleterValuesInvalid()
  118. {
  119. return [
  120. ['Potato'],
  121. [new \stdclass()],
  122. [false],
  123. ];
  124. }
  125. /**
  126. * @dataProvider providerSetAutocompleterValuesInvalid
  127. */
  128. public function testSetAutocompleterValuesInvalid($values)
  129. {
  130. self::expectException(InvalidArgumentException::class);
  131. self::expectExceptionMessage(
  132. 'Autocompleter values can be either an array, "null" or a "Traversable" object.'
  133. );
  134. $this->question->setAutocompleterValues($values);
  135. }
  136. public function testSetAutocompleterValuesWithTraversable()
  137. {
  138. $question1 = new Question('Test question 1');
  139. $iterator1 = $this->getMockForAbstractClass(\IteratorAggregate::class);
  140. $iterator1
  141. ->expects($this->once())
  142. ->method('getIterator')
  143. ->willReturn(new \ArrayIterator(['Potato']));
  144. $question1->setAutocompleterValues($iterator1);
  145. $question2 = new Question('Test question 2');
  146. $iterator2 = $this->getMockForAbstractClass(\IteratorAggregate::class);
  147. $iterator2
  148. ->expects($this->once())
  149. ->method('getIterator')
  150. ->willReturn(new \ArrayIterator(['Carrot']));
  151. $question2->setAutocompleterValues($iterator2);
  152. // Call multiple times to verify that Traversable result is cached, and
  153. // that there is no crosstalk between cached copies.
  154. self::assertSame(['Potato'], $question1->getAutocompleterValues());
  155. self::assertSame(['Carrot'], $question2->getAutocompleterValues());
  156. self::assertSame(['Potato'], $question1->getAutocompleterValues());
  157. self::assertSame(['Carrot'], $question2->getAutocompleterValues());
  158. }
  159. public function testGetAutocompleterValuesDefault()
  160. {
  161. self::assertNull($this->question->getAutocompleterValues());
  162. }
  163. public function testGetSetAutocompleterCallback()
  164. {
  165. $callback = function (string $input): array { return []; };
  166. $this->question->setAutocompleterCallback($callback);
  167. self::assertSame($callback, $this->question->getAutocompleterCallback());
  168. }
  169. public function testGetAutocompleterCallbackDefault()
  170. {
  171. self::assertNull($this->question->getAutocompleterCallback());
  172. }
  173. public function testSetAutocompleterCallbackWhenHidden()
  174. {
  175. $this->question->setHidden(true);
  176. $this->expectException(\LogicException::class);
  177. $this->expectExceptionMessage(
  178. 'A hidden question cannot use the autocompleter.'
  179. );
  180. $this->question->setAutocompleterCallback(
  181. function (string $input): array { return []; }
  182. );
  183. }
  184. public function testSetAutocompleterCallbackWhenNotHidden()
  185. {
  186. $this->question->setHidden(true);
  187. $this->question->setHidden(false);
  188. $exception = null;
  189. try {
  190. $this->question->setAutocompleterCallback(
  191. function (string $input): array { return []; }
  192. );
  193. } catch (\Exception $exception) {
  194. // Do nothing
  195. }
  196. $this->assertNull($exception);
  197. }
  198. public function providerGetSetValidator()
  199. {
  200. return [
  201. [function ($input) { return $input; }],
  202. [null],
  203. ];
  204. }
  205. /**
  206. * @dataProvider providerGetSetValidator
  207. */
  208. public function testGetSetValidator($callback)
  209. {
  210. $this->question->setValidator($callback);
  211. self::assertSame($callback, $this->question->getValidator());
  212. }
  213. public function testGetValidatorDefault()
  214. {
  215. self::assertNull($this->question->getValidator());
  216. }
  217. public function providerGetSetMaxAttempts()
  218. {
  219. return [[1], [5], [null]];
  220. }
  221. /**
  222. * @dataProvider providerGetSetMaxAttempts
  223. */
  224. public function testGetSetMaxAttempts($attempts)
  225. {
  226. $this->question->setMaxAttempts($attempts);
  227. self::assertSame($attempts, $this->question->getMaxAttempts());
  228. }
  229. public function providerSetMaxAttemptsInvalid()
  230. {
  231. return [['Potato'], [0], [-1]];
  232. }
  233. /**
  234. * @dataProvider providerSetMaxAttemptsInvalid
  235. */
  236. public function testSetMaxAttemptsInvalid($attempts)
  237. {
  238. self::expectException(\InvalidArgumentException::class);
  239. self::expectExceptionMessage('Maximum number of attempts must be a positive value.');
  240. $this->question->setMaxAttempts($attempts);
  241. }
  242. public function testGetMaxAttemptsDefault()
  243. {
  244. self::assertNull($this->question->getMaxAttempts());
  245. }
  246. public function testGetSetNormalizer()
  247. {
  248. $normalizer = function ($input) { return $input; };
  249. $this->question->setNormalizer($normalizer);
  250. self::assertSame($normalizer, $this->question->getNormalizer());
  251. }
  252. public function testGetNormalizerDefault()
  253. {
  254. self::assertNull($this->question->getNormalizer());
  255. }
  256. }