Skip to content
Open
Show file tree
Hide file tree
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
138 changes: 138 additions & 0 deletions src/Illuminate/Testing/Constraints/SeeInHtml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace Illuminate\Testing\Constraints;

use PHPUnit\Framework\Constraint\Constraint;
use ReflectionClass;

class SeeInHtml extends Constraint
{
/**
* The string under validation.
*
* @var string
*/
protected $content;

/**
* The last value that failed to pass validation.
*
* @var string
*/
protected $failedValue;

/**
* Whether to negate the assertion.
*
* @var bool
*/
protected $negate;

/**
* The values must appear in order.
*
* @var bool
*/
protected $ordered;

/**
* Create a new constraint instance.
*
* @param string $content
*/
public function __construct($content, $ordered = false, $negate = false)
{
$this->content = $content;
$this->ordered = $ordered;
$this->negate = $negate;
}

/**
* Determine if the rule passes validation.
*
* @param array $values
* @return bool
*/
public function matches($values): bool
{
$normalizedContent = $this->normalize($this->content);

$position = 0;

foreach ($values as $value) {
if (empty($value)) {
continue;
}

$normalizedValue = $this->normalize($value);

$valuePosition = mb_strpos($normalizedContent, $normalizedValue, $position);

if ($this->negate) {
if ($valuePosition !== false) {
$this->failedValue = $value;

return false;
}

continue;
}

if ($valuePosition === false || $valuePosition < $position) {
$this->failedValue = $value;

return false;
}

if ($this->ordered) {
$position = $valuePosition + mb_strlen($normalizedValue);
}
}

return true;
}

/**
* Get the description of the failure.
*
* @param array $values
* @return string
*/
public function failureDescription($values): string
{
if ($this->negate) {
return sprintf(
'\'%s\' does not contain "%s".',
$this->content,
$this->failedValue
);
}

return sprintf(
'\'%s\' contains "%s"%s',
$this->content,
$this->failedValue,
$this->ordered ? ' in specified order.' : '.'
);
}

/**
* Get a string representation of the object.
*
* @return string
*/
public function toString(): string
{
return (new ReflectionClass($this))->name;
}

protected function normalize(string $value): ?string
{
$value = strip_tags($value);
$value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
$value = trim($value);
$value = preg_replace('/\s+/', ' ', $value);

return $value;
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Testing/Constraints/SeeInOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function matches($values): bool
public function failureDescription($values): string
{
return sprintf(
'Failed asserting that \'%s\' contains "%s" in specified order.',
'\'%s\' contains "%s" in specified order.',
$this->content,
$this->failedValue
);
Expand Down
9 changes: 3 additions & 6 deletions src/Illuminate/Testing/TestComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Testing\Assert as PHPUnit;
use Illuminate\Testing\Constraints\SeeInHtml;
use Illuminate\Testing\Constraints\SeeInOrder;
use Stringable;

Expand Down Expand Up @@ -112,11 +113,7 @@ public function assertSeeText($value, $escape = true)

$values = $escape ? array_map(e(...), $value) : $value;

$rendered = strip_tags($this->rendered);

foreach ($values as $value) {
PHPUnit::assertStringContainsString((string) $value, $rendered);
}
PHPUnit::assertThat($values, new SeeInHtml($this->rendered));

return $this;
}
Expand All @@ -132,7 +129,7 @@ public function assertSeeTextInOrder(array $values, $escape = true)
{
$values = $escape ? array_map(e(...), $values) : $values;

PHPUnit::assertThat($values, new SeeInOrder(strip_tags($this->rendered)));
PHPUnit::assertThat($values, new SeeInHtml($this->rendered, true));

return $this;
}
Expand Down
15 changes: 4 additions & 11 deletions src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\Tappable;
use Illuminate\Support\ViewErrorBag;
use Illuminate\Testing\Constraints\SeeInHtml;
use Illuminate\Testing\Constraints\SeeInOrder;
use Illuminate\Testing\Fluent\AssertableJson;
use Illuminate\Testing\TestResponseAssert as PHPUnit;
Expand Down Expand Up @@ -764,11 +765,7 @@ public function assertSeeText($value, $escape = true)

$values = $escape ? array_map(e(...), $value) : $value;

$content = strip_tags($this->getContent());

foreach ($values as $value) {
PHPUnit::withResponse($this)->assertStringContainsString((string) $value, $content);
}
PHPUnit::withResponse($this)->assertThat($values, new SeeInHtml($this->getContent()));

return $this;
}
Expand All @@ -784,7 +781,7 @@ public function assertSeeTextInOrder(array $values, $escape = true)
{
$values = $escape ? array_map(e(...), $values) : $values;

PHPUnit::withResponse($this)->assertThat($values, new SeeInOrder(strip_tags($this->getContent())));
PHPUnit::withResponse($this)->assertThat($values, new SeeInHtml($this->getContent(), true));

return $this;
}
Expand Down Expand Up @@ -833,11 +830,7 @@ public function assertDontSeeText($value, $escape = true)

$values = $escape ? array_map(e(...), $value) : $value;

$content = strip_tags($this->getContent());

foreach ($values as $value) {
PHPUnit::withResponse($this)->assertStringNotContainsString((string) $value, $content);
}
PHPUnit::withResponse($this)->assertThat($values, new SeeInHtml($this->getContent(), negate: true));

return $this;
}
Expand Down
15 changes: 4 additions & 11 deletions src/Illuminate/Testing/TestView.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Testing\Assert as PHPUnit;
use Illuminate\Testing\Constraints\SeeInHtml;
use Illuminate\Testing\Constraints\SeeInOrder;
use Illuminate\View\View;
use Stringable;
Expand Down Expand Up @@ -189,11 +190,7 @@ public function assertSeeText($value, $escape = true)

