Unit testing with PHPUnit

2011-01-19

This will likely be a multi-part series of posts, but I've been doing a lot of unit testing lately with PHPUnit and I feel I've learned several things worth sharing.

Debugging

Not everything you try in PHPUnit will work right away, and this is rarely PHPUnit's fault. When things to break, you have several options:

  1. Turn on display_errors and set error_reporting to a high value. This can be done in php.ini, the phpunit.xml file, as a command-line switch to the phpunit shell script, or directly in your bootstrap or test files using the ini_set and error_reporting functions. If your server configuration allows it, you can also set these values in .htaccess or .user.ini files.
  2. Turn off processIsolation. This is set as a command-line flag or in phpunit.xml. The extra buffering and wrapping this directive uses (while generally a good idea) can sometimes hide the errors that cause your test suite to silently fail.

Read more of this post »

Tags: php, phpunit

Setting up PHP CodeSniffer for NetBeans to work with MAMP

2010-10-09

I ran into an issue using @beberlei's PHP_CodeSniffer plug-in for NetBeans (my current PHP editor, although I do still have a soft spot for ZendStudio, my editor for two years previously).

Environment

  • OSX 10.6
  • NetBeans 6.9.1 (reproduced on 6.9 and 6.8 as well)
  • MAMP version 1.9.2
  • PHP_CodeSniffer, installed into /Applications/MAMP/bin/php5.3/lib/php

After installing the plug-in, (with no errors) I went to the plug-in's preferences pane and saw that there was no list coding standards available in the Coding Standards dropdown, and the location of the phpcs binary (part of PHP_CodeSniffer, used by the plug-in) was blank as well. I was able to input its location, but attempting to save/apply the preferences resulted in a NPE.

I was able to resolve this by creating a symlink to the phpcs binary to /usr/bin (a location on my PATH environment variable). As soon as I did that, NB picked up the phpcs location automatically and populated the coding standards dropdown.

cd /usr/bin
sudo ln -s /Applications/MAMP/bin/php5.3/bin/phpcs

Your paths may vary from the above; replace the path in the 2nd line with the location of phpcs on your system.

Sources

Tags: netbeans, php