I wrote this for use at work, hence some of the wording. I may re-edit it one day, but it should do the trick. Feel free to leave any comments or questions below.
Installing PHPUnit on Windows is done with PEAR.
- Create the folder c:\program files\PEAR (This is your PEAR path)
- Download go-pear.phar from here: http://pear.php.net/go-pear.phar (the one which comes with PHP is usually broken) and save it in your PEAR path.
- Open a command prompt and navigate to your new PEAR folder, type php go-pear.phar
- Hit Enter to agree to a System install then Enter again to confirm all the settings are good (after checking them, of course).
- PEAR is now installed. When we use PEAR to install things, it creates a .bat files in our PEAR directory which can is used to run them. For example by the end of this page PHPUnit.bat will be installed and we can call it from the command line as ‘phpunit’ since windows is clever like that.
To be able to run these commands from outside of the PEAR directory we must add the PEAR directory to our PATH Environment Variable.
- Press Windows Key and Pause/Break to load the System screen.
- On the left click on Advanced System Settings.
- Click on the Advanced tab then “Environment Variables..” button at the bottom.
- In the bottom list, “System Variables” find and select the PATH variable and click Edit.
- Add a semi colon to the end of the list, and enter the path to your PEAR directory.
- Click OK and then OK again.
You will need to open a new CMD window if you already have one open so that you get one with the new environment variables available.
- Run “pear upgrade” to test and get the latest version in one handy step.
Navigate to your PEAR folder and run the following commands
pear config-set auto_discover 1
pear install --alldeps pear.phpunit.de/PHPUnit
PEAR creates a file called PHPUnit.bat, which we can run from anywhere because we put our PEAR directory into our PATH variable in the first step. It’s also created a bunch of other files which PHPUnit.bat uses.
You may wish to add C:\Program Files\PEAR\pear\PHPUnit to your php.ini include_path.
Installing Selenium for Integration Testing
Selenium is a server to which PHPUnit will connect and issue commands, the server then launches a browser and tells it what to do.
- You need Java installed, take the Offline version from here: http://java.com/en/download/manual.jsp
- Download Selenium Server here: http://seleniumhq.org/download/ Create a folder and save this in c:\program files\selenium\
- Right click on your desktop, go to New > Shortcut. For Location simply type “Java”, hit Next and type Selenium Server as the name.
- Right click your new shortcut and click Properties, at the end of the “Target” field add -jar “C:\Program Files (x86)\Selenium\selenium-server-standalone-2.23.1.jar”
- Running this shortcut should give you a console screen with happy output.
- To install the PHPUnit Selenium extensions (to be able to run the Integration Tests) download this file (or check for a later version of PHPUnit_Selenium from here) and merge the PHPUnit_Selenium-1.2.7\PHPUnit\Extentions folder with it’s sister in your PEAR Directory under pear\PHPUnit
Note that we are using Selenium 2, so you must use the Selenium2 classes.
Running the tests
Running the tests should take a matter of seconds and are run from the command line. From within the Squadify code base cd into the tests folder, type “phpunit” and hit enter, you should see something like:
PHPUnit 3.5.15 by Sebastian Bergmann.
.... Errors Come out here ...
Time: 0 seconds, Memory: 5.25Mb
PHPUnit looks for a file called phpunit.xml from which it learns how you with the tests to run. There are command line options for running only sub sets of the tests specified, controlling the output and other cool things.
Writing or Editing Tests
Tests are seperated into TestCases, each TestCase will test one unit. Examples of units are models, pages and forms. Refer to PHPUnit’s documentation for specific details on how to write tests.
It’s very easy to write useless tests, some pointers to avoid doing so:
- Before writing tests, write a Doc Comment description before the Test Case explaining why the test exists.
- Write descriptive names for tests, similar to a Doc Comment these force you to think about what and why you’re testing.
- You’re supposed to write tests that fail, this means running it so you can see it fail, then fix the code to make it pass. It’s important to remember that you must change the code being tested, not the test itself, for this.
For the sake of maintainability put as much care into reusing your test cases code as any other code, without violating these:
- TestCases should never be dependant on another Test Case, individual tests can be when it makes lots of sense but it should be avoided. The reason is so that (for speed) you run a subset of the tests (hence the name Unit Test).
- Factory methods (such as getEmptyUser() or getYoungUser()) should be begin with “get” and placed at the bottom of the class. They’re used to make maintaining easier.
PHP Unit Docs: http://www.phpunit.de/manual/3.2/en/
Advice on writing tests: http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/