Unit testing in PHP


Published on

A quick presentation about PHP unit testing I made for work

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Unit testing in PHP

  1. 1. Unit Testing in PHPLee Boynton
  2. 2. Advantages to Automating Testing● Faster to test○ Dont need to "click about"○ Dont need to login○ Dont 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 elses code● Find bugs before your users○ This means you keep your customers happy, thismeans more money!
  3. 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, itsprobably faster● Tests dont have to be amazing: "Imperfecttests, run frequently, are much better thanperfect tests that are never written at all"
  4. 4. Disadvantages● You have to suck it up and write tests toreally 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 codewhich is not easy to test● Harder to write bad code● You probably wont want to write codewithout tests again
  5. 5. Goals When Writing Tests● Tests should be○ Simple■ Dont have ifs or loops○ Easy to write○ Easy to read○ Easy to run○ Isolated● But these ideas may conflict or be hard toachieve○ Do your best○ Can improve tests over time○ Bad tests better than no tests
  6. 6. Common PHPUnit Methods● $this->assertTrue($value, Value shouldhave been true)● $this->assertFalse($value, Value shouldhave been false)● $this->assertEquals($expected, $actual,The result is not what was expected)● Try to write a message to indicate whatspecifically is being asserted○ Easier to understand whats wrong instead of e.g."Failed asserting that false is true"
  7. 7. Common PHPUnit Patterns● Use setUp() method to instantiate the classyou are testing○ This works well if you dont have lots of constructorparameters● Use data providers to test lots of differentinputs○ Tests shouldnt have duplicate code either● Use tearDown() method if you need to cleanup external resources after each test is run○ Files○ Database
  8. 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 whenrunning multiple tests● Private methods○ Usually you indirectly test these when you test publicmethods○ Can use reflection if you really want, but thisprobably means you should refactor
  9. 9. Hard to Test Things● Hard coded dependencies○ Such as when a class instantiates another class● Magic methods○ __call○ __get○ __set
  10. 10. How to Write Testable Code● Use dependency injection○ This means you dont instantiate classes unlessyoure in a factory● Keep methods relatively small● Avoid having lots of private methods● Write classes which do one thing (singleresponsibility principle)
  11. 11. Go go go!● http://www.phpunit.de/manual/current/en/index.html