SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Downloaden Sie, um offline zu lesen
Test your code like a pro
              PHPUnit in practice




Sebastian Marek, Software Architect
Me
a Pole living in Sheffield

over 12 years in
development

big fan of process
automation

TDD and CI

occasionally contributes
to open source software
You?

Who are you?

What’s you typical environment you work in?

What’s your experience in unit testing?

What are your expectations?
Agenda
Preparations / Setup   Asserting exceptions
Simple test case       Asserting PHP
Fixing first failure    errors
PHPUnit CLI options    Asserting output
Assertions             Using data providers
phpunit.xml file        Generating code
                       coverage report
Resources

WIFI: phpnw12-phpunit

hosts file: 192.168.254.55 phpnw12-tutorial

http://phpnw12-tutorial
Rescue plan
$ git clone http://phpnw12-tutorial/phpnw12-tutorial.git

$ cd phpnw12-tutorial

$ git reset --hard

$ git pull origin exercise-<number>
Exercise 1
    Simple test case
repository structure

  src & tests

naming conventions

  Tutorial.php and TutorialTest.php

extends PHPUnit_Framework_TestCase
Exercise 1 -Simple test case

               Test first!
  namespace PhpNw12TestsWorkshop

  class TutorialTest

  method testGreetingsReturnWelcomeMessage()

  $ phpunit tests/Workshop/TutorialTest.php
Exercise 1 -Simple test case

              Code later!
  namespace PhpNw12Workshop

  class Tutorial

  method greetings() - empty for now

  $ phpunit tests/Workshop/TutorialTest.php
Exercise 1 -Simple test case

           Fix the code!

  method greetings() - implement the functionality

  $ phpunit tests/Workshop/TutorialTest.php
Exercise 1 -Simple test case

           Another test
  @test annotation

  method GetAttendeesReturnsListOfNames()

  assertInternalType()

  $ phpunit tests/Workshop/TutorialTest.php
Exercise 1 -Simple test case

         Implementation

  method getAttendees() - return empty array

  $ phpunit tests/Workshop/TutorialTest.php
Exercise 2 - Fixing the failure

              Refactoring
   add $_attendees property

   method getAttendees() return $_attendees

   $ phpunit tests/Workshop/TutorialTest.php
Exercise 2 - Fixing the failure

     Refactoring continues

   initialize $_attendees in the constructor

   $ phpunit tests/Workshop/TutorialTest.php
Exercise 2 - Fixing the failure

          Fix broken code

   assign default value in the constructor

   $ phpunit tests/Workshop/TutorialTest.php
Exercise 3
PHPUnit CLI options

       --colors

       --testdox

       --debug

       --filter
Exercise 4 - More assertions

    Testing booleans - part 1
new test
testTutorialHasNoPlacesLeftWhenMoreThen3Attendees()

assertFalse()

new method arePlacesLeft()

$ phpunit tests/Workshop/TutorialTest.php
Exercise 4 - More assertions

  Testing booleans - part 2
  new test
  testTutorialHasPlacesLeftWhenLessThen3Attendees()

  assertTrue()

  $ phpunit tests/Workshop/TutorialTest.php
Exercise 4 - More assertions

            Testing numbers
new test
testTutorialIsNotFullWhenItNotExceedsMaximumCapacity()

assertGreaterThan(), assertLessThan(), assertNotNull()

$ phpunit tests/Workshop/TutorialTest.php
Exercise 4 - More assertions

 Refactor to add functionality

   new const MAX_CAPACITY

   new method getNumberOfAttendees()

   refactor arePlacesLeft() to use the above

   $ phpunit tests/Workshop/TutorialTest.php
Exercise 4 - More assertions

     Testing variable types
 new test testRoomIsAvailable()

 assertInstanceOf()

 new class Room

 new property $_room initialized in the constructor

 $ phpunit tests/Workshop/TutorialTest.php
Exercise 4 - More assertions

             Testing arrays
new test testAttendeeGotAddedToTheList()

assertContains()

new test
testGetAtendeesReturnCorrectNumberOfAttendees()

assertCount()

$ phpunit tests/Workshop/TutorialTest.php
Exercise 5 - phpunit.xml


   phpunit.xml.dist vs phpunit.xml
Exercise 6
            Asserting exceptions
new test
testAddAttendeeThrowsExceptionWhenAddingNewPersonToFullTutorial()

