Merge pull request #12269 from civicrm/5.2
[civicrm-core.git] / tools / scripts / phpunit
1 #!/usr/bin/env php
2 <?php
3
4 /**
5 * In the past, Civi bundled a hacked copy of PHPUnit used like:
6 *
7 * cd tools
8 * ./scripts/phpunit Some_Class_NameTest
9 *
10 * This script is an adapter for backwards compatibility.
11 */
12
13 if (PHP_SAPI !== 'cli') {
14 die("phpunit can only be run from command line.");
15 }
16 if (version_compare(PHP_VERSION, '5.6', '>=')) {
17 $phpunit = findCommand('phpunit5');
18 }
19 else {
20 $phpunit = findCommand('phpunit4');
21 }
22 if (!$phpunit) {
23 $phpunit = findCommand('phpunit');
24 }
25 if (!$phpunit) {
26 echo "Plesae ensure that:\n";
27 echo " * PHPUnit is installed.\n";
28 echo " * The extensions for dbunit and selenium are installed.\n" ;
29 echo " * The command \"phpunit\" is in the PATH.\n";
30 echo "See also: https://github.com/civicrm/civicrm-buildkit/\n";
31 exit(127);
32 }
33
34 chdir(dirname(dirname(__DIR__))); // civicrm-core root dir
35
36 array_shift($argv);
37
38 // Convert class names to file names
39 $CIVICRM_UF = 'UnitTests';
40 foreach ($argv as $k => $v) {
41 if (preg_match('/^(CRM_|api_v3_|EnvTest|WebTest_|E2E_)/', $v)) {
42 $argv[$k] = 'tests/phpunit/' . strtr($v, '_', '/') . '.php';
43 }
44 elseif (preg_match('/^Civi\\\\/', $v)) {
45 $argv[$k] = 'tests/phpunit/' . strtr($v, '\\', '/') . '.php';
46 }
47
48 if (preg_match('/^(WebTest|E2E)/', $v)) {
49 $CIVICRM_UF='';
50 }
51 }
52 putenv("CIVICRM_UF=$CIVICRM_UF");
53
54 // Transition: Make sure we use phpunit code from PATH, not
55 // civicrm-packages. This will be unnecessary once civicrm-packages is
56 // updated.
57 if (is_dir('packages/PHPUnit/')) {
58 if (!rename('packages/PHPUnit', 'packages/PHPUnit.bak')) {
59 echo "Failed to move aside stale copy of PHPUnit.\n";
60 exit(1);
61 }
62 }
63
64 $cmd =
65 findPhp() // In case this system has multiple copies of PHP, use the active/preferred one.
66 // . ' -ddisplay_errors=1'
67 . ' '
68 . escapeshellarg($phpunit)
69 . ' '
70 . implode(' ', array_map('escapeshellarg', $argv));
71 passthru($cmd);
72
73 function findPhp() {
74 if (defined('PHP_BINARY')) {
75 return PHP_BINARY; // php 5.4+
76 } elseif (defined('PHP_BINDIR') && file_exists(PHP_BINDIR . '/php')) {
77 return PHP_BINDIR . '/php'; // php 5.3
78 } else {
79 die("Failed to determine active PHP version.");
80 }
81 }
82
83 function findCommand($name) {
84 $paths = explode(PATH_SEPARATOR, getenv('PATH'));
85 foreach ($paths as $path) {
86 if (file_exists("$path/$name")) {
87 return "$path/$name";
88 }
89 }
90 return NULL;
91 }