From e691f0696e760e40094ae1d291fdf540cacb4864 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 12 Jun 2018 18:05:26 -0700 Subject: [PATCH] Add phpunit skeleton --- ext/afform/phpunit.xml.dist | 18 ++++++++ ext/afform/tests/phpunit/bootstrap.php | 62 ++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 ext/afform/phpunit.xml.dist create mode 100644 ext/afform/tests/phpunit/bootstrap.php diff --git a/ext/afform/phpunit.xml.dist b/ext/afform/phpunit.xml.dist new file mode 100644 index 0000000000..0f9f25d307 --- /dev/null +++ b/ext/afform/phpunit.xml.dist @@ -0,0 +1,18 @@ + + + + + ./tests/phpunit + + + + + ./ + + + + + + + + diff --git a/ext/afform/tests/phpunit/bootstrap.php b/ext/afform/tests/phpunit/bootstrap.php new file mode 100644 index 0000000000..afa827e95a --- /dev/null +++ b/ext/afform/tests/phpunit/bootstrap.php @@ -0,0 +1,62 @@ +add('CRM_', __DIR__); +$loader->add('Civi\\', __DIR__); +$loader->add('api_', __DIR__); +$loader->add('api\\', __DIR__); +$loader->register(); + +/** + * Call the "cv" command. + * + * @param string $cmd + * The rest of the command to send. + * @param string $decode + * Ex: 'json' or 'phpcode'. + * @return string + * Response output (if the command executed normally). + * @throws \RuntimeException + * If the command terminates abnormally. + */ +function cv($cmd, $decode = 'json') { + $cmd = 'cv ' . $cmd; + $descriptorSpec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => STDERR); + $oldOutput = getenv('CV_OUTPUT'); + putenv("CV_OUTPUT=json"); + + // Execute `cv` in the original folder. This is a work-around for + // phpunit/codeception, which seem to manipulate PWD. + $cmd = sprintf('cd %s; %s', escapeshellarg(getenv('PWD')), $cmd); + + $process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__); + putenv("CV_OUTPUT=$oldOutput"); + fclose($pipes[0]); + $result = stream_get_contents($pipes[1]); + fclose($pipes[1]); + if (proc_close($process) !== 0) { + throw new RuntimeException("Command failed ($cmd):\n$result"); + } + switch ($decode) { + case 'raw': + return $result; + + case 'phpcode': + // If the last output is /*PHPCODE*/, then we managed to complete execution. + if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") { + throw new \RuntimeException("Command failed ($cmd):\n$result"); + } + return $result; + + case 'json': + return json_decode($result, 1); + + default: + throw new RuntimeException("Bad decoder format ($decode)"); + } +} -- 2.25.1