SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Downloaden Sie, um offline zu lesen
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 stuïŹ€!

Allows for declarative tests.

Test generators.

BuïŹ€ering.
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 diïŹ€erence 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?

Weitere Àhnliche Inhalte

Was ist angesagt?

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
Oleksandr Zarichnyi
 
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
Michelangelo van Dam
 
ĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ Magento
ĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ MagentoĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ Magento
ĐĄĐžŃŃ‚Đ”ĐŒĐ° Ń€Đ”ĐœĐŽĐ”Ń€ĐžĐœĐłĐ° ĐČ Magento
Magecom Ukraine
 

Was ist angesagt? (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!
 

Andere mochten auch

Linkedin recruitment | A view tips...
Linkedin recruitment | A view tips...Linkedin recruitment | A view tips...
Linkedin recruitment | A view tips...
Sander Bredewout
 
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
 

Andere mochten auch (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
 

Ähnlich wie PHPunit and you

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
Michelangelo van Dam
 
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
smueller_sandsmedia
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
mfrost503
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
varuntaliyan
 
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
Enterprise PHP Center
 
Test in action week 4
Test in action   week 4Test in action   week 4
Test in action week 4
Yi-Huan Chan
 

Ähnlich wie 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
 

Mehr von markstory

Introduction to Twig
Introduction to TwigIntroduction to Twig
Introduction to Twig
markstory
 
Owasp top 10
Owasp top 10Owasp top 10
Owasp top 10
markstory
 

Mehr von 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
 

KĂŒrzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

KĂŒrzlich hochgeladen (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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...
 
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...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

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 stuïŹ€! Allows for declarative tests. Test generators. BuïŹ€ering.
  • 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 diïŹ€erence 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!