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 2bf44c5
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 78 deletions.
19 changes: 6 additions & 13 deletions Features/Context/ContentActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,21 @@
*/
namespace EzSystems\PlatformUIBundle\Features\Context;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use PHPUnit_Framework_Assert as Assertion;

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
19 changes: 6 additions & 13 deletions Features/Context/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
namespace EzSystems\PlatformUIBundle\Features\Context;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Mink\WebAssert;
use EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext as FieldType;

Expand All @@ -19,22 +18,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
37 changes: 37 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,22 @@ 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 +466,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;
}
}
15 changes: 3 additions & 12 deletions Features/Context/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,14 @@
namespace EzSystems\PlatformUIBundle\Features\Context;

use Behat\Gherkin\Node\TableNode;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;

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
6 changes: 3 additions & 3 deletions Features/Context/SubContext/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(Repository $repository, UserService $userService, Ro
}

/**
* Get credentials for a specific role
* Get credentials for a specific role.
*
* @uses \EzSystems\BehatBundle\Context\Object\User
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
Expand Down Expand Up @@ -85,8 +85,8 @@ protected function getCredentialsFor($roleIdentifier)
$this->roleService->assignRoleToUser($role, $user);

return array(
'login' => $username,
'password' => $password,
'login' => $username,
'password' => $password,
);
}

Expand Down
2 changes: 1 addition & 1 deletion Features/Context/SubContext/BrowserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function prepareHelpers()
}

/**
* Getter for Xpath
* Getter for Xpath.
*
* @return \EzSystems\BehatBundle\Helper\Xpath
*/
Expand Down
30 changes: 12 additions & 18 deletions Features/Context/SubContext/ContentEditContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,26 @@
namespace EzSystems\PlatformUIBundle\Features\Context\SubContext;

use PHPUnit_Framework_Assert as Assertion;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use EzSystems\PlatformUIBundle\Features\Context\PlatformUI;

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 All @@ -54,7 +48,7 @@ public function iCreateContentType($type, TableNode $fields)
}
}

/**
/**
* @Given I am on :name full view
*/
public function onFullView($name)
Expand Down
24 changes: 6 additions & 18 deletions Features/Context/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,21 @@
*/
namespace EzSystems\PlatformUIBundle\Features\Context;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;

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 2bf44c5

Please sign in to comment.