Merge pull request #15322 from alifrumin/removePrintIcon
[civicrm-core.git] / tools / scripts / phpunit
CommitLineData
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
15if (PHP_SAPI !== 'cli') {
16 die("phpunit can only be run from command line.");
89771184 17}
d717cfb6
SL
18if (version_compare(PHP_VERSION, '7.0', '>=')) {
19 $phpunit = findCommand('phpunit6');
71722e30
TO
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 };
d717cfb6
SL
27}
28elseif (version_compare(PHP_VERSION, '5.6', '>=')) {
fac6351c
SL
29 $phpunit = findCommand('phpunit5');
30}
31else {
32 $phpunit = findCommand('phpunit4');
33}
20a7a2be
TO
34if (!$phpunit) {
35 $phpunit = findCommand('phpunit');
36}
620c8b6e
TO
37if (!$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);
6a488035
TO
44}
45
620c8b6e 46chdir(dirname(dirname(__DIR__))); // civicrm-core root dir
6a488035 47
620c8b6e 48array_shift($argv);
6a488035 49
620c8b6e 50// Convert class names to file names
c5d26c27 51$CIVICRM_UF = 'UnitTests';
620c8b6e 52foreach ($argv as $k => $v) {
2c074028 53 if (preg_match('/^(CRM_|api_v3_|api_v4_|EnvTest|WebTest_|E2E_)/', $v)) {
620c8b6e 54 $argv[$k] = 'tests/phpunit/' . strtr($v, '_', '/') . '.php';
7e0295ce
TO
55 }
56 elseif (preg_match('/^Civi\\\\/', $v)) {
57 $argv[$k] = 'tests/phpunit/' . strtr($v, '\\', '/') . '.php';
620c8b6e 58 }
f5dfa52e 59
c5d26c27
TO
60 if (preg_match('/^(WebTest|E2E)/', $v)) {
61 $CIVICRM_UF='';
f5dfa52e 62 }
620c8b6e 63}
c5d26c27 64putenv("CIVICRM_UF=$CIVICRM_UF");
6a488035 65
620c8b6e
TO
66// Transition: Make sure we use phpunit code from PATH, not
67// civicrm-packages. This will be unnecessary once civicrm-packages is
68// updated.
69if (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}
6a488035 75
71722e30
TO
76foreach ($argFilters as $filter) {
77 $argv = $filter($argv);
78}
79
620c8b6e
TO
80$cmd =
81 findPhp() // In case this system has multiple copies of PHP, use the active/preferred one.
f5dfa52e 82 // . ' -ddisplay_errors=1'
620c8b6e
TO
83 . ' '
84 . escapeshellarg($phpunit)
85 . ' '
86 . implode(' ', array_map('escapeshellarg', $argv));
87passthru($cmd);
6a488035 88
620c8b6e 89function findPhp() {
62e6c49c
TO
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';
620c8b6e
TO
104 }
105}
106
107function 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}