Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature | Add assertSentInOrder assertion to MockClient #400

Merged
merged 3 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
57 changes: 50 additions & 7 deletions src/Http/Faking/MockClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,23 @@ public function assertNotSent(string|callable $request): void
PHPUnit::assertTrue($result, 'An unexpected request was sent.');
}

/**
* Assert that given requests were sent in order
*
* @param array<\Closure|class-string<Request>|string> $callbacks
* @return void
*/
public function assertSentInOrder(array $callbacks): void
{
$this->assertSentCount(count($callbacks));

foreach ($callbacks as $index => $callback) {
$result = $this->checkRequestWasSent($callback, $index);

PHPUnit::assertTrue($result, 'An expected request (#'.($index + 1).') was not sent.');
}
}

/**
* Assert JSON response data was received
*
Expand Down Expand Up @@ -300,19 +317,19 @@ public function assertSentCount(int $count, string $requestClass = null): void
/**
* Check if a given request was sent
*/
protected function checkRequestWasSent(string|callable $request): bool
protected function checkRequestWasSent(string|callable $request, ?int $index = null): bool
{
$passed = false;

if (is_callable($request)) {
return $this->checkClosureAgainstResponses($request);
return $this->checkClosureAgainstResponses($request, $index);
}

if (is_string($request)) {
if (class_exists($request) && Helpers::isSubclassOf($request, Request::class)) {
$passed = $this->findResponseByRequest($request) instanceof Response;
$passed = $this->findResponseByRequest($request, $index) instanceof Response;
} else {
$passed = $this->findResponseByRequestUrl($request) instanceof Response;
$passed = $this->findResponseByRequestUrl($request, $index) instanceof Response;
}
}

Expand All @@ -330,12 +347,20 @@ protected function checkRequestWasNotSent(string|callable $request): bool
/**
* Assert a given request was sent.
*/
public function findResponseByRequest(string $request): ?Response
public function findResponseByRequest(string $request, ?int $index = null): ?Response
{
if ($this->checkHistoryEmpty() === true) {
return null;
}

if(! is_null($index)) {
$recordedResponse = $this->getRecordedResponses()[$index];

if ($recordedResponse->getPendingRequest()->getRequest() instanceof $request) {
return $recordedResponse;
}
}

$lastRequest = $this->getLastRequest();

if ($lastRequest instanceof $request) {
Expand All @@ -354,12 +379,23 @@ public function findResponseByRequest(string $request): ?Response
/**
* Find a request that matches a given url pattern
*/
public function findResponseByRequestUrl(string $url): ?Response
public function findResponseByRequestUrl(string $url, ?int $index = null): ?Response
{
if ($this->checkHistoryEmpty() === true) {
return null;
}

if(! is_null($index)) {
$response = $this->getRecordedResponses()[$index];
$pendingRequest = $response->getPendingRequest();

if (URLHelper::matches($url, $pendingRequest->getUrl())) {
return $response;
}

return null;
}

$lastPendingRequest = $this->getLastPendingRequest();

if ($lastPendingRequest instanceof PendingRequest && URLHelper::matches($url, $lastPendingRequest->getUrl())) {
Expand Down Expand Up @@ -410,12 +446,19 @@ public static function destroyGlobal(): void
/**
* Test if the closure can pass with the history.
*/
private function checkClosureAgainstResponses(callable $closure): bool
private function checkClosureAgainstResponses(callable $closure, ?int $index = null): bool
{
if ($this->checkHistoryEmpty() === true) {
return false;
}

if(! is_null($index)) {
$response = $this->getRecordedResponses()[$index];
$request = $response->getPendingRequest()->getRequest();

return $closure($request, $response);
}

// Let's first check if the latest response resolves the callable
// with a successful result.

Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/MockClientAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Saloon\Http\Faking\MockResponse;
use Saloon\Tests\Fixtures\Requests\UserRequest;
use Saloon\Tests\Fixtures\Requests\ErrorRequest;
use PHPUnit\Framework\ExpectationFailedException;
use Saloon\Tests\Fixtures\Connectors\TestConnector;

test('assertSent works with a request', function () {
Expand Down Expand Up @@ -207,3 +208,49 @@
return $response->json() === ['name' => 'Marcel'] && $response->status() === 204;
});
});

test('it can assert requests are sent in a specific order', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
MockResponse::make(['name' => 'Taylor'], 201),
MockResponse::make(['name' => 'Marcel'], 204),
]);

$connector = new TestConnector;

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

$mockClient->assertSentInOrder([
UserRequest::class,
function (UserRequest $request, Response $response) {
return $response->json() === ['name' => 'Taylor'];
},
'/user',
]);
});

test('it can assert requests are sent in a specific order failure', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
MockResponse::make(['name' => 'Taylor'], 201),
MockResponse::make(['name' => 'Marcel'], 204),
]);

$connector = new TestConnector;

$connector->send(new UserRequest(userId: 2), $mockClient);
$connector->send(new UserRequest(userId: 1), $mockClient);
$connector->send(new UserRequest(), $mockClient);



$mockClient->assertSentInOrder([
UserRequest::class,
function (UserRequest $request) {
return $request->userId === 2;
},
'/user',
]);
})->expectException(ExpectationFailedException::class);
Loading