SlideShare a Scribd company logo
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 
Code-Coverage Analysis 
Advantages and disadvantages of PhpUnit
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.
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.
ZF2 PHPUnit 
Installation 
# pear config-set auto_discover 1 
# pear install pear.phpunit.de/PHPUnit 
// for code coverage 
# sudo pecl install xdebug
ZF2 PHPUnit 
Directory Tree 
Structure
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', 
), 
), 
);
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>
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
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; 
}
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.
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()); 
}
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.
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.
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.
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.
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.
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 executable but were not 
executed are highlighted red, and "dead code" is highlighted gray.
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.
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.
ZF2 PHPUnit 
Thank You
Ad

More Related Content

What's hot (20)

Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
Peter Arato
 
Automated testing in Drupal
Automated testing in DrupalAutomated testing in Drupal
Automated testing in Drupal
Artem Berdishev
 
How to test models using php unit testing framework?
How to test models using php unit testing framework?How to test models using php unit testing framework?
How to test models using php unit testing framework?
satejsahu
 
PHP Unit Testing in Yii
PHP Unit Testing in YiiPHP Unit Testing in Yii
PHP Unit Testing in Yii
IlPeach
 
Cursus phpunit
Cursus phpunitCursus phpunit
Cursus phpunit
Nick Belhomme
 
Codeception
CodeceptionCodeception
Codeception
少東 張
 
Testing PHP with Codeception
Testing PHP with CodeceptionTesting PHP with Codeception
Testing PHP with Codeception
John Paul Ada
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yii
madhavi Ghadge
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
Eddy Reyes
 
Acceptance testing in php with Codeception - Techmeetup Edinburgh
Acceptance testing in php with Codeception - Techmeetup EdinburghAcceptance testing in php with Codeception - Techmeetup Edinburgh
Acceptance testing in php with Codeception - Techmeetup Edinburgh
Engineor
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
Christian Johansen
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
Christian Johansen
 
Unit testing
Unit testingUnit testing
Unit testing
Arthur Purnama
 
