Skip to content

Commit

Permalink
Merge branch 'bugfix/see-in-order' of https://github.com/Jantho1990/l…
Browse files Browse the repository at this point in the history
…aravel-framework into Jantho1990-bugfix/see-in-order
  • Loading branch information
taylorotwell committed Mar 19, 2018
2 parents 87838b5 + 533f468 commit 92f5548
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 28 deletions.
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 string
*/
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);
}
}

0 comments on commit 92f5548

Please sign in to comment.