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

Add Conditionable trait to Testing\PendingCommand.php #50988

Merged
merged 1 commit into from
Apr 10, 2024

Conversation

tobz-nz
Copy link
Contributor

@tobz-nz tobz-nz commented Apr 10, 2024

When testing an Artisan command, the command it not actually run until the chain is destructed (see this discussion) - meaning you cannot set the result to a variable to append additional assertions to depending on test dataset context.
For example asserting the output based on different inputs.

// terrible example
test('example', function (bool $value) {
    $result = $this->artisan('update-table-command', ['value' => $value]);

    if ($value == true) {
        $result->assertOutput('setting X turned on');
    }

    if ($value == false) {
        $result->assertOutput('setting X turned off');
    }
});

The simple solution for this use case is to add the Conditionable trait to PendingCommand - problem solved.
You can now easily add more assertions based on dataset criteria.

// terrible example
test('example', function (bool $value) {
    $this->artisan('update-table-command', ['value' => $value])
        ->when(
            $value, 
            fn ($result) => $result->assertOutput('setting X turned on'),
            fn ($result) => $result->assertOutput('setting X turned off')
        );
});

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@tobz-nz
Copy link
Contributor Author

tobz-nz commented Apr 10, 2024

I've not got any tests for this because I'm not sure how to go about it to be honest. Could someone help with that?

@tobz-nz tobz-nz marked this pull request as ready for review April 10, 2024 01:23
@devajmeireles
Copy link
Contributor

devajmeireles commented Apr 10, 2024

I've not got any tests for this because I'm not sure how to go about it to be honest. Could someone help with that?

Fixing StyleCI should be enough. We all know what Conditionable does.

@tobz-nz
Copy link
Contributor Author

tobz-nz commented Apr 10, 2024

@devajmeireles .. I can't see the circle ci checked..?

image

@devajmeireles
Copy link
Contributor

@devajmeireles .. I can't see the circle ci checked..?

image

Scroll down, it's always the first check.

@taylorotwell taylorotwell merged commit 6cc951a into laravel:11.x Apr 10, 2024
29 of 30 checks passed
@tobz-nz tobz-nz deleted the patch-4 branch April 10, 2024 19:55
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

3 participants