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