|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ScenarioStateBehatExtension project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Rodrigue Villetard <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Gorghoa\ScenarioStateBehatExtension\Hook\Dispatcher; |
|
13
|
|
|
|
|
14
|
|
|
use Behat\Behat\Hook\Scope\AfterScenarioScope; |
|
15
|
|
|
use Behat\Testwork\Call\CallCenter; |
|
16
|
|
|
use Behat\Testwork\Call\CallResults; |
|
17
|
|
|
use Behat\Testwork\Environment\Call\EnvironmentCall; |
|
18
|
|
|
use Behat\Testwork\Hook\Call\HookCall; |
|
19
|
|
|
use Behat\Testwork\Hook\HookRepository; |
|
20
|
|
|
use Behat\Testwork\Hook\Scope\HookScope; |
|
21
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
22
|
|
|
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument; |
|
23
|
|
|
use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @author Vincent Chalamon <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
final class ScenarioStateHookDispatcher |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var HookRepository |
|
32
|
|
|
*/ |
|
33
|
|
|
private $repository; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var CallCenter |
|
37
|
|
|
*/ |
|
38
|
|
|
private $callCenter; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Reader |
|
42
|
|
|
*/ |
|
43
|
|
|
private $reader; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var ScenarioStateInitializer |
|
47
|
|
|
*/ |
|
48
|
|
|
private $store; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Initializes scenario state hook dispatcher. |
|
52
|
|
|
* |
|
53
|
|
|
* @param HookRepository $repository |
|
54
|
|
|
* @param CallCenter $callCenter |
|
55
|
|
|
* @param ScenarioStateInitializer $store |
|
56
|
|
|
* @param Reader $reader |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct(HookRepository $repository, CallCenter $callCenter, ScenarioStateInitializer $store, Reader $reader) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->repository = $repository; |
|
61
|
|
|
$this->callCenter = $callCenter; |
|
62
|
|
|
$this->reader = $reader; |
|
63
|
|
|
$this->store = $store; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Dispatches hooks for a specified event. |
|
68
|
|
|
* |
|
69
|
|
|
* @param HookScope $scope |
|
70
|
|
|
* |
|
71
|
|
|
* @return CallResults |
|
72
|
|
|
*/ |
|
73
|
|
|
public function dispatchScopeHooks(HookScope $scope) |
|
74
|
|
|
{ |
|
75
|
|
|
$results = array(); |
|
76
|
|
|
foreach ($this->repository->getScopeHooks($scope) as $hook) { |
|
77
|
|
|
/** @var \ReflectionMethod $function */ |
|
78
|
|
|
$function = $hook->getReflection(); |
|
79
|
|
|
|
|
80
|
|
|
// No `@ScenarioStateArgument` annotation found |
|
81
|
|
|
if (null === $this->reader->getMethodAnnotation($function, ScenarioStateArgument::class)) { |
|
82
|
|
|
$results[] = $this->callCenter->makeCall(new HookCall($scope, $hook)); |
|
83
|
|
|
continue; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$paramsKeys = array_map(function($element) { |
|
87
|
|
|
return $element->name; |
|
88
|
|
|
}, $function->getParameters()); |
|
89
|
|
|
$store = $this->store->getStore(); |
|
90
|
|
|
$params = $arguments = []; |
|
91
|
|
|
|
|
92
|
|
|
// Prepare arguments from annotations |
|
93
|
|
|
/** @var ScenarioStateArgument[] $annotations */ |
|
94
|
|
|
$annotations = $this->reader->getMethodAnnotations($function); |
|
95
|
|
|
foreach ($annotations as $annotation) { |
|
96
|
|
|
if ($annotation instanceof ScenarioStateArgument && |
|
97
|
|
|
in_array($annotation->getArgument(), $paramsKeys) && |
|
98
|
|
|
$store->hasStateFragment($annotation->getName()) |
|
99
|
|
|
) { |
|
100
|
|
|
$params[$annotation->getArgument()] = $store->getStateFragment($annotation->getName()); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Manage `scope` argument |
|
105
|
|
|
foreach ($function->getParameters() as $parameter) { |
|
106
|
|
|
if (null !== $parameter->getClass() && get_class($scope) === $parameter->getClass()->getName()) { |
|
|
|
|
|
|
107
|
|
|
$arguments[$parameter->getName()] = $scope; |
|
|
|
|
|
|
108
|
|
|
} elseif (isset($params[$parameter->getName()])) { |
|
|
|
|
|
|
109
|
|
|
$arguments[$parameter->getName()] = $params[$parameter->getName()]; |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$results[] = $this->callCenter->makeCall(new EnvironmentCall($scope->getEnvironment(), $hook, $arguments)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return new CallResults($results); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|