SlideShare ist ein Scribd-Unternehmen logo
1 von 51
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!
PRESENTERS
• Any types of transformation to user visible
strings.
• implode(‘ ‘, [$user->first_name, $user-
>last_name]);
THANK YOU!
Questions?
@tylerjohnst on the Twitters

Weitere ähnliche Inhalte

Was ist angesagt?

Selenium with Cucumber
Selenium  with Cucumber Selenium  with Cucumber
Selenium with Cucumber Knoldus Inc.
 
BDD with CucumberJS and WebdriverIO
BDD with CucumberJS and WebdriverIOBDD with CucumberJS and WebdriverIO
BDD with CucumberJS and WebdriverIOM Rizwanur Rashid
 
Step by Step Guide on Lazy Loading in Angular 11
Step by Step Guide on Lazy Loading in Angular 11Step by Step Guide on Lazy Loading in Angular 11
Step by Step Guide on Lazy Loading in Angular 11Katy Slemon
 
Requirements Management with HP ALM
Requirements Management with HP ALMRequirements Management with HP ALM
Requirements Management with HP ALMpd7.group
 
Integrating Black Duck into your Agile DevOps Environment
Integrating Black Duck into your Agile DevOps EnvironmentIntegrating Black Duck into your Agile DevOps Environment
Integrating Black Duck into your Agile DevOps EnvironmentBlack Duck by Synopsys
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOPDzmitry Naskou
 
Python Testing 101 with Selenium
Python Testing 101 with SeleniumPython Testing 101 with Selenium
Python Testing 101 with SeleniumLeonardo Jimenez
 
SonarQube - Should I Stay or Should I Go ?
SonarQube - Should I Stay or Should I Go ? SonarQube - Should I Stay or Should I Go ?
SonarQube - Should I Stay or Should I Go ? Geeks Anonymes
 
Selenium DeTox for Achieving the Right Testing Pyramid
Selenium DeTox for Achieving the Right Testing PyramidSelenium DeTox for Achieving the Right Testing Pyramid
Selenium DeTox for Achieving the Right Testing PyramidNaresh Jain
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework Serhat Can
 
Utilizing JSF Front Ends with Microservices
Utilizing JSF Front Ends with MicroservicesUtilizing JSF Front Ends with Microservices
Utilizing JSF Front Ends with MicroservicesJosh Juneau
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of SignalsCoding Academy
 

Was ist angesagt? (20)

Selenium with Cucumber
Selenium  with Cucumber Selenium  with Cucumber
Selenium with Cucumber
 
BDD with Cucumber
BDD with CucumberBDD with Cucumber
BDD with Cucumber
 
Spring Core
Spring CoreSpring Core
Spring Core
 
BDD with CucumberJS and WebdriverIO
BDD with CucumberJS and WebdriverIOBDD with CucumberJS and WebdriverIO
BDD with CucumberJS and WebdriverIO
 
Step by Step Guide on Lazy Loading in Angular 11
Step by Step Guide on Lazy Loading in Angular 11Step by Step Guide on Lazy Loading in Angular 11
Step by Step Guide on Lazy Loading in Angular 11
 
Requirements Management with HP ALM
Requirements Management with HP ALMRequirements Management with HP ALM
Requirements Management with HP ALM
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
 
Integrating Black Duck into your Agile DevOps Environment
Integrating Black Duck into your Agile DevOps EnvironmentIntegrating Black Duck into your Agile DevOps Environment
Integrating Black Duck into your Agile DevOps Environment
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
 
Sq lite presentation
Sq lite presentationSq lite presentation
Sq lite presentation
 
Python Testing 101 with Selenium
Python Testing 101 with SeleniumPython Testing 101 with Selenium
Python Testing 101 with Selenium
 
Test Automation Pyramid
Test Automation PyramidTest Automation Pyramid
Test Automation Pyramid
 
SonarQube - Should I Stay or Should I Go ?
SonarQube - Should I Stay or Should I Go ? SonarQube - Should I Stay or Should I Go ?
SonarQube - Should I Stay or Should I Go ?
 
Selenium DeTox for Achieving the Right Testing Pyramid
Selenium DeTox for Achieving the Right Testing PyramidSelenium DeTox for Achieving the Right Testing Pyramid
Selenium DeTox for Achieving the Right Testing Pyramid
 
github-actions.pdf
github-actions.pdfgithub-actions.pdf
github-actions.pdf
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Utilizing JSF Front Ends with Microservices
Utilizing JSF Front Ends with MicroservicesUtilizing JSF Front Ends with Microservices
Utilizing JSF Front Ends with Microservices
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of Signals
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 

Ähnlich wie Test Driven Development with Laravel

{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptxAmalEldhose2
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Danny Preussler
 
Test Driven Development and Automation
Test Driven Development and AutomationTest Driven Development and Automation
Test Driven Development and AutomationMahesh Salaria
 
Test-Driven Development Reference Card
Test-Driven Development Reference CardTest-Driven Development Reference Card
Test-Driven Development Reference CardSeapine Software
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven developmentEinar Ingebrigtsen
 
Understanding Why Testing is Importaint
Understanding Why Testing is ImportaintUnderstanding Why Testing is Importaint
Understanding Why Testing is ImportaintSana Nasar
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven DevelopmentPablo Villar
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017Xavi Hidalgo
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentjakubkoci
 
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019Jason Tice
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentShawn Jones
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonCefalo
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentdcsunu
 

Ähnlich wie Test Driven Development with Laravel (20)

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)
 
Test Driven Development and Automation
Test Driven Development and AutomationTest Driven Development and Automation
Test Driven Development and Automation
 
Test-Driven Development Reference Card
Test-Driven Development Reference CardTest-Driven Development Reference Card
Test-Driven Development Reference Card
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven development
 
Understanding Why Testing is Importaint
Understanding Why Testing is ImportaintUnderstanding Why Testing is Importaint
Understanding Why Testing is Importaint
 
Clean code
Clean codeClean code
Clean code
 
Intro to TDD
Intro to TDDIntro to TDD
Intro to TDD
 
Tdd
TddTdd
Tdd
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Tdd and bdd
Tdd and bddTdd and bdd
Tdd and bdd
 
Tdd
TddTdd
Tdd
 
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven Development
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 

Kürzlich hochgeladen

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 

Kürzlich hochgeladen (20)

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 

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!
  • 49.
  • 50. PRESENTERS • Any types of transformation to user visible strings. • implode(‘ ‘, [$user->first_name, $user- >last_name]);

Hinweis der Redaktion

  1. 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.
  2. we’