Monday, May 12, 2014

Upgrading to PHPUNIT 4.0 #CIUNIT #chef

#notetomyself

The phpunit test suite failed to execute on the newly built VM (configuration was done automatically via #chef), and that made me look into whats wrong with the test suite.

[CIUnit] PHP Error: Warning - require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory File Path: CIUnit/bootstrap_phpunit.php (line: 260)

PHP Fatal error:  require_once(): Failed opening required 'PHPUnit/Autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/picsyn/application/third_party/CIUnit/bootstrap_phpunit.php on line 260

the first test was to check whether there were any changes to unit test files, but few minutes later found out that it wasn't the case.

Then I did a comparison of the phpunit versions I had installed before and now. first lead of suspect is that there have been a major version change. reading more on the changeset I learnt that phpunit installation via PEAR will be dis-continued after this year, so the most logical step to take is to update the installation process to support PHAR or Composer.

I'm going to take the PHAR approach this time.

here are the steps I took to fix the issue.

1. Download the latest phpunit cookbook from (http://community.opscode.com/cookbooks/phpunit/)
or alternatively
cd path_to_cookbooks_folder
knife cookbook site download phpunit
tar -xf phpunit_cookbook_tar_file

2. change config to mention phar install (cookbooks/phpunit/attributes/default.rb)
default[:phpunit][:install_method] = 'phar'

3. change config of the install dir (cookbooks/phpunit/attributes/phar.rb)
default[:phpunit][:install_dir] = '/usr/local/bin'

4. I had to make the following hack to ensure 'phpunit' command works from the CLI
In cookbooks/phpunit/recipes/phar.rb change remote_file section as below.
remote_file "#{phpunit_dir}/phpunit" do
  source node[:phpunit][:phar_url]
  mode 0755
end

5. On the PHPUNIT bootstrap file remove all mentions of the include of PHPUnit/Autoload.php as it isn't needed anymore. eg. there were two places in bootstrap_phpunit.php file that is used by my test project.
require_once ('PHPUnit/Autoload.php');

Thats all, and the following command works again without any modifications to how it should be called.


phpunit -c php/phpunit.xml models/AllModelsTest.php