From 12b744e788dd246ad4393f25ca312cca529cd8dc Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Fri, 28 Jan 2022 22:33:00 +0000 Subject: [PATCH 1/2] Removed requirement for HasQueryParams trait --- src/Managers/RequestManager.php | 43 +++++++++++++++---- src/Traits/CollectsQueryParams.php | 14 +----- src/Traits/Features/HasQueryParams.php | 11 ----- .../Connectors/QueryParameterConnector.php | 1 - ...rwrittenQueryParameterConnectorRequest.php | 2 - .../QueryParameterConnectorBlankRequest.php | 2 - .../QueryParameterConnectorRequest.php | 2 - .../Requests/QueryParameterRequest.php | 2 - tests/Unit/Features/QueryParameterTest.php | 13 ++++++ 9 files changed, 48 insertions(+), 42 deletions(-) delete mode 100644 src/Traits/Features/HasQueryParams.php diff --git a/src/Managers/RequestManager.php b/src/Managers/RequestManager.php index 84af4550..8aca2014 100644 --- a/src/Managers/RequestManager.php +++ b/src/Managers/RequestManager.php @@ -9,6 +9,7 @@ use Sammyjo20\Saloon\Http\SaloonRequest; use Sammyjo20\Saloon\Http\SaloonResponse; use Sammyjo20\Saloon\Http\SaloonConnector; +use Sammyjo20\Saloon\Traits\CollectsQueryParams; use Sammyjo20\Saloon\Traits\ManagesGuzzle; use Sammyjo20\Saloon\Traits\CollectsConfig; use Sammyjo20\Saloon\Clients\BaseMockClient; @@ -27,6 +28,7 @@ class RequestManager ManagesFeatures, CollectsHeaders, CollectsConfig, + CollectsQueryParams, CollectsHandlers, CollectsInterceptors; @@ -89,6 +91,7 @@ public function __construct(SaloonRequest $request, MockClient $mockClient = nul * * @return void * @throws \ReflectionException + * @throws \Sammyjo20\Saloon\Exceptions\SaloonInvalidConnectorException */ public function hydrate(): void { @@ -111,10 +114,22 @@ public function hydrate(): void $this->mergeHeaders($this->connector->getHeaders(), $this->request->getHeaders()); + // Merge in query params + + $this->mergeQuery($this->connector->getQuery(), $this->request->getQuery()); + // Merge the config $this->mergeConfig($this->connector->getConfig(), $this->request->getConfig()); + // Add the query parameters to the config + + $query = $this->getQuery(); + + if (! empty($query)) { + $this->mergeConfig(['query' => $query]); + } + // Merge in any handlers $this->mergeHandlers($this->connector->getHandlers(), $this->request->getHandlers()); @@ -149,15 +164,7 @@ public function send() // Build up the config! - $requestOptions = [ - RequestOptions::HEADERS => $this->getHeaders(), - ]; - - // Recursively add config variables... - - foreach ($this->getConfig() as $configVariable => $value) { - $requestOptions[$configVariable] = $value; - } + $requestOptions = $this->buildRequestOptions(); // Boot up our Guzzle client... This will also boot up handlers... @@ -268,6 +275,24 @@ private function bootMockClient(MockClient|null $mockClient): void $this->mockClient = $mockClient; } + /** + * Build up all the request options + * + * @return array + */ + private function buildRequestOptions(): array + { + $requestOptions = [ + RequestOptions::HEADERS => $this->getHeaders(), + ]; + + foreach ($this->getConfig() as $configVariable => $value) { + $requestOptions[$configVariable] = $value; + } + + return $requestOptions; + } + /** * Is the manager in mocking mode? * diff --git a/src/Traits/CollectsQueryParams.php b/src/Traits/CollectsQueryParams.php index 6d804839..ef277796 100644 --- a/src/Traits/CollectsQueryParams.php +++ b/src/Traits/CollectsQueryParams.php @@ -77,7 +77,7 @@ public function addQuery(string $query, $value): self } /** - * Get all query or filter with a key. + * Get all query or filter with a key. * * @param string|null $key * @return mixed @@ -109,18 +109,6 @@ public function getQuery(string $key = null): mixed return $queryBag; } - /** - * Get an individual query param - * - * @param string $key - * @return string - * @throws \Sammyjo20\Saloon\Exceptions\SaloonInvalidConnectorException - */ - public function getQueryByKey(string $key): string - { - return $this->getQuery($key); - } - /** * Should we ignore the default query when calling `->getQuery()`? * diff --git a/src/Traits/Features/HasQueryParams.php b/src/Traits/Features/HasQueryParams.php deleted file mode 100644 index 3f03c29c..00000000 --- a/src/Traits/Features/HasQueryParams.php +++ /dev/null @@ -1,11 +0,0 @@ -addConfig('query', $this->getQuery()); - } -} diff --git a/tests/Resources/Connectors/QueryParameterConnector.php b/tests/Resources/Connectors/QueryParameterConnector.php index 3075a7cf..2da410e3 100644 --- a/tests/Resources/Connectors/QueryParameterConnector.php +++ b/tests/Resources/Connectors/QueryParameterConnector.php @@ -9,7 +9,6 @@ class QueryParameterConnector extends SaloonConnector { use AcceptsJson; - use HasQueryParams; public function defineBaseUrl(): string { diff --git a/tests/Resources/Requests/OverwrittenQueryParameterConnectorRequest.php b/tests/Resources/Requests/OverwrittenQueryParameterConnectorRequest.php index fd648418..b04f868f 100644 --- a/tests/Resources/Requests/OverwrittenQueryParameterConnectorRequest.php +++ b/tests/Resources/Requests/OverwrittenQueryParameterConnectorRequest.php @@ -9,8 +9,6 @@ class OverwrittenQueryParameterConnectorRequest extends SaloonRequest { - use HasQueryParams; - /** * Define the method that the request will use. * diff --git a/tests/Resources/Requests/QueryParameterConnectorBlankRequest.php b/tests/Resources/Requests/QueryParameterConnectorBlankRequest.php index d0f6a7e4..35b9fd57 100644 --- a/tests/Resources/Requests/QueryParameterConnectorBlankRequest.php +++ b/tests/Resources/Requests/QueryParameterConnectorBlankRequest.php @@ -9,8 +9,6 @@ class QueryParameterConnectorBlankRequest extends SaloonRequest { - use HasQueryParams; - /** * Define the method that the request will use. * diff --git a/tests/Resources/Requests/QueryParameterConnectorRequest.php b/tests/Resources/Requests/QueryParameterConnectorRequest.php index ee3bbc7d..949ecadc 100644 --- a/tests/Resources/Requests/QueryParameterConnectorRequest.php +++ b/tests/Resources/Requests/QueryParameterConnectorRequest.php @@ -9,8 +9,6 @@ class QueryParameterConnectorRequest extends SaloonRequest { - use HasQueryParams; - /** * Define the method that the request will use. * diff --git a/tests/Resources/Requests/QueryParameterRequest.php b/tests/Resources/Requests/QueryParameterRequest.php index e36b1103..aea9a4fc 100644 --- a/tests/Resources/Requests/QueryParameterRequest.php +++ b/tests/Resources/Requests/QueryParameterRequest.php @@ -9,8 +9,6 @@ class QueryParameterRequest extends SaloonRequest { - use HasQueryParams; - /** * Define the method that the request will use. * diff --git a/tests/Unit/Features/QueryParameterTest.php b/tests/Unit/Features/QueryParameterTest.php index 026bb0b2..85a74e85 100644 --- a/tests/Unit/Features/QueryParameterTest.php +++ b/tests/Unit/Features/QueryParameterTest.php @@ -5,6 +5,7 @@ use Sammyjo20\Saloon\Tests\Resources\Requests\QueryParameterConnectorRequest; use Sammyjo20\Saloon\Tests\Resources\Requests\QueryParameterConnectorBlankRequest; use Sammyjo20\Saloon\Tests\Resources\Requests\OverwrittenQueryParameterConnectorRequest; +use Sammyjo20\Saloon\Tests\Resources\Requests\UserRequest; test('a request with the hasQueryParams feature sends the query params', function () { $request = new QueryParameterRequest(); @@ -95,3 +96,15 @@ expect($config)->toHaveKey('query'); expect($config['query'])->toEqual(['per_page' => 500]); }); + +test('when not sending query parameters, the query option is not set', function () { + $request = new UserRequest(); + + $requestManager = new RequestManager($request); + + $requestManager->hydrate(); + + $config = $requestManager->getConfig(); + + expect(isset($config['query']))->toBeFalse(); +}); From 1d8ca97fa668491d411933e04c8b63b27febfce1 Mon Sep 17 00:00:00 2001 From: Sammyjo20 Date: Fri, 28 Jan 2022 22:48:52 +0000 Subject: [PATCH 2/2] Apply Code Style Fixes --- src/Managers/RequestManager.php | 2 +- tests/Resources/Connectors/QueryParameterConnector.php | 1 - .../Requests/OverwrittenQueryParameterConnectorRequest.php | 1 - .../Resources/Requests/QueryParameterConnectorBlankRequest.php | 1 - tests/Resources/Requests/QueryParameterConnectorRequest.php | 1 - tests/Resources/Requests/QueryParameterRequest.php | 1 - tests/Unit/Features/QueryParameterTest.php | 2 +- 7 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Managers/RequestManager.php b/src/Managers/RequestManager.php index 8aca2014..32d8f5ac 100644 --- a/src/Managers/RequestManager.php +++ b/src/Managers/RequestManager.php @@ -9,7 +9,6 @@ use Sammyjo20\Saloon\Http\SaloonRequest; use Sammyjo20\Saloon\Http\SaloonResponse; use Sammyjo20\Saloon\Http\SaloonConnector; -use Sammyjo20\Saloon\Traits\CollectsQueryParams; use Sammyjo20\Saloon\Traits\ManagesGuzzle; use Sammyjo20\Saloon\Traits\CollectsConfig; use Sammyjo20\Saloon\Clients\BaseMockClient; @@ -17,6 +16,7 @@ use Sammyjo20\Saloon\Traits\ManagesFeatures; use Sammyjo20\Saloon\Traits\CollectsHandlers; use GuzzleHttp\Exception\BadResponseException; +use Sammyjo20\Saloon\Traits\CollectsQueryParams; use Sammyjo20\Saloon\Traits\CollectsInterceptors; use Sammyjo20\Saloon\Exceptions\SaloonMultipleMockMethodsException; use Sammyjo20\Saloon\Exceptions\SaloonInvalidResponseClassException; diff --git a/tests/Resources/Connectors/QueryParameterConnector.php b/tests/Resources/Connectors/QueryParameterConnector.php index 2da410e3..6d2716ef 100644 --- a/tests/Resources/Connectors/QueryParameterConnector.php +++ b/tests/Resources/Connectors/QueryParameterConnector.php @@ -4,7 +4,6 @@ use Sammyjo20\Saloon\Http\SaloonConnector; use Sammyjo20\Saloon\Traits\Features\AcceptsJson; -use Sammyjo20\Saloon\Traits\Features\HasQueryParams; class QueryParameterConnector extends SaloonConnector { diff --git a/tests/Resources/Requests/OverwrittenQueryParameterConnectorRequest.php b/tests/Resources/Requests/OverwrittenQueryParameterConnectorRequest.php index b04f868f..26da3f56 100644 --- a/tests/Resources/Requests/OverwrittenQueryParameterConnectorRequest.php +++ b/tests/Resources/Requests/OverwrittenQueryParameterConnectorRequest.php @@ -4,7 +4,6 @@ use Sammyjo20\Saloon\Constants\Saloon; use Sammyjo20\Saloon\Http\SaloonRequest; -use Sammyjo20\Saloon\Traits\Features\HasQueryParams; use Sammyjo20\Saloon\Tests\Resources\Connectors\QueryParameterConnector; class OverwrittenQueryParameterConnectorRequest extends SaloonRequest diff --git a/tests/Resources/Requests/QueryParameterConnectorBlankRequest.php b/tests/Resources/Requests/QueryParameterConnectorBlankRequest.php index 35b9fd57..566f44e2 100644 --- a/tests/Resources/Requests/QueryParameterConnectorBlankRequest.php +++ b/tests/Resources/Requests/QueryParameterConnectorBlankRequest.php @@ -4,7 +4,6 @@ use Sammyjo20\Saloon\Constants\Saloon; use Sammyjo20\Saloon\Http\SaloonRequest; -use Sammyjo20\Saloon\Traits\Features\HasQueryParams; use Sammyjo20\Saloon\Tests\Resources\Connectors\QueryParameterConnector; class QueryParameterConnectorBlankRequest extends SaloonRequest diff --git a/tests/Resources/Requests/QueryParameterConnectorRequest.php b/tests/Resources/Requests/QueryParameterConnectorRequest.php index 949ecadc..7b241ce9 100644 --- a/tests/Resources/Requests/QueryParameterConnectorRequest.php +++ b/tests/Resources/Requests/QueryParameterConnectorRequest.php @@ -4,7 +4,6 @@ use Sammyjo20\Saloon\Constants\Saloon; use Sammyjo20\Saloon\Http\SaloonRequest; -use Sammyjo20\Saloon\Traits\Features\HasQueryParams; use Sammyjo20\Saloon\Tests\Resources\Connectors\QueryParameterConnector; class QueryParameterConnectorRequest extends SaloonRequest diff --git a/tests/Resources/Requests/QueryParameterRequest.php b/tests/Resources/Requests/QueryParameterRequest.php index aea9a4fc..03dbd8dd 100644 --- a/tests/Resources/Requests/QueryParameterRequest.php +++ b/tests/Resources/Requests/QueryParameterRequest.php @@ -4,7 +4,6 @@ use Sammyjo20\Saloon\Constants\Saloon; use Sammyjo20\Saloon\Http\SaloonRequest; -use Sammyjo20\Saloon\Traits\Features\HasQueryParams; use Sammyjo20\Saloon\Tests\Resources\Connectors\TestConnector; class QueryParameterRequest extends SaloonRequest diff --git a/tests/Unit/Features/QueryParameterTest.php b/tests/Unit/Features/QueryParameterTest.php index 85a74e85..9aabb7a3 100644 --- a/tests/Unit/Features/QueryParameterTest.php +++ b/tests/Unit/Features/QueryParameterTest.php @@ -1,11 +1,11 @@