SlideShare a Scribd company logo
TEST DRIVEN
DEVELOPMENT WITH
LARAVEL
By: Tyler Johnston
@tylerjohnst
HI, I’M TYLER
• Owner at Ahoy Consulting
• Mostly Ruby and Javascript, sometimes
PHP, sometimes iOS and sometimes
Android.
• Organizer of Suncoast.js Meetup Group
(You should come!)
• Find me on the Twitters: @tylerjohnst
WHAT IS TDD?
• TDD is the practice of software development
guided by tests.
STORYTIME
WHY TDD?
• What does TDD accomplish?
• Code Confidence
• Cleaner Code
• Fewer Bugs
CODE CONFIDENCE
You should be confident in your code. You as
the rockstar developer, designer, devops and
unicorn should be able to deploy your master
branch at any time with full certainty that it
works 100%.
CLEANER CODE
• Tests give you live feedback of your API.
• Are the tests hard to write?
• Are the tests breaking constantly?
• “This code is untestable”
CLEANER CODE
Only build the features that you need. When
you are only writing tests for functionality you
need, it can help you avoid “over-engineering”
such as building things you “might” need in the
future.
CLEANER CODE
Separate interface from implementation
thinking. You may have a tendency to pollute
API design decisions with implementation
speculation.
CLEANER CODE
• Software development is a never ending
process. Best practices change, patterns
change, business requirements change. Tests
allow you to adapt to this.
• Tests allow you to change or refactor your code
at any given time without changing the
interfaces to your objects.
• Changes to the system should not be painful.
A fully tested system can be changed without
fear. Breaking an API should cause tests to fail.
BUT ISN’T TDD SLOWER
• Yes, but that’s a good thing.
• “What the research team found was that the
TDD teams produced code that was 60 to 90
percent better in terms of defect density than
non-TDD teams. They also discovered that
TDD teams took longer to complete their
projects—15 to 35 percent longer.”
• Source: http://research.microsoft.com/en-us/news/features/nagappan-100609.aspx
I’m ready. Lets get started!
RULES OF TDD BY UNCLE
BOB MARTIN
• You may not write any production code
without a failing test
• You may not write more of a test than is
sufficient to fail
• You may not write more production code
than is sufficient to pass
THE TDD CYCLE
RED - Write a failing test
GREEN - Write the minimal amount of
code required to make the test pass
REFACTOR - Clean up any duplication
and make any code clarity changes
while ensuring that the test still passes.
REPEAT
GETTING STARTED WITH
LARAVEL
• Almost no work to get started! Laravel ships
with PHPUnit configured! (See phpunit.xml).
Hooray!
RUNNING YOUR TESTS
• Download the PHPUnit.phar package, add it
to your path. Run it and see:
TEST FRAMEWORKS
• There are a variety of test frameworks for
PHP. I’m using PHPUnit because it ships
with Laravel and I’m really lazy and easily
frustrated with dev tools.
• PHPUnit, PHPSpec, SimpleTest, etc.
WHAT TO TEST?
• The hardest thing about Test Driven
Development is writing your first test.
• Break the feature in to small bite sized parts.
Write a failing test for that small part.
EXAMPLE
I’m designing an car wash management tool for my
dealership. I need to track if and when a car needs a car
wash.
OUR FIRST FAILING TEST
r failure can be considered a failing test. A test suite that doesn’t
WRITE THE MINIMUM AMOUNT
OF CODE TO MAKE THE TEST
PASS
AND OUR TEST NOW
PASSES
REFACTOR
• Nothing to refactor yet. Lets add the other
test to make sure both ways work.
FAILING TEST
A car has an age. Lets write a test to be able to check that.
BACK IN THE RED
MAKE THE TEST PASS
ALL GREEN!
REFACTOR
REFACTOR
Tests should also be refactored to reduce duplication.
AND THE TESTS STILL
PASS!
FAILING TEST
Now we need the ability to set the age of the car.
AND IT FAILS
WRITE THE CODE
AND IT PASSES
REFACTOR
Looks pretty good, I don’t think we need to refactor.
FAILING TEST
AND THE TEST FAILS
MAKE THE TEST PASS
RUN THE TESTS, IN THE
GREEN!
REFACTOR
AND WE STILL PASS!
CELEBRATION!
We’ve done it! Our first set of tests are done!
I’M REALLY READY TO GO.
WHAT DO YOU DO?
• I’m pretty opinionated on how I do testing
and design.
• Feel free to use your own programming
patterns and idioms.
MODELS
• I tend to only test things that I do with the
data. Laravel is a tested and well worn
framework. I almost never test database
interactions unless I am doing my own
custom SQL or modifying getter/setter
attributes.
• Your models fetch and transform data. Test
the transformation and conversions, not the
DB access.
CONTROLLER
• I keep my controllers very skinny. They are
limited to:
• Fetching, creating, and updating Models.
• then
• Renders views or redirects to other
controller actions.
VIEWS
• I don’t test views. At all. Maybe one or two
sanity checks but the controller “integration”
style tests will cover the view actually
rendering.
• (If a view throws an exception, it will be
caught from your controller tests)
• View tests end up extremely brittle. A
designer goes in and changes a class name
or ID attribute or even heirarcy and your
tests are now broken.
VIEWS CONT.
• The better solution (in my opinion) is to use
the presenter pattern.
• No method calls from the view should
interact with anything other than presenters.
• Write tests for your presenters and now your
views are completely tested!
Test Driven Development with Laravel
PRESENTERS
• Any types of transformation to user visible
strings.
• implode(‘ ‘, [$user->first_name, $user-
>last_name]);
THANK YOU!
Questions?
@tylerjohnst on the Twitters

