Skip to content

Commit

Permalink
Update callback verification in HttpRequest. (#769)
Browse files Browse the repository at this point in the history
### Changes

'callback' is a property not a method of $mockedResponse

Changed the method_exists check to a property_exists check in the
HttpRequest class. This adjustment ensures that 'callback' is an
accessible property of the object before attempting to invoke it. This
change improves code reliability, specifically for the cases where
HttpRequest is used in unit tests with mock responses.

### References

[#768 Callback Never
Called](#768)

### Testing

A test has been added to assert the fix is working as expected

### Contributor Checklist

- [x] I agree to adhere to the [Auth0 General Contribution
Guidelines](https://github.com/auth0/open-source-template/blob/master/GENERAL-CONTRIBUTING.md).
- [x] I agree to uphold the [Auth0 Code of
Conduct](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md).
  • Loading branch information
midnite81 authored Jul 25, 2024
1 parent 520b2bf commit 96c4f34
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
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);
});

0 comments on commit 96c4f34

Please sign in to comment.