Skip to content

Commit

Permalink
minor #31042 Improve test coverage from #30997 (MikkelPaulson)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.3-dev branch.

Discussion
----------

Improve test coverage from #30997

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes (test enhancement against change on master)
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | enhancement for #30997
| License       | MIT
| Doc PR        | N/A

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.

Commits
-------

4693422 Improve test coverage from #30997
  • Loading branch information
fabpot committed Apr 10, 2019
2 parents 4d9f5ee + 4693422 commit 2243bf5
Showing 1 changed file with 61 additions and 14 deletions.
75 changes: 61 additions & 14 deletions src/Symfony/Component/Console/Tests/Question/QuestionTest.php
Expand Up @@ -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(
Expand All @@ -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 {
Expand Down Expand Up @@ -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);

Expand All @@ -163,29 +211,28 @@ 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
}

$this->assertNull($exception);
}

public function testGetAutocompleterValuesDefault()
{
self::assertNull($this->question->getAutocompleterValues());
}

public function providerGetSetValidator()
{
return [
Expand Down

0 comments on commit 2243bf5

Please sign in to comment.