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; |
13
|
|
|
|
14
|
|
|
use Behat\Testwork\Call\CallCenter; |
15
|
|
|
use Behat\Testwork\Call\CallResult; |
16
|
|
|
use Behat\Testwork\Call\CallResults; |
17
|
|
|
use Behat\Testwork\Hook\Call\HookCall; |
18
|
|
|
use Behat\Testwork\Hook\Hook; |
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
|
|
|
$function = $hook->getReflection(); |
78
|
|
|
|
79
|
|
|
// No `@ScenarioStateArgument` annotation found |
80
|
|
|
if (null === $this->reader->getMethodAnnotation($function, ScenarioStateArgument::class)) { |
|
|
|
|
81
|
|
|
$results[] = $this->callCenter->makeCall(new HookCall($scope, $hook)); |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// $match = []; |
|
|
|
|
86
|
|
|
// $i = array_slice(array_keys($match), -1, 1)[0]; |
87
|
|
|
$paramsKeys = array_map(function($element) { |
88
|
|
|
return $element->name; |
89
|
|
|
}, $function->getParameters()); |
90
|
|
|
var_dump($function->getParameters()); |
|
|
|
|
91
|
|
|
$store = $this->store->getStore(); |
92
|
|
|
|
93
|
|
|
/** @var ScenarioStateArgument[] $annotations */ |
94
|
|
|
$annotations = $this->reader->getMethodAnnotations($function); |
|
|
|
|
95
|
|
View Code Duplication |
foreach ($annotations as $annotation) { |
|
|
|
|
96
|
|
|
if ($annotation instanceof ScenarioStateArgument && |
97
|
|
|
in_array($annotation->getArgument(), $paramsKeys) && |
98
|
|
|
$store->hasStateFragment($annotation->getName()) |
99
|
|
|
) { |
100
|
|
|
$match[$annotation->getArgument()] = $store->getStateFragment($annotation->getName()); |
|
|
|
|
101
|
|
|
$match[strval(++$i)] = $store->getStateFragment($annotation->getName()); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$results[] = $this->callCenter->makeCall(new ScenarioStateCall($scope, $hook, ['foo', 'bar'])); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return new CallResults($results); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Dispatches single event hook. |
113
|
|
|
* |
114
|
|
|
* @param HookScope $scope |
115
|
|
|
* @param Hook $hook |
116
|
|
|
* |
117
|
|
|
* @return CallResult |
118
|
|
|
*/ |
119
|
|
|
private function dispatchHook(HookScope $scope, Hook $hook) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
return $this->callCenter->makeCall(new HookCall($scope, $hook)); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.