The Laravel app has a number of implemented feature test cases that may help you understand how to test which feature of your Laravel app. If you love to TDD your Laravel application or don't know how then this repo will help you there. So, get started with Test Driven Development and Happy TDD
Don't hesitate to ask me on LinkedIn or Twitter if you don't understand any of these tests.
Note that some tests are not refactored here.
In Test-Driven Development, you've two rules:
- Write new code only if you first have a failing automated test.
- Eliminate duplication.
The two rules imply an order to the tasks of programming:
- Red - write a little test that doesn’t work, perhaps doesn’t even compile at first
- Green - make the test work quickly, committing whatever sins necessary in the process
- Refactor - eliminate all the duplication created in just getting the test to work
Red / Green / Refactor - is called the TDD mantra.
This is from Kent Beck's TDD by example.
Where do I start? Don't worry! It happens. When you're not sure where to start, try to figure out what module or class or function you're going to deal with. If you get one, break it down into a set of tasks. Then pick a task and go with that.
If it does not work try to fake a list of tasks. Even if this one does not work too, take a walk and repeat.
- Test a simple form submission
- Test creating a blog post
- Test displaying a blog post
- Test redirection after creating a post
- Test validating a simple form
- Test updating a blog post
- Test deleting a blog post
- Test unauthenticated users cannot manage posts
- Test a simple image upload
- Test renaming an uploaded image
- Test validating an uploaded image
- Test uploading multiple files with validation
- Test resizing uploaded image
- Test downloading a private file
What to test while sending an email? Well, you don't need to test how to send emails under the hood. Because that's the job of Laravel email API, not yours. So, test Laravel's email API can be instructed to send emails.
- Test mailables are available
- Test previewing email template
- Test mailable has valid content
- Test setting content to mailable at runtime
- Test mailable can have attachments
- Test mailable can have attachments at runtime
- Test sending email
- Test sending email via queue
- Test sending email to bulk users
Test the code that triggers an event to check it was dispatched. You don't need to test the execution of
a listener. That's Laravel's event API's job. You may unit-test what the listener's handle()
method does and
mock the method's call.
- Test listeners can listen to given events
- Test triggering an event after creating orders
- Test triggering an event after updating orders
- Test triggering an event after deleting orders
- Test listening to builtin events
- Test subscribing to builtin event - Login
- Test subscribing to builtin event - Logout
- Test instructing an event listener to be queued
- Test handle method of an event listener
Test Laravel notification API can be instructed to send notifications. Because sending notifications is unrelated to the code you are actually testing.
- Test notifying users after shipping orders
- Test notifying user while requesting reset password link
- Test reset password screen can be seen with token
- Test password can be reset with valid token
- Test a notification can be instructed to be queued
This section is for how to upload, resize, and store image info into a database but via a queued job and applying TDD. What to test or where to start is outlined below. If you follow the steps given below, you'll have a good grasp of how to proceed with TDD while developing a new feature or making a change.
Note: Make sure you've installed predis/predis
and intervention/image
, configured redis, and run migrations.
Tip: Go through each file involved in a particular test.
Step 1 - Test a job is dispatchable
Step 2 - Test a job can be queued
Step 3 - Test a queued job can upload and resize an image
Step 4 - Test handle method returns false on upload fail
Step 5 - Test handle method returns false on invalid mime type
Step 6 - Test handle method returns false on invalid image content
Step 7 - Test handle method returns false on invalid resolutions
Step 8 - Test handle method deletes original image after resizing it
Step 9 - Test refactoring and storing image info into database
Hooray! You've done test-driven development!
Note that I've left some refactorings and tests for you. The calling of methods of this Intervention\Image\Facades\Image
class is not stubbed here, for example.
One more thing, most tests about the Job
class should go to the Unit
section. But I've put them here for you so that you can
see them all in a single place.