Testing with Codeception (Webelement #30)
Testing with Codeception (Webelement #30)Testing with Codeception (Webelement #30)
Testing with Codeception (Webelement #30)
Adam Štipák
 
prohuddle-utPLSQL v3 - Ultimate unit testing framework for Oracle
prohuddle-utPLSQL v3 - Ultimate unit testing framework for Oracleprohuddle-utPLSQL v3 - Ultimate unit testing framework for Oracle
prohuddle-utPLSQL v3 - Ultimate unit testing framework for Oracle
Jacek Gebal
 
Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyond
dn
 
Selenium interview questions and answers
Selenium interview questions and answersSelenium interview questions and answers
Selenium interview questions and answers
kavinilavuG
 
Presentation_C++UnitTest
Presentation_C++UnitTestPresentation_C++UnitTest
Presentation_C++UnitTest
Raihan Masud
 
Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++
Lars Thorup
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
Peter Arato
 
Automated testing in Drupal
Automated testing in DrupalAutomated testing in Drupal
Automated testing in Drupal
Artem Berdishev
 
How to test models using php unit testing framework?
How to test models using php unit testing framework?How to test models using php unit testing framework?
How to test models using php unit testing framework?
satejsahu
 
PHP Unit Testing in Yii
PHP Unit Testing in YiiPHP Unit Testing in Yii
PHP Unit Testing in Yii
IlPeach
 
Testing PHP with Codeception
Testing PHP with CodeceptionTesting PHP with Codeception
Testing PHP with Codeception
John Paul Ada
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yii
madhavi Ghadge
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
Eddy Reyes
 
Acceptance testing in php with Codeception - Techmeetup Edinburgh
Acceptance testing in php with Codeception - Techmeetup EdinburghAcceptance testing in php with Codeception - Techmeetup Edinburgh
Acceptance testing in php with Codeception - Techmeetup Edinburgh
Engineor
 
Testing with Codeception (Webelement #30)
Testing with Codeception (Webelement #30)Testing with Codeception (Webelement #30)
Testing with Codeception (Webelement #30)
Adam Štipák
 
prohuddle-utPLSQL v3 - Ultimate unit testing framework for Oracle
prohuddle-utPLSQL v3 - Ultimate unit testing framework for Oracleprohuddle-utPLSQL v3 - Ultimate unit testing framework for Oracle
prohuddle-utPLSQL v3 - Ultimate unit testing framework for Oracle
Jacek Gebal
 
Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyond
dn
 
Selenium interview questions and answers
Selenium interview questions and answersSelenium interview questions and answers
Selenium interview questions and answers
kavinilavuG
 
Presentation_C++UnitTest
Presentation_C++UnitTestPresentation_C++UnitTest
Presentation_C++UnitTest
Raihan Masud
 
Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++
Lars Thorup
 

Viewers also liked (18)

Zend Framework Estrutura e TDD
Zend Framework Estrutura e TDDZend Framework Estrutura e TDD
Zend Framework Estrutura e TDD
PHP Day Curitiba
 
Error Reporting in ZF2: form messages, custom error pages, logging
Error Reporting in ZF2: form messages, custom error pages, loggingError Reporting in ZF2: form messages, custom error pages, logging
Error Reporting in ZF2: form messages, custom error pages, logging
Steve Maraspin
 
Moduli su Zend Framework 2: come sfruttarli
Moduli su Zend Framework 2: come sfruttarliModuli su Zend Framework 2: come sfruttarli
Moduli su Zend Framework 2: come sfruttarli
Stefano Valle
 
Asset management with Zend Framework 2
Asset management with Zend Framework 2Asset management with Zend Framework 2
Asset management with Zend Framework 2
Stefano Valle
 
Implementing access control with zend framework
Implementing access control with zend frameworkImplementing access control with zend framework
Implementing access control with zend framework
George Mihailov
 
Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2
Stefano Valle
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnit
Michelangelo van Dam
 
Into the ZF2 Service Manager
Into the ZF2 Service ManagerInto the ZF2 Service Manager
Into the ZF2 Service Manager
Chris Tankersley
 
Zend Framework 2 : Dependency Injection
Zend Framework 2 : Dependency InjectionZend Framework 2 : Dependency Injection
Zend Framework 2 : Dependency Injection
Abdul Malik Ikhsan
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentation
Thanh Robi
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
Frank Appel
 
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Wim Godden
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
ikhwanhayat
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
Michelangelo van Dam
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
Derek Smith
 
Zend Framework Workshop Parte2
Zend Framework Workshop Parte2Zend Framework Workshop Parte2
Zend Framework Workshop Parte2
massimiliano.wosz
 
GAME ON! Integrating Games and Simulations in the Classroom
GAME ON! Integrating Games and Simulations in the Classroom GAME ON! Integrating Games and Simulations in the Classroom
GAME ON! Integrating Games and Simulations in the Classroom
Brian Housand
 
Zend Framework Estrutura e TDD
Zend Framework Estrutura e TDDZend Framework Estrutura e TDD
Zend Framework Estrutura e TDD
PHP Day Curitiba
 
Error Reporting in ZF2: form messages, custom error pages, logging
Error Reporting in ZF2: form messages, custom error pages, loggingError Reporting in ZF2: form messages, custom error pages, logging
Error Reporting in ZF2: form messages, custom error pages, logging
Steve Maraspin
 
Moduli su Zend Framework 2: come sfruttarli
Moduli su Zend Framework 2: come sfruttarliModuli su Zend Framework 2: come sfruttarli
Moduli su Zend Framework 2: come sfruttarli
Stefano Valle
 
Asset management with Zend Framework 2
Asset management with Zend Framework 2Asset management with Zend Framework 2
Asset management with Zend Framework 2
Stefano Valle
 
Implementing access control with zend framework
Implementing access control with zend frameworkImplementing access control with zend framework
Implementing access control with zend framework
George Mihailov
 
Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2
Stefano Valle
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnit
Michelangelo van Dam
 
Into the ZF2 Service Manager
Into the ZF2 Service ManagerInto the ZF2 Service Manager
Into the ZF2 Service Manager
Chris Tankersley
 
Zend Framework 2 : Dependency Injection
Zend Framework 2 : Dependency InjectionZend Framework 2 : Dependency Injection
Zend Framework 2 : Dependency Injection
Abdul Malik Ikhsan
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentation
Thanh Robi
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
Frank Appel
 
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Wim Godden
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
ikhwanhayat
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
Michelangelo van Dam
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
Derek Smith
 
Zend Framework Workshop Parte2
Zend Framework Workshop Parte2Zend Framework Workshop Parte2
Zend Framework Workshop Parte2
massimiliano.wosz
 
GAME ON! Integrating Games and Simulations in the Classroom
GAME ON! Integrating Games and Simulations in the Classroom GAME ON! Integrating Games and Simulations in the Classroom
GAME ON! Integrating Games and Simulations in the Classroom
Brian Housand
 
Ad

Similar to Zend Framework 2 - PHPUnit (20)

Phpunit testing
Phpunit testingPhpunit testing
Phpunit testing
Nikunj Bhatnagar
 
Phpunit
PhpunitPhpunit
Phpunit
japan_works
 
PHPUnit
PHPUnitPHPUnit
PHPUnit
Hampton Roads PHP User Grop
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHP
Radu Murzea
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
Jeremy Cook
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
Mike Lively
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
James Fuller
 
Unit testing
Unit testingUnit testing
Unit testing
davidahaskins
 
Getting started with PHPUnit
Getting started with PHPUnitGetting started with PHPUnit
Getting started with PHPUnit
Khyati Gala
 
Php unit (eng)
Php unit (eng)Php unit (eng)
Php unit (eng)
Anatoliy Okhotnikov
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
Mindfire Solutions
 
N Unit Presentation
N Unit PresentationN Unit Presentation
N Unit Presentation
priya_trivedi
 
Php unit
Php unitPhp unit
Php unit
Simona-Elena Stanescu
 
Code igniter unittest-part1
Code igniter unittest-part1Code igniter unittest-part1
Code igniter unittest-part1
Albert Rosa
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
varuntaliyan
 
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
Bruno Tanoue
 
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
kalichargn70th171
 
2010 07-28-testing-zf-apps
2010 07-28-testing-zf-apps2010 07-28-testing-zf-apps
2010 07-28-testing-zf-apps
Venkata Ramana
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPress
Harshad Mane
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHP
Radu Murzea
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
Jeremy Cook
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
Mike Lively
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
James Fuller
 
Getting started with PHPUnit
Getting started with PHPUnitGetting started with PHPUnit
Getting started with PHPUnit
Khyati Gala
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
Mindfire Solutions
 
Code igniter unittest-part1
Code igniter unittest-part1Code igniter unittest-part1
Code igniter unittest-part1
Albert Rosa
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
varuntaliyan
 
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
Bruno Tanoue
 
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
A Comprehensive Guide to Essential Workflows for Improving Flutter Unit Testi...
kalichargn70th171
 
2010 07-28-testing-zf-apps
2010 07-28-testing-zf-apps2010 07-28-testing-zf-apps
2010 07-28-testing-zf-apps
Venkata Ramana
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPress
Harshad Mane
 
Ad

Recently uploaded (20)

Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 

Zend Framework 2 - PHPUnit

  • 1. August 25, 2014 Tarun Kumar Singhal ZF2 PHPUnit
  • 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. 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. 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. ZF2 PHPUnit Installation # pear config-set auto_discover 1 # pear install pear.phpunit.de/PHPUnit // for code coverage # sudo pecl install xdebug
  • 6. ZF2 PHPUnit Directory Tree Structure
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.
  • 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. 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. 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.