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 #23592 (SeeInOrder) #23594

Merged
merged 6 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
85 changes: 85 additions & 0 deletions src/Illuminate/Foundation/Testing/Constraints/SeeInOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Illuminate\Foundation\Testing\Constraints;

use PHPUnit\Framework\Constraint\Constraint;

class SeeInOrder extends Constraint
{
/**
* The value that failed. Used to display in the error message.
*
* @var string
*/
protected $failedValue;

/**
* The string we want to check the content of.
*
* @var
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing var type.

Strange that Style CI didn't cached it 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a code style bug, that's why. It's just a syntax error.

*/
protected $content;

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

/**
* Check if the data is found in the given table.
*
* @param array $values
* @return bool
*/
public function matches($values) : bool
{
$position = 0;

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

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

return false;
}

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

return true;
}

/**
* Get the description of the failure.
*
* @param array $values
* @return string
*/
public function failureDescription($values) : string
{
return sprintf(
'Failed asserting that \'%s\' contains "%s" in specified order.',
$this->content,
$this->failedValue
);
}

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

return $class->name;
}
}
31 changes: 3 additions & 28 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Contracts\View\View;
use Illuminate\Support\Traits\Macroable;
use PHPUnit\Framework\Assert as PHPUnit;
use Illuminate\Foundation\Testing\Constraints\SeeInOrder;

/**
* @mixin \Illuminate\Http\Response
Expand Down Expand Up @@ -293,20 +294,7 @@ public function assertSee($value)
*/
public function assertSeeInOrder(array $values)
{
$position = 0;

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

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

$position = $valuePosition + mb_strlen($value);
}
PHPUnit::assertThat($values, new SeeInOrder($this->getContent()));

return $this;
}
Expand All @@ -332,20 +320,7 @@ public function assertSeeText($value)
*/
public function assertSeeTextInOrder(array $values)
{
$position = 0;

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

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

$position = $valuePosition + mb_strlen($value);
}
PHPUnit::assertThat($values, new SeeInOrder(strip_tags($this->getContent())));

return $this;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Foundation/Testing/Constraints/SeeInOrderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Illuminate\Tests\Foundation\Constraints;

use PHPUnit\Framework\TestCase;
use Illuminate\Tests\Foundation\FoundationTestResponseTest;

class SeeInOrderTest extends TestCase
{
public function testAllSeeInOrderAssertionsDetected()
{
$test = new FoundationTestResponseTest('testAssertSeeInOrder');
$test->run();
// If we get four assertions, that means all of the assertions
// were detected by PHPUnit, which is what we want to know.
// Doing it this way allows the test to work even though
// the global settings don't check for risky tests.
$this->assertEquals($test->getNumAssertions(), 4);
}

public function testAllSeeTextInOrderAssertionsDetected()
{
$test = new FoundationTestResponseTest('testAssertSeeTextInOrder');
$test->run();
// If we get four assertions, that means all of the assertions
// were detected by PHPUnit, which is what we want to know.
// Doing it this way allows the test to work even though
// the global settings don't check for risky tests.
$this->assertEquals($test->getNumAssertions(), 4);
}
}