SlideShare uma empresa Scribd logo
1 de 33
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
nicobn
 
Beginning PHPUnit
Beginning PHPUnitBeginning PHPUnit
Beginning PHPUnit
Jace Ju
 
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
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
David Xie
 

Mais procurados (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
 

Semelhante a Test your code like a pro - PHPUnit in practice

Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
Yi-Huan Chan
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 

Semelhante a 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
 

Mais de Sebastian 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
 
vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking
Sebastian Marek
 

Mais de 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
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
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
 
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
 
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...
 
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
 
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...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

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