Downloads

Google Ad

Fixing PHPUnit for CakePHP 2.x testing on Mac OSX

Date: Sat, Jun 9th 2012, 02:47 Author: nick Views: 8312 Comments share
I ran into a very peculiar problem when trying to run my CakePHP tests on my mac the other day. I would go to run my cake test command and it would just exit immediately -- no errors, no output, nothing.

  1. [nwb@cap:~/Websites/HH/app → dev]$ cake test
  2. [nwb@cap:~/Websites/HH/app → dev]$

Odd, the rest of my shell commands worked perfectly. I know I had PHPUnit installed via mac ports so what was the deal?

So I started digging into the core with die() commands to narrow down the problem, turnes out it was in the CakeTestSuiteDispatcher class when trying to run the loadTestFramework() function.

  1. //lib/Cake/TestSuite/CakeTestSuiteDispatcher.php
  2. public function loadTestFramework() {
  3.   $found = $path = null;
  4.  
  5.   if (@include 'PHPUnit' . DS . 'Autoload.php') {
  6.     $found = true;
  7.   }
  8.   if (!$found) {
  9.     foreach (App::path('vendors') as $vendor) {
  10.       if (is_dir($vendor . 'PHPUnit')) {
  11.         $path = $vendor;
  12.       }
  13.     }
  14.  
  15.     if ($path && ini_set('include_path', $path . PATH_SEPARATOR . ini_get('include_path'))) {
  16.       $found = include 'PHPUnit' . DS . 'Autoload.php';
  17.     }
  18.   }
  19.   return $found;
  20. }

The issue is on line 141:

  1. if (@include 'PHPUnit' . DS . 'Autoload.php') {
  2.   $found = true;
  3. }

CakePHP tries to include PHPUnit/Autoload.php which is there, but Autoload.php requires_once() on PHP/CodeCoverage/Filter.php (which wasn't installed). For some stupid reason CakePHP decided to suppresses this error message, which is just wrong. I understand they want to also allow you to install PHPUnit in your vendors path, but then update the paths before the include instead of trying it out first and suppressing the error message. I'd prefer the extra bloat to loading tests than to have it break without any error message. Suppression doesn't save a fatal_error it just leaves the user guessing as to why nothing happened.

Once I understood the problem the fix was simple, install php5-code-coverage and add the include_path to php.ini

  1. $ sudo port install php5-code-coverage

  1. ;php.ini file
  2. include_path = ".:/opt/local/lib/php/"

Stop and start PHP (apache, or fpm) and tried again. I'm back to testing in CakePHP.

Hope that helps someone else,
Nick