Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/Http/SaloonConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use ReflectionClass;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Sammyjo20\Saloon\Traits\HasMake;
use Sammyjo20\Saloon\Clients\MockClient;
use Sammyjo20\Saloon\Traits\CollectsData;
use Sammyjo20\Saloon\Traits\CollectsConfig;
use Sammyjo20\Saloon\Traits\CollectsHeaders;
Expand All @@ -29,8 +29,7 @@ abstract class SaloonConnector implements SaloonConnectorInterface
CollectsHandlers,
CollectsInterceptors,
AuthenticatesRequests,
HasCustomResponses,
HasMake;
HasCustomResponses;

/**
* Register Saloon requests that will become methods on the connector.
Expand All @@ -47,6 +46,17 @@ abstract class SaloonConnector implements SaloonConnectorInterface
*/
private ?array $registeredRequests = null;

/**
* Instantiate a new class with the arguments.
*
* @param mixed ...$arguments
* @return SaloonConnector
*/
public static function make(...$arguments): self
{
return new static(...$arguments);
}

/**
* Define anything that should be added to any requests
* with this connector before the request is sent.
Expand Down Expand Up @@ -127,6 +137,29 @@ public function requestExists(string $method): bool
return method_exists($this, $method) || array_key_exists($method, $this->getRegisteredRequests());
}

/**
* Prepare a new request by providing it the current instance of the connector.
*
* @param SaloonRequest $request
* @return SaloonRequest
*/
public function request(SaloonRequest $request): SaloonRequest
{
return $request->setConnector($this);
}

/**
* Send a Saloon request with the current instance of the connector.
*
* @throws \ReflectionException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Sammyjo20\Saloon\Exceptions\SaloonException
*/
public function send(SaloonRequest $request, MockClient $mockClient = null): SaloonResponse
{
return $this->request($request)->send($mockClient);
}

/**
* Dynamically proxy other methods to try and call a requests.
*
Expand Down
15 changes: 12 additions & 3 deletions src/Http/SaloonRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Sammyjo20\Saloon\Http;

use Sammyjo20\Saloon\Traits\HasMake;
use Sammyjo20\Saloon\Traits\CollectsData;
use Sammyjo20\Saloon\Traits\SendsRequests;
use Sammyjo20\Saloon\Traits\CollectsConfig;
Expand All @@ -27,8 +26,7 @@ abstract class SaloonRequest implements SaloonRequestInterface
CollectsInterceptors,
AuthenticatesRequests,
HasCustomResponses,
SendsRequests,
HasMake;
SendsRequests;

/**
* Define the method that the request will use.
Expand All @@ -51,6 +49,17 @@ abstract class SaloonRequest implements SaloonRequestInterface
*/
private ?SaloonConnector $loadedConnector = null;

/**
* Instantiate a new class with the arguments.
*
* @param mixed ...$arguments
* @return SaloonRequest
*/
public static function make(...$arguments): self
{
return new static(...$arguments);
}

/**
* Define anything that should be added to the request
* before it is sent.
Expand Down
8 changes: 4 additions & 4 deletions src/Http/SaloonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SaloonResponse
*
* @var object|null
*/
protected ?object $dto = null;
protected mixed $dto = null;

/**
* Determines if the response has been cached
Expand Down Expand Up @@ -195,7 +195,7 @@ public function collect($key = null): Collection
*
* @return object|null
*/
public function dto(): ?object
public function dto(): mixed
{
return $this->dto;
}
Expand Down Expand Up @@ -436,10 +436,10 @@ public function getGuzzleException(): ?RequestException
/**
* Set the DTO on the response.
*
* @param object $dto
* @param mixed $dto
* @return $this
*/
public function setDto(object $dto): self
public function setDto(mixed $dto): self
{
$this->dto = $dto;

Expand Down
17 changes: 0 additions & 17 deletions src/Traits/HasMake.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Traits/Plugins/CastsToDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function bootCastsToDto(SaloonRequest $request): void
* Define how Saloon should cast to your DTO.
*
* @param SaloonResponse $response
* @return object
* @return mixed
*/
abstract protected function castToDto(SaloonResponse $response): object;
abstract protected function castToDto(SaloonResponse $response): mixed;
}
8 changes: 4 additions & 4 deletions tests/Feature/MockRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@
$requestC = new DifferentServiceUserRequest;

