Skip to content

Commit 6edaad4

Browse files
authored
Merge pull request #487 from saloonphp/revert-424-feat/assert-sent-typehint
Revert "Feature | Accept type-hinted requests as `assertSent` parameters"
2 parents c3777e9 + cc1de84 commit 6edaad4

File tree

3 files changed

+1
-87
lines changed

3 files changed

+1
-87
lines changed

phpstan.baseline.neon

-15
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,3 @@ parameters:
4949
message: "#^Match arm is unreachable because previous comparison is always true.$#"
5050
count: 1
5151
path: src/Http/Pool.php
52-
53-
-
54-
message: "#^Method Saloon\\\\Http\\\\Faking\\\\MockClient\\:\\:getRequestClass\\(\\) should return class-string|null but returns class-string<Saloon\\\\Http\\\\Request>|Saloon\\\\Http\\\\Request$#"
55-
count: 1
56-
path: src/Http/Faking/MockClient.php
57-
58-
-
59-
message: "#^Call to an undefined method ReflectionType\\:\\:getName\\(\\)\\.$#"
60-
count: 1
61-
path: src/Http/Faking/MockClient.php
62-
63-
-
64-
message: "#^Parameter \\#1 \\$function of class ReflectionFunction constructor expects Closure|string, callable\\(\\)\\: mixed given\\.$#"
65-
count: 1
66-
path: src/Http/Faking/MockClient.php

src/Http/Faking/MockClient.php

+1-37
Original file line numberDiff line numberDiff line change
@@ -458,22 +458,7 @@ private function checkClosureAgainstResponses(callable $closure, ?int $index = n
458458
return $closure($request, $response);
459459
}
460460

461-
// Let's first check if the callable type-hints the latest request class.
462-
// If so, we try to find the corresponding request in the recorded responses
463-
// and call the callable accordingly. We will only fail if it returns `false`.
464-
465-
if ($fqcn = $this->getRequestClass($closure)) {
466-
/** @var Response */
467-
foreach ($this->getRecordedResponses() as $response) {
468-
if (get_class($request = $response->getPendingRequest()->getRequest()) !== $fqcn) {
469-
continue;
470-
}
471-
472-
return $closure($request, $response) !== false;
473-
}
474-
}
475-
476-
// Let's then check if the latest response resolves the callable
461+
// Let's first check if the latest response resolves the callable
477462
// with a successful result.
478463

479464
$lastResponse = $this->getLastResponse();
@@ -539,25 +524,4 @@ private function getRequestSentCount(): array
539524

540525
return array_count_values($requests);
541526
}
542-
543-
/**
544-
* Get the FQCN of the request class if type-hinted.
545-
*
546-
* @return class-string
547-
*/
548-
private function getRequestClass(callable $closure): ?string
549-
{
550-
$reflection = new \ReflectionFunction($closure);
551-
$parameters = $reflection->getParameters();
552-
553-
if (! ($fqcn = $parameters[0]->getType()?->getName())) {
554-
return null;
555-
}
556-
557-
if (! is_a($fqcn, Request::class, allow_string: true)) {
558-
return null;
559-
}
560-
561-
return $fqcn;
562-
}
563527
}

tests/Unit/MockClientTest.php

-35
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
declare(strict_types=1);
44

5-
use Pest\Expectation;
65
use Saloon\Http\Faking\MockClient;
76
use Saloon\Http\Faking\MockResponse;
87
use Saloon\Tests\Fixtures\Requests\UserRequest;
98
use Saloon\Tests\Fixtures\Requests\ErrorRequest;
10-
use PHPUnit\Framework\ExpectationFailedException;
119
use Saloon\Exceptions\NoMockResponseFoundException;
1210
use Saloon\Tests\Fixtures\Connectors\TestConnector;
1311
use Saloon\Tests\Fixtures\Exceptions\TestResponseException;
@@ -263,36 +261,3 @@
263261
$response = connector()->send(new UserRequest, $mockClient);
264262
$response->throw();
265263
});
266-
267-
test('`assertSent` accepts the request class as a type-hint', function () {
268-
$mockClient = new MockClient([
269-
MockResponse::make(['name' => 'Sam']),
270-
]);
271-
272-
$request = new UserRequest;
273-
$request->headers()->add('X-Foo', 'bar');
274-
275-
connector()->send($request, $mockClient);
276-
277-
$mockClient->assertSent(function (UserRequest $request) {
278-
expect($request->headers()->all())->toMatchArray([
279-
'X-Foo' => 'bar',
280-
]);
281-
});
282-
});
283-
284-
test('`assertSent` fails or succeeds depending on the closure result when the closure is type-hinted', function (mixed $returns, bool $shouldThrow) {
285-
$mockClient = new MockClient([
286-
MockResponse::make(['name' => 'Sam']),
287-
]);
288-
289-
connector()->send(new UserRequest, $mockClient);
290-
291-
expect(fn () => $mockClient->assertSent(fn (UserRequest $request) => $returns))
292-
->when($shouldThrow, fn (Expectation $e) => $e->toThrow(ExpectationFailedException::class))
293-
->when(! $shouldThrow, fn (Expectation $e) => $e->not->toThrow(ExpectationFailedException::class));
294-
})->with([
295-
[false, true],
296-
[true, false],
297-
[null, false],
298-
]);

0 commit comments

Comments
 (0)