Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit db2fe20

Browse files
author
Miguel
committedJun 3, 2016
[Behat] Improve gatherContexts, use @var annotation instead of custom @context
1 parent f16eefa commit db2fe20

File tree

6 files changed

+13
-41
lines changed

6 files changed

+13
-41
lines changed
 

‎Features/Context/ContentActions.php

-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ class ContentActions extends PlatformUI
1515
{
1616
/**
1717
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\ContentEditContext
18-
* @Context $contentEditContext EzSystems\PlatformUIBundle\Features\Context\SubContext\ContentEditContext
1918
*/
2019
protected $contentEditContext;
2120

2221
/**
2322
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
24-
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
2523
*/
2624
protected $dashboardContext;
2725

‎Features/Context/Fields.php

-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ class Fields extends PlatformUI
1919

2020
/**
2121
* @var EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext
22-
* @Context $fieldtypeContext EzSystems\PlatformBehatBundle\Context\Object\FieldTypeContext
2322
*/
2423
protected $fieldtypeContext;
2524

2625
/**
2726
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
28-
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
2927
*/
3028
protected $dashboardContext;
3129

‎Features/Context/PlatformUI.php

+12-30
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,24 @@ public function __construct($uri = self::PLATFORM_URI, $user = null, $password =
101101

102102
/**
103103
* @BeforeScenario
104+
*
105+
* Default gather contexts method.
106+
* Uses doc comment type hint to get the contexts and set them
104107
*/
105108
public function gatherContexts(BeforeScenarioScope $scope)
106109
{
107110
$refClass = new ReflectionClass($this);
108111
$refProperties = $refClass->getProperties();
109112
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);
113+
$propertyName = $refProperty->getName();
114+
// get property type hint from doc comment
115+
preg_match_all('#@var\s(.*?)\n#s', $refProperty->getDocComment(), $matches);
116+
foreach ($matches[1] as $typeHint) {
117+
// checks if the type hint implements Behat Context
118+
$implements = @class_implements($typeHint);
119+
if (!empty($implements) && in_array('Behat\Behat\Context\Context', $implements)) {
120+
$this->$propertyName = $scope->getEnvironment()->getContext($typeHint);
121+
}
114122
}
115123
}
116124
}
@@ -449,30 +457,4 @@ protected function closeEditView()
449457
} catch (\Exception $e) {
450458
}
451459
}
452-
453-
/**
454-
* Returns an array with the properties contexts,
455-
* if the properties use the Context Annotation.
456-
*
457-
* @return array array of methods and their service dependencies
458-
*/
459-
private function parseAnnotations($annotations)
460-
{
461-
// parse array from (numeric key => 'annotation <value>') to (annotation => value)
462-
$propertiesContexts = [];
463-
foreach ($annotations as $annotation) {
464-
if (!preg_match('/^(\w+)\s+\$(\w+)\s+([\w\.\\\\]+)/', $annotation, $matches)) {
465-
continue;
466-
}
467-
468-
array_shift($matches);
469-
$tag = array_shift($matches);
470-
if ($tag == 'Context') {
471-
list($property, $context) = $matches;
472-
$propertiesContexts[$property] = $context;
473-
}
474-
}
475-
476-
return $propertiesContexts;
477-
}
478460
}

‎Features/Context/Role.php

-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ class Role extends PlatformUI
1515
{
1616
/**
1717
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
18-
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
1918
*/
2019
protected $dashboardContext;
2120

2221
/**
2322
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
24-
* @Context $browserContext EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
2523
*/
2624
protected $browserContext;
2725

‎Features/Context/SubContext/ContentEditContext.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@
1515
class ContentEditContext extends PlatformUI
1616
{
1717
/**
18-
* @Context $basicContentContext EzSystems\PlatformBehatBundle\Context\Object\BasicContentContext
18+
* @var EzSystems\PlatformBehatBundle\Context\Object\BasicContentContext
1919
*/
2020
protected $basicContentContext;
2121

2222
/**
2323
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
24-
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
2524
*/
2625
protected $dashboardContext;
2726

2827
/**
2928
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
30-
* @Context $browserContext EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
3129
*/
3230
protected $browserContext;
3331

‎Features/Context/Users.php

-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ class Users extends PlatformUI
1515
{
1616
/**
1717
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
18-
* @Context $dashboardContext EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
1918
*/
2019
protected $dashboardContext;
2120

2221
/**
2322
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
24-
* @Context $browserContext EzSystems\PlatformUIBundle\Features\Context\SubContext\BrowserContext
2523
*/
2624
protected $browserContext;
2725

0 commit comments

Comments
 (0)
This repository has been archived.