setExpectedException()

new method addAttendee()

$ phpunit
Exercise 6
   Asserting exceptions
Refactor to add functionality

     refactor arePlacesLeft()

     refactor method addAttendee()

     $ phpunit
Exercise 6
Asserting exceptions
  Using annotations
  @expectedException

  @expectedExceptionMessage

  @expectedExceptionCode

  $ phpunit
Exercise 6
Asserting exceptions
         Using try...catch

catching exceptions using conventional
methods

fail()

$ phpunit
Exercise 7
        Asserting errors
        Testing PHP errors
new test
testInitiatingTutorialWithWrongParamThrowsError()

PHP errors converted to PHPUnit_Framework_Error

$ phpunit
Exercise 7
            Asserting errors
Testing PHP warnings/notices
new method Room::includeDependencies()

new test
testIncludeDependenciesThrowsWarningForMissingFiles()

PHP warnings converted to
PHPUnit_Framework_Error_Warning

PHP notices converted to PHPUnit_Framework_Error_Notice

$ phpunit
Exercise 8
        Asserting output

new test testDisplayShowsGreetingsMessage()

new method displaySummary()

expectOutputString()

$ phpunit
Exercise 9
             Data providers

new provider
provideListOfAttendeesNotExceedingMaximumTutorialCapacity()

modify test
testTutorialIsNotFullWhenItExceedsMaximumCapacity()

@dataProvider

$ phpunit
Exercise 10
              Code coverage

--coverage-html

forceCoversAnnotation

@covers

$ phpunit --coverage-html=/tmp/phpnw12-tutorial
Q&A
https://joind.in/talk/view/7069

Weitere ähnliche Inhalte

Was ist angesagt?

Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDPaweł Michalik
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentationnicobn
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitMichelangelo van Dam
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitmfrost503
 
Beginning PHPUnit
Beginning PHPUnitBeginning PHPUnit
Beginning PHPUnitJace Ju
 
Unit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingUnit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingRam Awadh Prasad, PMP
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applicationschartjes
 
Test Driven Development With Python
Test Driven Development With PythonTest Driven Development With Python
Test Driven Development With PythonSiddhi
 
Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)ENDelt260
 
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
 
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytestHector Canto
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit TestDavid Xie
 
Testes pythonicos com pytest
Testes pythonicos com pytestTestes pythonicos com pytest
Testes pythonicos com pytestviniciusban
 
Testing Code and Assuring Quality
Testing Code and Assuring QualityTesting Code and Assuring Quality
Testing Code and Assuring QualityKent Cowgill
 
Keep your repo clean
Keep your repo cleanKeep your repo clean
Keep your repo cleanHector Canto
 
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
 

Was ist angesagt? (20)

PHPUnit
PHPUnitPHPUnit
PHPUnit
 
Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDD
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnit
 
Phpunit testing
Phpunit testingPhpunit testing
Phpunit testing
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
 
Beginning PHPUnit
Beginning PHPUnitBeginning PHPUnit
Beginning PHPUnit
 
Unit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingUnit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step Training
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applications
 
Unit testing
Unit testingUnit testing
Unit testing
 
Test Driven Development With Python
Test Driven Development With PythonTest Driven Development With Python
Test Driven Development With Python
 
Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)
 
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
 
PHPUnit testing to Zend_Test
PHPUnit testing to Zend_TestPHPUnit testing to Zend_Test
PHPUnit testing to Zend_Test
 
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytest
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
 
Testes pythonicos com pytest
Testes pythonicos com pytestTestes pythonicos com pytest
Testes pythonicos com pytest
 
Testing Code and Assuring Quality
Testing Code and Assuring QualityTesting Code and Assuring Quality
Testing Code and Assuring Quality
 
Keep your repo clean
Keep your repo cleanKeep your repo clean
Keep your repo clean
 
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
 

Ähnlich wie Test your code like a pro - PHPUnit in practice

Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitJames Fuller
 
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
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2Yi-Huan Chan
 
13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboardsDenis Ristic
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit TestingMike Lively
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHPRadu Murzea
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPressHarshad Mane
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yiimadhavi Ghadge
 
Pengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDDPengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDDtlabamazing
 
