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

Commit 2bf44c5

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

8 files changed

+74
-78
lines changed

Features/Context/ContentActions.php

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

12-
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
1312
use PHPUnit_Framework_Assert as Assertion;
1413

1514
class ContentActions extends PlatformUI
1615
{
1716
/**
17+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\ContentEditContext
18+
* @Context $contentEditContext EzSystems\PlatformUIBundle\Features\Context\SubContext\ContentEditContext
1819
*/
19-
private $contentEditContext;
20-
private $dashboardContext;
20+
protected $contentEditContext;
2121

2222
/**
23-
* @BeforeScenario
23+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
24+
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
2425
*/
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-
}
26+
protected $dashboardContext;
3427

3528
/**
3629
*/

Features/Context/Fields.php

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

12-
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
1312
use Behat\Mink\WebAssert;
1413
use EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext as FieldType;
1514

@@ -19,22 +18,16 @@ class Fields extends PlatformUI
1918
const NOTIFICATION_PUBLISH_ERROR = 'An error occured while publishing the draft';
2019

2120
/**
21+
* @var EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext
22+
* @Context $fieldtypeContext EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext
2223
*/
23-
private $fieldtypeContext;
24-
private $dashboardContext;
24+
protected $fieldtypeContext;
2525

2626
/**
27-
* @BeforeScenario
27+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
28+
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
2829
*/
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-
}
30+
protected $dashboardContext;
3831

3932
protected function getFieldIdentCss($identifier, $contentId = '')
4033
{

Features/Context/PlatformUI.php

+37
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,22 @@ 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+
100118
/**
101119
* @BeforeScenario
102120
*/
@@ -448,11 +466,30 @@ protected function closeEditView()
448466
} catch (\Exception $e) {
449467
}
450468
}
469+
451470
/**
471+
* Returns an array with the properties contexts,
472+
* if the properties use the Context Annotation.
452473
*
474+
* @return array array of methods and their service dependencies
453475
*/
476+
private function parseAnnotations($annotations)
454477
{
478+
// parse array from (numeric key => 'annotation <value>') to (annotation => value)
479+
$propertiesContexts = [];
480+
foreach ($annotations as $annotation) {
481+
if (!preg_match('/^(\w+)\s+\$(\w+)\s+([\w\.\\\\]+)/', $annotation, $matches)) {
482+
continue;
483+
}
484+
485+
array_shift($matches);
486+
$tag = array_shift($matches);
487+
if ($tag == 'Context') {
488+
list($property, $context) = $matches;
489+
$propertiesContexts[$property] = $context;
455490
}
456491
}
492+
493+
return $propertiesContexts;
457494
}
458495
}

Features/Context/Role.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,14 @@
1010
namespace EzSystems\PlatformUIBundle\Features\Context;
1111

1212
use Behat\Gherkin\Node\TableNode;
13-
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
1413

1514
class Role extends PlatformUI
1615
{
1716
/**
17+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
18+
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
1819
*/
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-
}
20+
protected $dashboardContext;
3021

3122
/**
3223
* @Given I am on the Roles page

Features/Context/SubContext/Authentication.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(Repository $repository, UserService $userService, Ro
5252
}
5353

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

8787
return array(
88-
'login' => $username,
89-
'password' => $password,
88+
'login' => $username,
89+
'password' => $password,
9090
);
9191
}
9292

Features/Context/SubContext/BrowserContext.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function prepareHelpers()
3030
}
3131

3232
/**
33-
* Getter for Xpath
33+
* Getter for Xpath.
3434
*
3535
* @return \EzSystems\BehatBundle\Helper\Xpath
3636
*/

Features/Context/SubContext/ContentEditContext.php

+12-18
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,26 @@
1010
namespace EzSystems\PlatformUIBundle\Features\Context\SubContext;
1111

1212
use PHPUnit_Framework_Assert as Assertion;
13-
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
1413
use EzSystems\PlatformUIBundle\Features\Context\PlatformUI;
1514

1615
class ContentEditContext extends PlatformUI
1716
{
1817
/**
18+
* @Context $basicContentContext EzSystems\PlatformBehatBundle\Context\Object\BasicContentContext
1919
*/
20-
private $basicContentContext;
21-
private $dashboardContext;
22-
private $browserContext;
20+
protected $basicContentContext;
2321

2422
/**
25-
* @BeforeScenario
23+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
24+
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
2625
*/
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-
}
26+
protected $dashboardContext;
27+
28+
/**
29+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
30+
* @Context $browserContext EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
31+
*/
32+
protected $browserContext;
3933

4034
/**
4135
* @Given I create a content of content type :type with:
@@ -54,7 +48,7 @@ public function iCreateContentType($type, TableNode $fields)
5448
}
5549
}
5650

57-
/**
51+
/**
5852
* @Given I am on :name full view
5953
*/
6054
public function onFullView($name)

Features/Context/Users.php

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

12-
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
1312
use Behat\Gherkin\Node\TableNode;
1413

1514
class Users extends PlatformUI
1615
{
1716
/**
18-
* @var \EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
17+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
18+
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
1919
*/
20-
private $dashboardContext;
20+
protected $dashboardContext;
2121

2222
/**
23-
* @var \EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
23+
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
24+
* @Context $browserContext EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
2425
*/
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-
}
26+
protected $browserContext;
3927

4028
/**
4129
* @Given I am on the Users page

0 commit comments

Comments
 (0)