Getting started with test-driven Laravel

Getting started with test-driven Laravel

Where do I start when writing a new project? Do I define my models up front, my controllers, start designing views?

Let your tests guide the design of your application.

These slides accompany my talk at PHP London, August 2016.

The code written in the live demo part of this talk is available via:


Amo Chohan

August 04, 2016


  1. 2.

    About me • Lead developer, Energy Aspects • PHP developer

    for over 10 years • A frequent flyer • Level 18 Pokemon trainer @amo_chohan
  2. 3.

    Assumptions • Good grasp of object-oriented PHP • Some exposure

    to frameworks • Basic familiarity with automated testing @amo_chohan
  3. 4.

    What we won’t cover @amo_chohan • Setting up your development

    environment • Composer • Installing Laravel • Homestead / Valet • Laravel documentation
  4. 5.

    About Laravel @amo_chohan • 5 years old • A very

    powerful ORM (Eloquent) • Great support tools (Homestead, Forge, Envoyer) • A huge community • “The PHP framework for web artisans”
  5. 7.

    What is TDD? @amo_chohan • Writing tests before writing any

    of our application code • Drives our code, as well as verifying its suitability • A design activity • Clarify our ideas about what we want the code to do • Writing a test first gives us rapid feedback about the quality of our design ideas
  6. 8.

    @amo_chohan The Golden Rule of Test-Driven Development Never write new

    functionality without a failing test. Growing Object-Oriented Software, Guided By Tests. Steve Freeman and Nat Pryce, 2010
  7. 9.

    @amo_chohan TDD means we only ever write code as a

    consequence of our tests Every single line has a purpose, and every single line is needed Me, just now
  8. 10.
  9. 11.

    @amo_chohan A simple e-store • Users can sign up for

    an account • Users can browse a catalogue of products • Users can see the details of a product • Users can add products to their cart • Users can see the total of their cart • Users can remove items that they no longer want from their cart
  10. 14.

    @amo_chohan Acceptance tests • End to end documentation of a

    behaviour • Don’t directly call code from our api • Written from the perspective of the end user • Often describe an entire feature • Slow
  11. 16.

    @amo_chohan Integration tests • Verify that objects behave as we

    expect • Do objects interact with each other as we expect • Can make calls to external services, or hit a database • Speed depends on the component
  12. 17.

    @amo_chohan Unit tests • Test the a single part of

    a class in complete isolation • Aim for 100% code coverage • Fast!
  13. 22.

    @amo_chohan Test writing tips • Write your code using English

    (or your native language) • Write the code you wish you had • KISS • Be specific when arranging • Tell the full story, holes in the story mean gaps in your code / missing features