$values = $escape ? array_map(e(...), $value) : $value;

$rendered = strip_tags($this->rendered);

foreach ($values as $value) {
PHPUnit::assertStringContainsString((string) $value, $rendered);
}
PHPUnit::assertThat($values, new SeeInHtml($this->rendered));

return $this;
}
Expand All @@ -209,7 +206,7 @@ public function assertSeeTextInOrder(array $values, $escape = true)
{
$values = $escape ? array_map(e(...), $values) : $values;

PHPUnit::assertThat($values, new SeeInOrder(strip_tags($this->rendered)));
PHPUnit::assertThat($values, new SeeInHtml($this->rendered, true));

return $this;
}
Expand Down Expand Up @@ -258,11 +255,7 @@ public function assertDontSeeText($value, $escape = true)

$values = $escape ? array_map(e(...), $value) : $value;

$rendered = strip_tags($this->rendered);

foreach ($values as $value) {
PHPUnit::assertStringNotContainsString((string) $value, $rendered);
}
PHPUnit::assertThat($values, new SeeInHtml($this->rendered));

return $this;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ public function testAssertSeeText(): void
public function testAssertSeeTextCanFail(): void
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that \'foo<strong>bar</strong>\' contains "bazfoo".');

$response = $this->makeMockResponse([
'render' => 'foo<strong>bar</strong>',
Expand All @@ -613,6 +614,20 @@ public function testAssertSeeTextEscaped(): void
$response->assertSeeText(['php & friends', 'laravel & php']);
}

public function testAssertSeeTextWhitespace(): void
{
$response = $this->makeMockResponse([
'render' => <<<'EOT'
<p>
Hello,
laravel &amp; php &amp; friends
</p>,
EOT
]);

$response->assertSeeText('Hello, laravel & php & friends');
}

public function testAssertSeeTextEscapedCanFail(): void
{
$this->expectException(AssertionFailedError::class);
Expand Down Expand Up @@ -645,9 +660,24 @@ public function testAssertSeeTextInOrderEscaped(): void
$response->assertSeeTextInOrder(['laravel & php', 'phpstorm > sublime']);
}

public function testAssertSeeTextInOrderWhitespace(): void
{
$response = $this->makeMockResponse([
'render' => <<<'EOT'
<p>
Hello,
laravel &amp; php &amp; friends
</p>,
EOT
]);

$response->assertSeeTextInOrder(['Hello', 'laravel & php', 'friends']);
}

public function testAssertSeeTextInOrderCanFail(): void
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that \'foo<strong>bar</strong> baz <strong>foo</strong>\' contains "foobar" in specified order.');

$response = $this->makeMockResponse([
'render' => 'foo<strong>bar</strong> baz <strong>foo</strong>',
Expand Down Expand Up @@ -746,6 +776,7 @@ public function testAssertDontSeeText(): void
public function testAssertDontSeeTextCanFail(): void
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that \'foo<strong>bar</strong>baz<strong>qux</strong>\' does not contain "foobar".');

$response = $this->makeMockResponse([
'render' => 'foo<strong>bar</strong>baz<strong>qux</strong>',
Expand Down
Loading