Skip to content

Commit

Permalink
Refactored extension namespacing and added support for BEHAT_SCREENSH…
Browse files Browse the repository at this point in the history
…OT_DIR variable.

* Updated config requirements.

* Refactored extension classes to use simplified loading.

* Added dist behat config.

* Added support for BEHAT_SCREENSHOT_DIR env variable.
  • Loading branch information
AlexSkrypnyk authored Jun 28, 2017
1 parent 4714bd2 commit 5af005a
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 108 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ default:
suites:
default:
contexts:
- IntegratedExperts\Behat\Screenshot\Context\ScreenshotContext
- IntegratedExperts\BehatScreenshotExtension\Context\ScreenshotContext
- FeatureContext
extensions:
IntegratedExperts\Behat\Screenshot\ScreenshotExtension:
IntegratedExperts\BehatScreenshotExtension:
dir: %paths.base%/screenshots
fail: true
purge: false
Expand Down
6 changes: 3 additions & 3 deletions behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ default:
- IntegratedExperts\BehatPhpServer\PhpServerContext:
-
docroot: %paths.base%/tests/behat/features/fixtures
- IntegratedExperts\Behat\Screenshot\Context\ScreenshotContext
- IntegratedExperts\BehatScreenshotExtension\Context\ScreenshotContext
- FeatureContext:
-
screenshot_dir: %paths.base%/screenshots
Expand All @@ -17,7 +17,7 @@ default:
files_path: %paths.base%/tests/behat/features/fixtures
selenium2: ~
browser_name: chrome
IntegratedExperts\Behat\Screenshot\ScreenshotExtension:
IntegratedExperts\BehatScreenshotExtension:
dir: %paths.base%/screenshots
fail: false
purge: true
purge: true
11 changes: 11 additions & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
default:
suites:
default:
contexts:
- FeatureContext
- IntegratedExperts\BehatScreenshotExtension\Context\ScreenshotContext
extensions:
IntegratedExperts\BehatScreenshotExtension:
dir: %paths.base%/screenshots
fail: true
purge: false
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"autoload": {
"psr-0": {
"IntegratedExperts\\Behat\\Screenshot": "src/"
"IntegratedExperts\\BehatScreenshot": "src/"
}
},
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
<rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
<exclude-pattern>./tests/*</exclude-pattern>
</rule>
<rule ref="Symfony2.NamingConventions.ValidClassName.InvalidInterfaceName">
<exclude-pattern>.*context.*</exclude-pattern>
</rule>

</ruleset>
83 changes: 0 additions & 83 deletions src/IntegratedExperts/Behat/Screenshot/ScreenshotExtension.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* This file is part of the IntegratedExperts\BehatScreenshot package.
*/

namespace IntegratedExperts\Behat\Screenshot\Context\Initializer;
namespace IntegratedExperts\BehatScreenshotExtension\Context\Initializer;

use Behat\Behat\Context\Context;
use Behat\Behat\Context\Initializer\ContextInitializer;
use IntegratedExperts\Behat\Screenshot\Context\ScreenshotContextInterface;
use IntegratedExperts\BehatScreenshotExtension\Context\ScreenshotAwareContext;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

Expand All @@ -23,28 +23,28 @@ class ScreenshotContextInitializer implements ContextInitializer
*
* @var string
*/
private $dir;
protected $dir;

/**
* Makes screenshot when fail.
*
* @var bool
*/
private $fail;
protected $fail;

/**
* Purge dir before start test.
*
* @var bool
*/
private $purge;
protected $purge;

/**
* Does need to clear directory trigger.
* Check if need to actually purge.
*
* @var bool
*/
private $toPurge;
protected $needsPurging;

