Skip to content

Commit

Permalink
Reduce duplication via UsesFlexibleContext trait
Browse files Browse the repository at this point in the history
Problem:
Each context that wishes to access the FlexibleContext instance needs to implement gathersContexts. This causes duplication in the codebase, and even more so in projects that leverage Flexible Mink.

Solution:
I added a trait called UsesFlexibleContext that gathers the FlexibleContext instance and makes it available via the $this->flexibleContext property.
  • Loading branch information
Chekote committed Nov 12, 2017
1 parent d1af245 commit bd771fb
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 166 deletions.
10 changes: 2 additions & 8 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Behat\Mink\Exception\ExpectationException;
use features\Extensions\Assertion\AssertionContext;
use Medology\Behat\GathersContexts;
use Medology\Behat\Mink\FlexibleContext;
use Medology\Behat\Mink\UsesFlexibleContext;
use Medology\Behat\Mink\WebDownloadContext;
use Medology\Behat\TypeCaster;
use Medology\Behat\UsesStoreContext;
Expand All @@ -19,11 +19,9 @@ class FeatureContext implements Context, GathersContexts
// Depends
use AssertionContext;
use TypeCaster;
use UsesFlexibleContext;
use UsesStoreContext;

/** @var FlexibleContext */
protected $flexibleContext;

/** @var WebDownloadContext */
protected $webDownloadContext;

Expand All @@ -41,10 +39,6 @@ public function gatherContexts(BeforeScenarioScope $scope)
);
}

if (!$this->flexibleContext = $environment->getContext(FlexibleContext::class)) {
throw new RuntimeException('Failed to gather FlexibleContext');
}

if (!$this->webDownloadContext = $environment->getContext(WebDownloadContext::class)) {
throw new RuntimeException('Failed to gather WebDownloadContext');
}
Expand Down
30 changes: 3 additions & 27 deletions src/Medology/Behat/CsvContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,18 @@
namespace Medology\Behat;

use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Exception;
use Medology\Behat\Mink\FlexibleContext;
use RuntimeException;
use Medology\Behat\Mink\UsesFlexibleContext;

