From 09a1448b7dd8b6da56f71de3e6eb9ac5f50ecd65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Nabia=C5=82ek?= Date: Mon, 5 Feb 2018 23:48:24 +0100 Subject: [PATCH] [5.6] Fix inOrder assertions (#23038) * Fix inOrder assertions * Apply StyleCI patch --- .../Foundation/Testing/TestResponse.php | 12 +++++----- .../Foundation/FoundationTestResponseTest.php | 24 ++++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 776aececcb0c..6c1f9fd67fab 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -261,10 +261,10 @@ public function assertSee($value) */ public function assertSeeInOrder(array $values) { - $position = -1; + $position = 0; foreach ($values as $value) { - $valuePosition = mb_strpos($this->getContent(), $value); + $valuePosition = mb_strpos($this->getContent(), $value, $position); if ($valuePosition === false || $valuePosition < $position) { PHPUnit::fail( @@ -273,7 +273,7 @@ public function assertSeeInOrder(array $values) ); } - $position = $valuePosition; + $position = $valuePosition + mb_strlen($value); } return $this; @@ -300,10 +300,10 @@ public function assertSeeText($value) */ public function assertSeeTextInOrder(array $values) { - $position = -1; + $position = 0; foreach ($values as $value) { - $valuePosition = mb_strpos(strip_tags($this->getContent()), $value); + $valuePosition = mb_strpos(strip_tags($this->getContent()), $value, $position); if ($valuePosition === false || $valuePosition < $position) { PHPUnit::fail( @@ -312,7 +312,7 @@ public function assertSeeTextInOrder(array $values) ); } - $position = $valuePosition; + $position = $valuePosition + mb_strlen($value); } return $this; diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index be2fbe3bb669..6b8d80ffeba6 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -45,22 +45,26 @@ public function testAssertSeeInOrder() { $baseResponse = tap(new Response, function ($response) { $response->setContent(\Mockery::mock(View::class, [ - 'render' => '', + 'render' => '', ])); }); $response = TestResponse::fromBaseResponse($baseResponse); $response->assertSeeInOrder(['foo', 'bar', 'baz']); + $response->assertSeeInOrder(['foo', 'bar', 'baz', 'foo']); try { $response->assertSeeInOrder(['baz', 'bar', 'foo']); - $response->assertSeeInOrder(['foo', 'qux', 'bar', 'baz']); + TestCase::fail('Assertion was expected to fail.'); } catch (\PHPUnit\Framework\AssertionFailedError $e) { - return; } - TestCase::fail('Assertion was expected to fail.'); + try { + $response->assertSeeInOrder(['foo', 'qux', 'bar', 'baz']); + TestCase::fail('Assertion was expected to fail.'); + } catch (\PHPUnit\Framework\AssertionFailedError $e) { + } } public function testAssertSeeText() @@ -80,22 +84,26 @@ public function testAssertSeeTextInOrder() { $baseResponse = tap(new Response, function ($response) { $response->setContent(\Mockery::mock(View::class, [ - 'render' => 'foobar baz', + 'render' => 'foobar baz foo', ])); }); $response = TestResponse::fromBaseResponse($baseResponse); $response->assertSeeTextInOrder(['foobar', 'baz']); + $response->assertSeeTextInOrder(['foobar', 'baz', 'foo']); try { $response->assertSeeTextInOrder(['baz', 'foobar']); - $response->assertSeeTextInOrder(['foobar', 'qux', 'baz']); + TestCase::fail('Assertion was expected to fail.'); } catch (\PHPUnit\Framework\AssertionFailedError $e) { - return; } - TestCase::fail('Assertion was expected to fail.'); + try { + $response->assertSeeTextInOrder(['foobar', 'qux', 'baz']); + TestCase::fail('Assertion was expected to fail.'); + } catch (\PHPUnit\Framework\AssertionFailedError $e) { + } } public function testAssertHeader()