Skip to content

Instantly share code, notes, and snippets.

@cherimarie
Last active February 23, 2023 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cherimarie/c825bf65c164b71862de9245750b5823 to your computer and use it in GitHub Desktop.
Save cherimarie/c825bf65c164b71862de9245750b5823 to your computer and use it in GitHub Desktop.
Laravel Testing Cookbook

Use these checklists to ensure you’re testing the correct things for each type of code change.

Controller endpoint:

Permissions. Users without the necessary role or permissions get a 403 response. Users with the necessary permissions get a 200 response. Validations For each required parameter, requests without it get an error response. For each parameter with other kinds of validations, requests with invalid values get an error response. A request with all required parameters having valid values gets a 200 response. Events For valid requests, the expected event(s) are triggered. Only test that the event is triggered (via Event::assertDispatched or similar), do not test that the side effects of the event have occurred. Data changes For valid requests, the expected data changes are made. Response messages Test the value of response messages in different scenarios. Use these tests only when copy of message is extremely important.

Model method:

Traits:

Events:

For each listener, use Event::assertListening to ensure it is attached to the event. Listeners: Manually create the event and listener objects then call handle on the listener. Then, test that the expected behavior in the listener occurs.

Scripts:

Stores (typescript):

Resources

https://kirschbaumdevelopment.com/insights/structuring-and-testing-your-laravel-events-and-listeners

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment