123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Console\Tests\Question;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\Console\Exception\InvalidArgumentException;
- use Symfony\Component\Console\Question\Question;
- class QuestionTest extends TestCase
- {
- private $question;
- protected function setUp()
- {
- parent::setUp();
- $this->question = new Question('Test question');
- }
- public function providerTrueFalse()
- {
- return [[true], [false]];
- }
- public function testGetQuestion()
- {
- self::assertSame('Test question', $this->question->getQuestion());
- }
- public function testGetDefault()
- {
- $question = new Question('Test question', 'Default value');
- self::assertSame('Default value', $question->getDefault());
- }
- public function testGetDefaultDefault()
- {
- self::assertNull($this->question->getDefault());
- }
- /**
- * @dataProvider providerTrueFalse
- */
- public function testIsSetHidden(bool $hidden)
- {
- $this->question->setHidden($hidden);
- self::assertSame($hidden, $this->question->isHidden());
- }
- public function testIsHiddenDefault()
- {
- self::assertFalse($this->question->isHidden());
- }
- public function testSetHiddenWithAutocompleterCallback()
- {
- $this->question->setAutocompleterCallback(
- function (string $input): array { return []; }
- );
- $this->expectException(\LogicException::class);
- $this->expectExceptionMessage(
- 'A hidden question cannot use the autocompleter.'
- );
- $this->question->setHidden(true);
- }
- public function testSetHiddenWithNoAutocompleterCallback()
- {
- $this->question->setAutocompleterCallback(
- function (string $input): array { return []; }
- );
- $this->question->setAutocompleterCallback(null);
- $exception = null;
- try {
- $this->question->setHidden(true);
- } catch (\Exception $exception) {
- // Do nothing
- }
- $this->assertNull($exception);
- }
- /**
- * @dataProvider providerTrueFalse
- */
- public function testIsSetHiddenFallback(bool $hidden)
- {
- $this->question->setHiddenFallback($hidden);
- self::assertSame($hidden, $this->question->isHiddenFallback());
- }
- public function testIsHiddenFallbackDefault()
- {
- self::assertTrue($this->question->isHiddenFallback());
- }
- public function providerGetSetAutocompleterValues()
- {
- return [
- 'array' => [
- ['a', 'b', 'c', 'd'],
- ['a', 'b', 'c', 'd'],
- ],
- 'associative array' => [
- ['a' => 'c', 'b' => 'd'],
- ['a', 'b', 'c', 'd'],
- ],
- 'iterator' => [
- new \ArrayIterator(['a', 'b', 'c', 'd']),
- ['a', 'b', 'c', 'd'],
- ],
- 'null' => [null, null],
- ];
- }
- /**
- * @dataProvider providerGetSetAutocompleterValues
- */
- public function testGetSetAutocompleterValues($values, $expectValues)
- {
- $this->question->setAutocompleterValues($values);
- self::assertSame(
- $expectValues,
- $this->question->getAutocompleterValues()
- );
- }
- public function providerSetAutocompleterValuesInvalid()
- {
- return [
- ['Potato'],
- [new \stdclass()],
- [false],
- ];
- }
- /**
- * @dataProvider providerSetAutocompleterValuesInvalid
- */
- public function testSetAutocompleterValuesInvalid($values)
- {
- self::expectException(InvalidArgumentException::class);
- self::expectExceptionMessage(
- 'Autocompleter values can be either an array, "null" or a "Traversable" object.'
- );
- $this->question->setAutocompleterValues($values);
- }
- public function testSetAutocompleterValuesWithTraversable()
- {
- $question1 = new Question('Test question 1');
- $iterator1 = $this->getMockForAbstractClass(\IteratorAggregate::class);
- $iterator1
- ->expects($this->once())
- ->method('getIterator')
- ->willReturn(new \ArrayIterator(['Potato']));
- $question1->setAutocompleterValues($iterator1);
- $question2 = new Question('Test question 2');
- $iterator2 = $this->getMockForAbstractClass(\IteratorAggregate::class);
- $iterator2
- ->expects($this->once())
- ->method('getIterator')
- ->willReturn(new \ArrayIterator(['Carrot']));
- $question2->setAutocompleterValues($iterator2);
- // Call multiple times to verify that Traversable result is cached, and
- // that there is no crosstalk between cached copies.
- self::assertSame(['Potato'], $question1->getAutocompleterValues());
- self::assertSame(['Carrot'], $question2->getAutocompleterValues());
- self::assertSame(['Potato'], $question1->getAutocompleterValues());
- self::assertSame(['Carrot'], $question2->getAutocompleterValues());
- }
- public function testGetAutocompleterValuesDefault()
- {
- self::assertNull($this->question->getAutocompleterValues());
- }
- public function testGetSetAutocompleterCallback()
- {
- $callback = function (string $input): array { return []; };
- $this->question->setAutocompleterCallback($callback);
- self::assertSame($callback, $this->question->getAutocompleterCallback());
- }
- public function testGetAutocompleterCallbackDefault()
- {
- self::assertNull($this->question->getAutocompleterCallback());
- }
- public function testSetAutocompleterCallbackWhenHidden()
- {
- $this->question->setHidden(true);
- $this->expectException(\LogicException::class);
- $this->expectExceptionMessage(
- 'A hidden question cannot use the autocompleter.'
- );
- $this->question->setAutocompleterCallback(
- function (string $input): array { return []; }
- );
- }
- public function testSetAutocompleterCallbackWhenNotHidden()
- {
- $this->question->setHidden(true);
- $this->question->setHidden(false);
- $exception = null;
- try {
- $this->question->setAutocompleterCallback(
- function (string $input): array { return []; }
- );
- } catch (\Exception $exception) {
- // Do nothing
- }
- $this->assertNull($exception);
- }
- public function providerGetSetValidator()
- {
- return [
- [function ($input) { return $input; }],
- [null],
- ];
- }
- /**
- * @dataProvider providerGetSetValidator
- */
- public function testGetSetValidator($callback)
- {
- $this->question->setValidator($callback);
- self::assertSame($callback, $this->question->getValidator());
- }
- public function testGetValidatorDefault()
- {
- self::assertNull($this->question->getValidator());
- }
- public function providerGetSetMaxAttempts()
- {
- return [[1], [5], [null]];
- }
- /**
- * @dataProvider providerGetSetMaxAttempts
- */
- public function testGetSetMaxAttempts($attempts)
- {
- $this->question->setMaxAttempts($attempts);
- self::assertSame($attempts, $this->question->getMaxAttempts());
- }
- public function providerSetMaxAttemptsInvalid()
- {
- return [['Potato'], [0], [-1]];
- }
- /**
- * @dataProvider providerSetMaxAttemptsInvalid
- */
- public function testSetMaxAttemptsInvalid($attempts)
- {
- self::expectException(\InvalidArgumentException::class);
- self::expectExceptionMessage('Maximum number of attempts must be a positive value.');
- $this->question->setMaxAttempts($attempts);
- }
- public function testGetMaxAttemptsDefault()
- {
- self::assertNull($this->question->getMaxAttempts());
- }
- public function testGetSetNormalizer()
- {
- $normalizer = function ($input) { return $input; };
- $this->question->setNormalizer($normalizer);
- self::assertSame($normalizer, $this->question->getNormalizer());
- }
- public function testGetNormalizerDefault()
- {
- self::assertNull($this->question->getNormalizer());
- }
- }
|