Skip to content

Commit

Permalink
[5.6] Fix inOrder assertions (#23038)
Browse files Browse the repository at this point in the history
* Fix inOrder assertions

* Apply StyleCI patch
  • Loading branch information
mnabialek authored and taylorotwell committed Feb 5, 2018
1 parent 457f58a commit 09a1448
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
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

0 comments on commit 09a1448

Please sign in to comment.