-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'bugfix/see-in-order' of https://github.com/Jantho1990/l…
…aravel-framework into Jantho1990-bugfix/see-in-order
- Loading branch information
Showing
3 changed files
with
119 additions
and
28 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/Illuminate/Foundation/Testing/Constraints/SeeInOrder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |