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

Feature | Global Mock Client #359

Merged
merged 11 commits into from
Jan 18, 2024
Merged

Feature | Global Mock Client #359

merged 11 commits into from
Jan 18, 2024

Conversation

Sammyjo20
Copy link
Collaborator

@Sammyjo20 Sammyjo20 commented Jan 10, 2024

This PR introduces a global mock client for all applications, not just for Laravel. This allows you to register a mock client in your test and then execute application code like making a call to a route which might contain a Saloon request. This means you don't have to pass around a MockClient like you had to do before.

This PR is part of an exercise I am doing to improve the testing experience in Saloon. I feel that for people not using Laravel, there wasn't a good way to test globally.

This make use of PHP static properties which are the same throughout the PHP process lifecycle, essentially global variables.

public function test_my_application_can_make_api_call()
{
    $mockClient = MockClient::global([
        SomeRequest::class => MockResponse::make(['example' => 'data']);
    ]);

    $this->post('/some-internal-route/');

    $mockClient->assertSent(SomeRequest::class);
}

Note
You will need to configure MockClient::destroyGlobal() between tests either in a global beforeEach or in the setup method.

Pest

uses()
    ->beforeEach(fn () => MockClient::destroyGlobal())
    ->in(__DIR__);

PHP Unit

public function setUp()
{
    MockClient::destroyGlobal();
}

Upsides

  • Offers people with a way to test globally without having to pass around a $mockClient variable.
  • Uses the existing MockClient class under the hood so all expectations are the same

Downsides

  • Laravel testing might become slightly more confusing because there's another way to test. I will need to separate the documentation to be able to support all the different types of mock clients.
  • Can result in leaky tests if people forget to reset between tests

@Sammyjo20 Sammyjo20 self-assigned this Jan 10, 2024
Copy link
Collaborator Author

@Sammyjo20 Sammyjo20 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self review

Copy link
Collaborator Author

@Sammyjo20 Sammyjo20 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self review

@Sammyjo20 Sammyjo20 merged commit 1a69816 into v3 Jan 18, 2024
14 checks passed
@Sammyjo20 Sammyjo20 deleted the feature/global-mock-client branch January 21, 2024 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant