Skip to content

Improve test coverage from #30997 #31042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 61 additions & 14 deletions src/Symfony/Component/Console/Tests/Question/QuestionTest.php
Original file line number Diff line number Diff line change
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