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 toBeInvokable arch expectation #891

Merged

Conversation

ash-jc-allen
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
Fixed tickets N/A

Hey!

This PR is just a small one, but something that I think could be useful for some devs (including myself). This PR proposes a new toBeInvokable expectation that can be used to assert whether a class has an __invoke method.

I like to use invokable classes quite a bit (for things like action classes). An example action class might be something like this:

use App\DataTransferObjects\User\NewUserData;

class CreateUser
{
    public function __invoke(NewUserData $userData): void
    {
        // ...
    }
}

And then it can be called like so in my controller:

public function store(StoreUserRequest $request, CreateUser $createUser)
{
    // ...

    $createUser($request->toDto());

    // ...
}

I like to follow this pattern and like knowing that all my actions are invokable. So I thought it'd be handy to have an expectation that I can use to enforce this.

The expectation can be used like so:

test('class is invokable')
    ->expect('App\Actions')
    ->toBeInvokable();

I'm not entirely sure if I've done everything right here, but I've tried my best to make sure there are tests covering the code. If I've done anything wrong though, please give me a shout!

Likewise, if this is something you think might be useful to other devs but you want something changing first, let me know and I'll get to it ASAP! 😄

@ash-jc-allen ash-jc-allen mentioned this pull request Aug 1, 2023
@nunomaduro nunomaduro merged commit 4d7aa2b into pestphp:2.x Aug 1, 2023
@nunomaduro
Copy link
Member

@ash-jc-allen can you add docs to this please?

@ash-jc-allen
Copy link
Contributor Author

Hey @nunomaduro! Yeah sure, I'll get some written now 😄

@ash-jc-allen ash-jc-allen deleted the feature/invokable-arch-expectation branch August 1, 2023 13:27
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.

3 participants