Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test-driven Laravel - PHP Southcoast 2017

Test-driven Laravel - PHP Southcoast 2017

In this talk I’ll break down the concept of TDD into a an easy-to-follow set of steps and demonstrate the process of building an entire feature using Laravel in a test-driven approach. You’ll learn how the implementation of your application should be shaped by the tests that are written up front. You’ll implement the right thing more often, and improve your code - in less time than you needed without tests.

Amo Chohan

June 09, 2017
Tweet

More Decks by Amo Chohan

Other Decks in Programming

Transcript

  1. About me • Head of Development • developer for over

    10 years • @amo_chohan • ❤ TDD and Laravel @amo_chohan #phpsc17
  2. What you’ll learn (I hope) • Testing fundamentals • How

    Laravel enables TDD • How to write an end-to-end feature usingTDD • Tips on how to convince your boss that you should be writing tests @amo_chohan #phpsc17
  3. What is TDD? • 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 @amo_chohan #phpsc17
  4. 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 @amo_chohan #phpsc17
  5. Types of test • Acceptance tests (Business features) • Integration

    tests • Unit tests • Each type follows the same general steps ((A)AA) @amo_chohan #phpsc17 Less More
  6. The anatomy of a test - AAA Arrange - Given

    Act - When Assert - Then @amo_chohan #phpsc17
  7. 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 • Allow us to clarify business requirements before writing code • Slowest performance @amo_chohan #phpsc17
  8. A customer can add a product to their cart •

    Given there is a customer • And there is a ‘Blue T-shirt’ product in the database • And there is a Green T-shirt’ product in the database • When the customer visits the product catalogue • And the customer clicks on the ‘Blue T-shirt’ product • And the customer clicks ‘add to cart’ • Then the cart contains 1 product • And the product is the the ‘Blue T-shirt’ @amo_chohan #phpsc17
  9. 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 @amo_chohan #phpsc17
  10. Integration test examples • A customer has a cart •

    A cart can contain many products @amo_chohan #phpsc17
  11. Unit tests • Test a single part of a class

    in complete isolation • Fast! @amo_chohan #phpsc17
  12. Unit test examples • A customer can be instantiated •

    It checks if a product is out of stock • It checks if a cart has expired @amo_chohan #phpsc17
  13. The process, outside-in TDD Write a failing acceptance test (business

    feature) *This may lead you to write a failing integration or unit test @amo_chohan #phpsc17 *
  14. A simple e-store • Sign up for an account •

    Browse a catalogue of t-shirts • View the details of a t-shirt • Add t-shirts to their cart • Check the cart total • Remove items from the cart @amo_chohan #phpsc17
  15. Where to begin? • Routes • Controllers • Views •

    Domain models • Database migrations @amo_chohan #phpsc17
  16. A customer can add a product to their cart •

    Given there is a customer • And there is a ‘Blue T-shirt’ product in the database • And there is a Green T-shirt’ product in the database • When the customer visits the product catalogue • And the customer clicks on the ‘Blue T-shirt’ product • And the customer clicks ‘add to cart’ • Then the cart contains 1 product • And the product is the the ‘Blue T-shirt’ @amo_chohan #phpsc17