diff --git a/src/Http/SaloonConnector.php b/src/Http/SaloonConnector.php index d517c5e3..3feaf7d3 100644 --- a/src/Http/SaloonConnector.php +++ b/src/Http/SaloonConnector.php @@ -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; @@ -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. @@ -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. @@ -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. * diff --git a/src/Http/SaloonRequest.php b/src/Http/SaloonRequest.php index a6a3d64c..548a86da 100644 --- a/src/Http/SaloonRequest.php +++ b/src/Http/SaloonRequest.php @@ -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; @@ -27,8 +26,7 @@ abstract class SaloonRequest implements SaloonRequestInterface CollectsInterceptors, AuthenticatesRequests, HasCustomResponses, - SendsRequests, - HasMake; + SendsRequests; /** * Define the method that the request will use. @@ -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. diff --git a/src/Http/SaloonResponse.php b/src/Http/SaloonResponse.php index eb27c381..a9e7ae4d 100644 --- a/src/Http/SaloonResponse.php +++ b/src/Http/SaloonResponse.php @@ -62,7 +62,7 @@ class SaloonResponse * * @var object|null */ - protected ?object $dto = null; + protected mixed $dto = null; /** * Determines if the response has been cached @@ -195,7 +195,7 @@ public function collect($key = null): Collection * * @return object|null */ - public function dto(): ?object + public function dto(): mixed { return $this->dto; } @@ -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; diff --git a/src/Traits/HasMake.php b/src/Traits/HasMake.php deleted file mode 100644 index 28401b58..00000000 --- a/src/Traits/HasMake.php +++ /dev/null @@ -1,17 +0,0 @@ - $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, ]); @@ -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, ]); diff --git a/tests/Pest.php b/tests/Pest.php index 8c049d32..11c47420 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -41,5 +41,5 @@ function apiUrl() { - return 'https://saloon-test.samcarre.dev/api'; + return 'https://tests.saloon.dev/api'; } diff --git a/tests/Unit/ConnectorTest.php b/tests/Unit/ConnectorTest.php index dbbc9658..74949dbf 100644 --- a/tests/Unit/ConnectorTest.php +++ b/tests/Unit/ConnectorTest.php @@ -1,5 +1,10 @@ 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']); +}); diff --git a/tests/Unit/MockClientAssertionsTest.php b/tests/Unit/MockClientAssertionsTest.php index 6e2be3e9..7391fcd2 100644 --- a/tests/Unit/MockClientAssertionsTest.php +++ b/tests/Unit/MockClientAssertionsTest.php @@ -57,7 +57,7 @@ (new UserRequest())->send($mockClient); - $mockClient->assertSent('samcarre.dev/*'); + $mockClient->assertSent('saloon.dev/*'); $mockClient->assertSent('/user'); $mockClient->assertSent('api/user'); }); diff --git a/tests/Unit/MockClientTest.php b/tests/Unit/MockClientTest.php index e177d479..d611baac 100644 --- a/tests/Unit/MockClientTest.php +++ b/tests/Unit/MockClientTest.php @@ -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, ]); @@ -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, ]); @@ -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);