Skip to content

Commit cc895fe

Browse files
authored
Merge pull request #47 from Sammyjo20/feature/send-requests-on-connector
Added new method to send requests on connectors
2 parents 2a148a9 + d0c9fb6 commit cc895fe

File tree

10 files changed

+93
-41
lines changed

10 files changed

+93
-41
lines changed

src/Http/SaloonConnector.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use ReflectionClass;
66
use Illuminate\Support\Str;
77
use Illuminate\Support\Collection;
8-
use Sammyjo20\Saloon\Traits\HasMake;
8+
use Sammyjo20\Saloon\Clients\MockClient;
99
use Sammyjo20\Saloon\Traits\CollectsData;
1010
use Sammyjo20\Saloon\Traits\CollectsConfig;
1111
use Sammyjo20\Saloon\Traits\CollectsHeaders;
@@ -29,8 +29,7 @@ abstract class SaloonConnector implements SaloonConnectorInterface
2929
CollectsHandlers,
3030
CollectsInterceptors,
3131
AuthenticatesRequests,
32-
HasCustomResponses,
33-
HasMake;
32+
HasCustomResponses;
3433

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

49+
/**
50+
* Instantiate a new class with the arguments.
51+
*
52+
* @param mixed ...$arguments
53+
* @return SaloonConnector
54+
*/
55+
public static function make(...$arguments): self
56+
{
57+
return new static(...$arguments);
58+
}
59+
5060
/**
5161
* Define anything that should be added to any requests
5262
* with this connector before the request is sent.
@@ -127,6 +137,29 @@ public function requestExists(string $method): bool
127137
return method_exists($this, $method) || array_key_exists($method, $this->getRegisteredRequests());
128138
}
129139

140+
/**
141+
* Prepare a new request by providing it the current instance of the connector.
142+
*
143+
* @param SaloonRequest $request
144+
* @return SaloonRequest
145+
*/
146+
public function request(SaloonRequest $request): SaloonRequest
147+
{
148+
return $request->setConnector($this);
149+
}
150+
151+
/**
152+
* Send a Saloon request with the current instance of the connector.
153+
*
154+
* @throws \ReflectionException
155+
* @throws \GuzzleHttp\Exception\GuzzleException
156+
* @throws \Sammyjo20\Saloon\Exceptions\SaloonException
157+
*/
158+
public function send(SaloonRequest $request, MockClient $mockClient = null): SaloonResponse
159+
{
160+
return $this->request($request)->send($mockClient);
161+
}
162+
130163
/**
131164
* Dynamically proxy other methods to try and call a requests.
132165
*

src/Http/SaloonRequest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Sammyjo20\Saloon\Http;
44

5-
use Sammyjo20\Saloon\Traits\HasMake;
65
use Sammyjo20\Saloon\Traits\CollectsData;
76
use Sammyjo20\Saloon\Traits\SendsRequests;
87
use Sammyjo20\Saloon\Traits\CollectsConfig;
@@ -27,8 +26,7 @@ abstract class SaloonRequest implements SaloonRequestInterface
2726
CollectsInterceptors,
2827
AuthenticatesRequests,
2928
HasCustomResponses,
30-
SendsRequests,
31-
HasMake;
29+
SendsRequests;
3230

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

52+
/**
53+
* Instantiate a new class with the arguments.
54+
*
55+
* @param mixed ...$arguments
56+
* @return SaloonRequest
57+
*/
58+
public static function make(...$arguments): self
59+
{
60+
return new static(...$arguments);
61+
}
62+
5463
/**
5564
* Define anything that should be added to the request
5665
* before it is sent.

src/Http/SaloonResponse.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class SaloonResponse
6262
*
6363
* @var object|null
6464
*/
65-
protected ?object $dto = null;
65+
protected mixed $dto = null;
6666

