Skip to content

Commit 47b7895

Browse files
author
Mikkel Paulson
committed
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.
1 parent 425c202 commit 47b7895

File tree

1 file changed

+61
-14
lines changed

1 file changed

+61
-14
lines changed

Tests/Question/QuestionTest.php

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ public function testIsHiddenDefault()
6060
self::assertFalse($this->question->isHidden());
6161
}
6262

63-
public function testSetHiddenWithAutocompleterValues()
63+
public function testSetHiddenWithAutocompleterCallback()
6464
{
65-
$this->question->setAutocompleterValues(['a', 'b']);
65+
$this->question->setAutocompleterCallback(
66+
function (string $input): array { return []; }
67+
);
6668

6769
$this->expectException(\LogicException::class);
6870
$this->expectExceptionMessage(
@@ -72,10 +74,12 @@ public function testSetHiddenWithAutocompleterValues()
7274
$this->question->setHidden(true);
7375
}
7476

75-
public function testSetHiddenWithNoAutocompleterValues()
77+
public function testSetHiddenWithNoAutocompleterCallback()
7678
{
77-
$this->question->setAutocompleterValues(['a', 'b']);
78-
$this->question->setAutocompleterValues(null);
79+
$this->question->setAutocompleterCallback(
80+
function (string $input): array { return []; }
81+
);
82+
$this->question->setAutocompleterCallback(null);
7983

8084
$exception = null;
8185
try {
@@ -154,7 +158,51 @@ public function testSetAutocompleterValuesInvalid($values)
154158
$this->question->setAutocompleterValues($values);
155159
}
156160

157-
public function testSetAutocompleterValuesWhenHidden()
161+
public function testSetAutocompleterValuesWithTraversable()
162+
{
163+
$question1 = new Question('Test question 1');
164+
$iterator1 = $this->getMockForAbstractClass(\IteratorAggregate::class);
165+
$iterator1
166+
->expects($this->once())
167+
->method('getIterator')
168+
->willReturn(new \ArrayIterator(['Potato']));
169+
$question1->setAutocompleterValues($iterator1);
170+
171+
$question2 = new Question('Test question 2');
172+
$iterator2 = $this->getMockForAbstractClass(\IteratorAggregate::class);
173+
$iterator2
174+
->expects($this->once())
175+
->method('getIterator')
176+
->willReturn(new \ArrayIterator(['Carrot']));
177+
$question2->setAutocompleterValues($iterator2);
178+
179+
// Call multiple times to verify that Traversable result is cached, and
180+
// that there is no crosstalk between cached copies.
181+
self::assertSame(['Potato'], $question1->getAutocompleterValues());
182+
self::assertSame(['Carrot'], $question2->getAutocompleterValues());
183+
self::assertSame(['Potato'], $question1->getAutocompleterValues());
184+
self::assertSame(['Carrot'], $question2->getAutocompleterValues());
185+
}
186+
187+
public function testGetAutocompleterValuesDefault()
188+
{
189+
self::assertNull($this->question->getAutocompleterValues());
190+
}
191+
192+
public function testGetSetAutocompleterCallback()
193+
{
194+
$callback = function (string $input): array { return []; };
195+
196+
$this->question->setAutocompleterCallback($callback);
197+
self::assertSame($callback, $this->question->getAutocompleterCallback());
198+
}
199+
200+
public function testGetAutocompleterCallbackDefault()
201+
{
202+
self::assertNull($this->question->getAutocompleterCallback());
203+
}
204+
205+
public function testSetAutocompleterCallbackWhenHidden()
158206
{
159207
$this->question->setHidden(true);
160208

@@ -163,29 +211,28 @@ public function testSetAutocompleterValuesWhenHidden()
163211
'A hidden question cannot use the autocompleter.'
164212
);
165213

166-
$this->question->setAutocompleterValues(['a', 'b']);
214+
$this->question->setAutocompleterCallback(
215+
function (string $input): array { return []; }
216+
);
167217
}
168218

169-
public function testSetAutocompleterValuesWhenNotHidden()
219+
public function testSetAutocompleterCallbackWhenNotHidden()
170220
{
171221
$this->question->setHidden(true);
172222
$this->question->setHidden(false);
173223

174224
$exception = null;
175225
try {
176-
$this->question->setAutocompleterValues(['a', 'b']);
226+
$this->question->setAutocompleterCallback(
227+
function (string $input): array { return []; }
228+
);
177229
} catch (\Exception $exception) {
178230
// Do nothing
179231
}
180232

181233
$this->assertNull($exception);
182234
}
183235

184-
public function testGetAutocompleterValuesDefault()
185-
{
186-
self::assertNull($this->question->getAutocompleterValues());
187-
}
188-
189236
public function providerGetSetValidator()
190237
{
191238
return [

0 commit comments

Comments
 (0)