From 900ea0cd01044591c39648d3757a0cd4f9d062c1 Mon Sep 17 00:00:00 2001 From: luke Date: Sat, 20 Feb 2021 16:01:07 +0000 Subject: [PATCH 1/5] Adds a `collect` method to the HTTP client response. --- src/Illuminate/Http/Client/Response.php | 11 + tests/Http/HttpClientTest.php | 637 +++++++++++++++--------- 2 files changed, 400 insertions(+), 248 deletions(-) diff --git a/src/Illuminate/Http/Client/Response.php b/src/Illuminate/Http/Client/Response.php index f65a8d5ca1ba..12e98304ecb6 100644 --- a/src/Illuminate/Http/Client/Response.php +++ b/src/Illuminate/Http/Client/Response.php @@ -3,6 +3,7 @@ namespace Illuminate\Http\Client; use ArrayAccess; +use Illuminate\Support\Collection; use Illuminate\Support\Traits\Macroable; use LogicException; @@ -77,6 +78,16 @@ public function object() return json_decode($this->body(), false); } + /** + * Get the JSON decoded body of the response as a Collection. + * + * @return Collection + */ + public function collect() + { + return Collection::make($this->json()); + } + /** * Get a header from the response. * diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 12722b52b020..214930fd47cc 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -7,6 +7,7 @@ use Illuminate\Http\Client\Request; use Illuminate\Http\Client\RequestException; use Illuminate\Http\Client\Response; +use Illuminate\Support\Collection; use Illuminate\Support\Str; use OutOfBoundsException; use PHPUnit\Framework\TestCase; @@ -18,13 +19,6 @@ class HttpClientTest extends TestCase */ protected $factory; - protected function setUp(): void - { - parent::setUp(); - - $this->factory = new Factory; - } - public function testStubbedResponsesAreReturnedAfterFaking() { $this->factory->fake(); @@ -36,14 +30,16 @@ public function testStubbedResponsesAreReturnedAfterFaking() public function testResponseBodyCasting() { - $this->factory->fake([ - '*' => ['result' => ['foo' => 'bar']], - ]); + $this->factory->fake( + [ + '*' => ['result' => ['foo' => 'bar']], + ] + ); $response = $this->factory->get('http://foo.com/api'); $this->assertSame('{"result":{"foo":"bar"}}', $response->body()); - $this->assertSame('{"result":{"foo":"bar"}}', (string) $response); + $this->assertSame('{"result":{"foo":"bar"}}', (string)$response); $this->assertIsArray($response->json()); $this->assertSame(['foo' => 'bar'], $response->json()['result']); $this->assertSame(['foo' => 'bar'], $response->json('result')); @@ -54,13 +50,29 @@ public function testResponseBodyCasting() $this->assertSame('bar', $response->object()->result->foo); } + public function testResponseCanBeReturnedAsCollection() + { + $this->factory->fake( + [ + '*' => ['result' => ['foo' => 'bar']], + ] + ); + + $response = $this->factory->get('http://foo.com/api'); + + $this->assertInstanceOf(Collection::class, $response->collect()); + $this->assertEquals(collect(['result' => ['foo' => 'bar']]), $response->collect()); + } + public function testUrlsCanBeStubbedByPath() { - $this->factory->fake([ - 'foo.com/*' => ['page' => 'foo'], - 'bar.com/*' => ['page' => 'bar'], - '*' => ['page' => 'fallback'], - ]); + $this->factory->fake( + [ + 'foo.com/*' => ['page' => 'foo'], + 'bar.com/*' => ['page' => 'bar'], + '*' => ['page' => 'fallback'], + ] + ); $fooResponse = $this->factory->post('http://foo.com/test'); $barResponse = $this->factory->post('http://bar.com/test'); @@ -70,60 +82,79 @@ public function testUrlsCanBeStubbedByPath() $this->assertSame('bar', $barResponse['page']); $this->assertSame('fallback', $fallbackResponse['page']); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/test' && - $request->hasHeader('Content-Type', 'application/json'); - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/test' && + $request->hasHeader('Content-Type', 'application/json'); + } + ); } public function testCanSendJsonData() { $this->factory->fake(); - $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' => 'foo', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ] + )->post( + 'http://foo.com/json', + [ + 'name' => 'Taylor', + ] + ); - $this->factory->assertSent(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->assertSent( + 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'; + } + ); } public function testCanSendFormData() { $this->factory->fake(); - $this->factory->asForm()->post('http://foo.com/form', [ - 'name' => 'Taylor', - 'title' => 'Laravel Developer', - ]); + $this->factory->asForm()->post( + 'http://foo.com/form', + [ + 'name' => 'Taylor', + 'title' => 'Laravel Developer', + ] + ); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/form' && - $request->hasHeader('Content-Type', 'application/x-www-form-urlencoded') && - $request['name'] === 'Taylor'; - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/form' && + $request->hasHeader('Content-Type', 'application/x-www-form-urlencoded') && + $request['name'] === 'Taylor'; + } + ); } public function testSpecificRequestIsNotBeingSent() { $this->factory->fake(); - $this->factory->post('http://foo.com/form', [ - 'name' => 'Taylor', - ]); + $this->factory->post( + 'http://foo.com/form', + [ + 'name' => 'Taylor', + ] + ); - $this->factory->assertNotSent(function (Request $request) { - return $request->url() === 'http://foo.com/form' && - $request['name'] === 'Peter'; - }); + $this->factory->assertNotSent( + function (Request $request) { + return $request->url() === 'http://foo.com/form' && + $request['name'] === 'Peter'; + } + ); } public function testNoRequestIsNotBeingSent() @@ -138,15 +169,21 @@ public function testRequestCount() $this->factory->fake(); $this->factory->assertSentCount(0); - $this->factory->post('http://foo.com/form', [ - 'name' => 'Taylor', - ]); + $this->factory->post( + 'http://foo.com/form', + [ + 'name' => 'Taylor', + ] + ); $this->factory->assertSentCount(1); - $this->factory->post('http://foo.com/form', [ - 'name' => 'Jim', - ]); + $this->factory->post( + 'http://foo.com/form', + [ + 'name' => 'Jim', + ] + ); $this->factory->assertSentCount(2); } @@ -155,19 +192,24 @@ public function testCanSendMultipartData() { $this->factory->fake(); - $this->factory->asMultipart()->post('http://foo.com/multipart', [ + $this->factory->asMultipart()->post( + 'http://foo.com/multipart', [ - 'name' => 'foo', - 'contents' => 'data', - 'headers' => ['X-Test-Header' => 'foo'], - ], - ]); - - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/multipart' && - Str::startsWith($request->header('Content-Type')[0], 'multipart') && - $request[0]['name'] === 'foo'; - }); + [ + 'name' => 'foo', + 'contents' => 'data', + 'headers' => ['X-Test-Header' => 'foo'], + ], + ] + ); + + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/multipart' && + Str::startsWith($request->header('Content-Type')[0], 'multipart') && + $request[0]['name'] === 'foo'; + } + ); } public function testFilesCanBeAttached() @@ -175,54 +217,66 @@ public function testFilesCanBeAttached() $this->factory->fake(); $this->factory->attach('foo', 'data', 'file.txt', ['X-Test-Header' => 'foo']) - ->post('http://foo.com/file'); + ->post('http://foo.com/file'); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/file' && - Str::startsWith($request->header('Content-Type')[0], 'multipart') && - $request[0]['name'] === 'foo' && - $request->hasFile('foo', 'data', 'file.txt'); - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/file' && + Str::startsWith($request->header('Content-Type')[0], 'multipart') && + $request[0]['name'] === 'foo' && + $request->hasFile('foo', 'data', 'file.txt'); + } + ); } public function testCanSendMultipartDataWithSimplifiedParameters() { $this->factory->fake(); - $this->factory->asMultipart()->post('http://foo.com/multipart', [ - 'foo' => 'bar', - ]); + $this->factory->asMultipart()->post( + 'http://foo.com/multipart', + [ + 'foo' => 'bar', + ] + ); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/multipart' && - Str::startsWith($request->header('Content-Type')[0], 'multipart') && - $request[0]['name'] === 'foo' && - $request[0]['contents'] === 'bar'; - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/multipart' && + Str::startsWith($request->header('Content-Type')[0], 'multipart') && + $request[0]['name'] === 'foo' && + $request[0]['contents'] === 'bar'; + } + ); } public function testCanSendMultipartDataWithBothSimplifiedAndExtendedParameters() { $this->factory->fake(); - $this->factory->asMultipart()->post('http://foo.com/multipart', [ - 'foo' => 'bar', + $this->factory->asMultipart()->post( + 'http://foo.com/multipart', [ - 'name' => 'foobar', - 'contents' => 'data', - 'headers' => ['X-Test-Header' => 'foo'], - ], - ]); - - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/multipart' && - Str::startsWith($request->header('Content-Type')[0], 'multipart') && - $request[0]['name'] === 'foo' && - $request[0]['contents'] === 'bar' && - $request[1]['name'] === 'foobar' && - $request[1]['contents'] === 'data' && - $request[1]['headers']['X-Test-Header'] === 'foo'; - }); + 'foo' => 'bar', + [ + 'name' => 'foobar', + 'contents' => 'data', + 'headers' => ['X-Test-Header' => 'foo'], + ], + ] + ); + + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/multipart' && + Str::startsWith($request->header('Content-Type')[0], 'multipart') && + $request[0]['name'] === 'foo' && + $request[0]['contents'] === 'bar' && + $request[1]['name'] === 'foobar' && + $request[1]['contents'] === 'data' && + $request[1]['headers']['X-Test-Header'] === 'foo'; + } + ); } public function testItCanSendToken() @@ -231,10 +285,12 @@ public function testItCanSendToken() $this->factory->withToken('token')->post('http://foo.com/json'); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/json' && - $request->hasHeader('Authorization', 'Bearer token'); - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/json' && + $request->hasHeader('Authorization', 'Bearer token'); + } + ); } public function testItCanSendUserAgent() @@ -243,21 +299,25 @@ public function testItCanSendUserAgent() $this->factory->withUserAgent('Laravel')->post('http://foo.com/json'); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/json' && - $request->hasHeader('User-Agent', 'Laravel'); - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/json' && + $request->hasHeader('User-Agent', 'Laravel'); + } + ); } public function testSequenceBuilder() { - $this->factory->fake([ - '*' => $this->factory->sequence() - ->push('Ok', 201) - ->push(['fact' => 'Cats are great!']) - ->pushFile(__DIR__.'/fixtures/test.txt') - ->pushStatus(403), - ]); + $this->factory->fake( + [ + '*' => $this->factory->sequence() + ->push('Ok', 201) + ->push(['fact' => 'Cats are great!']) + ->pushFile(__DIR__ . '/fixtures/test.txt') + ->pushStatus(403), + ] + ); $response = $this->factory->get('https://example.com'); $this->assertSame('Ok', $response->body()); @@ -268,7 +328,10 @@ public function testSequenceBuilder() $this->assertSame(200, $response->status()); $response = $this->factory->get('https://example.com'); - $this->assertSame("This is a story about something that happened long ago when your grandfather was a child.\n", $response->body()); + $this->assertSame( + "This is a story about something that happened long ago when your grandfather was a child.\n", + $response->body() + ); $this->assertSame(200, $response->status()); $response = $this->factory->get('https://example.com'); @@ -283,11 +346,13 @@ public function testSequenceBuilder() public function testSequenceBuilderCanKeepGoingWhenEmpty() { - $this->factory->fake([ - '*' => $this->factory->sequence() - ->dontFailWhenEmpty() - ->push('Ok'), - ]); + $this->factory->fake( + [ + '*' => $this->factory->sequence() + ->dontFailWhenEmpty() + ->push('Ok'), + ] + ); $response = $this->factory->get('https://laravel.com'); $this->assertSame('Ok', $response->body()); @@ -298,11 +363,13 @@ public function testSequenceBuilderCanKeepGoingWhenEmpty() public function testAssertSequencesAreEmpty() { - $this->factory->fake([ - '*' => $this->factory->sequence() - ->push('1') - ->push('2'), - ]); + $this->factory->fake( + [ + '*' => $this->factory->sequence() + ->push('1') + ->push('2'), + ] + ); $this->factory->get('https://example.com'); $this->factory->get('https://example.com'); @@ -325,7 +392,8 @@ public function testWithCookies() $this->factory->fakeSequence()->pushStatus(200); $response = $this->factory->withCookies( - ['foo' => 'bar'], 'https://laravel.com' + ['foo' => 'bar'], + 'https://laravel.com' )->get('https://laravel.com'); $this->assertCount(1, $response->cookies()->toArray()); @@ -344,10 +412,12 @@ public function testGetWithArrayQueryParam() $this->factory->get('http://foo.com/get', ['foo' => 'bar']); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?foo=bar' - && $request['foo'] === 'bar'; - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/get?foo=bar' + && $request['foo'] === 'bar'; + } + ); } public function testGetWithStringQueryParam() @@ -356,10 +426,12 @@ public function testGetWithStringQueryParam() $this->factory->get('http://foo.com/get', 'foo=bar'); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?foo=bar' - && $request['foo'] === 'bar'; - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/get?foo=bar' + && $request['foo'] === 'bar'; + } + ); } public function testGetWithQuery() @@ -368,11 +440,13 @@ public function testGetWithQuery() $this->factory->get('http://foo.com/get?foo=bar&page=1'); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?foo=bar&page=1' - && $request['foo'] === 'bar' - && $request['page'] === '1'; - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/get?foo=bar&page=1' + && $request['foo'] === 'bar' + && $request['page'] === '1'; + } + ); } public function testGetWithQueryWontEncode() @@ -381,12 +455,14 @@ public function testGetWithQueryWontEncode() $this->factory->get('http://foo.com/get?foo;bar;1;5;10&page=1'); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?foo;bar;1;5;10&page=1' - && ! isset($request['foo']) - && ! isset($request['bar']) - && $request['page'] === '1'; - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/get?foo;bar;1;5;10&page=1' + && !isset($request['foo']) + && !isset($request['bar']) + && $request['page'] === '1'; + } + ); } public function testGetWithArrayQueryParamOverwrites() @@ -395,10 +471,12 @@ public function testGetWithArrayQueryParamOverwrites() $this->factory->get('http://foo.com/get?foo=bar&page=1', ['hello' => 'world']); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?hello=world' - && $request['hello'] === 'world'; - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/get?hello=world' + && $request['hello'] === 'world'; + } + ); } public function testGetWithArrayQueryParamEncodes() @@ -407,43 +485,55 @@ public function testGetWithArrayQueryParamEncodes() $this->factory->get('http://foo.com/get', ['foo;bar; space test' => 'laravel']); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/get?foo%3Bbar%3B%20space%20test=laravel' - && $request['foo;bar; space test'] === 'laravel'; - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/get?foo%3Bbar%3B%20space%20test=laravel' + && $request['foo;bar; space test'] === 'laravel'; + } + ); } public function testCanConfirmManyHeaders() { $this->factory->fake(); - $this->factory->withHeaders([ - 'X-Test-Header' => 'foo', - 'X-Test-ArrayHeader' => ['bar', 'baz'], - ])->post('http://foo.com/json'); + $this->factory->withHeaders( + [ + 'X-Test-Header' => 'foo', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ] + )->post('http://foo.com/json'); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/json' && - $request->hasHeaders([ - 'X-Test-Header' => 'foo', - 'X-Test-ArrayHeader' => ['bar', 'baz'], - ]); - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/json' && + $request->hasHeaders( + [ + 'X-Test-Header' => 'foo', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ] + ); + } + ); } public function testCanConfirmManyHeadersUsingAString() { $this->factory->fake(); - $this->factory->withHeaders([ - 'X-Test-Header' => 'foo', - 'X-Test-ArrayHeader' => ['bar', 'baz'], - ])->post('http://foo.com/json'); + $this->factory->withHeaders( + [ + 'X-Test-Header' => 'foo', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ] + )->post('http://foo.com/json'); - $this->factory->assertSent(function (Request $request) { - return $request->url() === 'http://foo.com/json' && - $request->hasHeaders('X-Test-Header'); - }); + $this->factory->assertSent( + function (Request $request) { + return $request->url() === 'http://foo.com/json' && + $request->hasHeaders('X-Test-Header'); + } + ); } public function testRequestExceptionSummary() @@ -465,7 +555,9 @@ public function testRequestExceptionSummary() public function testRequestExceptionTruncatedSummary() { $this->expectException(RequestException::class); - $this->expectExceptionMessage('{"error":{"code":403,"message":"The Request can not be completed because quota limit was exceeded. Please, check our sup (truncated...)'); + $this->expectExceptionMessage( + '{"error":{"code":403,"message":"The Request can not be completed because quota limit was exceeded. Please, check our sup (truncated...)' + ); $error = [ 'error' => [ @@ -491,14 +583,18 @@ public function testRequestExceptionEmptyBody() public function testOnErrorDoesntCallClosureOnInformational() { $status = 0; - $client = $this->factory->fake([ - 'laravel.com' => $this->factory::response('', 101), - ]); + $client = $this->factory->fake( + [ + 'laravel.com' => $this->factory::response('', 101), + ] + ); $response = $client->get('laravel.com') - ->onError(function ($response) use (&$status) { - $status = $response->status(); - }); + ->onError( + function ($response) use (&$status) { + $status = $response->status(); + } + ); $this->assertSame(0, $status); $this->assertSame(101, $response->status()); @@ -507,14 +603,18 @@ public function testOnErrorDoesntCallClosureOnInformational() public function testOnErrorDoesntCallClosureOnSuccess() { $status = 0; - $client = $this->factory->fake([ - 'laravel.com' => $this->factory::response('', 201), - ]); + $client = $this->factory->fake( + [ + 'laravel.com' => $this->factory::response('', 201), + ] + ); $response = $client->get('laravel.com') - ->onError(function ($response) use (&$status) { - $status = $response->status(); - }); + ->onError( + function ($response) use (&$status) { + $status = $response->status(); + } + ); $this->assertSame(0, $status); $this->assertSame(201, $response->status()); @@ -523,14 +623,18 @@ public function testOnErrorDoesntCallClosureOnSuccess() public function testOnErrorDoesntCallClosureOnRedirection() { $status = 0; - $client = $this->factory->fake([ - 'laravel.com' => $this->factory::response('', 301), - ]); + $client = $this->factory->fake( + [ + 'laravel.com' => $this->factory::response('', 301), + ] + ); $response = $client->get('laravel.com') - ->onError(function ($response) use (&$status) { - $status = $response->status(); - }); + ->onError( + function ($response) use (&$status) { + $status = $response->status(); + } + ); $this->assertSame(0, $status); $this->assertSame(301, $response->status()); @@ -539,14 +643,18 @@ public function testOnErrorDoesntCallClosureOnRedirection() public function testOnErrorCallsClosureOnClientError() { $status = 0; - $client = $this->factory->fake([ - 'laravel.com' => $this->factory::response('', 401), - ]); + $client = $this->factory->fake( + [ + 'laravel.com' => $this->factory::response('', 401), + ] + ); $response = $client->get('laravel.com') - ->onError(function ($response) use (&$status) { - $status = $response->status(); - }); + ->onError( + function ($response) use (&$status) { + $status = $response->status(); + } + ); $this->assertSame(401, $status); $this->assertSame(401, $response->status()); @@ -555,14 +663,18 @@ public function testOnErrorCallsClosureOnClientError() public function testOnErrorCallsClosureOnServerError() { $status = 0; - $client = $this->factory->fake([ - 'laravel.com' => $this->factory::response('', 501), - ]); + $client = $this->factory->fake( + [ + 'laravel.com' => $this->factory::response('', 501), + ] + ); $response = $client->get('laravel.com') - ->onError(function ($response) use (&$status) { - $status = $response->status(); - }); + ->onError( + function ($response) use (&$status) { + $status = $response->status(); + } + ); $this->assertSame(501, $status); $this->assertSame(501, $response->status()); @@ -572,7 +684,7 @@ public function testSinkToFile() { $this->factory->fakeSequence()->push('abc123'); - $destination = __DIR__.'/fixtures/sunk.txt'; + $destination = __DIR__ . '/fixtures/sunk.txt'; if (file_exists($destination)) { unlink($destination); @@ -600,9 +712,11 @@ public function testSinkToResource() public function testSinkWhenStubbedByPath() { - $this->factory->fake([ - 'foo.com/*' => ['page' => 'foo'], - ]); + $this->factory->fake( + [ + 'foo.com/*' => ['page' => 'foo'], + ] + ); $resource = fopen('php://temp', 'w'); @@ -695,33 +809,43 @@ public function testCanAssertAgainstOrderOfHttpRequestsWithCallablesAndHeaders() $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'; + $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'; + $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' => '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->withHeaders( + [ + 'X-Test-Header' => 'bar', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ] + )->post( + 'http://bar.com/json', + [ + 'name' => 'Taylor', + ] + ); $this->factory->assertSentInOrder($executionOrder); } @@ -733,36 +857,53 @@ public function testCanAssertAgainstOrderOfHttpRequestsWithCallablesAndHeadersFa $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'; + $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'; + $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' => '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->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); } + + protected function setUp(): void + { + parent::setUp(); + + $this->factory = new Factory; + } } From a8d7f107a12e98416525a9e64f3fdad6b916373f Mon Sep 17 00:00:00 2001 From: luke Date: Sat, 20 Feb 2021 16:10:49 +0000 Subject: [PATCH 2/5] Formatting change --- tests/Http/HttpClientTest.php | 630 ++++++++++++++-------------------- 1 file changed, 251 insertions(+), 379 deletions(-) diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 214930fd47cc..3681cbc93c35 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -19,6 +19,13 @@ class HttpClientTest extends TestCase */ protected $factory; + protected function setUp(): void + { + parent::setUp(); + + $this->factory = new Factory; + } + public function testStubbedResponsesAreReturnedAfterFaking() { $this->factory->fake(); @@ -30,16 +37,14 @@ public function testStubbedResponsesAreReturnedAfterFaking() public function testResponseBodyCasting() { - $this->factory->fake( - [ - '*' => ['result' => ['foo' => 'bar']], - ] - ); + $this->factory->fake([ + '*' => ['result' => ['foo' => 'bar']], + ]); $response = $this->factory->get('http://foo.com/api'); $this->assertSame('{"result":{"foo":"bar"}}', $response->body()); - $this->assertSame('{"result":{"foo":"bar"}}', (string)$response); + $this->assertSame('{"result":{"foo":"bar"}}', (string) $response); $this->assertIsArray($response->json()); $this->assertSame(['foo' => 'bar'], $response->json()['result']); $this->assertSame(['foo' => 'bar'], $response->json('result')); @@ -52,11 +57,9 @@ public function testResponseBodyCasting() public function testResponseCanBeReturnedAsCollection() { - $this->factory->fake( - [ - '*' => ['result' => ['foo' => 'bar']], - ] - ); + $this->factory->fake([ + '*' => ['result' => ['foo' => 'bar']], + ]); $response = $this->factory->get('http://foo.com/api'); @@ -66,13 +69,11 @@ public function testResponseCanBeReturnedAsCollection() public function testUrlsCanBeStubbedByPath() { - $this->factory->fake( - [ - 'foo.com/*' => ['page' => 'foo'], - 'bar.com/*' => ['page' => 'bar'], - '*' => ['page' => 'fallback'], - ] - ); + $this->factory->fake([ + 'foo.com/*' => ['page' => 'foo'], + 'bar.com/*' => ['page' => 'bar'], + '*' => ['page' => 'fallback'], + ]); $fooResponse = $this->factory->post('http://foo.com/test'); $barResponse = $this->factory->post('http://bar.com/test'); @@ -82,79 +83,60 @@ public function testUrlsCanBeStubbedByPath() $this->assertSame('bar', $barResponse['page']); $this->assertSame('fallback', $fallbackResponse['page']); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/test' && - $request->hasHeader('Content-Type', 'application/json'); - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/test' && + $request->hasHeader('Content-Type', 'application/json'); + }); } public function testCanSendJsonData() { $this->factory->fake(); - $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' => 'foo', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ])->post('http://foo.com/json', [ + 'name' => 'Taylor', + ]); - $this->factory->assertSent( - 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->assertSent(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'; + }); } public function testCanSendFormData() { $this->factory->fake(); - $this->factory->asForm()->post( - 'http://foo.com/form', - [ - 'name' => 'Taylor', - 'title' => 'Laravel Developer', - ] - ); + $this->factory->asForm()->post('http://foo.com/form', [ + 'name' => 'Taylor', + 'title' => 'Laravel Developer', + ]); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/form' && - $request->hasHeader('Content-Type', 'application/x-www-form-urlencoded') && - $request['name'] === 'Taylor'; - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/form' && + $request->hasHeader('Content-Type', 'application/x-www-form-urlencoded') && + $request['name'] === 'Taylor'; + }); } public function testSpecificRequestIsNotBeingSent() { $this->factory->fake(); - $this->factory->post( - 'http://foo.com/form', - [ - 'name' => 'Taylor', - ] - ); + $this->factory->post('http://foo.com/form', [ + 'name' => 'Taylor', + ]); - $this->factory->assertNotSent( - function (Request $request) { - return $request->url() === 'http://foo.com/form' && - $request['name'] === 'Peter'; - } - ); + $this->factory->assertNotSent(function (Request $request) { + return $request->url() === 'http://foo.com/form' && + $request['name'] === 'Peter'; + }); } public function testNoRequestIsNotBeingSent() @@ -169,21 +151,15 @@ public function testRequestCount() $this->factory->fake(); $this->factory->assertSentCount(0); - $this->factory->post( - 'http://foo.com/form', - [ - 'name' => 'Taylor', - ] - ); + $this->factory->post('http://foo.com/form', [ + 'name' => 'Taylor', + ]); $this->factory->assertSentCount(1); - $this->factory->post( - 'http://foo.com/form', - [ - 'name' => 'Jim', - ] - ); + $this->factory->post('http://foo.com/form', [ + 'name' => 'Jim', + ]); $this->factory->assertSentCount(2); } @@ -192,24 +168,19 @@ public function testCanSendMultipartData() { $this->factory->fake(); - $this->factory->asMultipart()->post( - 'http://foo.com/multipart', + $this->factory->asMultipart()->post('http://foo.com/multipart', [ [ - [ - 'name' => 'foo', - 'contents' => 'data', - 'headers' => ['X-Test-Header' => 'foo'], - ], - ] - ); - - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/multipart' && - Str::startsWith($request->header('Content-Type')[0], 'multipart') && - $request[0]['name'] === 'foo'; - } - ); + 'name' => 'foo', + 'contents' => 'data', + 'headers' => ['X-Test-Header' => 'foo'], + ], + ]); + + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/multipart' && + Str::startsWith($request->header('Content-Type')[0], 'multipart') && + $request[0]['name'] === 'foo'; + }); } public function testFilesCanBeAttached() @@ -217,66 +188,54 @@ public function testFilesCanBeAttached() $this->factory->fake(); $this->factory->attach('foo', 'data', 'file.txt', ['X-Test-Header' => 'foo']) - ->post('http://foo.com/file'); + ->post('http://foo.com/file'); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/file' && - Str::startsWith($request->header('Content-Type')[0], 'multipart') && - $request[0]['name'] === 'foo' && - $request->hasFile('foo', 'data', 'file.txt'); - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/file' && + Str::startsWith($request->header('Content-Type')[0], 'multipart') && + $request[0]['name'] === 'foo' && + $request->hasFile('foo', 'data', 'file.txt'); + }); } public function testCanSendMultipartDataWithSimplifiedParameters() { $this->factory->fake(); - $this->factory->asMultipart()->post( - 'http://foo.com/multipart', - [ - 'foo' => 'bar', - ] - ); + $this->factory->asMultipart()->post('http://foo.com/multipart', [ + 'foo' => 'bar', + ]); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/multipart' && - Str::startsWith($request->header('Content-Type')[0], 'multipart') && - $request[0]['name'] === 'foo' && - $request[0]['contents'] === 'bar'; - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/multipart' && + Str::startsWith($request->header('Content-Type')[0], 'multipart') && + $request[0]['name'] === 'foo' && + $request[0]['contents'] === 'bar'; + }); } public function testCanSendMultipartDataWithBothSimplifiedAndExtendedParameters() { $this->factory->fake(); - $this->factory->asMultipart()->post( - 'http://foo.com/multipart', + $this->factory->asMultipart()->post('http://foo.com/multipart', [ + 'foo' => 'bar', [ - 'foo' => 'bar', - [ - 'name' => 'foobar', - 'contents' => 'data', - 'headers' => ['X-Test-Header' => 'foo'], - ], - ] - ); - - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/multipart' && - Str::startsWith($request->header('Content-Type')[0], 'multipart') && - $request[0]['name'] === 'foo' && - $request[0]['contents'] === 'bar' && - $request[1]['name'] === 'foobar' && - $request[1]['contents'] === 'data' && - $request[1]['headers']['X-Test-Header'] === 'foo'; - } - ); + 'name' => 'foobar', + 'contents' => 'data', + 'headers' => ['X-Test-Header' => 'foo'], + ], + ]); + + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/multipart' && + Str::startsWith($request->header('Content-Type')[0], 'multipart') && + $request[0]['name'] === 'foo' && + $request[0]['contents'] === 'bar' && + $request[1]['name'] === 'foobar' && + $request[1]['contents'] === 'data' && + $request[1]['headers']['X-Test-Header'] === 'foo'; + }); } public function testItCanSendToken() @@ -285,12 +244,10 @@ public function testItCanSendToken() $this->factory->withToken('token')->post('http://foo.com/json'); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/json' && - $request->hasHeader('Authorization', 'Bearer token'); - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/json' && + $request->hasHeader('Authorization', 'Bearer token'); + }); } public function testItCanSendUserAgent() @@ -299,25 +256,21 @@ public function testItCanSendUserAgent() $this->factory->withUserAgent('Laravel')->post('http://foo.com/json'); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/json' && - $request->hasHeader('User-Agent', 'Laravel'); - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/json' && + $request->hasHeader('User-Agent', 'Laravel'); + }); } public function testSequenceBuilder() { - $this->factory->fake( - [ - '*' => $this->factory->sequence() - ->push('Ok', 201) - ->push(['fact' => 'Cats are great!']) - ->pushFile(__DIR__ . '/fixtures/test.txt') - ->pushStatus(403), - ] - ); + $this->factory->fake([ + '*' => $this->factory->sequence() + ->push('Ok', 201) + ->push(['fact' => 'Cats are great!']) + ->pushFile(__DIR__.'/fixtures/test.txt') + ->pushStatus(403), + ]); $response = $this->factory->get('https://example.com'); $this->assertSame('Ok', $response->body()); @@ -328,10 +281,7 @@ public function testSequenceBuilder() $this->assertSame(200, $response->status()); $response = $this->factory->get('https://example.com'); - $this->assertSame( - "This is a story about something that happened long ago when your grandfather was a child.\n", - $response->body() - ); + $this->assertSame("This is a story about something that happened long ago when your grandfather was a child.\n", $response->body()); $this->assertSame(200, $response->status()); $response = $this->factory->get('https://example.com'); @@ -346,13 +296,11 @@ public function testSequenceBuilder() public function testSequenceBuilderCanKeepGoingWhenEmpty() { - $this->factory->fake( - [ - '*' => $this->factory->sequence() - ->dontFailWhenEmpty() - ->push('Ok'), - ] - ); + $this->factory->fake([ + '*' => $this->factory->sequence() + ->dontFailWhenEmpty() + ->push('Ok'), + ]); $response = $this->factory->get('https://laravel.com'); $this->assertSame('Ok', $response->body()); @@ -363,13 +311,11 @@ public function testSequenceBuilderCanKeepGoingWhenEmpty() public function testAssertSequencesAreEmpty() { - $this->factory->fake( - [ - '*' => $this->factory->sequence() - ->push('1') - ->push('2'), - ] - ); + $this->factory->fake([ + '*' => $this->factory->sequence() + ->push('1') + ->push('2'), + ]); $this->factory->get('https://example.com'); $this->factory->get('https://example.com'); @@ -392,8 +338,7 @@ public function testWithCookies() $this->factory->fakeSequence()->pushStatus(200); $response = $this->factory->withCookies( - ['foo' => 'bar'], - 'https://laravel.com' + ['foo' => 'bar'], 'https://laravel.com' )->get('https://laravel.com'); $this->assertCount(1, $response->cookies()->toArray()); @@ -412,12 +357,10 @@ public function testGetWithArrayQueryParam() $this->factory->get('http://foo.com/get', ['foo' => 'bar']); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/get?foo=bar' - && $request['foo'] === 'bar'; - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/get?foo=bar' + && $request['foo'] === 'bar'; + }); } public function testGetWithStringQueryParam() @@ -426,12 +369,10 @@ public function testGetWithStringQueryParam() $this->factory->get('http://foo.com/get', 'foo=bar'); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/get?foo=bar' - && $request['foo'] === 'bar'; - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/get?foo=bar' + && $request['foo'] === 'bar'; + }); } public function testGetWithQuery() @@ -440,13 +381,11 @@ public function testGetWithQuery() $this->factory->get('http://foo.com/get?foo=bar&page=1'); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/get?foo=bar&page=1' - && $request['foo'] === 'bar' - && $request['page'] === '1'; - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/get?foo=bar&page=1' + && $request['foo'] === 'bar' + && $request['page'] === '1'; + }); } public function testGetWithQueryWontEncode() @@ -455,14 +394,12 @@ public function testGetWithQueryWontEncode() $this->factory->get('http://foo.com/get?foo;bar;1;5;10&page=1'); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/get?foo;bar;1;5;10&page=1' - && !isset($request['foo']) - && !isset($request['bar']) - && $request['page'] === '1'; - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/get?foo;bar;1;5;10&page=1' + && ! isset($request['foo']) + && ! isset($request['bar']) + && $request['page'] === '1'; + }); } public function testGetWithArrayQueryParamOverwrites() @@ -471,12 +408,10 @@ public function testGetWithArrayQueryParamOverwrites() $this->factory->get('http://foo.com/get?foo=bar&page=1', ['hello' => 'world']); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/get?hello=world' - && $request['hello'] === 'world'; - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/get?hello=world' + && $request['hello'] === 'world'; + }); } public function testGetWithArrayQueryParamEncodes() @@ -485,55 +420,43 @@ public function testGetWithArrayQueryParamEncodes() $this->factory->get('http://foo.com/get', ['foo;bar; space test' => 'laravel']); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/get?foo%3Bbar%3B%20space%20test=laravel' - && $request['foo;bar; space test'] === 'laravel'; - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/get?foo%3Bbar%3B%20space%20test=laravel' + && $request['foo;bar; space test'] === 'laravel'; + }); } public function testCanConfirmManyHeaders() { $this->factory->fake(); - $this->factory->withHeaders( - [ - 'X-Test-Header' => 'foo', - 'X-Test-ArrayHeader' => ['bar', 'baz'], - ] - )->post('http://foo.com/json'); + $this->factory->withHeaders([ + 'X-Test-Header' => 'foo', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ])->post('http://foo.com/json'); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/json' && - $request->hasHeaders( - [ - 'X-Test-Header' => 'foo', - 'X-Test-ArrayHeader' => ['bar', 'baz'], - ] - ); - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/json' && + $request->hasHeaders([ + 'X-Test-Header' => 'foo', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ]); + }); } public function testCanConfirmManyHeadersUsingAString() { $this->factory->fake(); - $this->factory->withHeaders( - [ - 'X-Test-Header' => 'foo', - 'X-Test-ArrayHeader' => ['bar', 'baz'], - ] - )->post('http://foo.com/json'); + $this->factory->withHeaders([ + 'X-Test-Header' => 'foo', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ])->post('http://foo.com/json'); - $this->factory->assertSent( - function (Request $request) { - return $request->url() === 'http://foo.com/json' && - $request->hasHeaders('X-Test-Header'); - } - ); + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/json' && + $request->hasHeaders('X-Test-Header'); + }); } public function testRequestExceptionSummary() @@ -555,9 +478,7 @@ public function testRequestExceptionSummary() public function testRequestExceptionTruncatedSummary() { $this->expectException(RequestException::class); - $this->expectExceptionMessage( - '{"error":{"code":403,"message":"The Request can not be completed because quota limit was exceeded. Please, check our sup (truncated...)' - ); + $this->expectExceptionMessage('{"error":{"code":403,"message":"The Request can not be completed because quota limit was exceeded. Please, check our sup (truncated...)'); $error = [ 'error' => [ @@ -583,18 +504,14 @@ public function testRequestExceptionEmptyBody() public function testOnErrorDoesntCallClosureOnInformational() { $status = 0; - $client = $this->factory->fake( - [ - 'laravel.com' => $this->factory::response('', 101), - ] - ); + $client = $this->factory->fake([ + 'laravel.com' => $this->factory::response('', 101), + ]); $response = $client->get('laravel.com') - ->onError( - function ($response) use (&$status) { - $status = $response->status(); - } - ); + ->onError(function ($response) use (&$status) { + $status = $response->status(); + }); $this->assertSame(0, $status); $this->assertSame(101, $response->status()); @@ -603,18 +520,14 @@ function ($response) use (&$status) { public function testOnErrorDoesntCallClosureOnSuccess() { $status = 0; - $client = $this->factory->fake( - [ - 'laravel.com' => $this->factory::response('', 201), - ] - ); + $client = $this->factory->fake([ + 'laravel.com' => $this->factory::response('', 201), + ]); $response = $client->get('laravel.com') - ->onError( - function ($response) use (&$status) { - $status = $response->status(); - } - ); + ->onError(function ($response) use (&$status) { + $status = $response->status(); + }); $this->assertSame(0, $status); $this->assertSame(201, $response->status()); @@ -623,18 +536,14 @@ function ($response) use (&$status) { public function testOnErrorDoesntCallClosureOnRedirection() { $status = 0; - $client = $this->factory->fake( - [ - 'laravel.com' => $this->factory::response('', 301), - ] - ); + $client = $this->factory->fake([ + 'laravel.com' => $this->factory::response('', 301), + ]); $response = $client->get('laravel.com') - ->onError( - function ($response) use (&$status) { - $status = $response->status(); - } - ); + ->onError(function ($response) use (&$status) { + $status = $response->status(); + }); $this->assertSame(0, $status); $this->assertSame(301, $response->status()); @@ -643,18 +552,14 @@ function ($response) use (&$status) { public function testOnErrorCallsClosureOnClientError() { $status = 0; - $client = $this->factory->fake( - [ - 'laravel.com' => $this->factory::response('', 401), - ] - ); + $client = $this->factory->fake([ + 'laravel.com' => $this->factory::response('', 401), + ]); $response = $client->get('laravel.com') - ->onError( - function ($response) use (&$status) { - $status = $response->status(); - } - ); + ->onError(function ($response) use (&$status) { + $status = $response->status(); + }); $this->assertSame(401, $status); $this->assertSame(401, $response->status()); @@ -663,18 +568,14 @@ function ($response) use (&$status) { public function testOnErrorCallsClosureOnServerError() { $status = 0; - $client = $this->factory->fake( - [ - 'laravel.com' => $this->factory::response('', 501), - ] - ); + $client = $this->factory->fake([ + 'laravel.com' => $this->factory::response('', 501), + ]); $response = $client->get('laravel.com') - ->onError( - function ($response) use (&$status) { - $status = $response->status(); - } - ); + ->onError(function ($response) use (&$status) { + $status = $response->status(); + }); $this->assertSame(501, $status); $this->assertSame(501, $response->status()); @@ -684,7 +585,7 @@ public function testSinkToFile() { $this->factory->fakeSequence()->push('abc123'); - $destination = __DIR__ . '/fixtures/sunk.txt'; + $destination = __DIR__.'/fixtures/sunk.txt'; if (file_exists($destination)) { unlink($destination); @@ -712,11 +613,9 @@ public function testSinkToResource() public function testSinkWhenStubbedByPath() { - $this->factory->fake( - [ - 'foo.com/*' => ['page' => 'foo'], - ] - ); + $this->factory->fake([ + 'foo.com/*' => ['page' => 'foo'], + ]); $resource = fopen('php://temp', 'w'); @@ -809,43 +708,33 @@ public function testCanAssertAgainstOrderOfHttpRequestsWithCallablesAndHeaders() $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'; + $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'; + $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' => '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->withHeaders([ + 'X-Test-Header' => 'bar', + 'X-Test-ArrayHeader' => ['bar', 'baz'], + ])->post('http://bar.com/json', [ + 'name' => 'Taylor', + ]); $this->factory->assertSentInOrder($executionOrder); } @@ -857,53 +746,36 @@ public function testCanAssertAgainstOrderOfHttpRequestsWithCallablesAndHeadersFa $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'; + $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'; + $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' => '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->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); } - - protected function setUp(): void - { - parent::setUp(); - - $this->factory = new Factory; - } } From 4e9396dafb0f7bade6dce27560d36eada3357e5b Mon Sep 17 00:00:00 2001 From: luke Date: Sat, 20 Feb 2021 17:19:28 +0000 Subject: [PATCH 3/5] Allows passing a key to the `collect` method that will return a subset of the json response before wrapping it in the collection. --- src/Illuminate/Http/Client/Response.php | 4 ++-- tests/Http/HttpClientTest.php | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Client/Response.php b/src/Illuminate/Http/Client/Response.php index 12e98304ecb6..d4b67441e392 100644 --- a/src/Illuminate/Http/Client/Response.php +++ b/src/Illuminate/Http/Client/Response.php @@ -83,9 +83,9 @@ public function object() * * @return Collection */ - public function collect() + public function collect($key = null) { - return Collection::make($this->json()); + return Collection::make($this->json($key)); } /** diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 3681cbc93c35..b407d6fb3f0e 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -65,6 +65,9 @@ public function testResponseCanBeReturnedAsCollection() $this->assertInstanceOf(Collection::class, $response->collect()); $this->assertEquals(collect(['result' => ['foo' => 'bar']]), $response->collect()); + $this->assertEquals(collect(['foo' => 'bar']), $response->collect('result')); + $this->assertEquals(collect(['bar']), $response->collect('result.foo')); + $this->assertEquals(collect(), $response->collect('missing_key')); } public function testUrlsCanBeStubbedByPath() From de4c7680212da753d741390b77575b175bdb2d72 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 21 Feb 2021 10:07:19 -0600 Subject: [PATCH 4/5] Update Response.php --- src/Illuminate/Http/Client/Response.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Client/Response.php b/src/Illuminate/Http/Client/Response.php index d4b67441e392..19556c8d8d46 100644 --- a/src/Illuminate/Http/Client/Response.php +++ b/src/Illuminate/Http/Client/Response.php @@ -79,9 +79,9 @@ public function object() } /** - * Get the JSON decoded body of the response as a Collection. + * Get the JSON decoded body of the response as a collection. * - * @return Collection + * @return \Illuminate\Support\Collection */ public function collect($key = null) { From aa1fa55fb6794cc883c687e4caaf668635c5bcb1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 21 Feb 2021 10:08:14 -0600 Subject: [PATCH 5/5] Update Response.php --- src/Illuminate/Http/Client/Response.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Http/Client/Response.php b/src/Illuminate/Http/Client/Response.php index 19556c8d8d46..ccbd631c7cb8 100644 --- a/src/Illuminate/Http/Client/Response.php +++ b/src/Illuminate/Http/Client/Response.php @@ -81,6 +81,7 @@ public function object() /** * Get the JSON decoded body of the response as a collection. * + * @param string|null $key * @return \Illuminate\Support\Collection */ public function collect($key = null)