Skip to content
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

[5.6] Fix inOrder assertions #23038

Merged
merged 2 commits into from
Feb 5, 2018
Merged
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
12 changes: 6 additions & 6 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ public function assertSee($value)
*/
public function assertSeeInOrder(array $values)
{
$position = -1;
$position = 0;

foreach ($values as $value) {
$valuePosition = mb_strpos($this->getContent(), $value);
$valuePosition = mb_strpos($this->getContent(), $value, $position);

if ($valuePosition === false || $valuePosition < $position) {
PHPUnit::fail(
Expand All @@ -273,7 +273,7 @@ public function assertSeeInOrder(array $values)
);
}

$position = $valuePosition;
$position = $valuePosition + mb_strlen($value);
}

return $this;
Expand All @@ -300,10 +300,10 @@ public function assertSeeText($value)
*/
public function assertSeeTextInOrder(array $values)
{
$position = -1;
$position = 0;

foreach ($values as $value) {
$valuePosition = mb_strpos(strip_tags($this->getContent()), $value);
$valuePosition = mb_strpos(strip_tags($this->getContent()), $value, $position);

if ($valuePosition === false || $valuePosition < $position) {
PHPUnit::fail(
Expand All @@ -312,7 +312,7 @@ public function assertSeeTextInOrder(array $values)
);
}

$position = $valuePosition;
$position = $valuePosition + mb_strlen($value);
}

return $this;
Expand Down
24 changes: 16 additions & 8 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,26 @@ public function testAssertSeeInOrder()
{
$baseResponse = tap(new Response, function ($response) {
$response->setContent(\Mockery::mock(View::class, [
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li></ul>',
'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
]));
});

$response = TestResponse::fromBaseResponse($baseResponse);

$response->assertSeeInOrder(['foo', 'bar', 'baz']);
$response->assertSeeInOrder(['foo', 'bar', 'baz', 'foo']);

try {
$response->assertSeeInOrder(['baz', 'bar', 'foo']);
$response->assertSeeInOrder(['foo', 'qux', 'bar', 'baz']);
TestCase::fail('Assertion was expected to fail.');
} catch (\PHPUnit\Framework\AssertionFailedError $e) {
return;
}

TestCase::fail('Assertion was expected to fail.');
try {
$response->assertSeeInOrder(['foo', 'qux', 'bar', 'baz']);
TestCase::fail('Assertion was expected to fail.');
} catch (\PHPUnit\Framework\AssertionFailedError $e) {
}
}

public function testAssertSeeText()
Expand All @@ -80,22 +84,26 @@ public function testAssertSeeTextInOrder()
{
$baseResponse = tap(new Response, function ($response) {
$response->setContent(\Mockery::mock(View::class, [
'render' => 'foo<strong>bar</strong> baz',
'render' => 'foo<strong>bar</strong> baz <strong>foo</strong>',
]));
});

$response = TestResponse::fromBaseResponse($baseResponse);

$response->assertSeeTextInOrder(['foobar', 'baz']);
$response->assertSeeTextInOrder(['foobar', 'baz', 'foo']);

try {
$response->assertSeeTextInOrder(['baz', 'foobar']);
$response->assertSeeTextInOrder(['foobar', 'qux', 'baz']);
TestCase::fail('Assertion was expected to fail.');
} catch (\PHPUnit\Framework\AssertionFailedError $e) {
return;
}

TestCase::fail('Assertion was expected to fail.');
try {
$response->assertSeeTextInOrder(['foobar', 'qux', 'baz']);
TestCase::fail('Assertion was expected to fail.');
} catch (\PHPUnit\Framework\AssertionFailedError $e) {
}
}

public function testAssertHeader()
Expand Down