More Related Content

What's hot (20)

PPTX
TestNG Framework
Levon Apreyan
 
PPTX
Testing soap UI
Razia Sultana
 
PDF
Corso Java 1 - BASE
Giuseppe Dell'Abate
 
PPT
J2ee
Prince Soni
 
PPT
Test Automation Framework Designs
Sauce Labs
 
PDF
Robot Framework Introduction
Pekka Klärck
 
PDF
Requirements Validation
Antonio Villegas
 
PPTX
TDD - Agile
harinderpisces
 
PPT
Agile QA presentation
Carl Bruiners
 
PDF
ATDD Using Robot Framework
Pekka Klärck
 
PPTX
Automation frameworks
Vishwanath KC
 
PDF
Testing APIs in the Cloud
SmartBear
 
PPTX
Controls
teach4uin
 
PPTX
Robot Framework
Onur Baskirt
 
PPTX
Debugging
Ajeng Savitri
 
PPTX
White Box Testing
Alisha Roy
 
PPT
Jpa
Manav Prasad
 
PDF
Introduction to TDD (Test Driven development) - Ahmed Shreef
Ahmed Shreef
 
PPTX
Chapter 2 c#
megersaoljira
 
PDF
[D35] 今ミッション・クリティカル環境で求められるデータベース・クラスタリング技術とは? by Kousuke Osaka
Insight Technology, Inc.
 
TestNG Framework
Levon Apreyan
 
Testing soap UI
Razia Sultana
 
Corso Java 1 - BASE
Giuseppe Dell'Abate
 
Test Automation Framework Designs
Sauce Labs
 
Robot Framework Introduction
Pekka Klärck
 
Requirements Validation
Antonio Villegas
 
TDD - Agile
harinderpisces
 
Agile QA presentation
Carl Bruiners
 
ATDD Using Robot Framework
Pekka Klärck
 
Automation frameworks
Vishwanath KC
 
Testing APIs in the Cloud
SmartBear
 
Controls
teach4uin
 
Robot Framework
Onur Baskirt
 
Debugging
Ajeng Savitri
 
White Box Testing
Alisha Roy
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Ahmed Shreef
 
Chapter 2 c#
megersaoljira
 
[D35] 今ミッション・クリティカル環境で求められるデータベース・クラスタリング技術とは? by Kousuke Osaka
Insight Technology, Inc.
 

Similar to Test Driven Development with Laravel (20)

PPTX
Test Driven Development
Md. Enamul Haque Chowdhury
 
PPTX
{10.0} Test Driven Development.pptx
AmalEldhose2
 
PPTX
Test Driven Development on Android (Kotlin Kenya)
Danny Preussler
 
