SlideShare a Scribd company logo
PHP Unit-Testing Techniques

Stubs, Mocks, Dummies,
and Other Test Doubles
or

We will, we will
MOCK YOU!
Mihail Irintchev
What is Unit-Testing?
unit testing is a method by which individual units of source

code, sets of one or more computer program modules
together with associated control data, usage procedures, and
operating procedures are tested to determine if they are fit for
use... Intuitively, one can view a unit as the smallest
testable part of an application.
http://en.wikipedia.org/wiki/Unit_testing

unit testing is the safe ground I can hold onto when the

deadline is coming, an insurance policy against the
unavoidable changes, a safe harbor for my soul when
everything is shaking, including my belief in whether I am
capable of writing working code at all.

Me
Why is Unit-Testing Important?

?
Why is Unit-Testing Important?
Why is Unit-Testing Important?
Unit tests:


Find problems early



Facilitate change



Simplify integration



Play the role of living documentation



Sometimes aid software design



Save time (and money, and nerves) in the long run

Build a safety layer which creates a warm fuzzy feeling
inside the soul of the developer

What Makes Unit-Tests Work?



Each unit test is independent from the others



Unit tests are ran automatically and frequently



Best used as part of an automated build procedure



They should run fast
Why so fast?
How Do Unit-Tests Become
Independent and Fast?
Through isolation from:


the environment (DB, file system)



other tests



external systems (APIs, web services)



collaborators (other objects)
How is Isolation achieved?

Through the use of test doubles: “an object or procedure that
looks and behaves like its release-intended counterpart, but
is actually a simplified version that reduces the complexity
and facilitates testing”
“Testing-essentials mock objects explained”, Jeff Carouth
Types of Test Doubles







dummy
stub
mock
spy
fake
Warning: Terminology Kills




Dummy, mock, and fake are often used
interchangeably by many authors to refer to a
kind (or many kinds, or a mixed type) of a testing
double
There is a degree of difference, depending of
what extend of real functionality do these test
objects posses and how they implement it, but
what matters is when can each of them can be
useful to you.
Warning: Terminology Really Kills

No Implementation

Full Implementation
“Exploring The Continuum Of Test Doubles”, Mark Seemann
http://msdn.microsoft.com/en-us/magazine/cc163358.aspx
The Mock Turtle

Then the Queen left off, quite out of breath,
and said to Alice,
"Have you seen the Mock Turtle yet?"
"No," said Alice. "I don't even know what a Mock Turtle is."
"It's the thing Mock Turtle Soup is made from,"
said the Queen.
(Alice in Wonderland, chapter 9)
Mock Turtle & Mock Turtle Soup




The Mock Turtle is a fictional character devised
by Lewis Carroll from his popular book Alice's
Adventures in Wonderland. Its name is taken
from a dish that was popular in the Victorian
period, mock turtle soup.
Mock turtle soup is an English soup that was
created in the mid-18th century as a cheaper
imitation of green turtle soup. It often uses brains
and organ meats such as calf's head or a calf's
foot to duplicate the texture and flavour of the
original turtle meat.
Wikipedia
A Word About The Tool-set
Popular unit-testing frameworks for PHP:




PHPUnit
Simpletest
Mockery
The Industry Standard: PHPUnit
Why?


Great documentation



Very powerful (you will see)



Very easy installation/upgrade via pear



Great userbase



Integration with phing



Very passionate and dedicated author:
Sebastian Bergmann
1.

Dummy

A dummy is just a placeholder for a real object,
required by the one you are testing, but not really
used.
Original class
Class under test
The test method
The test method v1.1
2.

Stub

A stub provides “canned responses” to method
calls on the doubled object
Original class
Unit under test
The test method
3.

Mock & Spy

A mock object sets and asserts pre-defined
expectations of the methods it should or should
not receive
A spy records information about what was called
and can be used to verify calls (or the absence of
calls)
Original class
Unit under test
Test #1 – Failure expected
Test #2 – Success expected
Expectations can sometimes
be confusing...
5. Fake

A fake is actually a simpler implementation of a
real object
Wrapping it in one sentence ...

The question is NOT whether you should test or not,
but HOW should you test.
Resources

“PHPUnit Documentation: Chapter 10. Test Doubles” by Sebastian Bergmann
http://phpunit.de/manual/3.7/en/test-doubles.html

“Testing Essentials: Mock Objects Explained” by Jeff Carouth
https://speakerdeck.com/jcarouth/zendcon-2013-testing-essentials-mock-objects-explained
Q&A

?
Thank You!

