phpci/PHPCI/Plugin/Codeception.php

165 lines
3.9 KiB
PHP
Raw Normal View History

<?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;
2015-02-04 15:21:21 +01:00
use PHPCI;
2013-10-10 02:01:06 +02:00
use PHPCI\Builder;
2014-12-04 16:48:52 +01:00
use PHPCI\Helper\Lang;
2013-10-10 02:01:06 +02:00
use PHPCI\Model\Build;
2015-02-04 15:21:21 +01:00
use PHPCI\Plugin\Util\TapParser;
2013-10-10 02:01:06 +02:00
/**
2015-02-04 15:21:21 +01:00
* Codeception Plugin - Enables full acceptance, unit, and functional testing
*
* @author Don Gilbert <don@dongilbert.net>
2015-02-04 15:21:21 +01:00
* @author Igor Timoshenko <contact@igortimoshenko.com>
* @package PHPCI
* @subpackage Plugins
*/
2015-02-04 15:21:21 +01:00
class Codeception implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin
{
2014-03-24 16:55:42 +01:00
/**
* @var string
*/
protected $args = '';
2015-02-04 15:21:21 +01:00
/**
* @var Build
*/
protected $build;
2014-03-24 16:55:42 +01:00
/**
* @var Builder
*/
protected $phpci;
2015-02-04 15:21:21 +01:00
/**
* @var string|string[] The path (or array of paths) of an yml config for Codeception
*/
protected $configFile;
2014-05-02 15:48:40 +02:00
/**
2015-02-04 15:21:21 +01:00
* @var string The path where the reports and logs are stored
*/
2015-02-04 15:21:21 +01:00
protected $logPath = 'tests/_output';
/**
* Set up the plugin, configure options, etc.
2015-02-04 15:21:21 +01:00
*
* @param Builder $phpci
* @param Build $build
* @param array $options
*/
2013-10-10 02:01:06 +02:00
public function __construct(Builder $phpci, Build $build, array $options = array())
{
2014-03-24 16:55:42 +01:00
$this->phpci = $phpci;
2014-05-02 15:48:40 +02:00
$this->build = $build;
if (isset($options['config'])) {
2015-02-04 15:21:21 +01:00
$this->configFile = $options['config'];
}
2015-02-04 15:21:21 +01:00
if (isset($options['args'])) {
2014-03-24 16:55:42 +01:00
$this->args = (string) $options['args'];
}
2015-02-04 15:21:21 +01:00
if (isset($options['log_path'])) {
$this->logPath = $options['log_path'];
}
}
/**
2015-02-04 15:21:21 +01:00
* {@inheritDoc}
*/
public function execute()
{
$success = true;
2015-02-04 15:21:21 +01:00
$this->phpci->logExecOutput(false);
// Run any config files first. This can be either a single value or an array
if ($this->configFile !== null) {
$success &= $this->runConfigFile($this->configFile);
}
2015-02-04 15:21:21 +01:00
$tapString = file_get_contents(
$this->phpci->buildPath . $this->logPath . DIRECTORY_SEPARATOR . 'report.tap.log'
);
try {
$tapParser = new TapParser($tapString);
$output = $tapParser->parse();
} catch (\Exception $ex) {
$this->phpci->logFailure($tapString);
throw $ex;
}
$failures = $tapParser->getTotalFailures();
$this->build->storeMeta('codeception-errors', $failures);
$this->build->storeMeta('codeception-data', $output);
$this->phpci->logExecOutput(true);
return $success;
}
/**
2015-02-04 15:21:21 +01:00
* {@inheritDoc}
*/
public static function canExecute($stage, Builder $builder, Build $build)
{
return $stage === 'test';
}
/**
* Run tests from a Codeception config file
*
* @param string $configPath
* @return bool|mixed
*/
protected function runConfigFile($configPath)
{
if (is_array($configPath)) {
2015-02-04 15:21:21 +01:00
return $this->recurseArg($configPath, array($this, 'runConfigFile'));
} else {
$codecept = $this->phpci->findBinary('codecept');
2015-02-04 15:21:21 +01:00
$cmd = 'cd "%s" && ' . $codecept . ' run -c "%s" --tap ' . $this->args;
2014-03-12 18:37:57 +01:00
if (IS_WIN) {
2015-02-04 15:21:21 +01:00
$cmd = 'cd /d "%s" && ' . $codecept . ' run -c "%s" --tap ' . $this->args;
2014-03-12 18:37:57 +01:00
}
$configPath = $this->phpci->buildPath . $configPath;
$success = $this->phpci->executeCommand($cmd, $this->phpci->buildPath, $configPath);
return $success;
}
}
/**
2015-02-04 15:21:21 +01:00
* @param array $array
* @param \Callback $callable
* @return bool|mixed
*/
2015-02-04 15:21:21 +01:00
protected function recurseArg(array $array, $callable)
{
$success = true;
2015-02-04 15:21:21 +01:00
foreach ($array as $subItem) {
$success &= call_user_func($callable, $subItem);
}
2015-02-04 15:21:21 +01:00
return $success;
}
2013-10-10 02:01:06 +02:00
}