PPT
Test Driven Development and Automation
Mahesh Salaria
 
PDF
Test-Driven Development Reference Card
Seapine Software
 
KEY
Driving application development through behavior driven development
Einar Ingebrigtsen
 
PPTX
Understanding Why Testing is Importaint
Sana Nasar
 
PPTX
Clean code
Simon Sönnby
 
PPTX
Intro to TDD
Jason Nocks
 
PDF
iOS Test-Driven Development
Pablo Villar
 
PPTX
Unit Testing and TDD 2017
Xavi Hidalgo
 
PDF
Test Driven Development
jakubkoci
 
PPT
Test Driven Development
Sachithra Gayan
 
PPTX
Tdd and bdd
MohamedSubhiBouchi
 
PDF
Tdd
Dmitry Savin
 
PDF
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Jason Tice
 
PPTX
A Brief Introduction to Test-Driven Development
Shawn Jones
 
PPTX
Test Driven Development
Rajesh Kumar
 
PPTX
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Cefalo
 
Test Driven Development
Md. Enamul Haque Chowdhury
 
{10.0} Test Driven Development.pptx
AmalEldhose2
 
Test Driven Development on Android (Kotlin Kenya)
Danny Preussler
 
Test Driven Development and Automation
Mahesh Salaria
 
Test-Driven Development Reference Card
Seapine Software
 
Driving application development through behavior driven development
Einar Ingebrigtsen
 
Understanding Why Testing is Importaint
Sana Nasar
 
Clean code
Simon Sönnby
 
Intro to TDD
Jason Nocks
 
iOS Test-Driven Development
Pablo Villar
 
Unit Testing and TDD 2017
Xavi Hidalgo
 
Test Driven Development
jakubkoci
 
Test Driven Development
Sachithra Gayan
 
Tdd and bdd
MohamedSubhiBouchi
 
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Jason Tice
 
A Brief Introduction to Test-Driven Development
Shawn Jones
 
Test Driven Development
Rajesh Kumar
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Cefalo
 
Ad

Recently uploaded (20)

PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Ad

