Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHPUnit from a developer's perspective


Published on

Introduction to PHPUnit unit testing tool for PHP
presented by Tushar Joshi, Nagpur @ PHPMeet help in Nagpur on 19th June 2010

Published in: Education, Technology

PHPUnit from a developer's perspective

  1. 1. PHPUnit A developer's perspective By Tushar Joshi, Nagpur Presented at: 19 June 2010, 4pm, PHPMeet Nagpur #ngpphpmeet
  2. 2. Assumptions for this presentation <ul><li>No earlier knowledge of PHPUnit
  3. 3. PHP Developers
  4. 4. Knowledge of Object Oriented Programming </li></ul>
  5. 5. Things covered <ul><li>Conceptual introduction to PHPUnit
  6. 6. Discussion about need of unit testing
  7. 7. Case for PHPUnit
  8. 8. Demonstration of working of PHPUnit (not part of slides but done side by side while presenting) </li></ul>
  9. 9. Things not covered <ul><li>History of PHPUnit, history of unit testing frameworks
  10. 10. Installation of PHP, PHPUnit, PEAR, Xdebug, NetBeans IDE
  11. 11. Configuration of the development environment
  12. 12. Detailed usage of PHPUnit, all cases which can be tested </li></ul>
  13. 13. Are there any bugs? <ul><li>Developer says: </li><ul><li>I have checked the code many times
  14. 14. There cannot be any bugs now
  15. 15. Manager believes the first time </li></ul></ul>Courtesy: Sam Fraser - Smith
  16. 16. Client sends issues <ul><li>Developer says: </li><ul><li>Oops, I don't know how I slipped that one
  17. 17. Never thought that value will be passed to this function
  18. 18. Next time manager will be skeptical to believe easily </li></ul></ul>Courtesy: cheerfulmonk
  19. 19. Introducing PHPUnit <ul><li>Tool to unit test your PHP code
  20. 20. To re-test the code after any modifications
  21. 21. Tool to ensure your code always work for given inputs </li></ul><ul><li>Can be used for Object Oriented PHP code </li></ul>
  22. 22. Is PHPUnit Easy? <ul><li>You cannot use the tool unless you know it
  23. 23. Learning a tool is a skill building activity
  24. 24. Assuming you know the tool, it will save you always </li></ul><ul><li>PHPUnit is written in PHP
  25. 25. You know PHP </li></ul>
  26. 26. PHPUnit Overhead? <ul><li>Developers tool to verify his code
  27. 27. No need to show test classes if not needed
  28. 28. If tool is readily available, it is a matter of habit </li></ul><ul><li>Peace of mind
  29. 29. Way to prove your code works in all situations
  30. 30. Opportunity to trap hidden bugs before client finds them
  31. 31. Life Saver </li></ul>
  32. 32. Case for PHPUnit <ul><li>What will you choose between </li><ul><li>Text Editor
  33. 33. Syntax highlighting editor with context help and intelli-sense (CTRL+SPACE) </li></ul><li>What will you choose between </li><ul><li>Testing methods by eyes
  34. 34. Testing by a tool, with advanced facilities </li></ul></ul>
  35. 35. Sample PHP class <ul><li>Let us test this class
  36. 36. Starting with getBalance method
  37. 37. Opportunity to think behavior of getBalance </li></ul>
  38. 38. <ul><li>Test Class
  39. 39. Result Red or Green </li></ul>Sample Test
  40. 40. Available Assertions
  41. 41. Using Constraints <ul><li>$this->lessThan(3000)
  42. 42. This creates a constraint expectation
  43. 43. $this->assertThat checks whether the value is according to the expectation </li></ul>
  44. 44. Available Constraints
  45. 45. Testing Exceptions <ul><li>@expectedException
  46. 46. To check whether exception was thrown </li></ul>
  47. 47. Data Providers <ul><li>Way to create multiple data sets
  48. 48. Sets of parameters for which the function must run correctly
  49. 49. Reduces effort for writing multiple test methods </li></ul>
  50. 50. Mock Objects and Expectations <ul><li>Quickest way to test dependencies
  51. 51. $object = $this->getMock('NameOfClass');
  52. 52. $object ->expects( invocationExpectation ) ->method( methodExpectation ) [->with( parameterExpectation )] [->will( returnValue Expectation)] </li></ul>
  53. 53. Available Invocation Expectations <ul><li>$this->any() - example </li></ul>
  54. 54. Parameters and Return Values <ul><li>Used with the with and will methods of mock object
  55. 55. $this->equals(val) type of constraints
  56. 56. $this->onConsecutiveCalls(val, val2, val3,..) </li><ul><li>List of values which will be used for each consecutive next call </li></ul></ul>
  57. 57. Advanced Features <ul><li>Virtual File System
  58. 58. DB testing with DBUnit
  59. 59. Mocking Web services
  60. 60. Testing for console output </li></ul>
  61. 61. References <ul><li>PHPUnit site </li><ul><li> </li></ul><li>PHPUnit Manual </li><ul><li> </li></ul><li>NetBeans IDE Site </li><ul><li> </li></ul></ul>