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

Commit 1a4407e

Browse files
author
Miguel
committed
[Behat] gather contexts using annotions in the properties
1 parent d25b07f commit 1a4407e

File tree

6 files changed

+70
-68
lines changed

6 files changed

+70
-68
lines changed

Features/Context/ContentActions.php

+6-12
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,16 @@
1515
class ContentActions extends PlatformUI
1616
{
1717
/**
18+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\ContentEditContext
19+
* @Context $contentEditContext EzSystems\PlatformUIBundle\Features\Context\SubContext\ContentEditContext
1820
*/
19-
private $contentEditContext;
20-
private $dashboardContext;
21+
protected $contentEditContext;
2122

2223
/**
23-
* @BeforeScenario
24+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
25+
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
2426
*/
25-
public function gatherContexts(BeforeScenarioScope $scope)
26-
{
27-
$this->contentEditContext = $scope->getEnvironment()->getContext(
28-
'EzSystems\PlatformUIBundle\Features\Context\SubContext\ContentEditContext'
29-
);
30-
$this->dashboardContext = $scope->getEnvironment()->getContext(
31-
'EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext'
32-
);
33-
}
27+
protected $dashboardContext;
3428

3529
/**
3630
*/

Features/Context/Fields.php

+6-12
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,16 @@ class Fields extends PlatformUI
1919
const NOTIFICATION_PUBLISH_ERROR = 'An error occured while publishing the draft';
2020

2121
/**
22+
* @var EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext
23+
* @Context $fieldtypeContext EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext
2224
*/
23-
private $fieldtypeContext;
24-
private $dashboardContext;
25+
protected $fieldtypeContext;
2526

2627
/**
27-
* @BeforeScenario
28+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
29+
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
2830
*/
29-
public function gatherContexts(BeforeScenarioScope $scope)
30-
{
31-
$this->fieldtypeContext = $scope->getEnvironment()->getContext(
32-
'EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext'
33-
);
34-
$this->dashboardContext = $scope->getEnvironment()->getContext(
35-
'EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext'
36-
);
37-
}
31+
protected $dashboardContext;
3832

3933
protected function getFieldIdentCss($identifier, $contentId = '')
4034
{

Features/Context/PlatformUI.php

+38
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace EzSystems\PlatformUIBundle\Features\Context;
1111

12+
use ReflectionClass;
13+
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
1214
use Behat\MinkExtension\Context\RawMinkContext;
1315

1416
class PlatformUI extends RawMinkContext
@@ -97,6 +99,23 @@ public function __construct($uri = self::PLATFORM_URI, $user = null, $password =
9799
}
98100
}
99101

102+
/**
103+
* @BeforeScenario
104+
*/
105+
public function gatherContexts(BeforeScenarioScope $scope)
106+
{
107+
$refClass = new ReflectionClass($this);
108+
$refProperties = $refClass->getProperties();
109+
foreach ($refProperties as $refProperty) {
110+
preg_match_all('#@(.*?)\n#s', $refProperty->getDocComment(), $matches);
111+
$contexts = $this->parseAnnotations($matches[1]);
112+
foreach ($contexts as $property => $context) {
113+
$this->$property = $scope->getEnvironment()->getContext($context);
114+
}
115+
116+
}
117+
}
118+
100119
/**
101120
* @BeforeScenario
102121
*/
@@ -448,11 +467,30 @@ protected function closeEditView()
448467
} catch (\Exception $e) {
449468
}
450469
}
470+
451471
/**
472+
* Returns an array with the properties contexts,
473+
* if the properties use the Context Annotation.
452474
*
475+
* @return array array of methods and their service dependencies
453476
*/
477+
private function parseAnnotations($annotations)
454478
{
479+
// parse array from (numeric key => 'annotation <value>') to (annotation => value)
480+
$propertiesContexts = [];
481+
foreach ($annotations as $annotation) {
482+
if (!preg_match('/^(\w+)\s+\$(\w+)\s+([\w\.\\\\]+)/', $annotation, $matches)) {
483+
continue;
484+
}
485+
486+
array_shift($matches);
487+
$tag = array_shift($matches);
488+
if ($tag == 'Context') {
489+
list($property, $context) = $matches;
490+
$propertiesContexts[$property] = $context;
455491
}
456492
}
493+
494+
return $propertiesContexts;
457495
}
458496
}

Features/Context/Role.php

+3-11
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,10 @@
1515
class Role extends PlatformUI
1616
{
1717
/**
18+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
19+
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
1820
*/
19-
private $dashboardContext;
20-
21-
/**
22-
* @BeforeScenario
23-
*/
24-
public function gatherContexts(BeforeScenarioScope $scope)
25-
{
26-
$this->dashboardContext = $scope->getEnvironment()->getContext(
27-
'EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext'
28-
);
29-
}
21+
protected $dashboardContext;
3022

3123
/**
3224
* @Given I am on the Roles page

Features/Context/SubContext/ContentEditContext.php

+11-16
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,21 @@
1616
class ContentEditContext extends PlatformUI
1717
{
1818
/**
19+
* @Context $basicContentContext EzSystems\PlatformBehatBundle\Context\Object\BasicContentContext
1920
*/
20-
private $basicContentContext;
21-
private $dashboardContext;
22-
private $browserContext;
21+
protected $basicContentContext;
2322

2423
/**
25-
* @BeforeScenario
24+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
25+
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
2626
*/
27-
public function gatherContexts(BeforeScenarioScope $scope)
28-
{
29-
$this->basicContentContext = $scope->getEnvironment()->getContext(
30-
'EzSystems\PlatformBehatBundle\Context\Object\BasicContentContext'
31-
);
32-
$this->dashboardContext = $scope->getEnvironment()->getContext(
33-
'EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext'
34-
);
35-
$this->browserContext = $scope->getEnvironment()->getContext(
36-
'EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext'
37-
);
38-
}
27+
protected $dashboardContext;
28+
29+
/**
30+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
31+
* @Context $browserContext EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
32+
*/
33+
protected $browserContext;
3934

4035
/**
4136
* @Given I create a content of content type :type with:

Features/Context/Users.php

+6-17
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,16 @@
1515
class Users extends PlatformUI
1616
{
1717
/**
18-
* @var \EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
18+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
19+
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
1920
*/
20-
private $dashboardContext;
21+
protected $dashboardContext;
2122

2223
/**
23-
* @var \EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
24+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
25+
* @Context $browserContext EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
2426
*/
25-
private $browserContext;
26-
27-
/**
28-
* @BeforeScenario
29-
*/
30-
public function gatherContexts(BeforeScenarioScope $scope)
31-
{
32-
$this->dashboardContext = $scope->getEnvironment()->getContext(
33-
'EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext'
34-
);
35-
$this->browserContext = $scope->getEnvironment()->getContext(
36-
'EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext'
37-
);
38-
}
27+
protected $browserContext;
3928

4029
/**
4130
* @Given I am on the Users page

0 commit comments

Comments
 (0)