$mockClient = new MockClient([
'saloon-test.samcarre.dev/api/user' => $responseA, // Test Exact Route
'saloon-test.samcarre.dev/*' => $responseB, // Test Wildcard Routes
'tests.saloon.dev/api/user' => $responseA, // Test Exact Route
'tests.saloon.dev/*' => $responseB, // Test Wildcard Routes
'google.com/*' => $responseC, // Test Different Route,
]);

Expand Down Expand Up @@ -134,8 +134,8 @@
$requestC = new DifferentServiceUserRequest;

$mockClient = new MockClient([
'saloon-test.samcarre.dev/api/user' => $responseA, // Test Exact Route
'saloon-test.samcarre.dev/*' => $responseB, // Test Wildcard Routes
'tests.saloon.dev/api/user' => $responseA, // Test Exact Route
'tests.saloon.dev/*' => $responseB, // Test Wildcard Routes
'*' => $responseC,
]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@

function apiUrl()
{
return 'https://saloon-test.samcarre.dev/api';
return 'https://tests.saloon.dev/api';
}
27 changes: 27 additions & 0 deletions tests/Unit/ConnectorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

use Sammyjo20\Saloon\Http\MockResponse;
use Sammyjo20\Saloon\Clients\MockClient;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Http\SaloonResponse;
use Sammyjo20\Saloon\Tests\Fixtures\Requests\UserRequest;
use Sammyjo20\Saloon\Tests\Fixtures\Connectors\TestConnector;
use Sammyjo20\Saloon\Tests\Fixtures\Connectors\RequestSelectionConnector;

Expand All @@ -13,3 +18,25 @@
expect($connectorB)->toBeInstanceOf(RequestSelectionConnector::class);
expect($connectorB)->apiKey->toEqual('yee-haw-1-2-3');
});

test('you can prepare a request through the connector', function () {
$connector = new TestConnector();
$connector->unique = true;

$request = $connector->request(new UserRequest);

expect($request)->toBeInstanceOf(SaloonRequest::class);
expect($request->getConnector())->toEqual($connector);
});

test('you can send a request through the connector', function () {
$mockClient = new MockClient([
new MockResponse(['name' => 'Sammyjo20', 'actual_name' => 'Sam Carré', 'twitter' => '@carre_sam']),
]);

$connector = new TestConnector();
$response = $connector->send(new UserRequest, $mockClient);

expect($response)->toBeInstanceOf(SaloonResponse::class);
expect($response->json())->toEqual(['name' => 'Sammyjo20', 'actual_name' => 'Sam Carré', 'twitter' => '@carre_sam']);
});
2 changes: 1 addition & 1 deletion tests/Unit/MockClientAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

(new UserRequest())->send($mockClient);

$mockClient->assertSent('samcarre.dev/*');
$mockClient->assertSent('saloon.dev/*');
$mockClient->assertSent('/user');
$mockClient->assertSent('api/user');
});
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/MockClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
$requestC = new DifferentServiceUserRequest;

$mockClient = new MockClient([
'saloon-test.samcarre.dev/api/user' => $responseA, // Test Exact Route
'saloon-test.samcarre.dev/*' => $responseB, // Test Wildcard Routes
'tests.saloon.dev/api/user' => $responseA, // Test Exact Route
'tests.saloon.dev/*' => $responseB, // Test Wildcard Routes
'google.com/*' => $responseC, // Test Different Route,
]);

Expand All @@ -85,8 +85,8 @@
$requestC = new DifferentServiceUserRequest;

$mockClient = new MockClient([
'saloon-test.samcarre.dev/api/user' => $responseA, // Test Exact Route
'saloon-test.samcarre.dev/*' => $responseB, // Test Wildcard Routes
'tests.saloon.dev/api/user' => $responseA, // Test Exact Route
'tests.saloon.dev/*' => $responseB, // Test Wildcard Routes
'*' => $responseC,
]);

Expand All @@ -105,8 +105,8 @@
$requestC = new DifferentServiceUserRequest;

$mockClient = new MockClient([
'saloon-test.samcarre.dev/api/user' => $responseA, // Test Exact Route
'saloon-test.samcarre.dev/*' => $responseB, // Test Wildcard Routes
'tests.saloon.dev/api/user' => $responseA, // Test Exact Route
'tests.saloon.dev/*' => $responseB, // Test Wildcard Routes
]);

expect($mockClient->guessNextResponse($requestA))->toEqual($responseA);
Expand Down