Skip to content

Commit

Permalink
add order assertions and test coverage (#22915)
Browse files Browse the repository at this point in the history
  • Loading branch information
helmut authored and taylorotwell committed Jan 26, 2018
1 parent ce1b57e commit 75d7e07
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,32 @@ public function assertSee($value)
return $this;
}

/**
* Assert that the given strings are contained in order within the response.
*
* @param array $values
* @return $this
*/
public function assertSeeInOrder(array $values)
{
$position = -1;

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

if ($valuePosition === false || $valuePosition < $position) {
PHPUnit::fail(
'Failed asserting that \''.$this->getContent().
'\' contains "'.$value.'" in specified order.'
);
}

$position = $valuePosition;
}

return $this;
}

/**
* Assert that the given string is contained within the response text.
*
Expand All @@ -266,6 +292,32 @@ public function assertSeeText($value)
return $this;
}

/**
* Assert that the given strings are contained in order within the response text.
*
* @param array $values
* @return $this
*/
public function assertSeeTextInOrder(array $values)
{
$position = -1;

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

if ($valuePosition === false || $valuePosition < $position) {
PHPUnit::fail(
'Failed asserting that \''.strip_tags($this->getContent()).
'\' contains "'.$value.'" in specified order.'
);
}

$position = $valuePosition;
}

return $this;
}

/**
* Assert that the given string is not contained within the response.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ public function testAssertViewHas()
$response->assertViewHas('foo');
}

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>',
]));
});

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

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

try {
$response->assertSeeInOrder(['baz', 'bar', 'foo']);
$response->assertSeeInOrder(['foo', 'qux', 'bar', 'baz']);
} catch (\PHPUnit\Framework\AssertionFailedError $e) {
return;
}

TestCase::fail('Assertion was expected to fail.');
}

public function testAssertSeeText()
{
$baseResponse = tap(new Response, function ($response) {
Expand All @@ -54,6 +76,28 @@ public function testAssertSeeText()
$response->assertSeeText('foobar');
}

public function testAssertSeeTextInOrder()
{
$baseResponse = tap(new Response, function ($response) {
$response->setContent(\Mockery::mock(View::class, [
'render' => 'foo<strong>bar</strong> baz',
]));
});

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

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

try {
$response->assertSeeTextInOrder(['baz', 'foobar']);
$response->assertSeeTextInOrder(['foobar', 'qux', 'baz']);
} catch (\PHPUnit\Framework\AssertionFailedError $e) {
return;
}

TestCase::fail('Assertion was expected to fail.');
}

public function testAssertHeader()
{
$baseResponse = tap(new Response, function ($response) {
Expand Down

0 comments on commit 75d7e07

Please sign in to comment.