- Merged the original PhpUnit plugin with the rewrite
This commit is contained in:
parent
00035a7d81
commit
cd54aefd5c
|
@ -11,54 +11,59 @@ namespace PHPCI\Plugin;
|
||||||
|
|
||||||
use PHPCI;
|
use PHPCI;
|
||||||
use PHPCI\Builder;
|
use PHPCI\Builder;
|
||||||
|
use PHPCI\Helper\Lang;
|
||||||
use PHPCI\Model\Build;
|
use PHPCI\Model\Build;
|
||||||
use PHPCI\Plugin\Util\TapParser;
|
use PHPCI\Model\BuildError;
|
||||||
|
use PHPCI\Plugin\Option\PhpUnitOptions;
|
||||||
|
use PHPCI\Plugin\Util\PhpUnitResult;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PHP Unit Plugin - Allows PHP Unit testing.
|
* PHP Unit Plugin - A rewrite of the original PHP Unit plugin
|
||||||
* @author Dan Cryer <dan@block8.co.uk>
|
*
|
||||||
* @package PHPCI
|
* @author Dan Cryer <dan@block8.co.uk>
|
||||||
* @subpackage Plugins
|
* @author Pablo Tejada <pablo@ptejada.com>
|
||||||
*/
|
* @package PHPCI
|
||||||
|
* @subpackage Plugins
|
||||||
|
*/
|
||||||
class PhpUnit implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin
|
class PhpUnit implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin
|
||||||
{
|
{
|
||||||
protected $args;
|
|
||||||
protected $phpci;
|
protected $phpci;
|
||||||
protected $build;
|
protected $build;
|
||||||
|
|
||||||
|
/** @var string[] Raw options from the PHPCI config file */
|
||||||
|
protected $options = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string|string[] $directory The directory (or array of dirs) to run PHPUnit on
|
* Standard Constructor
|
||||||
|
* $options['config'] Path to a PHPUnit XML configuration file.
|
||||||
|
* $options['run_from'] The directory where the phpunit command will run from when using 'config'.
|
||||||
|
* $options['coverage'] Value for the --coverage-html command line flag.
|
||||||
|
* $options['directory'] Optional directory or list of directories to run PHPUnit on.
|
||||||
|
* $options['args'] Command line args (in string format) to pass to PHP Unit
|
||||||
|
*
|
||||||
|
* @param Builder $phpci
|
||||||
|
* @param Build $build
|
||||||
|
* @param string[] $options
|
||||||
*/
|
*/
|
||||||
protected $directory;
|
public function __construct(Builder $phpci, Build $build, array $options = array())
|
||||||
|
{
|
||||||
|
$this->phpci = $phpci;
|
||||||
|
$this->build = $build;
|
||||||
|
$this->options = new PhpUnitOptions($options);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string $runFrom When running PHPUnit with an XML config, the command is run from this directory
|
* Check if the plugin can be executed without any configurations
|
||||||
*/
|
*
|
||||||
protected $runFrom;
|
* @param $stage
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string, in cases where tests files are in a sub path of the /tests path,
|
|
||||||
* allows this path to be set in the config.
|
|
||||||
*/
|
|
||||||
protected $path;
|
|
||||||
|
|
||||||
protected $coverage = "";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string|string[] $xmlConfigFile The path (or array of paths) of an xml config for PHPUnit
|
|
||||||
*/
|
|
||||||
protected $xmlConfigFile;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if this plugin can be executed.
|
|
||||||
* @param $stage
|
|
||||||
* @param Builder $builder
|
* @param Builder $builder
|
||||||
* @param Build $build
|
* @param Build $build
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function canExecute($stage, Builder $builder, Build $build)
|
public static function canExecute($stage, Builder $builder, Build $build)
|
||||||
{
|
{
|
||||||
if ($stage == 'test' && !is_null(self::findConfigFile($builder->buildPath))) {
|
if ($stage == 'test' && !is_null(PhpUnitOptions::findConfigFile($build->getBuildPath()))) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,183 +71,138 @@ class PhpUnit implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Try and find the phpunit XML config file.
|
* Runs PHP Unit tests in a specified directory, optionally using specified config file(s).
|
||||||
* @param $buildPath
|
|
||||||
* @return null|string
|
|
||||||
*/
|
*/
|
||||||
public static function findConfigFile($buildPath)
|
|
||||||
{
|
|
||||||
if (file_exists($buildPath . 'phpunit.xml')) {
|
|
||||||
return 'phpunit.xml';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file_exists($buildPath . 'tests' . DIRECTORY_SEPARATOR . 'phpunit.xml')) {
|
|
||||||
return 'tests' . DIRECTORY_SEPARATOR . 'phpunit.xml';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file_exists($buildPath . 'phpunit.xml.dist')) {
|
|
||||||
return 'phpunit.xml.dist';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file_exists($buildPath . 'tests/phpunit.xml.dist')) {
|
|
||||||
return 'tests' . DIRECTORY_SEPARATOR . 'phpunit.xml.dist';
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Standard Constructor
|
|
||||||
*
|
|
||||||
* $options['directory'] Output Directory. Default: %BUILDPATH%
|
|
||||||
* $options['filename'] Phar Filename. Default: build.phar
|
|
||||||
* $options['regexp'] Regular Expression Filename Capture. Default: /\.php$/
|
|
||||||
* $options['stub'] Stub Content. No Default Value
|
|
||||||
*
|
|
||||||
* @param Builder $phpci
|
|
||||||
* @param Build $build
|
|
||||||
* @param array $options
|
|
||||||
*/
|
|
||||||
public function __construct(Builder $phpci, Build $build, array $options = array())
|
|
||||||
{
|
|
||||||
$this->phpci = $phpci;
|
|
||||||
$this->build = $build;
|
|
||||||
|
|
||||||
if (empty($options['config']) && empty($options['directory'])) {
|
|
||||||
$this->xmlConfigFile = self::findConfigFile($phpci->buildPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($options['directory'])) {
|
|
||||||
$this->directory = $options['directory'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($options['config'])) {
|
|
||||||
$this->xmlConfigFile = $options['config'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($options['run_from'])) {
|
|
||||||
$this->runFrom = $options['run_from'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($options['args'])) {
|
|
||||||
$this->args = $this->phpci->interpolate($options['args']);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($options['path'])) {
|
|
||||||
$this->path = $options['path'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($options['coverage'])) {
|
|
||||||
$this->coverage = ' --coverage-html ' . $this->phpci->interpolate($options['coverage']) . ' ';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Runs PHP Unit tests in a specified directory, optionally using specified config file(s).
|
|
||||||
*/
|
|
||||||
public function execute()
|
public function execute()
|
||||||
{
|
{
|
||||||
if (empty($this->xmlConfigFile) && empty($this->directory)) {
|
$xmlConfigFiles = $this->options->getConfigFiles($this->build->getBuildPath());
|
||||||
$this->phpci->logFailure('Neither configuration file nor test directory found.');
|
$directories = $this->options->getDirectories();
|
||||||
|
if (empty($xmlConfigFiles) && empty($directories)) {
|
||||||
|
$this->phpci->logFailure(Lang::get('phpunit_fail_init'));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$success = true;
|
$success = array();
|
||||||
|
|
||||||
$this->phpci->logExecOutput(false);
|
// Run any directories
|
||||||
|
if (!empty($directories)) {
|
||||||
// Run any config files first. This can be either a single value or an array.
|
foreach ($directories as $directory) {
|
||||||
if ($this->xmlConfigFile !== null) {
|
$success[] = $this->runDir($directory);
|
||||||
$success &= $this->runConfigFile($this->xmlConfigFile);
|
}
|
||||||
|
} else {
|
||||||
|
// Run any config files
|
||||||
|
if (!empty($xmlConfigFiles)) {
|
||||||
|
foreach ($xmlConfigFiles as $configFile) {
|
||||||
|
$success[] = $this->runConfigFile($configFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run any dirs next. Again this can be either a single value or an array.
|
return !in_array(false, $success);
|
||||||
if ($this->directory !== null) {
|
}
|
||||||
$success &= $this->runDir($this->directory);
|
|
||||||
}
|
|
||||||
|
|
||||||
$tapString = $this->phpci->getLastOutput();
|
/**
|
||||||
$tapString = mb_convert_encoding($tapString, "UTF-8", "ISO-8859-1");
|
* Run the PHPUnit tests in a specific directory or array of directories.
|
||||||
|
*
|
||||||
|
* @param $directory
|
||||||
|
*
|
||||||
|
* @return bool|mixed
|
||||||
|
*/
|
||||||
|
protected function runDir($directory)
|
||||||
|
{
|
||||||
|
$options = clone $this->options;
|
||||||
|
|
||||||
try {
|
$buildPath = $this->build->getBuildPath() . DIRECTORY_SEPARATOR;
|
||||||
$tapParser = new TapParser($tapString);
|
|
||||||
$output = $tapParser->parse();
|
|
||||||
} catch (\Exception $ex) {
|
|
||||||
$this->phpci->logFailure($tapString);
|
|
||||||
throw $ex;
|
|
||||||
}
|
|
||||||
|
|
||||||
$failures = $tapParser->getTotalFailures();
|
$currentPath = getcwd();
|
||||||
|
// Change the directory
|
||||||
|
chdir($buildPath);
|
||||||
|
|
||||||
$this->build->storeMeta('phpunit-errors', $failures);
|
// Save the results into a json file
|
||||||
$this->build->storeMeta('phpunit-data', $output);
|
$jsonFile = tempnam(dirname($buildPath), 'jLog_');
|
||||||
|
$options->addArgument('log-json', $jsonFile);
|
||||||
|
|
||||||
$this->phpci->logExecOutput(true);
|
// Removes any current configurations files
|
||||||
|
$options->removeArgument('configuration');
|
||||||
|
|
||||||
|
$arguments = $this->phpci->interpolate($options->buildArgumentString());
|
||||||
|
$cmd = $this->phpci->findBinary('phpunit') . ' %s "%s"';
|
||||||
|
$success = $this->phpci->executeCommand($cmd, $arguments, $directory);
|
||||||
|
|
||||||
|
// Change to che original path
|
||||||
|
chdir($currentPath);
|
||||||
|
|
||||||
|
$this->processResults($jsonFile);
|
||||||
|
|
||||||
return $success;
|
return $success;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the tests defined in a PHPUnit config file.
|
* Run the tests defined in a PHPUnit config file.
|
||||||
* @param $configPath
|
*
|
||||||
|
* @param $configFile
|
||||||
|
*
|
||||||
* @return bool|mixed
|
* @return bool|mixed
|
||||||
*/
|
*/
|
||||||
protected function runConfigFile($configPath)
|
protected function runConfigFile($configFile)
|
||||||
{
|
{
|
||||||
if (is_array($configPath)) {
|
$options = clone $this->options;
|
||||||
return $this->recurseArg($configPath, array($this, "runConfigFile"));
|
$runFrom = $options->getRunFrom();
|
||||||
} else {
|
|
||||||
if ($this->runFrom) {
|
|
||||||
$curdir = getcwd();
|
|
||||||
chdir($this->phpci->buildPath . DIRECTORY_SEPARATOR . $this->runFrom);
|
|
||||||
}
|
|
||||||
|
|
||||||
$phpunit = $this->phpci->findBinary('phpunit');
|
$buildPath = $this->build->getBuildPath() . DIRECTORY_SEPARATOR;
|
||||||
|
if ($runFrom) {
|
||||||
$cmd = $phpunit . ' --tap %s -c "%s" ' . $this->coverage . $this->path;
|
$originalPath = getcwd();
|
||||||
$success = $this->phpci->executeCommand($cmd, $this->args, $this->phpci->buildPath . $configPath);
|
// Change the directory
|
||||||
|
chdir($buildPath . $runFrom);
|
||||||
if ($this->runFrom) {
|
|
||||||
chdir($curdir);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $success;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Save the results into a json file
|
||||||
* Run the PHPUnit tests in a specific directory or array of directories.
|
$jsonFile = tempnam($this->phpci->buildPath, 'jLog_');
|
||||||
* @param $directory
|
$options->addArgument('log-json', $jsonFile);
|
||||||
* @return bool|mixed
|
|
||||||
*/
|
|
||||||
protected function runDir($directory)
|
|
||||||
{
|
|
||||||
if (is_array($directory)) {
|
|
||||||
return $this->recurseArg($directory, array($this, "runDir"));
|
|
||||||
} else {
|
|
||||||
$curdir = getcwd();
|
|
||||||
chdir($this->phpci->buildPath);
|
|
||||||
|
|
||||||
$phpunit = $this->phpci->findBinary('phpunit');
|
// Removes any current configurations files
|
||||||
|
$options->removeArgument('configuration');
|
||||||
|
// Only the add the configuration file been passed
|
||||||
|
$options->addArgument('configuration', $buildPath . $configFile);
|
||||||
|
|
||||||
$cmd = $phpunit . ' --tap %s "%s"';
|
$arguments = $this->phpci->interpolate($options->buildArgumentString());
|
||||||
$success = $this->phpci->executeCommand($cmd, $this->args, $this->phpci->buildPath . $directory);
|
$cmd = $this->phpci->findBinary('phpunit') . ' %s %s';
|
||||||
chdir($curdir);
|
$success = $this->phpci->executeCommand($cmd, $arguments, $options->getTestsPath());
|
||||||
return $success;
|
|
||||||
|
if (!empty($originalPath)) {
|
||||||
|
// Change to che original path
|
||||||
|
chdir($originalPath);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
$this->processResults($jsonFile);
|
||||||
* @param $array
|
|
||||||
* @param $callable
|
|
||||||
* @return bool|mixed
|
|
||||||
*/
|
|
||||||
protected function recurseArg($array, $callable)
|
|
||||||
{
|
|
||||||
$success = true;
|
|
||||||
foreach ($array as $subItem) {
|
|
||||||
$success &= call_user_func($callable, $subItem);
|
|
||||||
}
|
|
||||||
return $success;
|
return $success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the test results
|
||||||
|
*
|
||||||
|
* @param string $jsonFile
|
||||||
|
*
|
||||||
|
* @throws \Exception If the failed to parse the JSON file
|
||||||
|
*/
|
||||||
|
protected function processResults($jsonFile)
|
||||||
|
{
|
||||||
|
if (is_file($jsonFile)) {
|
||||||
|
$parser = new PhpUnitResult($jsonFile, $this->build->getBuildPath());
|
||||||
|
|
||||||
|
$this->build->storeMeta('phpunit-data', $parser->parse()->getResults());
|
||||||
|
$this->build->storeMeta('phpunit-errors', $parser->getFailures());
|
||||||
|
|
||||||
|
foreach ($parser->getErrors() as $error) {
|
||||||
|
$severity = $error['severity'] == $parser::SEVERITY_ERROR ? BuildError::SEVERITY_CRITICAL : BuildError::SEVERITY_HIGH;
|
||||||
|
$this->build->reportError(
|
||||||
|
$this->phpci, 'php_unit', $error['message'], $severity, $error['file'], $error['line']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw new \Exception('JSON output file does not exist: ' . $jsonFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,207 +0,0 @@
|
||||||
<?php
|
|
||||||
/**
|
|
||||||
* PHPCI - Continuous Integration for PHP
|
|
||||||
*
|
|
||||||
* @copyright Copyright 2014, Block 8 Limited.
|
|
||||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
||||||
* @link https://www.phptesting.org/
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace PHPCI\Plugin;
|
|
||||||
|
|
||||||
use PHPCI;
|
|
||||||
use PHPCI\Builder;
|
|
||||||
use PHPCI\Helper\Lang;
|
|
||||||
use PHPCI\Model\Build;
|
|
||||||
use PHPCI\Model\BuildError;
|
|
||||||
use PHPCI\Plugin\Option\PhpUnitOptions;
|
|
||||||
use PHPCI\Plugin\Util\PhpUnitResult;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* PHP Unit Plugin V2 - Extends the functionality of the original PHP Unit plugin
|
|
||||||
*
|
|
||||||
* @author Pablo Tejada <pablo@ptejada.com>
|
|
||||||
* @package PHPCI
|
|
||||||
* @subpackage Plugins
|
|
||||||
*/
|
|
||||||
class PhpUnitV2 implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin
|
|
||||||
{
|
|
||||||
protected $phpci;
|
|
||||||
protected $build;
|
|
||||||
|
|
||||||
/** @var string[] Raw options from the PHPCI config file */
|
|
||||||
protected $options = array();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Standard Constructor
|
|
||||||
* $options['config'] Path to a PHPUnit XML configuration file.
|
|
||||||
* $options['run_from'] The directory where the phpunit command will run from when using 'config'.
|
|
||||||
* $options['coverage'] Value for the --coverage-html command line flag.
|
|
||||||
* $options['directory'] Optional directory or list of directories to run PHPUnit on.
|
|
||||||
* $options['args'] Command line args (in string format) to pass to PHP Unit
|
|
||||||
*
|
|
||||||
* @param Builder $phpci
|
|
||||||
* @param Build $build
|
|
||||||
* @param string[] $options
|
|
||||||
*/
|
|
||||||
public function __construct(Builder $phpci, Build $build, array $options = array())
|
|
||||||
{
|
|
||||||
$this->phpci = $phpci;
|
|
||||||
$this->build = $build;
|
|
||||||
$this->options = new PhpUnitOptions($options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the plugin can be executed without any configurations
|
|
||||||
*
|
|
||||||
* @param $stage
|
|
||||||
* @param Builder $builder
|
|
||||||
* @param Build $build
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function canExecute($stage, Builder $builder, Build $build)
|
|
||||||
{
|
|
||||||
if ($stage == 'test' && !is_null(PhpUnitOptions::findConfigFile($build->getBuildPath()))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Runs PHP Unit tests in a specified directory, optionally using specified config file(s).
|
|
||||||
*/
|
|
||||||
public function execute()
|
|
||||||
{
|
|
||||||
$xmlConfigFiles = $this->options->getConfigFiles($this->build->getBuildPath());
|
|
||||||
$directories = $this->options->getDirectories();
|
|
||||||
if (empty($xmlConfigFiles) && empty($directories)) {
|
|
||||||
$this->phpci->logFailure(Lang::get('phpunit_fail_init'));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$success = array();
|
|
||||||
|
|
||||||
// Run any directories
|
|
||||||
if (!empty($directories)) {
|
|
||||||
foreach ($directories as $directory) {
|
|
||||||
$success[] = $this->runDir($directory);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Run any config files
|
|
||||||
if (!empty($xmlConfigFiles)) {
|
|
||||||
foreach ($xmlConfigFiles as $configFile) {
|
|
||||||
$success[] = $this->runConfigFile($configFile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return !in_array(false, $success);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Run the PHPUnit tests in a specific directory or array of directories.
|
|
||||||
*
|
|
||||||
* @param $directory
|
|
||||||
*
|
|
||||||
* @return bool|mixed
|
|
||||||
*/
|
|
||||||
protected function runDir($directory)
|
|
||||||
{
|
|
||||||
$options = clone $this->options;
|
|
||||||
|
|
||||||
$buildPath = $this->build->getBuildPath() . DIRECTORY_SEPARATOR;
|
|
||||||
|
|
||||||
$currentPath = getcwd();
|
|
||||||
// Change the directory
|
|
||||||
chdir($buildPath);
|
|
||||||
|
|
||||||
// Save the results into a json file
|
|
||||||
$jsonFile = tempnam(dirname($buildPath), 'jLog_');
|
|
||||||
$options->addArgument('log-json', $jsonFile);
|
|
||||||
|
|
||||||
// Removes any current configurations files
|
|
||||||
$options->removeArgument('configuration');
|
|
||||||
|
|
||||||
$arguments = $this->phpci->interpolate($options->buildArgumentString());
|
|
||||||
$cmd = $this->phpci->findBinary('phpunit') . ' %s "%s"';
|
|
||||||
$success = $this->phpci->executeCommand($cmd, $arguments, $directory);
|
|
||||||
|
|
||||||
// Change to che original path
|
|
||||||
chdir($currentPath);
|
|
||||||
|
|
||||||
$this->processResults($jsonFile);
|
|
||||||
|
|
||||||
return $success;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Run the tests defined in a PHPUnit config file.
|
|
||||||
*
|
|
||||||
* @param $configFile
|
|
||||||
*
|
|
||||||
* @return bool|mixed
|
|
||||||
*/
|
|
||||||
protected function runConfigFile($configFile)
|
|
||||||
{
|
|
||||||
$options = clone $this->options;
|
|
||||||
$runFrom = $options->getRunFrom();
|
|
||||||
|
|
||||||
$buildPath = $this->build->getBuildPath() . DIRECTORY_SEPARATOR;
|
|
||||||
if ($runFrom) {
|
|
||||||
$originalPath = getcwd();
|
|
||||||
// Change the directory
|
|
||||||
chdir($buildPath . $runFrom);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the results into a json file
|
|
||||||
$jsonFile = tempnam($this->phpci->buildPath, 'jLog_');
|
|
||||||
$options->addArgument('log-json', $jsonFile);
|
|
||||||
|
|
||||||
// Removes any current configurations files
|
|
||||||
$options->removeArgument('configuration');
|
|
||||||
// Only the add the configuration file been passed
|
|
||||||
$options->addArgument('configuration', $buildPath . $configFile);
|
|
||||||
|
|
||||||
$arguments = $this->phpci->interpolate($options->buildArgumentString());
|
|
||||||
$cmd = $this->phpci->findBinary('phpunit') . ' %s %s';
|
|
||||||
$success = $this->phpci->executeCommand($cmd, $arguments, $options->getTestsPath());
|
|
||||||
|
|
||||||
if (!empty($originalPath)) {
|
|
||||||
// Change to che original path
|
|
||||||
chdir($originalPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->processResults($jsonFile);
|
|
||||||
|
|
||||||
return $success;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves the test results
|
|
||||||
*
|
|
||||||
* @param string $jsonFile
|
|
||||||
*
|
|
||||||
* @throws \Exception If the failed to parse the JSON file
|
|
||||||
*/
|
|
||||||
protected function processResults($jsonFile)
|
|
||||||
{
|
|
||||||
if (is_file($jsonFile)) {
|
|
||||||
$parser = new PhpUnitResult($jsonFile, $this->build->getBuildPath());
|
|
||||||
|
|
||||||
$this->build->storeMeta('phpunit-data', $parser->parse()->getResults());
|
|
||||||
$this->build->storeMeta('phpunit-errors', $parser->getFailures());
|
|
||||||
|
|
||||||
foreach ($parser->getErrors() as $error) {
|
|
||||||
$severity = $error['severity'] == $parser::SEVERITY_ERROR ? BuildError::SEVERITY_CRITICAL : BuildError::SEVERITY_HIGH;
|
|
||||||
$this->build->reportError(
|
|
||||||
$this->phpci, 'php_unit', $error['message'], $severity, $error['file'], $error['line']
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
throw new \Exception('JSON output file does not exist: ' . $jsonFile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,11 +11,11 @@
|
||||||
namespace Tests\PHPCI\Plugin;
|
namespace Tests\PHPCI\Plugin;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for the PHPUnit V2 plugin.
|
* Unit test for the PHPUnit plugin.
|
||||||
*
|
*
|
||||||
* @author Pablo Tejada <pablo@ptejada.com>
|
* @author Pablo Tejada <pablo@ptejada.com>
|
||||||
*/
|
*/
|
||||||
class PhpUnitV2Test extends \PHPUnit_Framework_TestCase
|
class PhpUnitTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testSingleConfigFile()
|
public function testSingleConfigFile()
|
||||||
{
|
{
|
Loading…
Reference in a new issue