Slide 1

Slide 1 text

TDD Test-Driven Development in PHP using PHPUnit · REVISITED · 1 Friday, March 30, 12

Slide 2

Slide 2 text

“Code that is not tested, is broken by design” 2 Friday, March 30, 12

Slide 3

Slide 3 text

“Even good programmers make mistakes. The difference between a good programmer and a bad programmer is that the good programer uses tests to detect his mistakes as soon as possible...” -- phpunit manual 3 Friday, March 30, 12

Slide 4

Slide 4 text

4 Friday, March 30, 12

Slide 5

Slide 5 text

TDD workflow 5 Friday, March 30, 12

Slide 6

Slide 6 text

write tests FAIL implementation SUCCESS REFACTOR: go back to 1 and start again 1 2 3 4 6 Friday, March 30, 12

Slide 7

Slide 7 text

“Whenever you are tempted to type something into a print statement or a debbuger expression, write it as a test instead” -- Martin Fowler 7 Friday, March 30, 12

Slide 8

Slide 8 text

Not cool 8 Friday, March 30, 12

Slide 9

Slide 9 text

Sorry, but that sucks You can’t see the NULL! 9 Friday, March 30, 12

Slide 10

Slide 10 text

These rocks 10 Friday, March 30, 12

Slide 11

Slide 11 text

PHPUnit installation (using composer) 11 Friday, March 30, 12

Slide 12

Slide 12 text

Create a composer.json in your project’s root directory: That will install PHPUnit under YourProject/vendor/ directory Then use the installed phpunit.php executable Then simply do: composer.phar install/update to install/update dependencies 12 Friday, March 30, 12

Slide 13

Slide 13 text

Configuration 13 Friday, March 30, 12

Slide 14

Slide 14 text

Drop a phpunit.xml.dist file in your project’s directory with this configuration: bootstrap.php is used to setup the test environment 14 Friday, March 30, 12

Slide 15

Slide 15 text

Real world example: AddBuyer 15 Friday, March 30, 12

Slide 16

Slide 16 text

Changes in the core in order to make it more “testeable”: Wrap $_ superglobals in more semantic objects suchas request and response Bootstrap test file is a mess due to all those F*** super global constants: DON’T USE CONSTANTS OR SINGLETONS, USE DEPENDENCY INJECTION Setup a test database (easy but don’t forget to use MyISAM as the database engine) We had to refactor the codebase in order to: 16 Friday, March 30, 12

Slide 17

Slide 17 text

Setup DB testing using DBUnit 17 Friday, March 30, 12

Slide 18

Slide 18 text

Add the following to the phpunit.xml.dist file That tells DBUnit how to connect to the test database 18 Friday, March 30, 12

Slide 19

Slide 19 text

Create the file tests/DatabaseTestCase.php: getDataSet() is something I did to ease fixtures load in my test cases 19 Friday, March 30, 12

Slide 20

Slide 20 text

Fixtures: “I want my DB with this data when running this test” 20 Friday, March 30, 12

Slide 21

Slide 21 text

I usually name fixtures after the test case. For example when I’m testing: tests/AddBuyer/Tests/RegistrationTest.php I load the fixture: tests/AddBuyer/Fixtures/RegistrationTest.yml test case fixture 21 Friday, March 30, 12

Slide 22

Slide 22 text

The End 22 Friday, March 30, 12