Merge pull request #14928 from lcdservices/dev-core-1158
[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 $argFilters = [];
14
15 if (PHP_SAPI !== 'cli') {
16 die("phpunit can only be run from command line.");
17 }
18 if (version_compare(PHP_VERSION, '7.0', '>=')) {
19 $phpunit = findCommand('phpunit6');
20 $argFilters[] = function ($argv) {
21 $pos = array_search('--tap', $argv);
22 if ($pos !== FALSE) {
23 array_splice($argv, $pos, 1, ['--printer', '\Civi\Test\TAP']);
24 }
25 return $argv;
26 };
27 }
28 elseif (version_compare(PHP_VERSION, '5.6', '>=')) {
29 $phpunit = findCommand('phpunit5');
30 }
31 else {
32 $phpunit = findCommand('phpunit4');
33 }
34 if (!$phpunit) {
35 $phpunit = findCommand('phpunit');
36 }
37 if (!$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";
43 exit(127);
44 }
45
46 chdir(dirname(dirname(__DIR__))); // civicrm-core root dir
47
48 array_shift($argv);
49
50 // Convert class names to file names
51 $CIVICRM_UF = 'UnitTests';
52 foreach ($argv as $k => $v) {
53 if (preg_match('/^(CRM_|api_v3_|api_v4_|EnvTest|WebTest_|E2E_)/', $v)) {
54 $argv[$k] = 'tests/phpunit/' . strtr($v, '_', '/') . '.php';
55 }
56 elseif (preg_match('/^Civi\\\\/', $v)) {
57 $argv[$k] = 'tests/phpunit/' . strtr($v, '\\', '/') . '.php';
58 }
59
60 if (preg_match('/^(WebTest|E2E)/', $v)) {
61 $CIVICRM_UF='';
62 }
63 }
64 putenv("CIVICRM_UF=$CIVICRM_UF");
65
66 // Transition: Make sure we use phpunit code from PATH, not
67 // civicrm-packages. This will be unnecessary once civicrm-packages is
68 // updated.
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";
72 exit(1);
73 }
74 }
75
76 foreach ($argFilters as $filter) {
77 $argv = $filter($argv);
78 }
79
80 $cmd =
81 findPhp() // In case this system has multiple copies of PHP, use the active/preferred one.
82 // . ' -ddisplay_errors=1'
83 . ' '
84 . escapeshellarg($phpunit)
85 . ' '
86 . implode(' ', array_map('escapeshellarg', $argv));
87 passthru($cmd);
88
89 function findPhp() {
90 // The autodetect behavior here is a potential point of contention. These two cases are hard to reconcile:
91 // 1. `php` is actually a wrapper script which delegates to another PHP binary and
92 // passes options (such as INI's and PECL extensions). Subprocesses should use the wrapper script.
93 // Examples: bitnami, bknix
94 // 2. There are multiple PHP binaries (eg `php55`, `php70`). Subprocesses should use
95 // the final executable (regardless of its name).
96 // Example: using MAMP and adding 'ln -s /Application/MAMP/.../php7.1.2/bin/php ~/bin/php71`
97 // Since the test infra uses a wrapper script like (1), we use autodetect logic that works there.
98 // If you're in situation (2), then set an env-var or just skip on using `tools/scripts/phpunit`.
99 if (getenv('PHP')) {
100 return getenv('PHP');
101 }
102 else {
103 return 'php';
104 }
105 }
106
107 function findCommand($name) {
108 $paths = explode(PATH_SEPARATOR, getenv('PATH'));
109 foreach ($paths as $path) {
110 if (file_exists("$path/$name")) {
111 return "$path/$name";
112 }
113 }
114 return NULL;
115 }