QuestionTest.php 8.6 KB

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