/**
* ScreenshotContextInitializer constructor.
Expand All @@ -55,7 +55,7 @@ class ScreenshotContextInitializer implements ContextInitializer
*/
public function __construct($dir, $fail, $purge)
{
$this->toPurge = true;
$this->needsPurging = true;
$this->dir = $dir;
$this->fail = $fail;
$this->purge = $purge;
Expand All @@ -66,12 +66,12 @@ public function __construct($dir, $fail, $purge)
*/
public function initializeContext(Context $context)
{
if ($context instanceof ScreenshotContextInterface) {
$context->setParameters($this->dir, $this->fail);
// Calling clearing screenshot directory function.
if ($this->purge && $this->toPurge) {
if ($context instanceof ScreenshotAwareContext) {
$dir = $this->resolveDir();
$context->setScreenshotParameters($dir, $this->fail);
if ($this->purge && $this->needsPurging) {
$this->purgeFilesInDir();
$this->toPurge = false;
$this->needsPurging = false;
}
}
}
Expand All @@ -87,4 +87,17 @@ protected function purgeFilesInDir()
$fs->remove($finder->files()->in($this->dir));
}
}

/**
* Resolve directory using one of supported paths.
*/
protected function resolveDir()
{
$dir = getenv('BEHAT_SCREENSHOT_DIR');
if (!empty($dir)) {
return $dir;
}

return $this->dir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
* Behat context interface to enable Screenshot.
*/

namespace IntegratedExperts\Behat\Screenshot\Context;
namespace IntegratedExperts\BehatScreenshotExtension\Context;

use Behat\Behat\Context\Context;

/**
* Interface ScreenshotContext.
*/
interface ScreenshotContextInterface extends Context
interface ScreenshotAwareContext extends Context
{

/**
Expand All @@ -23,5 +23,5 @@ interface ScreenshotContextInterface extends Context
*
* @return $this
*/
public function setParameters($dir, $fail);
public function setScreenshotParameters($dir, $fail);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Behat context to enable Screenshot support in tests.
*/

namespace IntegratedExperts\Behat\Screenshot\Context;
namespace IntegratedExperts\BehatScreenshotExtension\Context;

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\AfterStepScope;
Expand All @@ -18,7 +18,7 @@
/**
* Class ScreenshotContext.
*/
class ScreenshotContext extends RawMinkContext implements SnippetAcceptingContext, ScreenshotContextInterface
class ScreenshotContext extends RawMinkContext implements SnippetAcceptingContext, ScreenshotAwareContext
{

/**
Expand Down Expand Up @@ -52,7 +52,7 @@ class ScreenshotContext extends RawMinkContext implements SnippetAcceptingContex
/**
* {@inheritdoc}
*/
public function setParameters($dir, $fail)
public function setScreenshotParameters($dir, $fail)
{
$this->dir = $dir;
$this->fail = $fail;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* @file
* Behat screenshot extension.
*/

namespace IntegratedExperts\BehatScreenshotExtension\ServiceContainer;

use Behat\Behat\Context\ServiceContainer\ContextExtension;
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
use Behat\Testwork\ServiceContainer\ExtensionManager;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

/**
* Class ScreenshotExtension
*/
class BehatScreenshotExtension implements ExtensionInterface
{

/**
* Extension configuration ID.
*/
const MOD_ID = 'integratedexperts_screenshot';

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
}

/**
* {@inheritdoc}
*/
public function getConfigKey()
{
return self::MOD_ID;
}

/**
* {@inheritdoc}
*/
public function initialize(ExtensionManager $extensionManager)
{
}

/**
* {@inheritdoc}
*/
public function configure(ArrayNodeDefinition $builder)
{
$builder->children()
->scalarNode('dir')->isRequired()->cannotBeEmpty()->end()
->scalarNode('fail')->isRequired()->cannotBeEmpty()->end()
->scalarNode('purge')->isRequired()->cannotBeEmpty()->end();
}

/**
* {@inheritdoc}
*/
public function load(ContainerBuilder $container, array $config)
{
$definition = new Definition('IntegratedExperts\BehatScreenshotExtension\Context\Initializer\ScreenshotContextInitializer', [
$config['dir'],
$config['fail'],
$config['purge'],
]);
$definition->addTag(ContextExtension::INITIALIZER_TAG, ['priority' => 0]);
$container->setDefinition('integratedexperts_screenshot.screenshot_context_initializer', $definition);
}
}

0 comments on commit 5af005a

Please sign in to comment.