From 05b9d09b232ea319e8e7a581ce71cd67d55225cf Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Fri, 4 Dec 2020 17:11:45 -0500 Subject: [PATCH 1/6] Create assertSentInOrder to test against the order of Http Requests --- src/Illuminate/Http/Client/Factory.php | 17 ++++++++++++ tests/Http/HttpClientTest.php | 36 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/Illuminate/Http/Client/Factory.php b/src/Illuminate/Http/Client/Factory.php index 7d8911605f51..49038f62ce01 100644 --- a/src/Illuminate/Http/Client/Factory.php +++ b/src/Illuminate/Http/Client/Factory.php @@ -262,6 +262,23 @@ public function assertSentCount($count) PHPUnit::assertCount($count, $this->recorded); } + /** + * Assert that requests were sent in the order specified. + * + * @param array $requestSequence + * @return void + */ + public function assertSentInOrder($requestSequence) + { + + $this->assertSentCount(count($requestSequence)); + + foreach($requestSequence as $orderPosition => $url){ + PHPUnit::assertEquals($url, $this->recorded[$orderPosition][0]->url()); + } + + } + /** * Assert that every created response sequence is empty. * diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 39cf36aadfd6..202265e0bc78 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -610,4 +610,40 @@ public function testSinkWhenStubbedByPath() $this->assertSame(json_encode(['page' => 'foo']), stream_get_contents($resource)); } + + public function testCanAssertAgainstOrderOfHttpRequests() + { + $this->factory->fake(); + + $exampleUrls = [ + 'http://example.com/1', + 'http://example.com/2', + 'http://example.com/3', + ]; + + foreach($exampleUrls as $url){ + $this->factory->get($url); + } + + $this->factory->assertSentInOrder($exampleUrls); + } + + public function testAssertionsSentOutOfOrderThrowAssertionFailed() + { + $this->factory->fake(); + + $exampleUrls = [ + 'http://example.com/1', + 'http://example.com/2', + 'http://example.com/3', + ]; + + $this->factory->get($exampleUrls[0]); + $this->factory->get($exampleUrls[2]); + $this->factory->get($exampleUrls[1]); + + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); + + $this->factory->assertSentInOrder($exampleUrls); + } } From bda91d7a8c2b431fc912892004bffb52a10d29b5 Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Fri, 4 Dec 2020 17:17:45 -0500 Subject: [PATCH 2/6] Add a test to verify that sending the wrong number of requests will fail the assertion --- tests/Http/HttpClientTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 202265e0bc78..72576db9ad42 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -646,4 +646,22 @@ public function testAssertionsSentOutOfOrderThrowAssertionFailed() $this->factory->assertSentInOrder($exampleUrls); } + + public function testWrongNumberOfRequestsThrowAssertionFailed() + { + $this->factory->fake(); + + $exampleUrls = [ + 'http://example.com/1', + 'http://example.com/2', + 'http://example.com/3', + ]; + + $this->factory->get($exampleUrls[0]); + $this->factory->get($exampleUrls[1]); + + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); + + $this->factory->assertSentInOrder($exampleUrls); + } } From ec87137391062d3a9886654adcee7bef3929b9cf Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Fri, 4 Dec 2020 17:22:49 -0500 Subject: [PATCH 3/6] Fix style issues --- src/Illuminate/Http/Client/Factory.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Http/Client/Factory.php b/src/Illuminate/Http/Client/Factory.php index 49038f62ce01..0a1d4258a798 100644 --- a/src/Illuminate/Http/Client/Factory.php +++ b/src/Illuminate/Http/Client/Factory.php @@ -264,19 +264,17 @@ public function assertSentCount($count) /** * Assert that requests were sent in the order specified. - * + * * @param array $requestSequence * @return void */ public function assertSentInOrder($requestSequence) { - $this->assertSentCount(count($requestSequence)); - foreach($requestSequence as $orderPosition => $url){ + foreach ($requestSequence as $orderPosition => $url) { PHPUnit::assertEquals($url, $this->recorded[$orderPosition][0]->url()); - } - + } } /** From c477f732c8a2cae6ee53815fdc72344f7ce81aa0 Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Fri, 4 Dec 2020 17:23:31 -0500 Subject: [PATCH 4/6] Fix style issues --- tests/Http/HttpClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 72576db9ad42..c2b0fa22767f 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -621,7 +621,7 @@ public function testCanAssertAgainstOrderOfHttpRequests() 'http://example.com/3', ]; - foreach($exampleUrls as $url){ + foreach ($exampleUrls as $url) { $this->factory->get($url); } From 99735fcf6012192bf15c6e94a2fd8fb808c5d794 Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Fri, 4 Dec 2020 17:25:01 -0500 Subject: [PATCH 5/6] Fix style issues --- src/Illuminate/Http/Client/Factory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Client/Factory.php b/src/Illuminate/Http/Client/Factory.php index 0a1d4258a798..5aea0ae95bf4 100644 --- a/src/Illuminate/Http/Client/Factory.php +++ b/src/Illuminate/Http/Client/Factory.php @@ -271,7 +271,7 @@ public function assertSentCount($count) public function assertSentInOrder($requestSequence) { $this->assertSentCount(count($requestSequence)); - + foreach ($requestSequence as $orderPosition => $url) { PHPUnit::assertEquals($url, $this->recorded[$orderPosition][0]->url()); } From af6ba0adc9316531b5d2db1a610a6db3f27405e4 Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Mon, 7 Dec 2020 11:15:08 -0500 Subject: [PATCH 6/6] Add ability to assert against callables --- src/Illuminate/Http/Client/Factory.php | 11 ++- tests/Http/HttpClientTest.php | 100 ++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Client/Factory.php b/src/Illuminate/Http/Client/Factory.php index 5aea0ae95bf4..64e18f60935c 100644 --- a/src/Illuminate/Http/Client/Factory.php +++ b/src/Illuminate/Http/Client/Factory.php @@ -273,7 +273,16 @@ public function assertSentInOrder($requestSequence) $this->assertSentCount(count($requestSequence)); foreach ($requestSequence as $orderPosition => $url) { - PHPUnit::assertEquals($url, $this->recorded[$orderPosition][0]->url()); + + $callback = $url; + if(!is_callable($url)){ + $callback = function($request) use ($url) { + return $request->url() == $url; + }; + } + + PHPUnit::assertTrue( $callback( $this->recorded[ $orderPosition ][0], $this->recorded[ $orderPosition ][1] ) ); + } } diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index c2b0fa22767f..dba304072405 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -611,7 +611,7 @@ public function testSinkWhenStubbedByPath() $this->assertSame(json_encode(['page' => 'foo']), stream_get_contents($resource)); } - public function testCanAssertAgainstOrderOfHttpRequests() + public function testCanAssertAgainstOrderOfHttpRequestsWithUrlStrings() { $this->factory->fake(); @@ -664,4 +664,102 @@ public function testWrongNumberOfRequestsThrowAssertionFailed() $this->factory->assertSentInOrder($exampleUrls); } + + public function testCanAssertAgainstOrderOfHttpRequestsWithCallables() { + $this->factory->fake(); + + $exampleUrls = [ + function($request) { + return $request->url() == 'http://example.com/1'; + }, + function($request) { + return $request->url() == 'http://example.com/2'; + }, + function($request) { + return $request->url() == 'http://example.com/3'; + }, + ]; + + $this->factory->get('http://example.com/1'); + $this->factory->get('http://example.com/2'); + $this->factory->get('http://example.com/3'); + + $this->factory->assertSentInOrder($exampleUrls); + } + + public function testCanAssertAgainstOrderOfHttpRequestsWithCallablesAndHeaders() { + $this->factory->fake(); + + $executionOrder = [ + function (Request $request) { + return $request->url() === 'http://foo.com/json' && + $request->hasHeader('Content-Type', 'application/json') && + $request->hasHeader('X-Test-Header', 'foo') && + $request->hasHeader('X-Test-ArrayHeader', ['bar', 'baz']) && + $request['name'] === 'Taylor'; + }, + function (Request $request) { + return $request->url() === 'http://bar.com/json' && + $request->hasHeader('Content-Type', 'application/json') && + $request->hasHeader('X-Test-Header', 'bar') && + $request->hasHeader('X-Test-ArrayHeader', ['bar', 'baz']) && + $request['name'] === 'Taylor'; + } + ]; + + $this->factory->withHeaders([ + 'X-Test-Header' => 'foo', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ])->post('http://foo.com/json', [ + 'name' => 'Taylor', + ]); + + $this->factory->withHeaders([ + 'X-Test-Header' => 'bar', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ])->post('http://bar.com/json', [ + 'name' => 'Taylor', + ]); + + $this->factory->assertSentInOrder($executionOrder); + } + + public function testCanAssertAgainstOrderOfHttpRequestsWithCallablesAndHeadersFailsCorrectly() { + $this->factory->fake(); + + $executionOrder = [ + function (Request $request) { + return $request->url() === 'http://bar.com/json' && + $request->hasHeader('Content-Type', 'application/json') && + $request->hasHeader('X-Test-Header', 'bar') && + $request->hasHeader('X-Test-ArrayHeader', ['bar', 'baz']) && + $request['name'] === 'Taylor'; + }, + function (Request $request) { + return $request->url() === 'http://foo.com/json' && + $request->hasHeader('Content-Type', 'application/json') && + $request->hasHeader('X-Test-Header', 'foo') && + $request->hasHeader('X-Test-ArrayHeader', ['bar', 'baz']) && + $request['name'] === 'Taylor'; + }, + ]; + + $this->factory->withHeaders([ + 'X-Test-Header' => 'foo', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ])->post('http://foo.com/json', [ + 'name' => 'Taylor', + ]); + + $this->factory->withHeaders([ + 'X-Test-Header' => 'bar', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ])->post('http://bar.com/json', [ + 'name' => 'Taylor', + ]); + + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); + + $this->factory->assertSentInOrder($executionOrder); + } }