Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Code testing with PHP

Code testing with PHP

Martin Pernica

July 25, 2013
Tweet

More Decks by Martin Pernica

Other Decks in Programming

Transcript

  1. Why code testing? • Reduce bugs in your code •

    Stable class API • Automatic feedback • More readable code (automatic docs) • It is cool :) čtvrtek, 25. července 13
  2. Types of tests • Unit • Integration • System •

    Acceptance • ... and more Unit tests Integration tests Acceptance System tests čtvrtek, 25. července 13
  3. Requirements • Fast • Timely • Independent • Repeatable •

    Self-validating • Short • Simple čtvrtek, 25. července 13
  4. If you using Dependecy Injection Controller DI is not about

    container! čtvrtek, 25. července 13
  5. If you using Dependecy Injection Controller DI is not about

    container! čtvrtek, 25. července 13
  6. If you using Dependecy Injection Data Repository Controller DI is

    not about container! čtvrtek, 25. července 13
  7. If you using Dependecy Injection Database Data Repository Controller DI

    is not about container! čtvrtek, 25. července 13
  8. If you using Dependecy Injection Database Mamcache Data Repository Controller

    DI is not about container! čtvrtek, 25. července 13
  9. If you using Dependecy Injection Database Mamcache Data Repository Controller

    DI is not about container! čtvrtek, 25. července 13
  10. If you using Dependecy Injection Database Mamcache Data Repository Controller

    DI is not about container! čtvrtek, 25. července 13
  11. DI example in PHP <?php iterface  IGas  {   public

     fill($amount); } class  NaturalGas  implements  IGas  {   private  $content  =  0.0;   public  fill($amount)  {     $this-­‐>content  +=  max(0,  (float)$amount);   } } class  Oil  { } class  Engine  {   public  function  __construct(IGas  $gas,  Oil  $oil)  {} } čtvrtek, 25. července 13
  12. Unit testing • Test single class • Test all methods

    • Needs some dependencies? Mock it! čtvrtek, 25. července 13
  13. Unit testing • Test single class • Test all methods

    • Needs some dependencies? Mock it! • PHPUnit • Nette Tester • SimpleTest čtvrtek, 25. července 13
  14. Mocking? • Class Engine needs IGas and Oil objects •

    Unit testing is about testing single class • Create mocks of IGas and Oil objects • Mock is dummy object which return defined values Original object Mock čtvrtek, 25. července 13
  15. Mock example <?php class  NaturalGas  implements  IGas  {   private

     $content  =  0.0;   public  fill($amount)  {     $this-­‐>content  +=  max(0,  (float)$amount); if($this-­‐>content  >  100)  { throw  new  \Exception(“Overflow!); }   } } class  NaturalGasMock  extends  NaturalGas  implements  IGas  { pulic  fill($amount)  { return  true; } } čtvrtek, 25. července 13
  16. Mocking frameworks • PHPUnit • Phake • Mockery • Mockista

    (by Jan Marek) čtvrtek, 25. července 13
  17. Unit testing example <?php class  Foo  {      public

     function  multiplicationBy2($a)  { return  $a  *  2; } } class  FooTest  extends  \PHPUnit_Framework_TestCase  { public  function  testMultiplicationBy2()  { $subject  =  new  Foo(); $result  =  $subject-­‐>multiplicationBy2(2); $this-­‐>assertEquals($result,  2); } } Using PHPUnit čtvrtek, 25. července 13
  18. PHPUnit in PHPStorm • You can integrate your testing routine

    into PHPStorm • PHPStorm support testing with code coverage option čtvrtek, 25. července 13
  19. • Write testable code • Write tests • Deliver code

    with better quality čtvrtek, 25. července 13
  20. • Write some tests for your code • Next time

    tell us your experience • Next time we will learn how to write acceptance tests What next? čtvrtek, 25. července 13
  21. Thank you for your attention! Please fill feedback form on

    http://bit.ly/feedback-code-testing čtvrtek, 25. července 13