Contact Info:
m.irintchev@siteground.com
@irintchev

More Related Content

Viewers also liked (12)

PPT
How to test models using php unit testing framework?
satejsahu
 
PPTX
Ioc & in direction
育汶 郭
 
PPT
2007 5 30 肖镜辉 统计语言模型简介
xceman
 
KEY
IoC with PHP
Chris Weldon
 
PDF
PHPUnit でよりよくテストを書くために
Yuya Takeyama
 
PPTX
TDD Course (Spanish)
Pedro Ballesteros
 
PDF
PHPUnit 入門介紹
Jace Ju
 
PDF
PHPUnit best practices presentation
Thanh Robi
 
PDF
Introduction to Unit Testing with PHPUnit
Michelangelo van Dam
 
PDF
Jmeter Performance Testing
Atul Pant
 
PDF
B M Social Media Fortune 100
Burson-Marsteller
 
PPTX
Di – ioc (ninject)
ZealousysDev
 
How to test models using php unit testing framework?
satejsahu
 
Ioc & in direction
育汶 郭
 
2007 5 30 肖镜辉 统计语言模型简介
xceman
 
IoC with PHP
Chris Weldon
 
PHPUnit でよりよくテストを書くために
Yuya Takeyama
 
TDD Course (Spanish)
Pedro Ballesteros
 
PHPUnit 入門介紹
Jace Ju
 
PHPUnit best practices presentation
Thanh Robi
 
Introduction to Unit Testing with PHPUnit
Michelangelo van Dam
 
Jmeter Performance Testing
Atul Pant
 
B M Social Media Fortune 100
Burson-Marsteller
 
Di – ioc (ninject)
ZealousysDev
 

Similar to PHP Unit-Testing With Doubles (20)

PDF
Learn To Test Like A Grumpy Programmer - 3 hour workshop
chartjes
 
PPTX
presentation des google mock dans un contexte de tdd
Thierry Gayet
 
PPT
Mocking Dependencies in PHPUnit
mfrost503
 
PDF
CBDW2014 - MockBox, get ready to mock your socks off!
Ortus Solutions, Corp
 
PDF
Just Mock It - Mocks and Stubs
Gavin Pickin
 
PPT
Mocking Dependencies in PHPUnit
mfrost503
 
KEY
Developer testing 101: Become a Testing Fanatic
LB Denker
 
KEY
Developer testing 201: When to Mock and When to Integrate
LB Denker
 
PDF
Test doubles and EasyMock
Rafael Antonio Gutiérrez Turullols
 
PPT
Google mock training
Thierry Gayet
 
PDF
Unit testing with PHPUnit - there's life outside of TDD
Paweł Michalik
 
PDF
Unit Testing and role of Test doubles
Ritesh Mehrotra
 
PDF
Developers on test
Datio Big Data
 
PPTX
Unit testing and mocking in Python - PyCon 2018 - Kenya
Erick M'bwana
 
PDF
How and what to unit test
Eugenio Lentini
 
PPTX
Test Doubles
Bill Shelton
 
PPTX
Unit Testing in PHP
Radu Murzea
 
PDF
Win at life with unit testing
markstory
 
PPTX
Intro to Mocking - DjangoCon 2015
Excella
 
PDF
Testing Workshop
michaeldunstan
 
Learn To Test Like A Grumpy Programmer - 3 hour workshop
chartjes
 
presentation des google mock dans un contexte de tdd
Thierry Gayet
 
Mocking Dependencies in PHPUnit
mfrost503
 
CBDW2014 - MockBox, get ready to mock your socks off!
Ortus Solutions, Corp
 
Just Mock It - Mocks and Stubs
Gavin Pickin
 
Mocking Dependencies in PHPUnit
mfrost503
 
Developer testing 101: Become a Testing Fanatic
LB Denker
 
Developer testing 201: When to Mock and When to Integrate
LB Denker
 
Test doubles and EasyMock
Rafael Antonio Gutiérrez Turullols
 
Google mock training
Thierry Gayet
 
Unit testing with PHPUnit - there's life outside of TDD
Paweł Michalik
 
Unit Testing and role of Test doubles
Ritesh Mehrotra
 
Developers on test
Datio Big Data
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Erick M'bwana
 
How and what to unit test
Eugenio Lentini
 
Test Doubles
Bill Shelton
 
Unit Testing in PHP
Radu Murzea
 
Win at life with unit testing
markstory
 
Intro to Mocking - DjangoCon 2015
Excella
 
Testing Workshop
michaeldunstan
 
Ad

Recently uploaded (20)

