Comments (8)
Hi Sam!
I was already preparing a reply by sniffing through the Laravel docs for examples. But your draft is truly amazing!
Personally I like the Laravel Saloon::assertSent(GetForgeServerRequest::class);
style because it is very close to what Laravel does wit jobs, queues, facades, mailables, et cetera.
Is there anything you want me to do? Since this is open source stuff π
from saloon.
Hi @bobmulder!
Thanks for submitting your issue.
Saloon has a bunch of tests already written to ensure that the mocking is working correctly. It looks like you're using the type of mock that will respond to the specific request if it fails. Perhaps you might need to use the "sequence" test which will respond to the first Saloon request that is made and then is dropped off the list.
Would you be able to give me a real life example of what this test prevents?
Perhaps I could also add an option on the MockResponse so it only occurs once?
from saloon.
Sorry for the delay @Sammyjo20!
Maybe my explanation wasn't that clear, sorry for that. In my case, the problem is not about having a feature where the same API is called multiple times. In fact, that is exactly what Saloon solves for me, so you have my thanks for that.
I think there are two things I am trying to do:
- Make sure the API call is called
- Make sure what data is passed to the API (and apply assertions on that)
Make sure the API call is called
When applying your example of mocking in the docs, it won't test if the request has been ehh requested:
Saloon::fake([
new MockResponse(['name' => 'Sam'], 200),
]);
This is why I mocked the MockResponse
and adding a exects
on the getStatus
method to assert that the request will be called once (or twice or never or whatever):
$mock = $this->getMockBuilder(MockResponse::class)
->onlyMethods(['getStatus'])
->setConstructorArgs([ $data, $status ])
->getMock();
$mock
->expects($expected ?: $this->once())
->method('getStatus')
->willReturn($status);
Make sure what data is passed to the API (and apply assertions on that)
When I am mocking the response according to the docs of Saloon, I have influence of what will be returned. However, when doing so, I want to make sure as well what has been sent towards the API.
Of this usecase I didn't add an example, and I do not have a solution for that yet.
Talking about mocks, in an ideal world you want to add the with
method to validate arguments like you'd do with PHPUnit mocks.
from saloon.
Hey Bob, no worries, thanks for explaining this further.
Maybe Saloon can have assert methods on the MockClient/Facade that can be used to check if certain requests have been sent, and maybe check if the specified request data has been sent.
For example:
Saloon::assertSent(MyRequest::class);
Saloon::assertSentWith(MyRequest::class, $data)
What do you think?
from saloon.
Or maybe something like Illuminate/Http, like this?
Http::assertSent(function (Request $request) {
return $request->hasHeader('X-First', 'foo') &&
$request->url() == 'http://example.com/users' &&
$request['name'] == 'Taylor' &&
$request['role'] == 'Developer';
});
from saloon.
Hey @bobmulder just wanted to let you know that I'm almost finished adding these features in. Soon you will have access to the following methods:
- AssertSent (accepts request classes, closures and URL patterns)
- AssertNotSent (inverse of assert sent)
- AssertSentJson (accepts a class and an array to check both are equal)
- AssertNothingSent
- AssertSentCount
Here's how you can use it
// Laravel (using Facade)
Saloon::assertSent(GetForgeServerRequest::class);
// Normal PHP
$mockClient->assertSent(GetForgeServerRequest::class);
from saloon.
Iβm super happy you like it!
I think itβs pretty much done, I just want to run a few more tests and maybe install it on a local version of Laravel before I publish a release so just sit tight for another day or two!
from saloon.
I can handle that ;) Thank you for the effort!
from saloon.
Related Issues (20)
- [3.x] specify default page overwrited by `rewind` method HOT 2
- Error when only connector has a body and request does not HOT 2
- Get clean API response when an exception occurs HOT 6
- Removal of key customisation in pagination? HOT 2
- Inquiring about the Option to Send the Path of a Certificate HOT 2
- BindingResolutionException on Connector if request is sent async HOT 17
- Asserting sent request fails on first test when recording responses HOT 6
- File Uploads Not Working HOT 18
- Log request and response HOT 5
- Caching with Offset Paginator returns same results for each API call. HOT 3
- InvalidArgumentException: A 'contents' key is required HOT 3
- Query parameters customisation HOT 10
- HasMultipartBody and multiple files with the same key HOT 3
- "Shifted" `$this->page` between `isLastPage()` and `applyPagination()` in pagination HOT 6
- API logs table HOT 6
- Rate Limit Handler Seems to Be Unreliable With Multiple Workers HOT 2
- AuthConnector::createOAuthAuthenticator(): Argument #2 ($refreshToken) must be of type string, null given HOT 5
- `HasReponse` trait causes exception HOT 5
- `HasReponse` only works on single item HOT 1
- Undefined class 'HeaderAuthenticator' HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from saloon.