diff --git a/src/Illuminate/Foundation/Testing/Constraints/SeeInOrder.php b/src/Illuminate/Foundation/Testing/Constraints/SeeInOrder.php new file mode 100644 index 000000000000..f81386b00455 --- /dev/null +++ b/src/Illuminate/Foundation/Testing/Constraints/SeeInOrder.php @@ -0,0 +1,85 @@ +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; + } +} diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 15d9655cfb48..51cf2e21399d 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -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 @@ -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; } @@ -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; } diff --git a/tests/Foundation/Testing/Constraints/SeeInOrderTest.php b/tests/Foundation/Testing/Constraints/SeeInOrderTest.php new file mode 100644 index 000000000000..ae1a01b4b779 --- /dev/null +++ b/tests/Foundation/Testing/Constraints/SeeInOrderTest.php @@ -0,0 +1,31 @@ +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); + } +}