Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
[Behat] gather contexts using annotions in the properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel committed Jun 1, 2016
1 parent d25b07f commit 1a4407e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 68 deletions.
18 changes: 6 additions & 12 deletions Features/Context/ContentActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,16 @@
class ContentActions extends PlatformUI
{
/**
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\ContentEditContext
* @Context $contentEditContext EzSystems\PlatformUIBundle\Features\Context\SubContext\ContentEditContext
*/
private $contentEditContext;
private $dashboardContext;
protected $contentEditContext;

/**
* @BeforeScenario
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$this->contentEditContext = $scope->getEnvironment()->getContext(
'EzSystems\PlatformUIBundle\Features\Context\SubContext\ContentEditContext'
);
$this->dashboardContext = $scope->getEnvironment()->getContext(
'EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext'
);
}
protected $dashboardContext;

/**
*/
Expand Down
18 changes: 6 additions & 12 deletions Features/Context/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,16 @@ class Fields extends PlatformUI
const NOTIFICATION_PUBLISH_ERROR = 'An error occured while publishing the draft';

/**
* @var EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext
* @Context $fieldtypeContext EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext
*/
private $fieldtypeContext;
private $dashboardContext;
protected $fieldtypeContext;

/**
* @BeforeScenario
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$this->fieldtypeContext = $scope->getEnvironment()->getContext(
'EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext'
);
$this->dashboardContext = $scope->getEnvironment()->getContext(
'EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext'
);
}
protected $dashboardContext;

protected function getFieldIdentCss($identifier, $contentId = '')
{
Expand Down
38 changes: 38 additions & 0 deletions Features/Context/PlatformUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
namespace EzSystems\PlatformUIBundle\Features\Context;

use ReflectionClass;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\MinkExtension\Context\RawMinkContext;

class PlatformUI extends RawMinkContext
Expand Down Expand Up @@ -97,6 +99,23 @@ public function __construct($uri = self::PLATFORM_URI, $user = null, $password =
}
}

/**
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$refClass = new ReflectionClass($this);
$refProperties = $refClass->getProperties();
foreach ($refProperties as $refProperty) {
preg_match_all('#@(.*?)\n#s', $refProperty->getDocComment(), $matches);
$contexts = $this->parseAnnotations($matches[1]);
foreach ($contexts as $property => $context) {
$this->$property = $scope->getEnvironment()->getContext($context);
}

}
}

/**
* @BeforeScenario
*/
Expand Down Expand Up @@ -448,11 +467,30 @@ protected function closeEditView()
} catch (\Exception $e) {
}
}

/**
* Returns an array with the properties contexts,
* if the properties use the Context Annotation.
*
* @return array array of methods and their service dependencies
*/
private function parseAnnotations($annotations)
{
// parse array from (numeric key => 'annotation <value>') to (annotation => value)
$propertiesContexts = [];
foreach ($annotations as $annotation) {
if (!preg_match('/^(\w+)\s+\$(\w+)\s+([\w\.\\\\]+)/', $annotation, $matches)) {
continue;
}

array_shift($matches);
$tag = array_shift($matches);
if ($tag == 'Context') {
list($property, $context) = $matches;
$propertiesContexts[$property] = $context;
}
}

return $propertiesContexts;
}
}
14 changes: 3 additions & 11 deletions Features/Context/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,10 @@
class Role extends PlatformUI
{
/**
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
*/
private $dashboardContext;

/**
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$this->dashboardContext = $scope->getEnvironment()->getContext(
'EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext'
);
}
protected $dashboardContext;

/**
* @Given I am on the Roles page
Expand Down
27 changes: 11 additions & 16 deletions Features/Context/SubContext/ContentEditContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,21 @@
class ContentEditContext extends PlatformUI
{
/**
* @Context $basicContentContext EzSystems\PlatformBehatBundle\Context\Object\BasicContentContext
*/
private $basicContentContext;
private $dashboardContext;
private $browserContext;
protected $basicContentContext;

/**
* @BeforeScenario
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$this->basicContentContext = $scope->getEnvironment()->getContext(
'EzSystems\PlatformBehatBundle\Context\Object\BasicContentContext'
);
$this->dashboardContext = $scope->getEnvironment()->getContext(
'EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext'
);
$this->browserContext = $scope->getEnvironment()->getContext(
'EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext'
);
}
protected $dashboardContext;

/**
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
* @Context $browserContext EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
*/
protected $browserContext;

/**
* @Given I create a content of content type :type with:
Expand Down
23 changes: 6 additions & 17 deletions Features/Context/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,16 @@
class Users extends PlatformUI
{
/**
* @var \EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
*/
private $dashboardContext;
protected $dashboardContext;

/**
* @var \EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
* @Context $browserContext EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
*/
private $browserContext;

/**
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$this->dashboardContext = $scope->getEnvironment()->getContext(
'EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext'
);
$this->browserContext = $scope->getEnvironment()->getContext(
'EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext'
);
}
protected $browserContext;

/**
* @Given I am on the Users page
Expand Down

0 comments on commit 1a4407e

Please sign in to comment.