diff --git a/src/Illuminate/Http/Client/Factory.php b/src/Illuminate/Http/Client/Factory.php index 7d8911605f51..64e18f60935c 100644 --- a/src/Illuminate/Http/Client/Factory.php +++ b/src/Illuminate/Http/Client/Factory.php @@ -262,6 +262,30 @@ 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) { + + $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] ) ); + + } + } + /** * Assert that every created response sequence is empty. * diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 39cf36aadfd6..dba304072405 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -610,4 +610,156 @@ public function testSinkWhenStubbedByPath() $this->assertSame(json_encode(['page' => 'foo']), stream_get_contents($resource)); } + + public function testCanAssertAgainstOrderOfHttpRequestsWithUrlStrings() + { + $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); + } + + 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); + } + + 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); + } }