3. * www.ExigenServices.com
PHPUNIT
• Beginning
• Installation
• Test Driven Development
• Parameterized Unit Testing
• Testing Exceptions
• Dependencies
• Fixtures
• Patterns and Antipatterns
Contents
4. * www.ExigenServices.com
PHPUNIT: BEGINNING
• easy to learn to write
• easy to write
• easy to read
• easy to execute
• quick to execute
• isolated
• composable
What should be the tests
11. * www.ExigenServices.com
PHPUNIT: TEST DRIVEN DEVELOPMENT
Tests execution
~# cd <tests_dir>/models
~# phpunit WeekdayTest
PHPUnit 3.6.0 by Sebastian Bergmann.
..
Time: 0 seconds, Memory: 5.25Mb
OK (2 test, 0 assertion)
12. * www.ExigenServices.com
PHPUNIT: TEST DRIVEN DEVELOPMENT
Tests execution results
. - success
F - assertion fails
E - error occurs
S - test skipped
I - test incomplete or not implemented
21. * www.ExigenServices.com
PHPUNIT: FIXTURES
• setUpBeforeClass (once before all tests)
• setUp (before each test)
• assertPreConditions (before each test)
• assertPostConditions (after each successful test)
• tearDown (after each test)
• onNotSuccessfulTest (after each failed test)
• tearDownAfterClass (once after all tests)
All methods available
The difference between good programmer and bad programmer is that good programmer uses unit tests.
Some of these constraints always conflict with each other. For example, "easy to learn write" conflicts with "easy to write" - later you'll see that this is true. Composable is ability to run tests in any combination.
Name of the file with tests also should contains "Test".
assertEquals also useful for array comparison
You should have root privileges to execute these commands.
Let's assume that we have to develop a model of week day which should have such methods. First of all we'll create a PHP file with following content. After that we may start to write tests.
It is highly recommended to use same directory structure as the original code. Let's create a file with the following content. The above PHPDoc like expressions are PHPUnit specific constraints.
You have to remember about including file with definitions of necessary classes in your tests (require_once). You may also register your own autoload method in a bootstrap file which name can be transferred through the special command-line option. After execution of previous empty tests we'll see that all was successful because of no any assertion was written yet.
Now when we execute these tests we'll see info about two failed tests. It's not bad, we expected it because of we didn't implement our model yet. Let's do it!
There is another way to write parameterized unit tests using @assert decorator
There is another way to test expectation of exception: using @expectedException decorator. For test PHP errors you can use special type of exceptions: PHPUnit_Framework_Error.
Units are separate functions and methods.
Friendly reminder before practical task
Who may tell about what is wrong in this example? There are at least three separate places that should to be tested: getting comments from DB by post_id, comments filtering and viewing result. So there must be three separate units. Also following the concepts of MVC pattern viewComments() should be placed in the Controller, not in the Model.