The day you realised that you can’t really tell what your code does is the day you stop being an amateur programmer and you turn into a professional developer. During this workshop you will learn about the most famous unit testing framework – PHPUnit, how it can help you gain confidence in your code and what to do (and what to avoid) to make your code testable. We will discuss unit testing best practices and talk about tools that can help you automate the whole process, so it becomes more of a habit then a necessity.
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
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
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