Merge pull request #18880 from vingle/master
[civicrm-core.git] / ext / oauth-client / tests / phpunit / bootstrap.php
1 <?php
2
3 ini_set('memory_limit', '2G');
4 ini_set('safe_mode', 0);
5 define('CIVICRM_TEST', 1);
6 // phpcs:disable
7 eval(cv('php:boot --level=classloader', 'phpcode'));
8 // phpcs:enable
9 // Allow autoloading of PHPUnit helper classes in this extension.
10 $loader = new \Composer\Autoload\ClassLoader();
11 $loader->add('CRM_', __DIR__);
12 $loader->add('Civi\\', __DIR__);
13 $loader->add('api_', __DIR__);
14 $loader->add('api\\', __DIR__);
15 $loader->register();
16
17 /**
18 * Call the "cv" command.
19 *
20 * @param string $cmd
21 * The rest of the command to send.
22 * @param string $decode
23 * Ex: 'json' or 'phpcode'.
24 * @return string
25 * Response output (if the command executed normally).
26 * @throws \RuntimeException
27 * If the command terminates abnormally.
28 */
29 function cv($cmd, $decode = 'json') {
30 $cmd = 'cv ' . $cmd;
31 $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR);
32 $oldOutput = getenv('CV_OUTPUT');
33 putenv("CV_OUTPUT=json");
34
35 // Execute `cv` in the original folder. This is a work-around for
36 // phpunit/codeception, which seem to manipulate PWD.
37 $cmd = sprintf('cd %s; %s', escapeshellarg(getenv('PWD')), $cmd);
38
39 $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__);
40 putenv("CV_OUTPUT=$oldOutput");
41 fclose($pipes[0]);
42 $result = stream_get_contents($pipes[1]);
43 fclose($pipes[1]);
44 if (proc_close($process) !== 0) {
45 throw new RuntimeException("Command failed ($cmd):\n$result");
46 }
47 switch ($decode) {
48 case 'raw':
49 return $result;
50
51 case 'phpcode':
52 // If the last output is /*PHPCODE*/, then we managed to complete execution.
53 if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") {
54 throw new \RuntimeException("Command failed ($cmd):\n$result");
55 }
56 return $result;
57
58 case 'json':
59 return json_decode($result, 1);
60
61 default:
62 throw new RuntimeException("Bad decoder format ($decode)");
63 }
64 }