5 * In the past, Civi bundled a hacked copy of PHPUnit used like:
8 * ./scripts/phpunit Some_Class_NameTest
10 * This script is an adapter for backwards compatibility.
15 if (PHP_SAPI
!== 'cli') {
16 die("phpunit can only be run from command line.");
18 if (version_compare(PHP_VERSION
, '7.0', '>=')) {
19 $phpunit = findCommand('phpunit6');
20 $argFilters[] = function ($argv) {
21 $pos = array_search('--tap', $argv);
23 array_splice($argv, $pos, 1, ['--printer', '\Civi\Test\TAP']);
28 elseif (version_compare(PHP_VERSION
, '5.6', '>=')) {
29 $phpunit = findCommand('phpunit5');
32 $phpunit = findCommand('phpunit4');
35 $phpunit = findCommand('phpunit');
38 echo "Plesae ensure that:\n";
39 echo " * PHPUnit is installed.\n";
40 echo " * The extensions for dbunit and selenium are installed.\n" ;
41 echo " * The command \"phpunit\" is in the PATH.\n";
42 echo "See also: https://github.com/civicrm/civicrm-buildkit/\n";
46 chdir(dirname(dirname(__DIR__
))); // civicrm-core root dir
50 // Convert class names to file names
51 $CIVICRM_UF = 'UnitTests';
52 foreach ($argv as $k => $v) {
53 if (preg_match('/^(CRM_|api_v3_|EnvTest|WebTest_|E2E_)/', $v)) {
54 $argv[$k] = 'tests/phpunit/' . strtr($v, '_', '/') . '.php';
56 elseif (preg_match('/^Civi\\\\/', $v)) {
57 $argv[$k] = 'tests/phpunit/' . strtr($v, '\\', '/') . '.php';
60 if (preg_match('/^(WebTest|E2E)/', $v)) {
64 putenv("CIVICRM_UF=$CIVICRM_UF");
66 // Transition: Make sure we use phpunit code from PATH, not
67 // civicrm-packages. This will be unnecessary once civicrm-packages is
69 if (is_dir('packages/PHPUnit/')) {
70 if (!rename('packages/PHPUnit', 'packages/PHPUnit.bak')) {
71 echo "Failed to move aside stale copy of PHPUnit.\n";
76 foreach ($argFilters as $filter) {
77 $argv = $filter($argv);
81 findPhp() // In case this system has multiple copies of PHP, use the active/preferred one.
82 // . ' -ddisplay_errors=1'
84 . escapeshellarg($phpunit)
86 . implode(' ', array_map('escapeshellarg', $argv));
90 if (defined('PHP_BINARY')) {
91 return PHP_BINARY
; // php 5.4+
92 } elseif (defined('PHP_BINDIR') && file_exists(PHP_BINDIR
. '/php')) {
93 return PHP_BINDIR
. '/php'; // php 5.3
95 die("Failed to determine active PHP version.");
99 function findCommand($name) {
100 $paths = explode(PATH_SEPARATOR
, getenv('PATH'));
101 foreach ($paths as $path) {
102 if (file_exists("$path/$name")) {
103 return "$path/$name";