Skip to content

Commit ceb3f18

Browse files
[11.x] assertSee handle empty values (#47597)
* fail when `assertSee` is passed an empty value currently when passing an empty value to `assertSee` the test will pass. these values include: - `null` - `[]` - `''` - `['foo', '', 'bar']` rarely (IMO) is this the desired behavior, and may create false confidence in the test. this change will fail the test by default when passed empty values, but also allow users to override if appropriate. * minor formatting * minor formatting * Update TestResponse.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 6e4f04e commit ceb3f18

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/Illuminate/Testing/TestResponse.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,13 +518,21 @@ public function assertStreamedContent($value)
518518
* @param bool $escape
519519
* @return $this
520520
*/
521-
public function assertSee($value, $escape = true)
521+
public function assertSee($value, $escape = true, $allowEmptyValues = false)
522522
{
523523
$value = Arr::wrap($value);
524524

525+
if (! $allowEmptyValues && empty($value)) {
526+
PHPUnit::fail('An empty value was passed to `assertSee`.');
527+
}
528+
525529
$values = $escape ? array_map('e', $value) : $value;
526530

527531
foreach ($values as $value) {
532+
if (! $allowEmptyValues && (string) $value === '') {
533+
PHPUnit::fail('An empty value was passed to `assertSee`.');
534+
}
535+
528536
PHPUnit::assertStringContainsString((string) $value, $this->getContent());
529537
}
530538

tests/Testing/TestResponseTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,62 @@ public function testAssertSeeEscapedCanFail()
329329
$response->assertSee(['bar & baz', 'baz & qux']);
330330
}
331331

332+
public function testAssertSeeCatchesNullValue()
333+
{
334+
$this->expectException(AssertionFailedError::class);
335+
336+
$response = $this->makeMockResponse([
337+
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
338+
]);
339+
340+
$response->assertSee(null);
341+
}
342+
343+
public function testAssertSeeCatchesEmptyArray()
344+
{
345+
$this->expectException(AssertionFailedError::class);
346+
347+
$response = $this->makeMockResponse([
348+
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
349+
]);
350+
351+
$response->assertSee([]);
352+
}
353+
354+
public function testAssertSeeCatchesEmptyString()
355+
{
356+
$this->expectException(AssertionFailedError::class);
357+
358+
$response = $this->makeMockResponse([
359+
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
360+
]);
361+
362+
$response->assertSee('');
363+
}
364+
365+
public function testAssertSeeCatchesEmptyStringInArray()
366+
{
367+
$this->expectException(AssertionFailedError::class);
368+
369+
$response = $this->makeMockResponse([
370+
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
371+
]);
372+
373+
$response->assertSee(['foo', '', 'bar']);
374+
}
375+
376+
public function testAssertSeeAllowsEmptyValues()
377+
{
378+
$response = $this->makeMockResponse([
379+
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
380+
]);
381+
382+
$response->assertSee(null, allowEmptyValues: true);
383+
$response->assertSee([], allowEmptyValues: true);
384+
$response->assertSee('', allowEmptyValues: true);
385+
$response->assertSee(['foo', '', 'bar'], allowEmptyValues: true);
386+
}
387+
332388
public function testAssertSeeInOrder()
333389
{
334390
$response = $this->makeMockResponse([

0 commit comments

Comments
 (0)