Giter Site home page Giter Site logo

Comments (8)

bobmulder avatar bobmulder commented on May 3, 2024 1

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.

Sammyjo20 avatar Sammyjo20 commented on May 3, 2024

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.

bobmulder avatar bobmulder commented on May 3, 2024

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.

Sammyjo20 avatar Sammyjo20 commented on May 3, 2024

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.

Sammyjo20 avatar Sammyjo20 commented on May 3, 2024

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.

Sammyjo20 avatar Sammyjo20 commented on May 3, 2024

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.

Sammyjo20 avatar Sammyjo20 commented on May 3, 2024

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.

bobmulder avatar bobmulder commented on May 3, 2024

I can handle that ;) Thank you for the effort!

from saloon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.