php unit-testing with doubles
DESCRIPTION
Some details on what the different doubles are and examples on their usage with PHPUnitTRANSCRIPT
![Page 1: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/1.jpg)
PHP Unit-Testing Techniques
Stubs, Mocks, Dummies,
and Other Test Doubles
Mihail Irintchev
or
We will, we will
MOCK YOU!
![Page 2: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/2.jpg)
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
![Page 3: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/3.jpg)
Why is Unit-Testing Important?
?
![Page 4: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/4.jpg)
Why is Unit-Testing Important?
![Page 5: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/5.jpg)
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
![Page 6: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/6.jpg)
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
![Page 7: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/7.jpg)
Why so fast?
![Page 8: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/8.jpg)
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)
![Page 9: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/9.jpg)
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
![Page 10: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/10.jpg)
Types of Test Doubles
dummy stub mock spy fake
![Page 11: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/11.jpg)
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.
![Page 12: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/12.jpg)
Warning: Terminology Really Kills
“Exploring The Continuum Of Test Doubles”, Mark Seemannhttp://msdn.microsoft.com/en-us/magazine/cc163358.aspx
No Implementation Full Implementation
![Page 13: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/13.jpg)
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)
![Page 14: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/14.jpg)
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
![Page 15: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/15.jpg)
A Word About The Tool-set
Popular unit-testing frameworks for PHP:
PHPUnit Simpletest Mockery
![Page 16: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/16.jpg)
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
![Page 17: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/17.jpg)
1. Dummy
A dummy is just a placeholder for a real object, required by the one you are testing, but not really used.
![Page 18: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/18.jpg)
Original class
![Page 19: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/19.jpg)
Class under test
![Page 20: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/20.jpg)
The test method
![Page 21: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/21.jpg)
The test method v1.1
![Page 22: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/22.jpg)
2. Stub
A stub provides “canned responses” to method calls on the doubled object
![Page 23: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/23.jpg)
Original class
![Page 24: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/24.jpg)
Unit under test
![Page 25: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/25.jpg)
The test method
![Page 26: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/26.jpg)
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)
![Page 27: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/27.jpg)
Original class
![Page 28: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/28.jpg)
Unit under test
![Page 29: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/29.jpg)
Test #1 – Failure expected
![Page 30: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/30.jpg)
Test #2 – Success expected
![Page 31: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/31.jpg)
Expectations can sometimes be confusing...
![Page 32: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/32.jpg)
5. Fake
A fake is actually a simpler implementation of a real object
![Page 33: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/33.jpg)
Wrapping it in one sentence ...
The question is NOT whether you should test or not,
but HOW should you test.
![Page 34: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/34.jpg)
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
![Page 35: PHP Unit-Testing With Doubles](https://reader033.vdocuments.net/reader033/viewer/2022061214/549e71bbb379597b4b8b45a3/html5/thumbnails/35.jpg)
Q & A
?