Skip to content

unclexo/tdd

Repository files navigation

Test Your Laravel Application

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.

TDD in few words

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:

  1. Red - write a little test that doesn’t work, perhaps doesn’t even compile at first
  2. Green - make the test work quickly, committing whatever sins necessary in the process
  3. 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 to start

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.

Testing Form Submission

How to

Testing File Upload

How to

Testing Sending Email

What to test?

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.

How to

Testing Events and Listeners

What to test?

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.

How to

Testing Notifications

What to test?

Test Laravel notification API can be instructed to send notifications. Because sending notifications is unrelated to the code you are actually testing.

How to

Testing Jobs / Queue

Test-Driven Development with a real world example

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.