Drupal - How to run PHPUnit tests?

You can get PHPUnit tests to run by bootstrapping Drupal within each of your test files. It's not ideal, but it does work.

define('DRUPAL_ROOT', 'your/path/to/drupal');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';

// Bootstrap Drupal.
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Proceed with PHPUnit tests as usual from here.
class MyTest extends \PHPUnit_Framework_TestCase {
  ...

PHPUnit provides a nice API for building the objects whereas the Drupal's simpletest doesn't. There is one library available in gist for integrating PHPUnit with Drupal 7.
To execute those scripts you need to checkout this gist-repository. To execute Unit-Tests in the command-line simply go to a Drupal site (ie. <DRUPAL_ROOT>/sites/default) and use dphpunit.bash just as you would use the phpunit command.

The script consists of 3 files:

  1. dphpunit.bash - which simply invokes drun-dphpunit.php with a few extra parameters. It's needed because PHP is incapable of dealing with symlinks correctly.
  2. drun-dphpunit.php - which is basically the same as the upstream phpunit runner, except that it handles the extra parameter.
  3. bootstrap.inc.php - which makes a Drupal bootstrap very similar to drush.

Source: http://devblog.more-onion.com/content/writing-unit-tests-drupal-7


bootstrap.inc.php

<?php

$path = CWD;

$site_dir = NULL;
$dpl_dir = NULL;

while ($path != '/') {
    if (file_exists($path . '/settings.php')) {
        $site_dir = $path;
    }
    if (file_exists($path . '/index.php') && file_exists($path . '/includes/bootstrap.inc')) {
        $dpl_dir = $path;
        break;
    }
    $path = dirname($path);
}

if (!$dpl_dir) {
    echo "No drupal directory found in or above current working directory. Aborting. \n";
    exit(1);
}
if (!$site_dir) {
    $site_dir = $dpl_dir . '/sites/default';
    if (!file_exists($site_dir . '/settings.php')) {
        echo "No configured site found. Aborting.\n";
        exit(1);
    }
}

$_SERVER['HTTP_HOST'] = basename($site_dir);
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';

define('DRUPAL_ROOT', $dpl_dir);
set_include_path($dpl_dir . PATH_SEPARATOR . get_include_path());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

dphpunit.bash

#!/bin/bash

# get dirname of the script
DIR="$(dirname $(readlink "$0"))"

# assume the boostrap script is stored in the same directory
php "$DIR/drun-phpunit.php" "$(pwd)" --bootstrap "$DIR/bootstrap.inc.php" "$@"

drun-phpunit.php

<?php
require_once 'PHP/CodeCoverage/Filter.php';
PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'PHPUNIT');

if (extension_loaded('xdebug')) {
  xdebug_disable();
}

if (strpos('/usr/bin/php', '@php_bin') === 0) {
  set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
}

require_once 'PHPUnit/Autoload.php';
define('PHPUnit_MAIN_METHOD', 'PHPUnit_TextUI_Command::main');
define('CWD', $_SERVER['argv'][1]);
unset($_SERVER['argv'][1]);

$command = new PHPUnit_TextUI_Command;
$command->run($_SERVER['argv']);

There is one more library available for integration PHPUnit with Drupal 7: https://github.com/sebastianbergmann/phpunit

More information about this scripts can be checked here: http://thomaslattimore.com/blog/using-phpunit-with-drupal-7

Tags:

Testing

7