Skip to content

Commit

Permalink
Merge pull request #20 from Sammyjo20/fix/no-more-has-query-params-trait
Browse files Browse the repository at this point in the history
Removed requirement for HasQueryParams trait
  • Loading branch information
Sammyjo20 authored Jan 28, 2022
2 parents 9ac3bca + 1d8ca97 commit d5bbca4
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 47 deletions.
43 changes: 34 additions & 9 deletions src/Managers/RequestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,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;
Expand All @@ -27,6 +28,7 @@ class RequestManager
ManagesFeatures,
CollectsHeaders,
CollectsConfig,
CollectsQueryParams,
CollectsHandlers,
CollectsInterceptors;

Expand Down Expand Up @@ -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
{
Expand All @@ -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());
Expand Down Expand Up @@ -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...

Expand Down Expand Up @@ -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?
*
Expand Down
14 changes: 1 addition & 13 deletions src/Traits/CollectsQueryParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()`?
*
Expand Down
11 changes: 0 additions & 11 deletions src/Traits/Features/HasQueryParams.php

This file was deleted.

2 changes: 0 additions & 2 deletions tests/Resources/Connectors/QueryParameterConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use Sammyjo20\Saloon\Http\SaloonConnector;
use Sammyjo20\Saloon\Traits\Features\AcceptsJson;
use Sammyjo20\Saloon\Traits\Features\HasQueryParams;

class QueryParameterConnector extends SaloonConnector
{
use AcceptsJson;
use HasQueryParams;

public function defineBaseUrl(): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

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
{
use HasQueryParams;

/**
* Define the method that the request will use.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

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
{
use HasQueryParams;

/**
* Define the method that the request will use.
*
Expand Down
3 changes: 0 additions & 3 deletions tests/Resources/Requests/QueryParameterConnectorRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

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
{
use HasQueryParams;

/**
* Define the method that the request will use.
*
Expand Down
3 changes: 0 additions & 3 deletions tests/Resources/Requests/QueryParameterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

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
{
use HasQueryParams;

/**
* Define the method that the request will use.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Features/QueryParameterTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Sammyjo20\Saloon\Managers\RequestManager;
use Sammyjo20\Saloon\Tests\Resources\Requests\UserRequest;
use Sammyjo20\Saloon\Tests\Resources\Requests\QueryParameterRequest;
use Sammyjo20\Saloon\Tests\Resources\Requests\QueryParameterConnectorRequest;
use Sammyjo20\Saloon\Tests\Resources\Requests\QueryParameterConnectorBlankRequest;
Expand Down Expand Up @@ -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();
});

0 comments on commit d5bbca4

Please sign in to comment.