Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
August 25, 2014 
Tarun Kumar Singhal 
ZF2 PHPUnit
ZF2 PHPUnit 
Agenda 
What is PhpUnit 
Installation 
How to write an automated test 
Writing and running tests with PHPUnit...
ZF2 PHPUnit 
Ballon Effect 
Testing your application what you build/changed. 
Retesting everything all the time is very im...
ZF2 PHPUnit 
PhpUnit 
Testing with PHPUnit means checking that your 
program behaves as expected, and performing a 
batter...
ZF2 PHPUnit 
Installation 
# pear config-set auto_discover 1 
# pear install pear.phpunit.de/PHPUnit 
// for code coverage...
ZF2 PHPUnit 
Directory Tree 
Structure
ZF2 PHPUnit 
<?php 
return array( 
'modules' => array( 
TestConfig File 
//modules needed 
'User', 
'Products' 
), 
'modul...
ZF2 PHPUnit 
PhpUnit XML File 
<?xml version="1.0" encoding="UTF-8"?> 
<phpunit bootstrap="Bootstrap.php"> 
<php> 
<server...
ZF2 PHPUnit 
<?php 
namespace UserTest; // our namespace 
use ZendLoaderAutoloaderFactory; 
use ZendMvcServiceServiceManag...
ZF2 PHPUnit 
<?php 
namespace UserTestController; 
use ZendTestPHPUnitControllerAbstractHttpControllerTestCase; 
use ZendT...
ZF2 PHPUnit 
public function setUp() 
{ 
$this->serviceManager = Bootstrap::getServiceManager(); 
$this->controller = new ...
ZF2 PHPUnit 
Writing Tests with PHPUnit 
// To test the call-summary action 
public function testCallSummaryAction() 
{ 
$...
ZF2 PHPUnit 
Test class should extend the class 
AbstractControllerTestCase 
The tests are public methods that expect no p...
ZF2 PHPUnit 
The PHPUnit command-line test runner can be invoked through 
the phpunit command. 
# phpunit 
PHPUnit 4.1.3 b...
ZF2 PHPUnit 
Incomplete Tests 
public function testSomething() 
{ 
// Stop here and mark this test as incomplete. 
$this->...
ZF2 PHPUnit 
Fixtures 
setUp() method – is called before a test method run 
tearDown() method – is called after a test met...
ZF2 PHPUnit 
Code-Coverage Analysis 
How do you find code that is not yet tested? 
How do you measure testing completeness...
ZF2 PHPUnit 
Classes
ZF2 PHPUnit
ZF2 PHPUnit 
Lines of code that were executed while running the tests are 
highlighted green, lines of code that are execu...
ZF2 PHPUnit 
Advantages 
• Testing gives code authors and reviewers confidence 
that patches produce the correct results. ...
ZF2 PHPUnit 
Disadvantages 
Some people have trouble with getting started: 
where to put the files, how big the scope of o...
ZF2 PHPUnit 
Thank You
Prochain SlideShare
Chargement dans…5
×

Zend Framework 2 - PHPUnit

On PHPUnit used in the Zend Framework 2

  • Soyez le premier à commenter

Zend Framework 2 - PHPUnit

  1. 1. August 25, 2014 Tarun Kumar Singhal ZF2 PHPUnit
  2. 2. ZF2 PHPUnit Agenda What is PhpUnit Installation How to write an automated test Writing and running tests with PHPUnit Code-Coverage Analysis Advantages and disadvantages of PhpUnit
  3. 3. ZF2 PHPUnit Ballon Effect Testing your application what you build/changed. Retesting everything all the time is very important. That's take a lot of time.
  4. 4. ZF2 PHPUnit PhpUnit Testing with PHPUnit means checking that your program behaves as expected, and performing a battery of tests. These runnable code-fragments are called unit tests.
  5. 5. ZF2 PHPUnit Installation # pear config-set auto_discover 1 # pear install pear.phpunit.de/PHPUnit // for code coverage # sudo pecl install xdebug
  6. 6. ZF2 PHPUnit Directory Tree Structure
  7. 7. ZF2 PHPUnit <?php return array( 'modules' => array( TestConfig File //modules needed 'User', 'Products' ), 'module_listener_options' => array( 'config_glob_paths' => array( '../../../config/autoload/{,*.}{global,local,message}.php', ), 'module_paths' => array( 'module', 'vendor', ), ), );
  8. 8. ZF2 PHPUnit PhpUnit XML File <?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="Bootstrap.php"> <php> <server name="HTTP_HOST" value="http://irizf2.local.com" /> <server name="SERVER_PORT" value="80"/> <server name="REMOTE_ADDR" value=""/> <server name="PDO::MYSQL_ATTR_INIT_COMMAND" value="" /> </php> <testsuites> <testsuite name="Example Controller Tests"> <directory>./UserTest</directory> </testsuite> </testsuites> </phpunit>
  9. 9. ZF2 PHPUnit <?php namespace UserTest; // our namespace use ZendLoaderAutoloaderFactory; use ZendMvcServiceServiceManagerConfig; use ZendServiceManagerServiceManager; use ZendSessionContainer; use IridiumAcl; class Bootstrap { public static function init() { // Load the user-defined test configuration file, if it exists; otherwise, load if (is_readable(__DIR__ . '/TestConfig.php')) { $testConfig = include __DIR__ . '/TestConfig.php'; } else { $testConfig = include __DIR__ . '/TestConfig.php.dist'; } …........................ ….......... Bootstrap File
  10. 10. ZF2 PHPUnit <?php namespace UserTestController; use ZendTestPHPUnitControllerAbstractHttpControllerTestCase; use ZendTestPHPUnitControllerAbstractControllerTestCase; class AdminControllerTest extends AbstractControllerTestCase { protected $controller; protected $request; protected $response; protected $routeMatch; protected $event; protected $loginForm; protected $userMockObj; protected $callSummaryForm; protected $serviceManager; }
  11. 11. ZF2 PHPUnit public function setUp() { $this->serviceManager = Bootstrap::getServiceManager(); $this->controller = new AdminController(); $this->request = new Request(); $tis->routeMatch = new RouteMatch(array('controller' => 'admin')); $this->event = new MvcEvent(); $config = $this->serviceManager->get('Config'); $routerConfig = isset($config['router']) ? $config['router'] : array(); $router = HttpRouter::factory($routerConfig); …............. …........ } PHPUnit supports sharing the setup code. Before a test method is run, a template method called setUp() is invoked. setUp() is where you create the objects against which you will test.
  12. 12. ZF2 PHPUnit Writing Tests with PHPUnit // To test the call-summary action public function testCallSummaryAction() { $this->serviceManager->setAllowOverride(true); $this->serviceManager->setService('UserModelUserModel', $this->userMockObj); $this->routeMatch->setParam('action', 'call-summary'); $result = $this->controller->dispatch($this->request); $response = $this->controller->getResponse(); $this->assertEquals(200, $response->getStatusCode()); } //To test the delete-user action public function testDeleteUserAction() { $this->routeMatch->setParam('action', 'delete-user'); $result = $this->controller->dispatch($this->request); $response = $this->controller->getResponse(); $this->assertEquals(302, $response->getStatusCode()); }
  13. 13. ZF2 PHPUnit Test class should extend the class AbstractControllerTestCase The tests are public methods that expect no parameters and are named test* Inside the test methods, assertion methods such as assertEquals() are used to assert that an actual value matches an expected value.
  14. 14. ZF2 PHPUnit The PHPUnit command-line test runner can be invoked through the phpunit command. # phpunit PHPUnit 4.1.3 by Sebastian Bergmann. .. Time: 00:00 OK (2 tests) . Printed when the test succeeds. F Printed when an assertion fails while running the test method. E Printed when an error occurs while running the test method. S Printed when the test has been skipped. I Printed when the test is marked as being incomplete or not yet implemented.
  15. 15. ZF2 PHPUnit Incomplete Tests public function testSomething() { // Stop here and mark this test as incomplete. $this->markTestIncomplete( 'This test has not been implemented yet.‘); } A test as being marked as incomplete or not yet implemented.
  16. 16. ZF2 PHPUnit Fixtures setUp() method – is called before a test method run tearDown() method – is called after a test method run setUp() and tearDown() will be called once for each test method run.
  17. 17. ZF2 PHPUnit Code-Coverage Analysis How do you find code that is not yet tested? How do you measure testing completeness? #phpunit --coverage-html dir-name PHPUnit 4.1.3 by Sebastian Bergmann. .... Time: 00:00 OK (4 tests) Generating report, this may take a moment.
  18. 18. ZF2 PHPUnit Classes
  19. 19. ZF2 PHPUnit
  20. 20. ZF2 PHPUnit Lines of code that were executed while running the tests are highlighted green, lines of code that are executable but were not executed are highlighted red, and "dead code" is highlighted gray.
  21. 21. ZF2 PHPUnit Advantages • Testing gives code authors and reviewers confidence that patches produce the correct results. • Detect errors just after code is written • The tests are run at the touch of a button and present their results in a clear format. • Tests run fast • The tests do not affect each other. If some changes are made in one test, the results of others tests do not change.
  22. 22. ZF2 PHPUnit Disadvantages Some people have trouble with getting started: where to put the files, how big the scope of one unit test is and when to write a separate testing suite and so on. It would be difficult to write a test for people who are not programmers or familiar with PHP.
  23. 23. ZF2 PHPUnit Thank You

×