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 | V3 Added a request option to assertSentCount #364

Merged
merged 2 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 26 additions & 2 deletions src/Http/Faking/MockClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ public function assertNotSent(string|callable $request): void
}

/**
* Assert JSON data was sent
* Assert JSON response data was received
*
* @deprecated This method will be removed in v4
*
* @param array<string, mixed> $data
*/
Expand All @@ -282,8 +284,16 @@ public function assertNothingSent(): void
/**
* Assert a request count has been met.
*/
public function assertSentCount(int $count): void
public function assertSentCount(int $count, string $requestClass = null): void
{
if (is_string($requestClass)) {
$actualCount = $this->getRequestSentCount()[$requestClass] ?? 0;

PHPUnit::assertEquals($count, $actualCount);

return;
}

PHPUnit::assertCount($count, $this->getRecordedResponses());
}

Expand Down Expand Up @@ -458,4 +468,18 @@ private function mockResponseValue(MockResponse|Fixture|callable $mockable, Pend

return $mockable($pendingRequest);
}

/**
* Get an array of requests recorded with their count
*
* @return array<class-string, int>
*/
private function getRequestSentCount(): array
{
$requests = array_map(static function (Response $response) {
return $response->getRequest()::class;
}, $this->getRecordedResponses());

return array_count_values($requests);
}
}
19 changes: 19 additions & 0 deletions tests/Unit/MockClientAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@
$mockClient->assertSentCount(3);
});

test('can assert count of requests', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
MockResponse::make(['name' => 'Taylor']),
MockResponse::make(['name' => 'Marcel']),
MockResponse::make(['message' => 'Error'], 500),
]);

$connector = new TestConnector;

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

$mockClient->assertSentCount(3, UserRequest::class);
$mockClient->assertSentCount(1, ErrorRequest::class);
});

test('assertSent with a closure works with more than one request in the history', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
Expand Down