Skip to content

Commit

Permalink
Merge pull request #9 from etki/master
Browse files Browse the repository at this point in the history
Update readme.md and simplified internal code
  • Loading branch information
robot-bucket committed Feb 1, 2015
2 parents 058d0db + b4a8e89 commit b2e3b80
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 22 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,35 @@ In order to use this adapter you need to add a new dependency to your **composer
}
}
```
To enable this adapter in Codeception tests simply enabled it in extensions section of **codeception.yml**:
To enable this adapter in Codeception tests simply put it in "enabled" extensions section of **codeception.yml**:
```yaml
extensions:
enabled: [Yandex\Allure\Adapter\AllureAdapter]
enabled:
- Yandex\Allure\Adapter\AllureAdapter
config:
Yandex\Allure\Adapter\AllureAdapter:
deletePreviousResults: false
outputDirectory: allure-results
```
`deletePreviousResults` will clear all `.xml` files from output directory (this
behavior may change to complete cleanup later). It is set to `false` by default.

`outputDirectory` is used to store Allure results and will be calculated
relatively to Codeception output directory (also known as `paths: log` in
codeception.yml) unless you specify an absolute path. You can traverse up using
`..` as usual. `outputDirectory` defaults to `allure-results`.

To generate report from your favourite terminal,
[install](https://github.com/allure-framework/allure-cli#installation)
allure-cli and run following command (assuming you're in project root and using
default configuration):

```bash
allure generate --report-version 1.4.5 --report-path tests/_output/allure-report -- tests/_output/allure-results
```

Report will be generated in `tests/_output/allure-report`.

## Main features
See respective [PHPUnit](https://github.com/allure-framework/allure-phpunit#advanced-features) section.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"require": {
"php": ">=5.4.0",
"codeception/codeception": "~2.0.0",
"allure-framework/allure-php-api": "~1.1.0"
"allure-framework/allure-php-api": "~1.1.0",
"symfony/filesystem": "~2.6",
"symfony/finder": "~2.6"
},
"autoload": {
"psr-0": {
Expand Down
108 changes: 89 additions & 19 deletions src/Yandex/Allure/Adapter/AllureAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Codeception\Event\TestEvent;
use Codeception\Events;
use Codeception\Platform\Extension;
use Codeception\Exception\Configuration as ConfigurationException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
use Yandex\Allure\Adapter\Annotation;
use Yandex\Allure\Adapter\Event\StepFailedEvent;
use Yandex\Allure\Adapter\Event\StepFinishedEvent;
Expand All @@ -25,10 +28,10 @@
const OUTPUT_DIRECTORY_PARAMETER = 'outputDirectory';
const DELETE_PREVIOUS_RESULTS_PARAMETER = 'deletePreviousResults';
const DEFAULT_RESULTS_DIRECTORY = 'allure-results';
const DEFAULT_REPORT_DIRECTORY = 'allure-report';

class AllureAdapter extends Extension
{

//NOTE: here we implicitly assume that PHP runs in single-threaded mode
private $uuid;

Expand All @@ -55,30 +58,97 @@ public function _initialize()
{
parent::_initialize();
Annotation\AnnotationProvider::registerAnnotationNamespaces();
$outputDirectory = (isset($this->config[OUTPUT_DIRECTORY_PARAMETER])) ?
$this->config[OUTPUT_DIRECTORY_PARAMETER] : self::getDefaultOutputDirectory();
$deletePreviousResults = (isset($this->config[DELETE_PREVIOUS_RESULTS_PARAMETER])) ?
$this->config[DELETE_PREVIOUS_RESULTS_PARAMETER] : false;
if (!file_exists($outputDirectory)) {
mkdir($outputDirectory, 0755, true);
}
if ($deletePreviousResults) {
$files = glob($outputDirectory . DIRECTORY_SEPARATOR . '{,.}*', GLOB_BRACE);
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
}
$outputDirectory = $this->getOutputDirectory();
$deletePreviousResults =
$this->tryGetOption(DELETE_PREVIOUS_RESULTS_PARAMETER, false);
$this->prepareOutputDirectory($outputDirectory, $deletePreviousResults);
if (is_null(Model\Provider::getOutputDirectory())) {
Model\Provider::setOutputDirectory($outputDirectory);
}
}

private static function getDefaultOutputDirectory()
/**
* Retrieves option or returns default value.
*
* @param string $optionKey Configuration option key.
* @param mixed $defaultValue Value to return in case option isn't set.
*
* @return mixed Option value.
* @since 0.1.0
*/
private function tryGetOption($optionKey, $defaultValue = null)
{
if (array_key_exists($optionKey, $this->config)) {
return $this->config[$optionKey];
}
return $defaultValue;
}

/** @noinspection PhpUnusedPrivateMethodInspection */
/**
* Retrieves option or dies.
*
* @param string $optionKey Configuration option key.
*
* @throws ConfigurationException Thrown if option can't be retrieved.
*
* @return mixed Option value.
* @since 0.1.0
*/
private function getOption($optionKey)
{
if (!array_key_exists($optionKey, $this->config)) {
$template = '%s: Couldn\'t find required configuration option `%s`';
$message = sprintf($template, __CLASS__, $optionKey);
throw new ConfigurationException($message);
}
return $this->config[$optionKey];
}

/**
* Returns output directory.
*
* @throws ConfigurationException Thrown if there is Codeception-wide
* problem with output directory
* configuration.
*
* @return string Absolute path to output directory.
* @since 0.1.0
*/
private function getOutputDirectory()
{
// outputDir return value already contains directory separator
return Configuration::outputDir() . DEFAULT_RESULTS_DIRECTORY;
$outputDirectory = $this->tryGetOption(
OUTPUT_DIRECTORY_PARAMETER,
DEFAULT_RESULTS_DIRECTORY
);
$filesystem = new Filesystem;
if (!$filesystem->isAbsolutePath($outputDirectory)) {
$outputDirectory = Configuration::outputDir() . $outputDirectory;
}
return $outputDirectory;
}

/**
* Creates output directory (if it hasn't been created yet) and cleans it
* up (if corresponding argument has been set to true).
*
* @param string $outputDirectory
* @param bool $deletePreviousResults Whether to delete previous results
* or keep 'em.
*
* @since 0.1.0
*/
private function prepareOutputDirectory(
$outputDirectory,
$deletePreviousResults = false
) {
$filesystem = new Filesystem;
$filesystem->mkdir($outputDirectory, 0775);
if ($deletePreviousResults) {
$finder = new Finder;
$files = $finder->files()->in($outputDirectory)->name('*.xml');
$filesystem->remove($files);
}
}

public function suiteBefore(SuiteEvent $suiteEvent)
Expand Down

0 comments on commit b2e3b80

Please sign in to comment.