Share
Embed
Transcript
25 slides
-
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 -
Types of tests • Unit • Integration • System •
Acceptance • ... and more Unit tests Integration tests Acceptance System tests čtvrtek, 25. července 13 -
Requirements • Fast • Timely • Independent • Repeatable •
Self-validating • Short • Simple čtvrtek, 25. července 13 -
If you using Dependecy Injection DI is not about container!
čtvrtek, 25. července 13 -
If you using Dependecy Injection Controller DI is not about
container! čtvrtek, 25. července 13 -
If you using Dependecy Injection Controller DI is not about
container! čtvrtek, 25. července 13 -
If you using Dependecy Injection Data Repository Controller DI is
not about container! čtvrtek, 25. července 13 -
If you using Dependecy Injection Database Data Repository Controller DI
is not about container! čtvrtek, 25. července 13 -
If you using Dependecy Injection Database Mamcache Data Repository Controller
DI is not about container! čtvrtek, 25. července 13 -
If you using Dependecy Injection Database Mamcache Data Repository Controller
DI is not about container! čtvrtek, 25. července 13 -
If you using Dependecy Injection Database Mamcache Data Repository Controller
DI is not about container! čtvrtek, 25. července 13 -
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 -
Unit testing • Test single class • Test all methods
• Needs some dependencies? Mock it! čtvrtek, 25. července 13 -
Unit testing • Test single class • Test all methods
• Needs some dependencies? Mock it! • PHPUnit • Nette Tester • SimpleTest čtvrtek, 25. července 13 -
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 -
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 -
Mocking frameworks • PHPUnit • Phake • Mockery • Mockista
(by Jan Marek) čtvrtek, 25. července 13 -
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 -
PHPUnit in PHPStorm • You can integrate your testing routine
into PHPStorm • PHPStorm support testing with code coverage option čtvrtek, 25. července 13 -
• Write testable code • Write tests • Deliver code
with better quality čtvrtek, 25. července 13 -
• 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 -
Thank you for your attention! Please fill feedback form on
http://bit.ly/feedback-code-testing čtvrtek, 25. července 13