SlideShare a Scribd company logo
Unit Testing in PHP
Lee Boynton
Advantages to Automating Testing
● Faster to test
○ Don't need to "click about"
○ Don't need to login
○ Don't need to have commented out test code
● Prevent recurring bugs
○ Write a test for bug, run the test after every change
● Easier to test edge cases
● Less fear when making changes
○ You may have forgotten how the code works
○ You may inherit someone else's code
● Find bugs before your users
○ This means you keep your customers happy, this
means more money!
Advantages to Automating Testing
● Better designed code, especially with TDD
○ Unit tested code tends to be
■ Less coupled
■ Less complex
● Tests document how code should work
● Once you are good at unit testing, it's
probably faster
● Tests don't have to be amazing: "Imperfect
tests, run frequently, are much better than
perfect tests that are never written at all"
Disadvantages
● You have to suck it up and write tests to
really see the benefits
● Hard to retrofit tests
○ You may have to refactor old code, or
○ You may have to write a lot of test code for code
which is not easy to test
● Harder to write bad code
● You probably won't want to write code
without tests again
Goals When Writing Tests
● Tests should be
○ Simple
■ Don't have ifs or loops
○ Easy to write
○ Easy to read
○ Easy to run
○ Isolated
● But these ideas may conflict or be hard to
achieve
○ Do your best
○ Can improve tests over time
○ Bad tests better than no tests
Common PHPUnit Methods
● $this->assertTrue($value, 'Value should
have been true')
● $this->assertFalse($value, 'Value should
have been false')
● $this->assertEquals($expected, $actual,
'The result is not what was expected')
● Try to write a message to indicate what
specifically is being asserted
○ Easier to understand what's wrong instead of e.g.
"Failed asserting that false is true"
Common PHPUnit Patterns
● Use setUp() method to instantiate the class
you are testing
○ This works well if you don't have lots of constructor
parameters
● Use data providers to test lots of different
inputs
○ Tests shouldn't have duplicate code either
● Use tearDown() method if you need to clean
up external resources after each test is run
○ Files
○ Database
Hard to Test Things
● Static calls to other classes
○ You cannot mock the dependency
● Singletons
○ Constructor is private or protected
○ Uses static call to get instance
○ Cannot guarantee there are no side effects when
running multiple tests
● Private methods
○ Usually you indirectly test these when you test public
methods
○ Can use reflection if you really want, but this
probably means you should refactor
Hard to Test Things
● Hard coded dependencies
○ Such as when a class instantiates another class
● Magic methods
○ __call
○ __get
○ __set
How to Write Testable Code
● Use dependency injection
○ This means you don't instantiate classes unless
you're in a factory
● Keep methods relatively small
● Avoid having lots of private methods
● Write classes which do one thing (single
responsibility principle)
Go go go!
● http://www.phpunit.
de/manual/current/en/index.html

More Related Content

Viewers also liked (6)

PDF
PHP Doesn't Suck
John Hobbs
 
PPTX
PHP and node.js Together
Chris Tankersley
 
PDF
PHP meets NodeJS
Takayuki Yamaguchi
 
ODP
PHP + Node.js
Patrick Kaminski
 
PDF
Integrating Node.js with PHP
Lee Boynton
 
PDF
How Google Works
Eric Schmidt
 
PHP Doesn't Suck
John Hobbs
 
PHP and node.js Together
Chris Tankersley
 
PHP meets NodeJS
Takayuki Yamaguchi
 
PHP + Node.js
Patrick Kaminski
 
Integrating Node.js with PHP
Lee Boynton
 
How Google Works
Eric Schmidt
 

Similar to Unit testing in PHP (20)

PPTX
PHPUnit: from zero to hero
Jeremy Cook
 
PDF
Test Automation
Rodrigo Paiva
 
PPT
Automated Unit Testing
Mike Lively
 
PPTX
Unit Testng with PHP Unit - A Step by Step Training
Ram Awadh Prasad, PMP
 
PDF
Fighting Fear-Driven-Development With PHPUnit
James Fuller
 
PPT
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
PPT
Unit testing
davidahaskins
 
ZIP
Test
Eddie Kao
 
KEY
Developer testing 101: Become a Testing Fanatic
LB Denker
 
PPT
Test Driven Development with PHPUnit
Mindfire Solutions
 
PPTX
Test in action week 2
Yi-Huan Chan
 
KEY
Unit Testing Your Application
Paladin Web Services
 
PPTX
Test in action week 4
Yi-Huan Chan
 
PDF
Cursus phpunit
Nick Belhomme
 
KEY
Php Unit With Zend Framework Zendcon09
Michelangelo van Dam
 
PPT
Unit Testing using PHPUnit
varuntaliyan
 
PPTX
Getting started-php unit
mfrost503
 
PDF
Unit testing in PHP
Chonlasith Jucksriporn
 
PDF
Leveling Up With Unit Testing - LonghornPHP 2022
Mark Niebergall
 
PHPUnit: from zero to hero
Jeremy Cook
 
Test Automation
Rodrigo Paiva
 
Automated Unit Testing
Mike Lively
 
Unit Testng with PHP Unit - A Step by Step Training
Ram Awadh Prasad, PMP
 
Fighting Fear-Driven-Development With PHPUnit
James Fuller
 
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
Unit testing
davidahaskins
 
Test
Eddie Kao
 
Developer testing 101: Become a Testing Fanatic
LB Denker
 
Test Driven Development with PHPUnit
Mindfire Solutions
 
Test in action week 2
Yi-Huan Chan
 
Unit Testing Your Application
Paladin Web Services
 
Test in action week 4
Yi-Huan Chan
 
Cursus phpunit
Nick Belhomme
 
Php Unit With Zend Framework Zendcon09
Michelangelo van Dam
 
Unit Testing using PHPUnit
varuntaliyan
 
Getting started-php unit
mfrost503
 
Unit testing in PHP
Chonlasith Jucksriporn
 
Leveling Up With Unit Testing - LonghornPHP 2022
Mark Niebergall
 
Ad

Recently uploaded (20)

DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PPTX
Designing Production-Ready AI Agents
Kunal Rai
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
July Patch Tuesday
Ivanti
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Designing Production-Ready AI Agents
Kunal Rai
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Ad

Unit testing in PHP

  • 1. Unit Testing in PHP Lee Boynton
  • 2. Advantages to Automating Testing ● Faster to test ○ Don't need to "click about" ○ Don't need to login ○ Don't need to have commented out test code ● Prevent recurring bugs ○ Write a test for bug, run the test after every change ● Easier to test edge cases ● Less fear when making changes ○ You may have forgotten how the code works ○ You may inherit someone else's code ● Find bugs before your users ○ This means you keep your customers happy, this means more money!
  • 3. Advantages to Automating Testing ● Better designed code, especially with TDD ○ Unit tested code tends to be ■ Less coupled ■ Less complex ● Tests document how code should work ● Once you are good at unit testing, it's probably faster ● Tests don't have to be amazing: "Imperfect tests, run frequently, are much better than perfect tests that are never written at all"
  • 4. Disadvantages ● You have to suck it up and write tests to really see the benefits ● Hard to retrofit tests ○ You may have to refactor old code, or ○ You may have to write a lot of test code for code which is not easy to test ● Harder to write bad code ● You probably won't want to write code without tests again
  • 5. Goals When Writing Tests ● Tests should be ○ Simple ■ Don't have ifs or loops ○ Easy to write ○ Easy to read ○ Easy to run ○ Isolated ● But these ideas may conflict or be hard to achieve ○ Do your best ○ Can improve tests over time ○ Bad tests better than no tests
  • 6. Common PHPUnit Methods ● $this->assertTrue($value, 'Value should have been true') ● $this->assertFalse($value, 'Value should have been false') ● $this->assertEquals($expected, $actual, 'The result is not what was expected') ● Try to write a message to indicate what specifically is being asserted ○ Easier to understand what's wrong instead of e.g. "Failed asserting that false is true"
  • 7. Common PHPUnit Patterns ● Use setUp() method to instantiate the class you are testing ○ This works well if you don't have lots of constructor parameters ● Use data providers to test lots of different inputs ○ Tests shouldn't have duplicate code either ● Use tearDown() method if you need to clean up external resources after each test is run ○ Files ○ Database
  • 8. Hard to Test Things ● Static calls to other classes ○ You cannot mock the dependency ● Singletons ○ Constructor is private or protected ○ Uses static call to get instance ○ Cannot guarantee there are no side effects when running multiple tests ● Private methods ○ Usually you indirectly test these when you test public methods ○ Can use reflection if you really want, but this probably means you should refactor
  • 9. Hard to Test Things ● Hard coded dependencies ○ Such as when a class instantiates another class ● Magic methods ○ __call ○ __get ○ __set
  • 10. How to Write Testable Code ● Use dependency injection ○ This means you don't instantiate classes unless you're in a factory ● Keep methods relatively small ● Avoid having lots of private methods ● Write classes which do one thing (single responsibility principle)
  • 11. Go go go! ● http://www.phpunit. de/manual/current/en/index.html