Getting Started with Test-Driven Development at Midwest PHP 2021
Getting Started with Test-Driven Development at Midwest PHP 2021Getting Started with Test-Driven Development at Midwest PHP 2021
Getting Started with Test-Driven Development at Midwest PHP 2021Scott Keck-Warren
 
(Unit )-Testing for Joomla
(Unit )-Testing for Joomla(Unit )-Testing for Joomla
(Unit )-Testing for JoomlaDavid Jardin
 
Dependency Injection Smells
Dependency Injection SmellsDependency Injection Smells
Dependency Injection SmellsMatthias Noback
 
Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2markstory
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and youmarkstory
 
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_v2Tricode (part of Dept)
 

Ähnlich wie Test your code like a pro - PHPUnit in practice (20)

Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
 
13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Phpunit
PhpunitPhpunit
Phpunit
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHP
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPress
 
PHPUnit with CakePHP and Yii
PHPUnit with CakePHP and YiiPHPUnit with CakePHP and Yii
PHPUnit with CakePHP and Yii
 
Pengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDDPengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDD
 
Php unit (eng)
Php unit (eng)Php unit (eng)
Php unit (eng)
 
Getting Started with Test-Driven Development at Midwest PHP 2021
Getting Started with Test-Driven Development at Midwest PHP 2021Getting Started with Test-Driven Development at Midwest PHP 2021
Getting Started with Test-Driven Development at Midwest PHP 2021
 
(Unit )-Testing for Joomla
(Unit )-Testing for Joomla(Unit )-Testing for Joomla
(Unit )-Testing for Joomla
 
PHPSpec BDD Framework
PHPSpec BDD FrameworkPHPSpec BDD Framework
PHPSpec BDD Framework
 
Dependency Injection Smells
Dependency Injection SmellsDependency Injection Smells
Dependency Injection Smells
 
Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2
 
Php unit
Php unitPhp unit
Php unit
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
 
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
 
Test driven development_for_php
Test driven development_for_phpTest driven development_for_php
Test driven development_for_php
 

Mehr von Sebastian Marek

The Journey Towards Continuous Integration
The Journey Towards Continuous IntegrationThe Journey Towards Continuous Integration
The Journey Towards Continuous IntegrationSebastian Marek
 
CodeClub - Teaching the young generation programming
CodeClub - Teaching the young generation programmingCodeClub - Teaching the young generation programming
CodeClub - Teaching the young generation programmingSebastian Marek
 
Praktyczne code reviews - PHPConPl
Praktyczne code reviews - PHPConPlPraktyczne code reviews - PHPConPl
Praktyczne code reviews - PHPConPlSebastian Marek
 
Managing and Monitoring Application Performance
Managing and Monitoring Application PerformanceManaging and Monitoring Application Performance
Managing and Monitoring Application PerformanceSebastian Marek
 
Ten Commandments Of A Software Engineer
Ten Commandments Of A Software EngineerTen Commandments Of A Software Engineer
Ten Commandments Of A Software EngineerSebastian Marek
 
Continuous Inspection: Fight back the 7 deadly sins of a developer!
Continuous Inspection: Fight back the 7 deadly sins of a developer!Continuous Inspection: Fight back the 7 deadly sins of a developer!
Continuous Inspection: Fight back the 7 deadly sins of a developer!Sebastian Marek
 
PHP Forum Paris 2012: Magic behind the numbers. Software metrics in practice
PHP Forum Paris 2012: Magic behind the numbers. Software metrics in practicePHP Forum Paris 2012: Magic behind the numbers. Software metrics in practice
PHP Forum Paris 2012: Magic behind the numbers. Software metrics in practiceSebastian Marek
 
Ten Commandments Of A Software Engineer
Ten Commandments Of A Software EngineerTen Commandments Of A Software Engineer
Ten Commandments Of A Software EngineerSebastian Marek
 
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice Sebastian Marek
 
Magic behind the numbers - software metrics in practice
Magic behind the numbers - software metrics in practiceMagic behind the numbers - software metrics in practice
Magic behind the numbers - software metrics in practiceSebastian Marek
 
Back to basics - PHPUnit
Back to basics - PHPUnitBack to basics - PHPUnit
Back to basics - PHPUnitSebastian Marek
 
Back to basics - PHP_Codesniffer
Back to basics - PHP_CodesnifferBack to basics - PHP_Codesniffer
Back to basics - PHP_CodesnifferSebastian Marek
 
