Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | #!/usr/bin/env php |
2 | <?php | |
620c8b6e TO |
3 | |
4 | /** | |
5 | * In the past, Civi bundled a hacked copy of PHPUnit used like: | |
6a488035 | 6 | * |
620c8b6e TO |
7 | * cd tools |
8 | * ./scripts/phpunit Some_Class_NameTest | |
6a488035 | 9 | * |
620c8b6e | 10 | * This script is an adapter for backwards compatibility. |
6a488035 TO |
11 | */ |
12 | ||
71722e30 TO |
13 | $argFilters = []; |
14 | ||
620c8b6e TO |
15 | if (PHP_SAPI !== 'cli') { |
16 | die("phpunit can only be run from command line."); | |
89771184 | 17 | } |
04c46dad SL |
18 | if (version_compare(PHP_VERSION, '7.2', '>=')) { |
19 | $phpunit = findCommand('phpunit8'); | |
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, '7.1', '>=')) { | |
8851bdf5 SL |
29 | $phpunit = findCommand('phpunit7'); |
30 | $argFilters[] = function ($argv) { | |
31 | $pos = array_search('--tap', $argv); | |
32 | if ($pos !== FALSE) { | |
33 | array_splice($argv, $pos, 1, ['--printer', '\Civi\Test\TAP']); | |
34 | } | |
35 | return $argv; | |
36 | }; | |
37 | } | |
38 | elseif (version_compare(PHP_VERSION, '7.0', '>=')) { | |
d717cfb6 | 39 | $phpunit = findCommand('phpunit6'); |
71722e30 TO |
40 | $argFilters[] = function ($argv) { |
41 | $pos = array_search('--tap', $argv); | |
42 | if ($pos !== FALSE) { | |
43 | array_splice($argv, $pos, 1, ['--printer', '\Civi\Test\TAP']); | |
44 | } | |
45 | return $argv; | |
46 | }; | |
d717cfb6 SL |
47 | } |
48 | elseif (version_compare(PHP_VERSION, '5.6', '>=')) { | |
fac6351c SL |
49 | $phpunit = findCommand('phpunit5'); |
50 | } | |
51 | else { | |
52 | $phpunit = findCommand('phpunit4'); | |
53 | } | |
20a7a2be TO |
54 | if (!$phpunit) { |
55 | $phpunit = findCommand('phpunit'); | |
56 | } | |
620c8b6e TO |
57 | if (!$phpunit) { |
58 | echo "Plesae ensure that:\n"; | |
59 | echo " * PHPUnit is installed.\n"; | |
60 | echo " * The extensions for dbunit and selenium are installed.\n" ; | |
61 | echo " * The command \"phpunit\" is in the PATH.\n"; | |
62 | echo "See also: https://github.com/civicrm/civicrm-buildkit/\n"; | |
63 | exit(127); | |
6a488035 TO |
64 | } |
65 | ||
620c8b6e | 66 | chdir(dirname(dirname(__DIR__))); // civicrm-core root dir |
6a488035 | 67 | |
620c8b6e | 68 | array_shift($argv); |
6a488035 | 69 | |
620c8b6e | 70 | // Convert class names to file names |
c5d26c27 | 71 | $CIVICRM_UF = 'UnitTests'; |
620c8b6e | 72 | foreach ($argv as $k => $v) { |
2c074028 | 73 | if (preg_match('/^(CRM_|api_v3_|api_v4_|EnvTest|WebTest_|E2E_)/', $v)) { |
620c8b6e | 74 | $argv[$k] = 'tests/phpunit/' . strtr($v, '_', '/') . '.php'; |
7e0295ce TO |
75 | } |
76 | elseif (preg_match('/^Civi\\\\/', $v)) { | |
77 | $argv[$k] = 'tests/phpunit/' . strtr($v, '\\', '/') . '.php'; | |
620c8b6e | 78 | } |
f5dfa52e | 79 | |
c5d26c27 TO |
80 | if (preg_match('/^(WebTest|E2E)/', $v)) { |
81 | $CIVICRM_UF=''; | |
f5dfa52e | 82 | } |
620c8b6e | 83 | } |
c5d26c27 | 84 | putenv("CIVICRM_UF=$CIVICRM_UF"); |
6a488035 | 85 | |
620c8b6e TO |
86 | // Transition: Make sure we use phpunit code from PATH, not |
87 | // civicrm-packages. This will be unnecessary once civicrm-packages is | |
88 | // updated. | |
89 | if (is_dir('packages/PHPUnit/')) { | |
90 | if (!rename('packages/PHPUnit', 'packages/PHPUnit.bak')) { | |
91 | echo "Failed to move aside stale copy of PHPUnit.\n"; | |
92 | exit(1); | |
93 | } | |
94 | } | |
6a488035 | 95 | |
71722e30 TO |
96 | foreach ($argFilters as $filter) { |
97 | $argv = $filter($argv); | |
98 | } | |
99 | ||
620c8b6e TO |
100 | $cmd = |
101 | findPhp() // In case this system has multiple copies of PHP, use the active/preferred one. | |
f5dfa52e | 102 | // . ' -ddisplay_errors=1' |
620c8b6e TO |
103 | . ' ' |
104 | . escapeshellarg($phpunit) | |
105 | . ' ' | |
106 | . implode(' ', array_map('escapeshellarg', $argv)); | |
107 | passthru($cmd); | |
6a488035 | 108 | |
620c8b6e | 109 | function findPhp() { |
62e6c49c TO |
110 | // The autodetect behavior here is a potential point of contention. These two cases are hard to reconcile: |
111 | // 1. `php` is actually a wrapper script which delegates to another PHP binary and | |
112 | // passes options (such as INI's and PECL extensions). Subprocesses should use the wrapper script. | |
113 | // Examples: bitnami, bknix | |
114 | // 2. There are multiple PHP binaries (eg `php55`, `php70`). Subprocesses should use | |
115 | // the final executable (regardless of its name). | |
116 | // Example: using MAMP and adding 'ln -s /Application/MAMP/.../php7.1.2/bin/php ~/bin/php71` | |
117 | // Since the test infra uses a wrapper script like (1), we use autodetect logic that works there. | |
118 | // If you're in situation (2), then set an env-var or just skip on using `tools/scripts/phpunit`. | |
119 | if (getenv('PHP')) { | |
120 | return getenv('PHP'); | |
121 | } | |
122 | else { | |
123 | return 'php'; | |
620c8b6e TO |
124 | } |
125 | } | |
126 | ||
127 | function findCommand($name) { | |
128 | $paths = explode(PATH_SEPARATOR, getenv('PATH')); | |
129 | foreach ($paths as $path) { | |
130 | if (file_exists("$path/$name")) { | |
131 | return "$path/$name"; | |
132 | } | |
133 | } | |
134 | return NULL; | |
135 | } |