From 1a4407e74c07b88c4a3624599bd1c1d47a4dfbef Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 1 Jun 2016 15:41:50 +0100 Subject: [PATCH] [Behat] gather contexts using annotions in the properties --- Features/Context/ContentActions.php | 18 +++------ Features/Context/Fields.php | 18 +++------ Features/Context/PlatformUI.php | 38 +++++++++++++++++++ Features/Context/Role.php | 14 ++----- .../Context/SubContext/ContentEditContext.php | 27 ++++++------- Features/Context/Users.php | 23 +++-------- 6 files changed, 70 insertions(+), 68 deletions(-) diff --git a/Features/Context/ContentActions.php b/Features/Context/ContentActions.php index a389d4ea8..3eb1d7dc0 100644 --- a/Features/Context/ContentActions.php +++ b/Features/Context/ContentActions.php @@ -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; /** */ diff --git a/Features/Context/Fields.php b/Features/Context/Fields.php index b0fcf53cd..a6bda3f20 100644 --- a/Features/Context/Fields.php +++ b/Features/Context/Fields.php @@ -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 = '') { diff --git a/Features/Context/PlatformUI.php b/Features/Context/PlatformUI.php index 218eb793e..a7111f952 100644 --- a/Features/Context/PlatformUI.php +++ b/Features/Context/PlatformUI.php @@ -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 @@ -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 */ @@ -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 ') 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; } } diff --git a/Features/Context/Role.php b/Features/Context/Role.php index af316de5a..b755e37fb 100644 --- a/Features/Context/Role.php +++ b/Features/Context/Role.php @@ -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 diff --git a/Features/Context/SubContext/ContentEditContext.php b/Features/Context/SubContext/ContentEditContext.php index 7df7de34d..ce325826f 100644 --- a/Features/Context/SubContext/ContentEditContext.php +++ b/Features/Context/SubContext/ContentEditContext.php @@ -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: diff --git a/Features/Context/Users.php b/Features/Context/Users.php index 9f6e08605..1edb65c26 100644 --- a/Features/Context/Users.php +++ b/Features/Context/Users.php @@ -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