4. WORKING FOR
ResearchGate gives science back to the people who make it happen.
We help researchers build reputation and accelerate scientific
progress.
On their terms.
8. THE FASTEST THING YOU CAN DO
ht=cr - saigpoetcm|ge 'oi: |w -`
is`ul s tgn.rjc.o rp Lgn' c l;
ts $is-q1| eh "rnpg err"
et ht e | co Fotae ro!
Staging server
Testing your builds
All without even touching PHPUnit
dt=lgnts&asotscr&sf"crTkn
aa"oi=etpswr=euecr=$sfoe
ht=cr - PS - saigpoetcm|ge 'el,tsue'|w -`
is`ul X OT d tgn.rjc.o rp Hlo etsr c l;
ts $is-q1| eh "oi err"
et ht e | co Lgn ro!
11. PHAR
we ht:/erppntd/e/hui.hr
gt tp/pa.hui.egtppntpa
cmd+ ppntpa
ho x hui.hr
.ppntpa -vrin
/hui.hr -eso
or
we ht:/erppntd/e/hui.hr
gt tp/pa.hui.egtppntpa
cmd+ ppntpa
ho x hui.hr
m ppntpa /s/oa/i/hui
v hui.hr urlclbnppnt
ppnt-vrin
hui -eso
12. COMPOSER
The Dependency Manager for PHP
With the best from zypper, bundler, pip, gem and npm
16. USE SPECIFIC ASSERTIONS
PHPUnit ships with over 90 assertions.
http://www.phpunit.de/manual/current/en/appendixes.assertions.html
Use them to get pretty and helpful error messages.
17. assertTrue vs assertInstanceOf
$o =nwSdls(;
fo e tCas)
$hs>setre$o isacO Cutbe;
ti-asrTu(fo ntnef onal)
“Failed asserting that false is true.”
$o =nwSdls(;
fo e tCas)
$hs>setntnef'onal' $o)
ti-asrIsacO(Cutbe, fo;
“Failed asserting that
stdClass() is an instance of interface 'Countable'.”
24. OR HOWEVER YOU SEE FIT
Use @group
Use @filter and naming conventions
25. BOOTSTRAP ONLY WHAT YOU NEED
You can use a test listener:
http://www.phpunit.de/manual/current/en/extending-
phpunit.html#extending-phpunit.PHPUnitFrameworkTestListener
pbi fnto satetut(HUi_rmwr_etut $ut)
ulc ucin trTsSiePPntFaeokTsSie sie
{
/ Js a eapeo wa i psil
/ ut n xml f ht s osbe
rqie_DR_.$ut-gtae).'otta.h'
eur _I_ sie>eNm( Bosrppp;
}
26. HOW MANY TESTS?
Web: 7
Funtional: One per feature
Integration: One per 3 classes
Unit: Find a balance
*totally made up numbers to drive home the point I'm trying to make
27. WEB TESTS?
behat (mink) for js-through-the-server testing - Great for testing
your whole stack
Really hard to maintain
Mink relives some of the pain
Test through your front controller instead of the webserver with
behat or phpunit
Faster, easier once set up
28. TEST CLASSES,
NOT METHODS
> Unit testing, in PHP, is about testing the observable behaviors of a
class!
Observable from the outside! Nobody cares about the internal state of
a class if it never changes the outcome of a method call.
29. SAMPLE
What should we test there?
pbi fnto stau(vle {
ulc ucin eVle$au)
$hs>au =$au;
ti-vle vle
}
pbi fnto eeue){
ulc ucin xct(
i ($hs>au){
f !ti-vle
trwnwEcpin"oVle n go";
ho e xeto(N au, o od)
}
rtr $au *1;/ bsns lgc
eun vle 0 / uies oi
}
If we don't call setValue calling execute will throw an exception
If we do call setValue calling execute will return the computed
result.
So we are testing two behaviors of your class and not the methods in
isolation!
30. RELEVANT BEHAVIORS
What to test then?
return values
pbi fcinclisoaehi(dgesaehi){
ulc uto ecuTFrnet$ereFrnet
rtr (dgesaehi -3)*5⁄9
eun $ereFrnet 2 ;
}
method calls to other objects
pbi fcinsoCr){
ulc uto tpa(
$hs>adra-egg(;
ti-hnbek>nae)
$hs>nie>hton)
ti-egn-sudw(;
}
Global state
pbi fcinaodhshrPsil(lgesg){
ulc uto viTiWeeosbe$oMsae
fl_u_otnssai:$OFL,$oMsae FL_PED;
ieptcnet(ttc:LGIE lgesg, IEAPN)
$SSIN'ocls]+
_ESO[lgal'+;
}
31. DON'T TEST GETTERS AND SETTERS
One test case per behavior
You waste time
Your code coverage reports won't tell you about dead code
If they don't impact the outcome delete them
32. QUESTIONS?
“The secret in testing is in writing testable code”
- Miško Hevery
Additional resources
"The Clean Code Talks -- Unit Testing
How to Write Clean, Testable Code
The Clean Code Talks - Don't Look For Things!
Flaw: Brittle Global State & Singletons
static considered harmful
The UNIT in unit testing
An introduction to PHPUnits @covers annotation