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] Assert that response contains strings in a specific order #22915

Merged
merged 1 commit into from
Jan 26, 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
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