6767
/**
6868
* Determines if the response has been cached
@@ -195,7 +195,7 @@ public function collect($key = null): Collection
195195
*
196196
* @return object|null
197197
*/
198-
public function dto(): ?object
198+
public function dto(): mixed
199199
{
200200
return $this->dto;
201201
}
@@ -436,10 +436,10 @@ public function getGuzzleException(): ?RequestException
436436
/**
437437
* Set the DTO on the response.
438438
*
439-
* @param object $dto
439+
* @param mixed $dto
440440
* @return $this
441441
*/
442-
public function setDto(object $dto): self
442+
public function setDto(mixed $dto): self
443443
{
444444
$this->dto = $dto;
445445

src/Traits/HasMake.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Traits/Plugins/CastsToDto.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function bootCastsToDto(SaloonRequest $request): void
2929
* Define how Saloon should cast to your DTO.
3030
*
3131
* @param SaloonResponse $response
32-
* @return object
32+
* @return mixed
3333
*/
34-
abstract protected function castToDto(SaloonResponse $response): object;
34+
abstract protected function castToDto(SaloonResponse $response): mixed;
3535
}

tests/Feature/MockRequestTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@
100100
$requestC = new DifferentServiceUserRequest;
101101

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

@@ -134,8 +134,8 @@
134134
$requestC = new DifferentServiceUserRequest;
135135

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

tests/Pest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@
4141

4242
function apiUrl()
4343
{
44-
return 'https://saloon-test.samcarre.dev/api';
44+
return 'https://tests.saloon.dev/api';
4545
}

tests/Unit/ConnectorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
use Sammyjo20\Saloon\Http\MockResponse;
4+
use Sammyjo20\Saloon\Clients\MockClient;
5+
use Sammyjo20\Saloon\Http\SaloonRequest;
6+
use Sammyjo20\Saloon\Http\SaloonResponse;
7+
use Sammyjo20\Saloon\Tests\Fixtures\Requests\UserRequest;
38
use Sammyjo20\Saloon\Tests\Fixtures\Connectors\TestConnector;
49
use Sammyjo20\Saloon\Tests\Fixtures\Connectors\RequestSelectionConnector;
510

@@ -13,3 +18,25 @@
1318
expect($connectorB)->toBeInstanceOf(RequestSelectionConnector::class);
1419
expect($connectorB)->apiKey->toEqual('yee-haw-1-2-3');
1520
});
21+
22+
test('you can prepare a request through the connector', function () {
23+
$connector = new TestConnector();
24+
$connector->unique = true;
25+
26+
$request = $connector->request(new UserRequest);
27+
28+
expect($request)->toBeInstanceOf(SaloonRequest::class);
29+
expect($request->getConnector())->toEqual($connector);
30+
});
31+
32+
test('you can send a request through the connector', function () {
33+
$mockClient = new MockClient([
34+
new MockResponse(['name' => 'Sammyjo20', 'actual_name' => 'Sam Carré', 'twitter' => '@carre_sam']),
35+
]);
36+
37+
$connector = new TestConnector();
38+
$response = $connector->send(new UserRequest, $mockClient);
39+
40+
expect($response)->toBeInstanceOf(SaloonResponse::class);
41+
expect($response->json())->toEqual(['name' => 'Sammyjo20', 'actual_name' => 'Sam Carré', 'twitter' => '@carre_sam']);
42+
});

tests/Unit/MockClientAssertionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

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

60-
$mockClient->assertSent('samcarre.dev/*');
60+
$mockClient->assertSent('saloon.dev/*');
6161
$mockClient->assertSent('/user');
6262
$mockClient->assertSent('api/user');
6363
});

tests/Unit/MockClientTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
$requestC = new DifferentServiceUserRequest;
6666

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

@@ -85,8 +85,8 @@
8585
$requestC = new DifferentServiceUserRequest;
8686

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

@@ -105,8 +105,8 @@
105105
$requestC = new DifferentServiceUserRequest;
106106

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

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

0 commit comments

Comments
 (0)