|
9 | 9 | */
|
10 | 10 | namespace EzSystems\PlatformUIBundle\Features\Context;
|
11 | 11 |
|
| 12 | +use ReflectionClass; |
| 13 | +use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
12 | 14 | use Behat\MinkExtension\Context\RawMinkContext;
|
13 | 15 |
|
14 | 16 | class PlatformUI extends RawMinkContext
|
@@ -97,6 +99,22 @@ public function __construct($uri = self::PLATFORM_URI, $user = null, $password =
|
97 | 99 | }
|
98 | 100 | }
|
99 | 101 |
|
| 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 | + |
100 | 118 | /**
|
101 | 119 | * @BeforeScenario
|
102 | 120 | */
|
@@ -448,11 +466,30 @@ protected function closeEditView()
|
448 | 466 | } catch (\Exception $e) {
|
449 | 467 | }
|
450 | 468 | }
|
| 469 | + |
451 | 470 | /**
|
| 471 | + * Returns an array with the properties contexts, |
| 472 | + * if the properties use the Context Annotation. |
452 | 473 | *
|
| 474 | + * @return array array of methods and their service dependencies |
453 | 475 | */
|
| 476 | + private function parseAnnotations($annotations) |
454 | 477 | {
|
| 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; |
455 | 490 | }
|
456 | 491 | }
|
| 492 | + |
| 493 | + return $propertiesContexts; |
457 | 494 | }
|
458 | 495 | }
|
0 commit comments