Skip to content

Commit cfbaa1d

Browse files
committed
More coverage
1 parent e250ad9 commit cfbaa1d

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/Clients/BaseMockClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ protected function checkRequestWasSent(string|callable $request): bool
331331
if (class_exists($request) && ReflectionHelper::isSubclassOf($request, SaloonRequest::class)) {
332332
$result = $this->findResponseByRequest($request) instanceof SaloonResponse;
333333
} else {
334-
$result = $this->findResponseWithRequestUrl($request) instanceof SaloonResponse;
334+
$result = $this->findResponseByRequestUrl($request) instanceof SaloonResponse;
335335
}
336336
}
337337

@@ -357,7 +357,7 @@ protected function checkRequestWasNotSent(string|callable $request): bool
357357
* @param string $request
358358
* @return SaloonResponse|null
359359
*/
360-
protected function findResponseByRequest(string $request): ?SaloonResponse
360+
public function findResponseByRequest(string $request): ?SaloonResponse
361361
{
362362
$lastRequest = $this->getLastRequest();
363363

@@ -381,7 +381,7 @@ protected function findResponseByRequest(string $request): ?SaloonResponse
381381
* @return SaloonResponse|null
382382
* @throws \Sammyjo20\Saloon\Exceptions\SaloonInvalidConnectorException
383383
*/
384-
protected function findResponseWithRequestUrl(string $url): ?SaloonResponse
384+
public function findResponseByRequestUrl(string $url): ?SaloonResponse
385385
{
386386
$lastRequest = $this->getLastRequest();
387387

tests/Unit/MockClientTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,43 @@
168168

169169
expect($mockClient)->getLastRequest()->toBeNull();
170170
});
171+
172+
test('if the response is not the last response it will use the loop to find it', function () {
173+
$mockClient = new MockClient([
174+
new MockResponse(['name' => 'Sam'], 200),
175+
new MockResponse(['error' => 'Server Error'], 500),
176+
]);
177+
178+
$responseA = (new ErrorRequest())->send($mockClient);
179+
$responseB = (new UserRequest())->send($mockClient);
180+
181+
expect($mockClient)->getLastResponse()->toBe($responseB);
182+
183+
// Uses last response
184+
185+
expect($mockClient)->findResponseByRequest(UserRequest::class)->toBe($responseB);
186+
187+
// Does not use the last response
188+
189+
expect($mockClient)->findResponseByRequest(ErrorRequest::class)->toBe($responseA);
190+
});
191+
192+
test('it will find the response by url if it is not the last response', function () {
193+
$mockClient = new MockClient([
194+
'/user' => new MockResponse(['name' => 'Sam'], 200),
195+
'/error' => new MockResponse(['error' => 'Server Error'], 500),
196+
]);
197+
198+
$responseA = (new ErrorRequest())->send($mockClient);
199+
$responseB = (new UserRequest())->send($mockClient);
200+
201+
expect($mockClient)->getLastResponse()->toBe($responseB);
202+
203+
// Uses last response
204+
205+
expect($mockClient)->findResponseByRequestUrl('/user')->toBe($responseB);
206+
207+
// Does not use the last response
208+
209+
expect($mockClient)->findResponseByRequestUrl('/error')->toBe($responseA);
210+
});

0 commit comments

Comments
 (0)