-
Notifications
You must be signed in to change notification settings - Fork 12
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
Mock All External Requests #2
Comments
Hey @MannikJ, sorry for taking so long to get back to you! Looks like I somehow didn't have issue notifications on for this repository. Embarrassing truth: I have written precisely 0 tests for this library, or for the
Someone else reported this issue with made a recent update to |
I have actually excluded your ServiceProvider from auto-discovery and wrote an extension of it which skips registration when in testing environment, because I realized that the token requests were made directly when the API singletons are registered. Then when I actually need the APIs in a test I create a mock for it: It looks like this: public function mockSpapi(string $apiClass, array $responses, $mockAuthentication = true)
{
$mock = new MockHandler($responses);
$handlerStack = HandlerStack::create($mock);
$client = new Client(['handler' => $handlerStack]);
$creds = $this->credentials();
// To avoid token requests:
Cache::put($creds->getAccessTokenCacheKey(), '123');
Cache::put($creds->getExpiresAtCacheKey(), 3600);
$config = $creds->toSpApiConfiguration();
$config->setEndpoint(Endpoint::EU_SANDBOX);
if ($mockAuthentication) {
$this->mockAuthentication($config);
}
$this->app->singleton($apiClass, fn () => new $apiClass($config, $client));
}
public function mockAuthentication(Configuration $configuration)
{
$authSigner = $this->createMock(AuthorizationSignerContract::class);
$authSigner->expects($this->any())
->method('sign')
->will(
$this->returnCallback(function ($request) {
return $request;
})
);
$requestSigner = $this->createMock(RequestSignerContract::class);
$requestSigner->expects($this->any())
->method('signRequest')
->will(
$this->returnCallback(function ($request) {
return $request;
})
);
$this->mockTokensApi();
$configuration->setRequestSigner($requestSigner);
}
public function mockTokensApi(array $responses = null)
{
$accessToken = 'the-access-token';
$responses = ! $responses
? [
new Response(200, [], "{\"access_token\": \"{$accessToken}\", \"expires_in\": 3660}"),
new Response(200, [], "{\"restricted_data_token\": \"{$accessToken}\", \"expires_in\": 3660}"),
]
: $responses;
$this->mockSpapi(TokensV20210301Api::class, $responses, false);
} |
It should be possible to mock requests using Saloon's mocking infrastructure as of |
First of all, thank you for this great package!
However, I'm struggling with mocking the API calls in my tests. I managed to create mocks for the different API classes like OrderV0Api. This is the basic code to create the mocks of the API classes.
But I realized later that this does not mock the Token requests etc. which are made in the background before calling these API endpoints, because the classes create their own instances of API classes which then create the unmocked requests.
So I tinkered a bit with it and tried to mock the authentication part as well.
But I am very confused about the different classes like AuthorizationSigner, RequestSigner, Authentication, Credentials, Configuration and what not. You can see that in the code I've come up with so far:
You see its quite chaotic because I don't understand 100% what I'm actually doing.
I found that setting the Endpoint to EU_SANDBOX should avoid requesting restrictedDataTokens, but this can not be set from .env vars, because the function SellingPartnerApi::regionToEndpoint throws an exception when SPAPI_ENDPOINT_REGION=EU_SANDBOX.
I just want a setup that ensures that there are no outgoing requests made. I would appreciate help on how to set this up in a simple but robust way.
The text was updated successfully, but these errors were encountered: