SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
PHPUnit & You
PHPUnit?

PHPUnit is a test suite framework.

Pretty much the standard in PHP. Used by:
   Zend Framework

   Symfony

   ezComponents and many many more.


Fully featured (more later).
Improvements over
   SimpleTest
Better command line tools.

Better coverage reports.

Better selection of assertions.

Better mock objects.

Better tool chain support.

Pretty much all win.
Assertions
Assertions

Assertions are stricter in PHPUnit.

More assertions to use.

Better reporting on failure

More consistent interface.

Easily extensible.
Stricter

1   <?php
2   $this->assertFalse(array()); // FAIL
3   $this->assertNull(false); // FAIL
4   $this->assertTrue(1); // FAIL
5   $this->assertTrue('oh noes'); // FAIL
Moar
 1   <?php
 2   // General
 3   $this->assertContains('some', 'Awesome');
 4   $this->assertEmpty(array());
 5
 6   // files
 7   $this->assertFileExists('/path/to/file');
 8   $this->assertFileEquals('/path/to/expected', '/path/to/actual');
 9
10   //objects
11   $this->assertInstanceOf('Controller', $thing);
Better failures
 1   1) MyTest::makeSureStuffIsTheSame
 2   Failed asserting that two strings are equal.
 3   --- Expected
 4   +++ Actual
 5   @@ @@
 6    This is some
 7   -text
 8   -on multiple lines
 9   +words
10   +on
11   +a second line
Consistent
1   <?php
2   // Always expected, result
3   $this->assertEquals($expect, $result, $message);
4   $this->assertSame($expect, $result, $message);
5   $this->assertContains($expect, $result, $message);
6   $this->assertInstanceOf($expect, $result, $message);
7   $this->assertRegExp($pattern, $result, $message);
8   $this->assertTrue($result, $message);
9   $this->assertFalse($result, $message);
Extensible
1   <?php
2   // Create new assertions using the built-in ones.
3   function assertJsonEquals($expected, $result, $message = null) {
4       $this->assertEquals(
5           json_encode($expected),
6           json_encode($result),
7           $message
8       );
9   }
Annotations
Annotations

Doc block comment tags that do stuff!

Allows for declarative tests.

Test generators.

Buffering.
Testing exceptions
 1   <?php
 2   /**
 3     * Test to make sure ohNo() explodes
 4     *
 5     * @expectedException RuntimeException
 6     * @expectedExceptionMessage On noes.
 7     * @expectedExceptionCode 25
 8     */
 9   function testSomething() {
10        $this->Thing->ohNo();
11   }
Generators
 1   <?php
 2   public static function dataGenerator() {
 3        return array(
 4            array(1, 1, 2),
 5            array(2, 2, 4)
 6        );
 7   }
 8   /**
 9     * Adding stuff up
10     *
11     * @dataProvider dataGenerator
12     */
13   function testAdding($a, $b, $expected) {
14        $this->assertEquals($expected, $a + $b);
15   }
Output buffering
 1   <?php
 2   /**
 3     * Check saying hello.
 4     *
 5     * @ouputBuffering enabled
 6     */
 7   function testHello() {
 8        $this->Helper->hello('Sam');
 9        $this->assertEquals('Hello Sam', ob_get_contents());
10   }
Fixtures
Fixtures


They are the same!

No really, they are totally the same as before.
Mock Objects
Mock objects

Better built, and a better interface.

More expressive and powerful.

Works with exceptions.

Extensible.
Create a stub
 1 <?php
 2 function testProcess() {
 3     $mock = $this->getMock('AuthorizeNet', 'process');
 4     $mock->expects($this->once())
 5         ->method('process')
 6         ->with($this->Order)
 7         ->will($this->returnValue('Order successful'));
 8
 9     $this->Order->setGateway($mock);
10     $this->Order->process();
11 }


    Stub out expensive resources during testing.
Break stuff
 1 <?php
 2 function testProcessFailure() {
 3     $mock = $this->getMock('AuthorizeNet', array('process'));
 4     $mock->expects($this->at(2))
 5         ->method('process')
 6         ->with($this->Order)
 7         ->will($this->throwException(
 8             new GatewayOrderFailedException()
 9         ));
10
11     $this->Order->setGateway($mock);
12     $this->Order->process();
13 }

         Simulate failures that are hard to test.
Safety check
1   <?php
2   $response = $this->getMock('CakeResponse');
3   $response->expects($this->once())
4       ->method('header')
5       ->with('Location: /posts');
6   $this->Controller->response = $response;
7   $this->Controller->myMethod();



      Check objects are calling others correctly.
Strategies for mocks


Constructor injection.

Setter injection.

You’ll need ways of getting mocks into your code.
Code coverage
Code coverage
How much of your code gets run in your tests.

Requires xDebug.

Comes in several formats:

  Clover format.

  Comprehensive HTML report.

  Single test reports.
Clover reports
Clover reports. XML le that works well with
Jenkins/Hudson.


Console/cake testsuite 
--coverage-clover clover.xml 
app AllTests
Complete HTML
       report
 Full detailed report for your entire project.

 Slower to generate, but more complete.


Console/cake testsuite 
--coverage-html ./webroot/coverage 
app AllTests
¡Sample/Demo!
Individual reports

Single test results, shows coverage for each test
case/group.

Generated at run time.

Faster than comprehensive report.
¡Sample/Demo!
Runners

Command line runner.

Web runner.

Some con gurations have troubles running
sessions in cli. But its a con g option.

Main difference is how you use them.
Demo time!
Continuous
         integration

CakePHP plays nice with Jenkins, and other CI
servers.

Run your tests all the time.

Automate everything.
DEMO!
Questions?

Contenu connexe

Tendances

November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2Kacper Gunia
 
Top 5 Magento Secure Coding Best Practices
Top 5 Magento Secure Coding Best PracticesTop 5 Magento Secure Coding Best Practices
Top 5 Magento Secure Coding Best PracticesOleksandr Zarichnyi
 
PhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examplesPhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examplesMarcello Duarte
 
How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014Guillaume POTIER
 
Zf2 how arrays will save your project
Zf2   how arrays will save your projectZf2   how arrays will save your project
Zf2 how arrays will save your projectMichelangelo van Dam
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using CodeceptionJeroen van Dijk
 
Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Michelangelo van Dam
 
UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013Michelangelo van Dam
 
Dealing With Legacy PHP Applications
Dealing With Legacy PHP ApplicationsDealing With Legacy PHP Applications
Dealing With Legacy PHP ApplicationsViget Labs
 
TDC2016SP - Trilha Developing for Business
TDC2016SP - Trilha Developing for BusinessTDC2016SP - Trilha Developing for Business
TDC2016SP - Trilha Developing for Businesstdc-globalcode
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Colin O'Dell
 
Manipulating Magento - Meet Magento Netherlands 2018
Manipulating Magento - Meet Magento Netherlands 2018Manipulating Magento - Meet Magento Netherlands 2018
Manipulating Magento - Meet Magento Netherlands 2018Joke Puts
 
Introduction to Zend Framework web services
Introduction to Zend Framework web servicesIntroduction to Zend Framework web services
Introduction to Zend Framework web servicesMichelangelo van Dam
 
Proposed PHP function: is_literal()
Proposed PHP function: is_literal()Proposed PHP function: is_literal()
Proposed PHP function: is_literal()Craig Francis
 
Система рендеринга в Magento
Система рендеринга в MagentoСистема рендеринга в Magento
Система рендеринга в MagentoMagecom Ukraine
 
Building a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesBuilding a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesCiaranMcNulty
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Kacper Gunia
 

Tendances (20)

November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
 
Top 5 Magento Secure Coding Best Practices
Top 5 Magento Secure Coding Best PracticesTop 5 Magento Secure Coding Best Practices
Top 5 Magento Secure Coding Best Practices
 
PhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examplesPhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examples
 
How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
Zf2 how arrays will save your project
Zf2   how arrays will save your projectZf2   how arrays will save your project
Zf2 how arrays will save your project
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
 
Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013
 
UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013
 
Dealing With Legacy PHP Applications
Dealing With Legacy PHP ApplicationsDealing With Legacy PHP Applications
Dealing With Legacy PHP Applications
 
CakePHP workshop
CakePHP workshopCakePHP workshop
CakePHP workshop
 
TDC2016SP - Trilha Developing for Business
TDC2016SP - Trilha Developing for BusinessTDC2016SP - Trilha Developing for Business
TDC2016SP - Trilha Developing for Business
 
Developing for Business
Developing for BusinessDeveloping for Business
Developing for Business
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016
 
Manipulating Magento - Meet Magento Netherlands 2018
Manipulating Magento - Meet Magento Netherlands 2018Manipulating Magento - Meet Magento Netherlands 2018
Manipulating Magento - Meet Magento Netherlands 2018
 
Introduction to Zend Framework web services
Introduction to Zend Framework web servicesIntroduction to Zend Framework web services
Introduction to Zend Framework web services
 
Proposed PHP function: is_literal()
Proposed PHP function: is_literal()Proposed PHP function: is_literal()
Proposed PHP function: is_literal()
 
Система рендеринга в Magento
Система рендеринга в MagentoСистема рендеринга в Magento
Система рендеринга в Magento
 
Building a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesBuilding a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing Strategies
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!
 

En vedette

Introduction to osha mac safety
Introduction to osha mac safetyIntroduction to osha mac safety
Introduction to osha mac safetyMAC Safety, Inc
 
Linkedin recruitment | A view tips...
Linkedin recruitment | A view tips...Linkedin recruitment | A view tips...
Linkedin recruitment | A view tips...Sander Bredewout
 
50 years of Diverse Abilities Plus
50 years of Diverse Abilities Plus 50 years of Diverse Abilities Plus
50 years of Diverse Abilities Plus Julie Williams
 
Collapse of the welfare state slides (3)
Collapse of the welfare state slides (3)Collapse of the welfare state slides (3)
Collapse of the welfare state slides (3)E Lyle Gross
 
Intro to continuous integration
Intro to continuous integration Intro to continuous integration
Intro to continuous integration markstory
 
Dunedin cost containment
Dunedin cost containment Dunedin cost containment
Dunedin cost containment E Lyle Gross
 
Introduction To Osha Mac Safety
Introduction To Osha Mac SafetyIntroduction To Osha Mac Safety
Introduction To Osha Mac SafetyMAC Safety, Inc
 

En vedette (9)

Introduction to osha mac safety
Introduction to osha mac safetyIntroduction to osha mac safety
Introduction to osha mac safety
 
Linkedin recruitment | A view tips...
Linkedin recruitment | A view tips...Linkedin recruitment | A view tips...
Linkedin recruitment | A view tips...
 
50 years of Diverse Abilities Plus
50 years of Diverse Abilities Plus 50 years of Diverse Abilities Plus
50 years of Diverse Abilities Plus
 
Collapse of the welfare state slides (3)
Collapse of the welfare state slides (3)Collapse of the welfare state slides (3)
Collapse of the welfare state slides (3)
 
Intro to continuous integration
Intro to continuous integration Intro to continuous integration
Intro to continuous integration
 
Dunedin cost containment
Dunedin cost containment Dunedin cost containment
Dunedin cost containment
 
Introduction To Osha Mac Safety
Introduction To Osha Mac SafetyIntroduction To Osha Mac Safety
Introduction To Osha Mac Safety
 
intro to OSHA
intro to OSHAintro to OSHA
intro to OSHA
 
Fire Egress Training
Fire Egress TrainingFire Egress Training
Fire Egress Training
 

Similaire à PHPunit and you

Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2markstory
 
Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012Michelangelo van Dam
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitsmueller_sandsmedia
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09Michelangelo van Dam
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitmfrost503
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitmfrost503
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnitvaruntaliyan
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentationThanh Robi
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
Testing Code and Assuring Quality
Testing Code and Assuring QualityTesting Code and Assuring Quality
Testing Code and Assuring QualityKent Cowgill
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitMichelangelo van Dam
 
Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Michelangelo van Dam
 
EPHPC Webinar Slides: Unit Testing by Arthur Purnama
EPHPC Webinar Slides: Unit Testing by Arthur PurnamaEPHPC Webinar Slides: Unit Testing by Arthur Purnama
EPHPC Webinar Slides: Unit Testing by Arthur PurnamaEnterprise PHP Center
 
Test in action week 4
Test in action   week 4Test in action   week 4
Test in action week 4Yi-Huan Chan
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormMichelangelo van Dam
 
Test driven node.js
Test driven node.jsTest driven node.js
Test driven node.jsJay Harris
 

Similaire à PHPunit and you (20)

Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2
 
Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
Php tests tips
Php tests tipsPhp tests tips
Php tests tips
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentation
 
PHPUnit testing to Zend_Test
PHPUnit testing to Zend_TestPHPUnit testing to Zend_Test
PHPUnit testing to Zend_Test
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Testing Code and Assuring Quality
Testing Code and Assuring QualityTesting Code and Assuring Quality
Testing Code and Assuring Quality
 
Test driven development_for_php
Test driven development_for_phpTest driven development_for_php
Test driven development_for_php
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8
 
EPHPC Webinar Slides: Unit Testing by Arthur Purnama
EPHPC Webinar Slides: Unit Testing by Arthur PurnamaEPHPC Webinar Slides: Unit Testing by Arthur Purnama
EPHPC Webinar Slides: Unit Testing by Arthur Purnama
 
Test in action week 4
Test in action   week 4Test in action   week 4
Test in action week 4
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStorm
 
Test driven node.js
Test driven node.jsTest driven node.js
Test driven node.js
 

Plus de markstory

Dependency injection in CakePHP
Dependency injection in CakePHPDependency injection in CakePHP
Dependency injection in CakePHPmarkstory
 
Safer, More Helpful CakePHP
Safer, More Helpful CakePHPSafer, More Helpful CakePHP
Safer, More Helpful CakePHPmarkstory
 
CakePHP - The Road Ahead
CakePHP - The Road AheadCakePHP - The Road Ahead
CakePHP - The Road Aheadmarkstory
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHPmarkstory
 
CakePHP mistakes made 2015
CakePHP mistakes made 2015CakePHP mistakes made 2015
CakePHP mistakes made 2015markstory
 
New in cakephp3
New in cakephp3New in cakephp3
New in cakephp3markstory
 
CakePHP 3.0 and beyond
CakePHP 3.0 and beyondCakePHP 3.0 and beyond
CakePHP 3.0 and beyondmarkstory
 
CakePHP mistakes made confoo 2015
CakePHP mistakes made confoo 2015CakePHP mistakes made confoo 2015
CakePHP mistakes made confoo 2015markstory
 
CakePHP mistakes made
CakePHP mistakes madeCakePHP mistakes made
CakePHP mistakes mademarkstory
 
Performance and optimization CakeFest 2014
Performance and optimization CakeFest 2014Performance and optimization CakeFest 2014
Performance and optimization CakeFest 2014markstory
 
Road to CakePHP 3.0
Road to CakePHP 3.0Road to CakePHP 3.0
Road to CakePHP 3.0markstory
 
Performance and optimization
Performance and optimizationPerformance and optimization
Performance and optimizationmarkstory
 
OWASP Top 10 2013
OWASP Top 10 2013OWASP Top 10 2013
OWASP Top 10 2013markstory
 
CakePHP the yum & yuck
CakePHP the yum & yuckCakePHP the yum & yuck
CakePHP the yum & yuckmarkstory
 
Introduction to Twig
Introduction to TwigIntroduction to Twig
Introduction to Twigmarkstory
 
Owasp top 10
Owasp top 10Owasp top 10
Owasp top 10markstory
 
Simple search with elastic search
Simple search with elastic searchSimple search with elastic search
Simple search with elastic searchmarkstory
 
Making the most of 2.2
Making the most of 2.2Making the most of 2.2
Making the most of 2.2markstory
 
Evented applications with RabbitMQ and CakePHP
Evented applications with RabbitMQ and CakePHPEvented applications with RabbitMQ and CakePHP
Evented applications with RabbitMQ and CakePHPmarkstory
 

Plus de markstory (20)

Dependency injection in CakePHP
Dependency injection in CakePHPDependency injection in CakePHP
Dependency injection in CakePHP
 
Safer, More Helpful CakePHP
Safer, More Helpful CakePHPSafer, More Helpful CakePHP
Safer, More Helpful CakePHP
 
CakePHP - The Road Ahead
CakePHP - The Road AheadCakePHP - The Road Ahead
CakePHP - The Road Ahead
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHP
 
CakePHP mistakes made 2015
CakePHP mistakes made 2015CakePHP mistakes made 2015
CakePHP mistakes made 2015
 
New in cakephp3
New in cakephp3New in cakephp3
New in cakephp3
 
PHP WTF
PHP WTFPHP WTF
PHP WTF
 
CakePHP 3.0 and beyond
CakePHP 3.0 and beyondCakePHP 3.0 and beyond
CakePHP 3.0 and beyond
 
CakePHP mistakes made confoo 2015
CakePHP mistakes made confoo 2015CakePHP mistakes made confoo 2015
CakePHP mistakes made confoo 2015
 
CakePHP mistakes made
CakePHP mistakes madeCakePHP mistakes made
CakePHP mistakes made
 
Performance and optimization CakeFest 2014
Performance and optimization CakeFest 2014Performance and optimization CakeFest 2014
Performance and optimization CakeFest 2014
 
Road to CakePHP 3.0
Road to CakePHP 3.0Road to CakePHP 3.0
Road to CakePHP 3.0
 
Performance and optimization
Performance and optimizationPerformance and optimization
Performance and optimization
 
OWASP Top 10 2013
OWASP Top 10 2013OWASP Top 10 2013
OWASP Top 10 2013
 
CakePHP the yum & yuck
CakePHP the yum & yuckCakePHP the yum & yuck
CakePHP the yum & yuck
 
Introduction to Twig
Introduction to TwigIntroduction to Twig
Introduction to Twig
 
Owasp top 10
Owasp top 10Owasp top 10
Owasp top 10
 
Simple search with elastic search
Simple search with elastic searchSimple search with elastic search
Simple search with elastic search
 
Making the most of 2.2
Making the most of 2.2Making the most of 2.2
Making the most of 2.2
 
Evented applications with RabbitMQ and CakePHP
Evented applications with RabbitMQ and CakePHPEvented applications with RabbitMQ and CakePHP
Evented applications with RabbitMQ and CakePHP
 

Dernier

Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Dernier (20)

Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

PHPunit and you

  • 2. PHPUnit? PHPUnit is a test suite framework. Pretty much the standard in PHP. Used by: Zend Framework Symfony ezComponents and many many more. Fully featured (more later).
  • 3. Improvements over SimpleTest Better command line tools. Better coverage reports. Better selection of assertions. Better mock objects. Better tool chain support. Pretty much all win.
  • 5. Assertions Assertions are stricter in PHPUnit. More assertions to use. Better reporting on failure More consistent interface. Easily extensible.
  • 6. Stricter 1 <?php 2 $this->assertFalse(array()); // FAIL 3 $this->assertNull(false); // FAIL 4 $this->assertTrue(1); // FAIL 5 $this->assertTrue('oh noes'); // FAIL
  • 7. Moar 1 <?php 2 // General 3 $this->assertContains('some', 'Awesome'); 4 $this->assertEmpty(array()); 5 6 // files 7 $this->assertFileExists('/path/to/file'); 8 $this->assertFileEquals('/path/to/expected', '/path/to/actual'); 9 10 //objects 11 $this->assertInstanceOf('Controller', $thing);
  • 8. Better failures 1 1) MyTest::makeSureStuffIsTheSame 2 Failed asserting that two strings are equal. 3 --- Expected 4 +++ Actual 5 @@ @@ 6 This is some 7 -text 8 -on multiple lines 9 +words 10 +on 11 +a second line
  • 9. Consistent 1 <?php 2 // Always expected, result 3 $this->assertEquals($expect, $result, $message); 4 $this->assertSame($expect, $result, $message); 5 $this->assertContains($expect, $result, $message); 6 $this->assertInstanceOf($expect, $result, $message); 7 $this->assertRegExp($pattern, $result, $message); 8 $this->assertTrue($result, $message); 9 $this->assertFalse($result, $message);
  • 10. Extensible 1 <?php 2 // Create new assertions using the built-in ones. 3 function assertJsonEquals($expected, $result, $message = null) { 4 $this->assertEquals( 5 json_encode($expected), 6 json_encode($result), 7 $message 8 ); 9 }
  • 12. Annotations Doc block comment tags that do stuff! Allows for declarative tests. Test generators. Buffering.
  • 13. Testing exceptions 1 <?php 2 /** 3 * Test to make sure ohNo() explodes 4 * 5 * @expectedException RuntimeException 6 * @expectedExceptionMessage On noes. 7 * @expectedExceptionCode 25 8 */ 9 function testSomething() { 10 $this->Thing->ohNo(); 11 }
  • 14. Generators 1 <?php 2 public static function dataGenerator() { 3 return array( 4 array(1, 1, 2), 5 array(2, 2, 4) 6 ); 7 } 8 /** 9 * Adding stuff up 10 * 11 * @dataProvider dataGenerator 12 */ 13 function testAdding($a, $b, $expected) { 14 $this->assertEquals($expected, $a + $b); 15 }
  • 15. Output buffering 1 <?php 2 /** 3 * Check saying hello. 4 * 5 * @ouputBuffering enabled 6 */ 7 function testHello() { 8 $this->Helper->hello('Sam'); 9 $this->assertEquals('Hello Sam', ob_get_contents()); 10 }
  • 17. Fixtures They are the same! No really, they are totally the same as before.
  • 19. Mock objects Better built, and a better interface. More expressive and powerful. Works with exceptions. Extensible.
  • 20. Create a stub 1 <?php 2 function testProcess() { 3 $mock = $this->getMock('AuthorizeNet', 'process'); 4 $mock->expects($this->once()) 5 ->method('process') 6 ->with($this->Order) 7 ->will($this->returnValue('Order successful')); 8 9 $this->Order->setGateway($mock); 10 $this->Order->process(); 11 } Stub out expensive resources during testing.
  • 21. Break stuff 1 <?php 2 function testProcessFailure() { 3 $mock = $this->getMock('AuthorizeNet', array('process')); 4 $mock->expects($this->at(2)) 5 ->method('process') 6 ->with($this->Order) 7 ->will($this->throwException( 8 new GatewayOrderFailedException() 9 )); 10 11 $this->Order->setGateway($mock); 12 $this->Order->process(); 13 } Simulate failures that are hard to test.
  • 22. Safety check 1 <?php 2 $response = $this->getMock('CakeResponse'); 3 $response->expects($this->once()) 4 ->method('header') 5 ->with('Location: /posts'); 6 $this->Controller->response = $response; 7 $this->Controller->myMethod(); Check objects are calling others correctly.
  • 23. Strategies for mocks Constructor injection. Setter injection. You’ll need ways of getting mocks into your code.
  • 25. Code coverage How much of your code gets run in your tests. Requires xDebug. Comes in several formats: Clover format. Comprehensive HTML report. Single test reports.
  • 26. Clover reports Clover reports. XML le that works well with Jenkins/Hudson. Console/cake testsuite --coverage-clover clover.xml app AllTests
  • 27. Complete HTML report Full detailed report for your entire project. Slower to generate, but more complete. Console/cake testsuite --coverage-html ./webroot/coverage app AllTests
  • 29. Individual reports Single test results, shows coverage for each test case/group. Generated at run time. Faster than comprehensive report.
  • 31. Runners Command line runner. Web runner. Some con gurations have troubles running sessions in cli. But its a con g option. Main difference is how you use them.
  • 33. Continuous integration CakePHP plays nice with Jenkins, and other CI servers. Run your tests all the time. Automate everything.
  • 34. DEMO!