2013-10-08 11:21:54 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Plugin;
|
2013-10-08 11:21:54 +02:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Builder;
|
|
|
|
use PHPCensor\Model\Build;
|
|
|
|
use PHPCensor\Plugin\Util\TestResultParsers\Codeception as Parser;
|
|
|
|
use PHPCensor\Plugin;
|
2017-01-08 17:00:32 +01:00
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser;
|
2017-01-13 16:35:41 +01:00
|
|
|
use PHPCensor\ZeroConfigPluginInterface;
|
2013-10-10 02:01:06 +02:00
|
|
|
|
2013-10-08 11:21:54 +02:00
|
|
|
/**
|
2014-09-08 12:45:19 +02:00
|
|
|
* Codeception Plugin - Enables full acceptance, unit, and functional testing.
|
2017-01-10 18:46:19 +01:00
|
|
|
*
|
2017-03-04 16:39:56 +01:00
|
|
|
* @author Don Gilbert <don@dongilbert.net>
|
|
|
|
* @author Igor Timoshenko <contact@igortimoshenko.com>
|
|
|
|
* @author Adam Cooper <adam@networkpie.co.uk>
|
2013-10-08 11:21:54 +02:00
|
|
|
*/
|
2017-01-13 16:35:41 +01:00
|
|
|
class Codeception extends Plugin implements ZeroConfigPluginInterface
|
2013-10-08 11:21:54 +02:00
|
|
|
{
|
2014-09-08 12:45:19 +02:00
|
|
|
/** @var string */
|
2014-03-24 16:55:42 +01:00
|
|
|
protected $args = '';
|
|
|
|
|
2015-02-04 15:21:21 +01:00
|
|
|
/**
|
2014-09-08 12:45:19 +02:00
|
|
|
* @var string $ymlConfigFile The path of a yml config for Codeception
|
2015-02-04 15:21:21 +01:00
|
|
|
*/
|
2014-09-08 12:45:19 +02:00
|
|
|
protected $ymlConfigFile;
|
2015-02-04 15:21:21 +01:00
|
|
|
|
2014-03-24 16:55:42 +01:00
|
|
|
/**
|
2017-01-21 05:18:31 +01:00
|
|
|
* @var array $path The path to the codeception tests folder.
|
2014-03-24 16:55:42 +01:00
|
|
|
*/
|
2017-01-21 05:18:31 +01:00
|
|
|
protected $path = [
|
|
|
|
'tests/_output',
|
|
|
|
'tests/_log',
|
|
|
|
];
|
2013-10-08 11:21:54 +02:00
|
|
|
|
2017-01-11 16:15:54 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function pluginName()
|
|
|
|
{
|
|
|
|
return 'codeception';
|
|
|
|
}
|
|
|
|
|
2016-07-11 18:00:04 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-07-22 09:05:34 +02:00
|
|
|
public function __construct(Builder $builder, Build $build, array $options = [])
|
2016-07-11 18:00:04 +02:00
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
parent::__construct($builder, $build, $options);
|
2016-07-11 18:00:04 +02:00
|
|
|
|
|
|
|
if (empty($options['config'])) {
|
2016-07-22 09:05:34 +02:00
|
|
|
$this->ymlConfigFile = self::findConfigFile($this->builder->buildPath);
|
2016-07-11 18:00:04 +02:00
|
|
|
} else {
|
|
|
|
$this->ymlConfigFile = $options['config'];
|
|
|
|
}
|
2017-02-09 16:03:53 +01:00
|
|
|
|
2016-07-11 18:00:04 +02:00
|
|
|
if (isset($options['args'])) {
|
|
|
|
$this->args = (string) $options['args'];
|
|
|
|
}
|
2017-02-09 16:03:53 +01:00
|
|
|
|
2017-02-09 15:06:05 +01:00
|
|
|
if (isset($options['path'])) {
|
2017-02-09 16:03:53 +01:00
|
|
|
array_unshift($this->path, $options['path']);
|
2017-02-09 15:06:05 +01:00
|
|
|
}
|
2016-07-11 18:00:04 +02:00
|
|
|
}
|
|
|
|
|
2015-02-04 15:21:21 +01:00
|
|
|
/**
|
2014-09-08 12:45:19 +02:00
|
|
|
* @param $stage
|
|
|
|
* @param Builder $builder
|
|
|
|
* @param Build $build
|
|
|
|
* @return bool
|
2015-02-04 15:21:21 +01:00
|
|
|
*/
|
2014-09-08 12:45:19 +02:00
|
|
|
public static function canExecute($stage, Builder $builder, Build $build)
|
|
|
|
{
|
2017-03-16 15:54:25 +01:00
|
|
|
return $stage == Build::STAGE_TEST && !is_null(self::findConfigFile($builder->buildPath));
|
2014-09-08 12:45:19 +02:00
|
|
|
}
|
2014-05-02 15:48:40 +02:00
|
|
|
|
2013-10-08 11:21:54 +02:00
|
|
|
/**
|
2014-09-08 12:45:19 +02:00
|
|
|
* Try and find the codeception YML config file.
|
|
|
|
* @param $buildPath
|
|
|
|
* @return null|string
|
2013-10-08 11:21:54 +02:00
|
|
|
*/
|
2014-09-08 12:45:19 +02:00
|
|
|
public static function findConfigFile($buildPath)
|
|
|
|
{
|
|
|
|
if (file_exists($buildPath . 'codeception.yml')) {
|
|
|
|
return 'codeception.yml';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($buildPath . 'codeception.dist.yml')) {
|
|
|
|
return 'codeception.dist.yml';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2013-10-08 11:21:54 +02:00
|
|
|
|
|
|
|
/**
|
2015-10-06 11:53:23 +02:00
|
|
|
* Runs Codeception tests
|
2013-10-08 11:21:54 +02:00
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
2014-09-08 12:45:19 +02:00
|
|
|
if (empty($this->ymlConfigFile)) {
|
2015-05-05 10:36:33 +02:00
|
|
|
throw new \Exception("No configuration file found");
|
2015-02-04 15:21:21 +01:00
|
|
|
}
|
|
|
|
|
2014-09-08 12:45:19 +02:00
|
|
|
// Run any config files first. This can be either a single value or an array.
|
2015-05-05 10:36:33 +02:00
|
|
|
return $this->runConfigFile($this->ymlConfigFile);
|
2013-10-08 11:21:54 +02:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
2014-09-08 12:45:19 +02:00
|
|
|
* Run tests from a Codeception config file.
|
|
|
|
* @param $configPath
|
2014-12-08 12:25:33 +01:00
|
|
|
* @return bool|mixed
|
2014-09-08 12:45:19 +02:00
|
|
|
* @throws \Exception
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2013-10-08 11:21:54 +02:00
|
|
|
protected function runConfigFile($configPath)
|
|
|
|
{
|
2017-07-19 05:26:51 +02:00
|
|
|
$codeception = $this->findBinary('codecept');
|
2015-02-04 15:21:21 +01:00
|
|
|
|
2016-07-11 18:00:04 +02:00
|
|
|
if (!$codeception) {
|
2017-02-07 13:05:44 +01:00
|
|
|
$this->builder->logFailure(sprintf('Could not find %s', 'codecept'));
|
2014-09-08 12:45:19 +02:00
|
|
|
|
2015-05-05 10:36:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-01 17:53:29 +02:00
|
|
|
|
2016-07-11 18:00:04 +02:00
|
|
|
$cmd = 'cd "%s" && ' . $codeception . ' run -c "%s" --xml ' . $this->args;
|
2015-10-02 22:53:39 +02:00
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$configPath = $this->builder->buildPath . $configPath;
|
|
|
|
$success = $this->builder->executeCommand($cmd, $this->builder->buildPath, $configPath);
|
2013-10-08 11:21:54 +02:00
|
|
|
|
2017-01-08 17:00:32 +01:00
|
|
|
$parser = new YamlParser();
|
|
|
|
$yaml = file_get_contents($configPath);
|
|
|
|
$config = (array)$parser->parse($yaml);
|
|
|
|
|
2017-01-21 05:18:31 +01:00
|
|
|
$outputPath = null;
|
2017-01-08 17:00:32 +01:00
|
|
|
if ($config && isset($config['paths']['log'])) {
|
2017-01-21 05:18:31 +01:00
|
|
|
$outputPath = $this->builder->buildPath . $config['paths']['log'] . '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_exists($outputPath . 'report.xml')) {
|
|
|
|
foreach ($this->path as $path) {
|
2017-02-09 16:03:53 +01:00
|
|
|
$outputPath = $this->builder->buildPath . rtrim($path, '/\\') . '/';
|
2017-01-21 05:18:31 +01:00
|
|
|
if (file_exists($outputPath . 'report.xml')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 17:00:32 +01:00
|
|
|
}
|
2015-05-22 14:53:44 +02:00
|
|
|
|
2018-05-27 08:33:27 +02:00
|
|
|
$parser = new Parser($this->builder, ($outputPath . 'report.xml'));
|
2015-05-05 10:36:33 +02:00
|
|
|
$output = $parser->parse();
|
2015-02-04 15:21:21 +01:00
|
|
|
|
2016-04-21 06:58:09 +02:00
|
|
|
$meta = [
|
2015-10-23 05:18:22 +02:00
|
|
|
'tests' => $parser->getTotalTests(),
|
2015-05-05 10:36:33 +02:00
|
|
|
'timetaken' => $parser->getTotalTimeTaken(),
|
2015-10-23 05:18:22 +02:00
|
|
|
'failures' => $parser->getTotalFailures()
|
2016-04-21 06:58:09 +02:00
|
|
|
];
|
2014-09-08 12:45:19 +02:00
|
|
|
|
2015-05-05 10:36:33 +02:00
|
|
|
$this->build->storeMeta('codeception-meta', $meta);
|
|
|
|
$this->build->storeMeta('codeception-data', $output);
|
|
|
|
$this->build->storeMeta('codeception-errors', $parser->getTotalFailures());
|
2014-09-08 12:45:19 +02:00
|
|
|
|
2015-05-05 10:36:33 +02:00
|
|
|
return $success;
|
2013-10-08 11:21:54 +02:00
|
|
|
}
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|