PDF
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
PDF
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
PDF
The Growing Value and Application of FME & GenAI
Safe Software
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PDF
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
PPTX
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
PDF
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
The Growing Value and Application of FME & GenAI
Safe Software
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Practical Applications of AI in Local Government
OnBoard
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
Ad

PHP Unit-Testing With Doubles

  • 1. PHP Unit-Testing Techniques Stubs, Mocks, Dummies, and Other Test Doubles or We will, we will MOCK YOU! Mihail Irintchev
  • 2. What is Unit-Testing? unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use... Intuitively, one can view a unit as the smallest testable part of an application. http://en.wikipedia.org/wiki/Unit_testing unit testing is the safe ground I can hold onto when the deadline is coming, an insurance policy against the unavoidable changes, a safe harbor for my soul when everything is shaking, including my belief in whether I am capable of writing working code at all. Me
  • 3. Why is Unit-Testing Important? ?
  • 4. Why is Unit-Testing Important?
  • 5. Why is Unit-Testing Important? Unit tests:  Find problems early  Facilitate change  Simplify integration  Play the role of living documentation  Sometimes aid software design  Save time (and money, and nerves) in the long run Build a safety layer which creates a warm fuzzy feeling inside the soul of the developer 
  • 6. What Makes Unit-Tests Work?  Each unit test is independent from the others  Unit tests are ran automatically and frequently  Best used as part of an automated build procedure  They should run fast
  • 8. How Do Unit-Tests Become Independent and Fast? Through isolation from:  the environment (DB, file system)  other tests  external systems (APIs, web services)  collaborators (other objects)
  • 9. How is Isolation achieved? Through the use of test doubles: “an object or procedure that looks and behaves like its release-intended counterpart, but is actually a simplified version that reduces the complexity and facilitates testing” “Testing-essentials mock objects explained”, Jeff Carouth
  • 10. Types of Test Doubles      dummy stub mock spy fake
  • 11. Warning: Terminology Kills   Dummy, mock, and fake are often used interchangeably by many authors to refer to a kind (or many kinds, or a mixed type) of a testing double There is a degree of difference, depending of what extend of real functionality do these test objects posses and how they implement it, but what matters is when can each of them can be useful to you.
  • 12. Warning: Terminology Really Kills No Implementation Full Implementation “Exploring The Continuum Of Test Doubles”, Mark Seemann http://msdn.microsoft.com/en-us/magazine/cc163358.aspx
  • 13. The Mock Turtle Then the Queen left off, quite out of breath, and said to Alice, "Have you seen the Mock Turtle yet?" "No," said Alice. "I don't even know what a Mock Turtle is." "It's the thing Mock Turtle Soup is made from," said the Queen. (Alice in Wonderland, chapter 9)
  • 14. Mock Turtle & Mock Turtle Soup   The Mock Turtle is a fictional character devised by Lewis Carroll from his popular book Alice's Adventures in Wonderland. Its name is taken from a dish that was popular in the Victorian period, mock turtle soup. Mock turtle soup is an English soup that was created in the mid-18th century as a cheaper imitation of green turtle soup. It often uses brains and organ meats such as calf's head or a calf's foot to duplicate the texture and flavour of the original turtle meat. Wikipedia
  • 15. A Word About The Tool-set Popular unit-testing frameworks for PHP:    PHPUnit Simpletest Mockery
  • 16. The Industry Standard: PHPUnit Why?  Great documentation  Very powerful (you will see)  Very easy installation/upgrade via pear  Great userbase  Integration with phing  Very passionate and dedicated author: Sebastian Bergmann
  • 17. 1. Dummy A dummy is just a placeholder for a real object, required by the one you are testing, but not really used.
  • 22. 2. Stub A stub provides “canned responses” to method calls on the doubled object
  • 26. 3. Mock & Spy A mock object sets and asserts pre-defined expectations of the methods it should or should not receive A spy records information about what was called and can be used to verify calls (or the absence of calls)
  • 29. Test #1 – Failure expected
  • 30. Test #2 – Success expected
  • 32. 5. Fake A fake is actually a simpler implementation of a real object
  • 33. Wrapping it in one sentence ... The question is NOT whether you should test or not, but HOW should you test.
  • 34. Resources “PHPUnit Documentation: Chapter 10. Test Doubles” by Sebastian Bergmann http://phpunit.de/manual/3.7/en/test-doubles.html “Testing Essentials: Mock Objects Explained” by Jeff Carouth https://speakerdeck.com/jcarouth/zendcon-2013-testing-essentials-mock-objects-explained
  • 35. Q&A ?