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

Update callback verification in HttpRequest. #769

Merged
merged 4 commits into from
Jul 25, 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
2 changes: 1 addition & 1 deletion src/Utility/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function call(): ResponseInterface

// Used for unit testing: if we're mocking responses and have a callback assigned, invoke that callback with our request and response.
// @codeCoverageIgnoreStart
if ($mockedResponse && method_exists($mockedResponse, 'callback') && is_callable($mockedResponse->callback)) { // @phpstan-ignore-line
if ($mockedResponse && property_exists($mockedResponse, 'callback') && is_callable($mockedResponse->callback)) { // @phpstan-ignore-line
($mockedResponse->callback)($httpRequest, $httpResponse);
}
// @codeCoverageIgnoreEnd
Expand Down
25 changes: 21 additions & 4 deletions tests/Unit/Utility/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
}

$response = $this->client->method('get')
->addPath(['client'])
->addPath(['client'])
->call();

expect(HttpResponse::getStatusCode($response))->toEqual(429);
Expand All @@ -75,7 +75,7 @@
}

$response = $this->client->method('get')
->addPath(['client'])
->addPath(['client'])
->call();

$requestCount = $this->client->getLastRequest()->getRequestCount();
Expand Down Expand Up @@ -142,7 +142,7 @@
$this->client->mockResponse(clone $this->httpResponse200);

$response = $this->client->method('get')
->addPath(['client'])
->addPath(['client'])
->call();

expect(HttpResponse::getStatusCode($response))->toEqual(429);
Expand All @@ -160,7 +160,7 @@
]);

$response = $this->client->method('get')
->addPath(['client'])
->addPath(['client'])
->call();

expect(HttpResponse::getStatusCode($response))->toEqual(200);
Expand All @@ -169,3 +169,20 @@

expect($requestCount)->toEqual(2);
});

test('the callback of a mock response is called', function(): void {
$count = 0;

$this->client->mockResponse(
clone $this->httpResponse200,
function ($request) use (&$count) {
$count++;
}
);

$response = $this->client->method('get')
->addPath(['client'])
->call();

expect($count)->toEqual(1);
});
Loading