by Silviu Butnariu (sbutnariu@pentalog.fr)
What’s that?
 Unit testing = testing bunches of code, not the whole

application
 PhpUnit was created by Sebastian Bergamann
 It’s part of xUnit family – most used unit testers

 Used for automatic tests – makes the machine do the

work
What are the benefits
of unit testing?
 Uncover bugs easier – good tests go trough all possible

program paths
 Assure high code coverage – measurable code quality

indicator
 Faster than manual testing
 Suitable for teamwork
Installing PhpUnit
 With PEAR:

pear config-set auto_discover 1
pear install phpunit/PHPUnit
 With composer (dependency manager for php); just

add to composer.json:
{ "require-dev": { "phpunit/phpunit": "3.7.*" } }
and update the composer
Basic test
Test result notations
. – success
F – failure
E – error
I – incomplete
S - skipped
Assertions
 Helper functions that compare an expected result with
-

the actual one:
assertEquals()
assertFalse()
assertArrayHasKey()
assertInstanceOf()
assertNotNull()
assertRegExp()
etc.
Dependency injection
 DI is a software pattern that allows the removal of

hard-coded dependencies
 Highly coupled dependencies – bad
 Loose coupled dependencies - awesome
 Improves code readability, reusability
DI guidelines
 Don’t use ‘new’
 Pass dependencies as method parameters
 DI allows real unit testing, by separating modules
 Read the DI best practices 
Mocking objects
 Creating fake objects that act in a predefined and

predictable way
Stubbing methods
 In close relation to mocking
 Stubbing implies faking a method from the mock

object
Static methods
 Avoid static methods

Can’t mock
static calls to
outside classes!
Annotations
 These syntactic metadata are used for specifying
-

some special case behaviors
@covers
@test
@dataProvider
@expectedException
@group
etc.
Data Provider
 Allows to bunch up more test cases into one, thus

reducing code duplicity
Testing exceptions
setUp & tearDown
 setUp(), tearDown() – executed once for each test method
 setUpBeforeClass(), tearDownAfterClass() - called before the first

test of the test case and after the last test
DB testing
 Need extension: pear install phpunit/DbUnit
 Supported dbs: mysql, postgre, oracle, sqlite
 There are 4 stages of db testing:
- set up fixture
- exercise SUT
- verify outcome
- teardown
 Important methods:
- getConnection() - connection data (host,db,pass)
- getDataSet() – defines state of DB before each test
DB testing
 Once setting up the ‘fake’ db connection tests can be

executed to check queries

 The db changes that occur during the tests don’t persist
 Can verify number of rows after insertion, check query

results, etc.

 This is the safest way of fully testing an application

(versus mocking db connection)
Skeleton Generator
 Automatically generates test classes based on

assertions in the code
IDE integration
 Integrated in most ides: Eclipse, NetBeans,

PhpStorm, etc
Code coverage
 Generating statistics with naked PhpUnit
 phpunit -c app/ --coverage-html={foldername}
Code coverage
 FFFFFFF
Documentation
 http://phpunit.de/manual/3.7/en/
Q&A
THANKS !!!

Unit Testing in PHP

  • 1.
    by Silviu Butnariu(sbutnariu@pentalog.fr)
  • 2.
    What’s that?  Unittesting = testing bunches of code, not the whole application  PhpUnit was created by Sebastian Bergamann  It’s part of xUnit family – most used unit testers  Used for automatic tests – makes the machine do the work
  • 3.
    What are thebenefits of unit testing?  Uncover bugs easier – good tests go trough all possible program paths  Assure high code coverage – measurable code quality indicator  Faster than manual testing  Suitable for teamwork
  • 4.
    Installing PhpUnit  WithPEAR: pear config-set auto_discover 1 pear install phpunit/PHPUnit  With composer (dependency manager for php); just add to composer.json: { "require-dev": { "phpunit/phpunit": "3.7.*" } } and update the composer
  • 5.
  • 6.
    Test result notations .– success F – failure E – error I – incomplete S - skipped
  • 7.
    Assertions  Helper functionsthat compare an expected result with - the actual one: assertEquals() assertFalse() assertArrayHasKey() assertInstanceOf() assertNotNull() assertRegExp() etc.
  • 8.
    Dependency injection  DIis a software pattern that allows the removal of hard-coded dependencies  Highly coupled dependencies – bad  Loose coupled dependencies - awesome  Improves code readability, reusability
  • 9.
    DI guidelines  Don’tuse ‘new’  Pass dependencies as method parameters  DI allows real unit testing, by separating modules  Read the DI best practices 
  • 10.
    Mocking objects  Creatingfake objects that act in a predefined and predictable way
  • 11.
    Stubbing methods  Inclose relation to mocking  Stubbing implies faking a method from the mock object
  • 12.
    Static methods  Avoidstatic methods Can’t mock static calls to outside classes!
  • 13.
    Annotations  These syntacticmetadata are used for specifying - some special case behaviors @covers @test @dataProvider @expectedException @group etc.
  • 14.
    Data Provider  Allowsto bunch up more test cases into one, thus reducing code duplicity
  • 15.
  • 16.
    setUp & tearDown setUp(), tearDown() – executed once for each test method  setUpBeforeClass(), tearDownAfterClass() - called before the first test of the test case and after the last test
  • 17.
    DB testing  Needextension: pear install phpunit/DbUnit  Supported dbs: mysql, postgre, oracle, sqlite  There are 4 stages of db testing: - set up fixture - exercise SUT - verify outcome - teardown  Important methods: - getConnection() - connection data (host,db,pass) - getDataSet() – defines state of DB before each test
  • 18.
    DB testing  Oncesetting up the ‘fake’ db connection tests can be executed to check queries  The db changes that occur during the tests don’t persist  Can verify number of rows after insertion, check query results, etc.  This is the safest way of fully testing an application (versus mocking db connection)
  • 19.
    Skeleton Generator  Automaticallygenerates test classes based on assertions in the code
  • 20.
    IDE integration  Integratedin most ides: Eclipse, NetBeans, PhpStorm, etc
  • 21.
    Code coverage  Generatingstatistics with naked PhpUnit  phpunit -c app/ --coverage-html={foldername}
  • 22.
  • 23.
  • 24.
  • 25.