From 4693422642b7faa0470b5d97e879445a2299e8c2 Mon Sep 17 00:00:00 2001 From: Mikkel Paulson Date: Tue, 9 Apr 2019 14:49:00 -0400 Subject: [PATCH] Improve test coverage from #30997 Test coverage added in #30997 did a good job of validating previous behaviour, but didn't adequately cover the new callback logic. Added coverage for new methods on the Question object. --- .../Console/Tests/Question/QuestionTest.php | 75 +++++++++++++++---- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Question/QuestionTest.php b/src/Symfony/Component/Console/Tests/Question/QuestionTest.php index 537cd30144e6..13c8e362e145 100644 --- a/src/Symfony/Component/Console/Tests/Question/QuestionTest.php +++ b/src/Symfony/Component/Console/Tests/Question/QuestionTest.php @@ -60,9 +60,11 @@ public function testIsHiddenDefault() self::assertFalse($this->question->isHidden()); } - public function testSetHiddenWithAutocompleterValues() + public function testSetHiddenWithAutocompleterCallback() { - $this->question->setAutocompleterValues(['a', 'b']); + $this->question->setAutocompleterCallback( + function (string $input): array { return []; } + ); $this->expectException(\LogicException::class); $this->expectExceptionMessage( @@ -72,10 +74,12 @@ public function testSetHiddenWithAutocompleterValues() $this->question->setHidden(true); } - public function testSetHiddenWithNoAutocompleterValues() + public function testSetHiddenWithNoAutocompleterCallback() { - $this->question->setAutocompleterValues(['a', 'b']); - $this->question->setAutocompleterValues(null); + $this->question->setAutocompleterCallback( + function (string $input): array { return []; } + ); + $this->question->setAutocompleterCallback(null); $exception = null; try { @@ -154,7 +158,51 @@ public function testSetAutocompleterValuesInvalid($values) $this->question->setAutocompleterValues($values); } - public function testSetAutocompleterValuesWhenHidden() + 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); @@ -163,17 +211,21 @@ public function testSetAutocompleterValuesWhenHidden() 'A hidden question cannot use the autocompleter.' ); - $this->question->setAutocompleterValues(['a', 'b']); + $this->question->setAutocompleterCallback( + function (string $input): array { return []; } + ); } - public function testSetAutocompleterValuesWhenNotHidden() + public function testSetAutocompleterCallbackWhenNotHidden() { $this->question->setHidden(true); $this->question->setHidden(false); $exception = null; try { - $this->question->setAutocompleterValues(['a', 'b']); + $this->question->setAutocompleterCallback( + function (string $input): array { return []; } + ); } catch (\Exception $exception) { // Do nothing } @@ -181,11 +233,6 @@ public function testSetAutocompleterValuesWhenNotHidden() $this->assertNull($exception); } - public function testGetAutocompleterValuesDefault() - { - self::assertNull($this->question->getAutocompleterValues()); - } - public function providerGetSetValidator() { return [