Sonar - the ring to rule them all
Sonar - the ring to rule them allSonar - the ring to rule them all
Sonar - the ring to rule them allSebastian Marek
 
vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking Sebastian Marek
 

Mehr von Sebastian Marek (16)

The Journey Towards Continuous Integration
The Journey Towards Continuous IntegrationThe Journey Towards Continuous Integration
The Journey Towards Continuous Integration
 
CodeClub - Teaching the young generation programming
CodeClub - Teaching the young generation programmingCodeClub - Teaching the young generation programming
CodeClub - Teaching the young generation programming
 
Praktyczne code reviews - PHPConPl
Praktyczne code reviews - PHPConPlPraktyczne code reviews - PHPConPl
Praktyczne code reviews - PHPConPl
 
Managing and Monitoring Application Performance
Managing and Monitoring Application PerformanceManaging and Monitoring Application Performance
Managing and Monitoring Application Performance
 
Ten Commandments Of A Software Engineer
Ten Commandments Of A Software EngineerTen Commandments Of A Software Engineer
Ten Commandments Of A Software Engineer
 
Continuous Inspection: Fight back the 7 deadly sins of a developer!
Continuous Inspection: Fight back the 7 deadly sins of a developer!Continuous Inspection: Fight back the 7 deadly sins of a developer!
Continuous Inspection: Fight back the 7 deadly sins of a developer!
 
Effective code reviews
Effective code reviewsEffective code reviews
Effective code reviews
 
Effective code reviews
Effective code reviewsEffective code reviews
Effective code reviews
 
PHP Forum Paris 2012: Magic behind the numbers. Software metrics in practice
PHP Forum Paris 2012: Magic behind the numbers. Software metrics in practicePHP Forum Paris 2012: Magic behind the numbers. Software metrics in practice
PHP Forum Paris 2012: Magic behind the numbers. Software metrics in practice
 
Ten Commandments Of A Software Engineer
Ten Commandments Of A Software EngineerTen Commandments Of A Software Engineer
Ten Commandments Of A Software Engineer
 
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
 
Magic behind the numbers - software metrics in practice
Magic behind the numbers - software metrics in practiceMagic behind the numbers - software metrics in practice
Magic behind the numbers - software metrics in practice
 
Back to basics - PHPUnit
Back to basics - PHPUnitBack to basics - PHPUnit
Back to basics - PHPUnit
 
Back to basics - PHP_Codesniffer
Back to basics - PHP_CodesnifferBack to basics - PHP_Codesniffer
Back to basics - PHP_Codesniffer
 
Sonar - the ring to rule them all
Sonar - the ring to rule them allSonar - the ring to rule them all
Sonar - the ring to rule them all
 
vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking
 

Kürzlich hochgeladen

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 WoodJuan lago vázquez
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
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 REVIEWERMadyBayot
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
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 TerraformAndrey Devyatkin
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 

Kürzlich hochgeladen (20)

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
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 

