Giter Site home page Giter Site logo

unclexo / tdd Goto Github PK

View Code? Open in Web Editor NEW
13.0 1.0 0.0 261 KB

Test your Laravel application

Shell 0.53% PHP 76.12% JavaScript 0.17% Blade 23.04% SCSS 0.14%
app-testing integration-testing laravel-testing unittesting laravel-test-driven-development tdd feature-testing

tdd's Introduction

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.

tdd's People

Contributors

unclexo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.