/**
* Provides functionality for working with CSV data.
*/
class CsvContext implements Context, GathersContexts
class CsvContext implements Context
{
use UsesFlexibleContext;
use UsesStoreContext;

/** @var FlexibleContext */
protected $flexibleContext;

/**
* {@inheritdoc}
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();

if (!($environment instanceof InitializedContextEnvironment)) {
throw new RuntimeException(
'Expected Environment to be ' . InitializedContextEnvironment::class .
', but got ' . get_class($environment)
);
}

if (!$this->flexibleContext = $environment->getContext(FlexibleContext::class)) {
throw new RuntimeException('Failed to gather FlexibleContext');
}
}

/**
* Ensures that the given variable in the store is a CSV containing the given rows.
* The given rows do not have to contain all columns in the store, but the CSV in the store
Expand Down
28 changes: 2 additions & 26 deletions src/Medology/Behat/Mink/AlertContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,20 @@
namespace Medology\Behat\Mink;

use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Exception\ExpectationException;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Medology\Behat\GathersContexts;
use Medology\Spinner;
use RuntimeException;
use WebDriver\Exception\NoAlertOpenError;

/**
* A context for handling JavaScript alerts. Based on a gist by Benjamin Lazarecki with improvements.
*
* @link https://gist.github.com/blazarecki/2888851
*/
class AlertContext implements Context, GathersContexts
class AlertContext implements Context
{
/** @var FlexibleContext */
protected $flexibleContext;

/**
* {@inheritdoc}
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();

if (!($environment instanceof InitializedContextEnvironment)) {
throw new RuntimeException(
'Expected Environment to be ' . InitializedContextEnvironment::class .
', but got ' . get_class($environment)
);
}

if (!$this->flexibleContext = $environment->getContext(FlexibleContext::class)) {
throw new RuntimeException('Failed to gather FlexibleContext');
}
}
use UsesFlexibleContext;

/**
* Clears out any alerts or prompts that may be open.
Expand Down
28 changes: 2 additions & 26 deletions src/Medology/Behat/Mink/JavaScriptContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,15 @@
namespace Medology\Behat\Mink;

use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ExpectationException;
use Medology\Behat\GathersContexts;
use RuntimeException;

/**
* {@inheritdoc}
*/
class JavaScriptContext implements Context, GathersContexts
class JavaScriptContext implements Context
{
/** @var FlexibleContext */
protected $flexibleContext;

/**
* {@inheritdoc}
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();

if (!($environment instanceof InitializedContextEnvironment)) {
throw new RuntimeException(
'Expected Environment to be ' . InitializedContextEnvironment::class .
', but got ' . get_class($environment)
);
}

if (!$this->flexibleContext = $environment->getContext(FlexibleContext::class)) {
throw new RuntimeException('Failed to gather FlexibleContext');
}
}
use UsesFlexibleContext;

/**
* Determines if a javascript variable is set and has a value.
Expand Down
28 changes: 2 additions & 26 deletions src/Medology/Behat/Mink/ScreenShotContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,18 @@
namespace Medology\Behat\Mink;

use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Behat\Testwork\Tester\Result\TestResult;
use Medology\Behat\GathersContexts;
use RuntimeException;

/**
* Context for capturing screenshots of the web browser.
*
* Note: Only works with Mink drivers that support the getScreenshot() method.
*/
class ScreenShotContext implements Context, GathersContexts
class ScreenShotContext implements Context
{
/** @var FlexibleContext */
protected $flexibleContext;

/**
* {@inheritdoc}
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();

if (!($environment instanceof InitializedContextEnvironment)) {
throw new RuntimeException(
'Expected Environment to be ' . InitializedContextEnvironment::class .
', but got ' . get_class($environment)
);
}

if (!$this->flexibleContext = $environment->getContext(FlexibleContext::class)) {
throw new RuntimeException('Failed to gather FlexibleContext');
}
}
use UsesFlexibleContext;

/**
* Captures a screenshot and saves it to the artifacts directory.
Expand Down
28 changes: 2 additions & 26 deletions src/Medology/Behat/Mink/TableContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,23 @@
namespace Medology\Behat\Mink;

use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Exception\ExpectationException;
use InvalidArgumentException;
use Medology\Behat\GathersContexts;
use Medology\Behat\UsesStoreContext;
use Medology\Spinner;
use RuntimeException;

/**
* Class TableContext.
*/
class TableContext implements Context, GathersContexts
class TableContext implements Context
{
use UsesFlexibleContext;
use UsesStoreContext;

/** @var FlexibleContext */
protected $flexibleContext;

/**
* {@inheritdoc}
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();

if (!($environment instanceof InitializedContextEnvironment)) {
throw new RuntimeException(
'Expected Environment to be ' . InitializedContextEnvironment::class .
', but got ' . get_class($environment)
);
}

if (!$this->flexibleContext = $environment->getContext(FlexibleContext::class)) {
throw new RuntimeException('Failed to gather FlexibleContext');
}
}

/**
* Finds a table with a given data-qa-id, name, or id. data-qa-id is given preference and matched exactly, while
* name and id are matched partially.
Expand Down
38 changes: 38 additions & 0 deletions src/Medology/Behat/Mink/UsesFlexibleContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php namespace Medology\Behat\Mink;

use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use RuntimeException;

/**
* Trait that grants access to the FlexibleContext instance via $this->flexibleContext.
*/
trait UsesFlexibleContext
{
/** @var FlexibleContext */
protected $flexibleContext;

/**
* Gathers the FlexibleContext instance and stores a reference in $this->flexibleContext.
*
* @param BeforeScenarioScope $scope
* @throws RuntimeException If the current environment is not initialized.
* @return void
* @BeforeScenario
*/
public function gatherFlexibleContext(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();

if (!($environment instanceof InitializedContextEnvironment)) {
throw new RuntimeException(
'Expected Environment to be ' . InitializedContextEnvironment::class .
', but got ' . get_class($environment)
);
}

if (!$this->flexibleContext = $environment->getContext(FlexibleContext::class)) {
throw new RuntimeException('Failed to gather FlexibleContext');
}
}
}
29 changes: 2 additions & 27 deletions src/Medology/Behat/Mink/WebDownloadContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,22 @@
namespace Medology\Behat\Mink;

use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Exception\ExpectationException;
use Exception;
use Medology\Behat\GathersContexts;
use Medology\Behat\UsesStoreContext;
use Medology\Spinner;
use RuntimeException;

/**
* {@inheritdoc}
*/
class WebDownloadContext implements Context, GathersContexts
class WebDownloadContext implements Context
{
use UsesFlexibleContext;
use UsesStoreContext;

protected static $baseUrlRegExp = '/^((http(s|):[\/]{2}|)([a-zA-Z]+\.|)[a-zA-Z0-9]+\.[a-zA-Z]+(\:[\d]+|)|[a-zA-Z0-9]+)/';

/** @var FlexibleContext */
protected $flexibleContext;

/**
* {@inheritdoc}
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();

if (!($environment instanceof InitializedContextEnvironment)) {
throw new RuntimeException(
'Expected Environment to be ' . InitializedContextEnvironment::class .
', but got ' . get_class($environment)
);
}

if (!$this->flexibleContext = $environment->getContext(FlexibleContext::class)) {
throw new RuntimeException('Failed to gather FlexibleContext');
}
}

/**
* Downloads the file references by the link and stores the content under the given key in the store.
*
Expand Down

0 comments on commit bd771fb

Please sign in to comment.