Test your code like a pro - PHPUnit in practice

  • 1. Test your code like a pro PHPUnit in practice Sebastian Marek, Software Architect
  • 2. Me a Pole living in Sheffield over 12 years in development big fan of process automation TDD and CI occasionally contributes to open source software
  • 3. You? Who are you? What’s you typical environment you work in? What’s your experience in unit testing? What are your expectations?
  • 4. Agenda Preparations / Setup Asserting exceptions Simple test case Asserting PHP Fixing first failure errors PHPUnit CLI options Asserting output Assertions Using data providers phpunit.xml file Generating code coverage report
  • 5. Resources WIFI: phpnw12-phpunit hosts file: 192.168.254.55 phpnw12-tutorial http://phpnw12-tutorial
  • 6. Rescue plan $ git clone http://phpnw12-tutorial/phpnw12-tutorial.git $ cd phpnw12-tutorial $ git reset --hard $ git pull origin exercise-<number>
  • 7. Exercise 1 Simple test case repository structure src & tests naming conventions Tutorial.php and TutorialTest.php extends PHPUnit_Framework_TestCase
  • 8. Exercise 1 -Simple test case Test first! namespace PhpNw12TestsWorkshop class TutorialTest method testGreetingsReturnWelcomeMessage() $ phpunit tests/Workshop/TutorialTest.php
  • 9. Exercise 1 -Simple test case Code later! namespace PhpNw12Workshop class Tutorial method greetings() - empty for now $ phpunit tests/Workshop/TutorialTest.php
  • 10. Exercise 1 -Simple test case Fix the code! method greetings() - implement the functionality $ phpunit tests/Workshop/TutorialTest.php
  • 11. Exercise 1 -Simple test case Another test @test annotation method GetAttendeesReturnsListOfNames() assertInternalType() $ phpunit tests/Workshop/TutorialTest.php
  • 12. Exercise 1 -Simple test case Implementation method getAttendees() - return empty array $ phpunit tests/Workshop/TutorialTest.php
  • 13. Exercise 2 - Fixing the failure Refactoring add $_attendees property method getAttendees() return $_attendees $ phpunit tests/Workshop/TutorialTest.php
  • 14. Exercise 2 - Fixing the failure Refactoring continues initialize $_attendees in the constructor $ phpunit tests/Workshop/TutorialTest.php
  • 15. Exercise 2 - Fixing the failure Fix broken code assign default value in the constructor $ phpunit tests/Workshop/TutorialTest.php
  • 16. Exercise 3 PHPUnit CLI options --colors --testdox --debug --filter
  • 17. Exercise 4 - More assertions Testing booleans - part 1 new test testTutorialHasNoPlacesLeftWhenMoreThen3Attendees() assertFalse() new method arePlacesLeft() $ phpunit tests/Workshop/TutorialTest.php
  • 18. Exercise 4 - More assertions Testing booleans - part 2 new test testTutorialHasPlacesLeftWhenLessThen3Attendees() assertTrue() $ phpunit tests/Workshop/TutorialTest.php
  • 19. Exercise 4 - More assertions Testing numbers new test testTutorialIsNotFullWhenItNotExceedsMaximumCapacity() assertGreaterThan(), assertLessThan(), assertNotNull() $ phpunit tests/Workshop/TutorialTest.php
  • 20. Exercise 4 - More assertions Refactor to add functionality new const MAX_CAPACITY new method getNumberOfAttendees() refactor arePlacesLeft() to use the above $ phpunit tests/Workshop/TutorialTest.php
  • 21. Exercise 4 - More assertions Testing variable types new test testRoomIsAvailable() assertInstanceOf() new class Room new property $_room initialized in the constructor $ phpunit tests/Workshop/TutorialTest.php
  • 22. Exercise 4 - More assertions Testing arrays new test testAttendeeGotAddedToTheList() assertContains() new test testGetAtendeesReturnCorrectNumberOfAttendees() assertCount() $ phpunit tests/Workshop/TutorialTest.php
  • 23. Exercise 5 - phpunit.xml phpunit.xml.dist vs phpunit.xml
  • 24. Exercise 6 Asserting exceptions new test testAddAttendeeThrowsExceptionWhenAddingNewPersonToFullTutorial() setExpectedException() new method addAttendee() $ phpunit
  • 25. Exercise 6 Asserting exceptions Refactor to add functionality refactor arePlacesLeft() refactor method addAttendee() $ phpunit
  • 26. Exercise 6 Asserting exceptions Using annotations @expectedException @expectedExceptionMessage @expectedExceptionCode $ phpunit
  • 27. Exercise 6 Asserting exceptions Using try...catch catching exceptions using conventional methods fail() $ phpunit
  • 28. Exercise 7 Asserting errors Testing PHP errors new test testInitiatingTutorialWithWrongParamThrowsError() PHP errors converted to PHPUnit_Framework_Error $ phpunit
  • 29. Exercise 7 Asserting errors Testing PHP warnings/notices new method Room::includeDependencies() new test testIncludeDependenciesThrowsWarningForMissingFiles() PHP warnings converted to PHPUnit_Framework_Error_Warning PHP notices converted to PHPUnit_Framework_Error_Notice $ phpunit
  • 30. Exercise 8 Asserting output new test testDisplayShowsGreetingsMessage() new method displaySummary() expectOutputString() $ phpunit
  • 31. Exercise 9 Data providers new provider provideListOfAttendeesNotExceedingMaximumTutorialCapacity() modify test testTutorialIsNotFullWhenItExceedsMaximumCapacity() @dataProvider $ phpunit
  • 32. Exercise 10 Code coverage --coverage-html forceCoversAnnotation @covers $ phpunit --coverage-html=/tmp/phpnw12-tutorial