Test Driven Development with Laravel

  • 1. TEST DRIVEN DEVELOPMENT WITH LARAVEL By: Tyler Johnston @tylerjohnst
  • 2. HI, I’M TYLER • Owner at Ahoy Consulting • Mostly Ruby and Javascript, sometimes PHP, sometimes iOS and sometimes Android. • Organizer of Suncoast.js Meetup Group (You should come!) • Find me on the Twitters: @tylerjohnst
  • 3. WHAT IS TDD? • TDD is the practice of software development guided by tests.
  • 5. WHY TDD? • What does TDD accomplish? • Code Confidence • Cleaner Code • Fewer Bugs
  • 6. CODE CONFIDENCE You should be confident in your code. You as the rockstar developer, designer, devops and unicorn should be able to deploy your master branch at any time with full certainty that it works 100%.
  • 7. CLEANER CODE • Tests give you live feedback of your API. • Are the tests hard to write? • Are the tests breaking constantly? • “This code is untestable”
  • 8. CLEANER CODE Only build the features that you need. When you are only writing tests for functionality you need, it can help you avoid “over-engineering” such as building things you “might” need in the future.
  • 9. CLEANER CODE Separate interface from implementation thinking. You may have a tendency to pollute API design decisions with implementation speculation.
  • 10. CLEANER CODE • Software development is a never ending process. Best practices change, patterns change, business requirements change. Tests allow you to adapt to this. • Tests allow you to change or refactor your code at any given time without changing the interfaces to your objects. • Changes to the system should not be painful. A fully tested system can be changed without fear. Breaking an API should cause tests to fail.
  • 11. BUT ISN’T TDD SLOWER • Yes, but that’s a good thing. • “What the research team found was that the TDD teams produced code that was 60 to 90 percent better in terms of defect density than non-TDD teams. They also discovered that TDD teams took longer to complete their projects—15 to 35 percent longer.” • Source: http://research.microsoft.com/en-us/news/features/nagappan-100609.aspx
  • 12. I’m ready. Lets get started!
  • 13. RULES OF TDD BY UNCLE BOB MARTIN • You may not write any production code without a failing test • You may not write more of a test than is sufficient to fail • You may not write more production code than is sufficient to pass
  • 15. RED - Write a failing test GREEN - Write the minimal amount of code required to make the test pass REFACTOR - Clean up any duplication and make any code clarity changes while ensuring that the test still passes. REPEAT
  • 16. GETTING STARTED WITH LARAVEL • Almost no work to get started! Laravel ships with PHPUnit configured! (See phpunit.xml). Hooray!
  • 17. RUNNING YOUR TESTS • Download the PHPUnit.phar package, add it to your path. Run it and see:
  • 18. TEST FRAMEWORKS • There are a variety of test frameworks for PHP. I’m using PHPUnit because it ships with Laravel and I’m really lazy and easily frustrated with dev tools. • PHPUnit, PHPSpec, SimpleTest, etc.
  • 19. WHAT TO TEST? • The hardest thing about Test Driven Development is writing your first test. • Break the feature in to small bite sized parts. Write a failing test for that small part.
  • 20. EXAMPLE I’m designing an car wash management tool for my dealership. I need to track if and when a car needs a car wash.
  • 21. OUR FIRST FAILING TEST r failure can be considered a failing test. A test suite that doesn’t
  • 22. WRITE THE MINIMUM AMOUNT OF CODE TO MAKE THE TEST PASS
  • 23. AND OUR TEST NOW PASSES
  • 24. REFACTOR • Nothing to refactor yet. Lets add the other test to make sure both ways work.
  • 25. FAILING TEST A car has an age. Lets write a test to be able to check that.
  • 26. BACK IN THE RED
  • 30. REFACTOR Tests should also be refactored to reduce duplication.
  • 31. AND THE TESTS STILL PASS!
  • 32. FAILING TEST Now we need the ability to set the age of the car.
  • 36. REFACTOR Looks pretty good, I don’t think we need to refactor.
  • 38. AND THE TEST FAILS
  • 40. RUN THE TESTS, IN THE GREEN!
  • 42. AND WE STILL PASS!
  • 43. CELEBRATION! We’ve done it! Our first set of tests are done!
  • 44. I’M REALLY READY TO GO. WHAT DO YOU DO? • I’m pretty opinionated on how I do testing and design. • Feel free to use your own programming patterns and idioms.
  • 45. MODELS • I tend to only test things that I do with the data. Laravel is a tested and well worn framework. I almost never test database interactions unless I am doing my own custom SQL or modifying getter/setter attributes. • Your models fetch and transform data. Test the transformation and conversions, not the DB access.
  • 46. CONTROLLER • I keep my controllers very skinny. They are limited to: • Fetching, creating, and updating Models. • then • Renders views or redirects to other controller actions.
  • 47. VIEWS • I don’t test views. At all. Maybe one or two sanity checks but the controller “integration” style tests will cover the view actually rendering. • (If a view throws an exception, it will be caught from your controller tests) • View tests end up extremely brittle. A designer goes in and changes a class name or ID attribute or even heirarcy and your tests are now broken.
  • 48. VIEWS CONT. • The better solution (in my opinion) is to use the presenter pattern. • No method calls from the view should interact with anything other than presenters. • Write tests for your presenters and now your views are completely tested!
  • 50. PRESENTERS • Any types of transformation to user visible strings. • implode(‘ ‘, [$user->first_name, $user- >last_name]);

Editor's Notes

  • #5: Starting a new greenfield project: Quipbot - Quip Up Your Twitter Feed Boss says we need this awesome new feature: It should auto punctuate the tweet. No problem you say, whip up the new feature and an hour later you are done! Cheers all around, your boss is super excited. Everyone loves you! The boss is back again, this time he wants it to only auto punctuate the tweet if its in English and we need it quick! No problem, so we add the new feature, this time it took six hours because we had to change our auto punctuation code. Awesome! We’ve shipped another feature. Boss gives you a big pat on the back. Now the boss is back again, this time we need this to autocorrect any language that comes in and we need this feature ASAP! Now we have to go through and change even more things. But we get it in, this time it only took a week. Well now, the only thing we need before Facebook will buy us out for a trillion dollars is now the tweet also needs to post to Facebook and Friendster and we need it. But this time the feature takes us weeks.
  • #33: we’