zend framework 2 - phpunit

23
August 25, 2014 Tarun Kumar Singhal ZF2 PHPUnit

Upload: tarun-singhal

Post on 28-Nov-2014

367 views

Category:

Technology


1 download

DESCRIPTION

On PHPUnit used in the Zend Framework 2

TRANSCRIPT

Page 1: Zend Framework 2 - PHPUnit

August 25, 2014Tarun Kumar Singhal

ZF2 PHPUnit

Page 2: Zend Framework 2 - PHPUnit

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

Page 3: Zend Framework 2 - 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.

ZF2 PHPUnit

Page 4: Zend Framework 2 - 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.

ZF2 PHPUnit

Page 5: Zend Framework 2 - PHPUnit

Installation

# pear config-set auto_discover 1# pear install pear.phpunit.de/PHPUnit

// for code coverage# sudo pecl install xdebug

ZF2 PHPUnit

Page 6: Zend Framework 2 - PHPUnit

Directory Tree Structure

ZF2 PHPUnit

Page 7: Zend Framework 2 - PHPUnit

ZF2 PHPUnit

<?phpreturn array( 'modules' => array( //modules needed 'User', 'Products' ), 'module_listener_options' => array( 'config_glob_paths' => array( '../../../config/autoload/{,*.}{global,local,message}.php', ), 'module_paths' => array( 'module', 'vendor', ), ),);

TestConfig File

Page 8: Zend Framework 2 - PHPUnit

ZF2 PHPUnit

<?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>

PhpUnit XML File

Page 9: Zend Framework 2 - PHPUnit

ZF2 PHPUnit

<?phpnamespace UserTest; // our namespaceuse Zend\Loader\AutoloaderFactory;use Zend\Mvc\Service\ServiceManagerConfig;use Zend\ServiceManager\ServiceManager;use Zend\Session\Container;use Iridium\Acl;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

Page 10: Zend Framework 2 - PHPUnit

ZF2 PHPUnit

<?php namespace UserTest\Controller; use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; use Zend\Test\PHPUnit\Controller\AbstractControllerTestCase;  class AdminControllerTest extends AbstractControllerTestCase { protected $controller; protected $request; protected $response; protected $routeMatch; protected $event; protected $loginForm; protected $userMockObj; protected $callSummaryForm; protected $serviceManager; }

Page 11: Zend Framework 2 - PHPUnit

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.

Page 12: Zend Framework 2 - PHPUnit

ZF2 PHPUnit

// To test the call-summary action public function testCallSummaryAction() { $this->serviceManager->setAllowOverride(true); $this->serviceManager->setService('User\Model\UserModel', $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()); }

Writing Tests with PHPUnit

Page 13: Zend Framework 2 - PHPUnit

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.

Page 14: Zend Framework 2 - PHPUnit

ZF2 PHPUnit

The PHPUnit command-line test runner can be invoked through the phpunit command. # phpunitPHPUnit 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.

Page 15: Zend Framework 2 - PHPUnit

Incomplete Tests

ZF2 PHPUnit

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.

Page 16: Zend Framework 2 - PHPUnit

Fixtures

ZF2 PHPUnit

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.

Page 17: Zend Framework 2 - PHPUnit

Code-Coverage Analysis

ZF2 PHPUnit

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.

Page 18: Zend Framework 2 - PHPUnit

ZF2 PHPUnit

Classes

Page 19: Zend Framework 2 - PHPUnit

ZF2 PHPUnit

Page 20: Zend Framework 2 - PHPUnit

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.

Page 21: Zend Framework 2 - 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.

ZF2 PHPUnit

Page 22: Zend Framework 2 - PHPUnit

Disadvantages

ZF2 PHPUnit

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.

Page 23: Zend Framework 2 - PHPUnit

Thank You

ZF2 PHPUnit