From af6ba0adc9316531b5d2db1a610a6db3f27405e4 Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Mon, 7 Dec 2020 11:15:08 -0500 Subject: [PATCH] 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); + } }