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\Testwork\Call\CallCenter; |
15
|
|
|
use Behat\Testwork\Call\CallResults; |
16
|
|
|
use Behat\Testwork\Hook\Call\HookCall; |
17
|
|
|
use Behat\Testwork\Hook\HookRepository; |
18
|
|
|
use Behat\Testwork\Hook\Scope\HookScope; |
19
|
|
|
use Doctrine\Common\Annotations\Reader; |
20
|
|
|
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument; |
21
|
|
|
use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer; |
22
|
|
|
use Gorghoa\ScenarioStateBehatExtension\Hook\Call\ScenarioStateCall; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Vincent Chalamon <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class ScenarioStateHookDispatcher |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var HookRepository |
31
|
|
|
*/ |
32
|
|
|
private $repository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var CallCenter |
36
|
|
|
*/ |
37
|
|
|
private $callCenter; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Reader |
41
|
|
|
*/ |
42
|
|
|
private $reader; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ScenarioStateInitializer |
46
|
|
|
*/ |
47
|
|
|
private $store; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initializes scenario state hook dispatcher. |
51
|
|
|
* |
52
|
|
|
* @param HookRepository $repository |
53
|
|
|
* @param CallCenter $callCenter |
54
|
|
|
* @param ScenarioStateInitializer $store |
55
|
|
|
* @param Reader $reader |
56
|
|
|
*/ |
57
|
|
|
public function __construct(HookRepository $repository, CallCenter $callCenter, ScenarioStateInitializer $store, Reader $reader) |
58
|
|
|
{ |
59
|
|
|
$this->repository = $repository; |
60
|
|
|
$this->callCenter = $callCenter; |
61
|
|
|
$this->reader = $reader; |
62
|
|
|
$this->store = $store; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Dispatches hooks for a specified event. |
67
|
|
|
* |
68
|
|
|
* @param HookScope $scope |
69
|
|
|
* |
70
|
|
|
* @return CallResults |
71
|
|
|
*/ |
72
|
|
|
public function dispatchScopeHooks(HookScope $scope) |
73
|
|
|
{ |
74
|
|
|
$results = array(); |
75
|
|
|
foreach ($this->repository->getScopeHooks($scope) as $hook) { |
76
|
|
|
/** @var \ReflectionMethod $function */ |
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());die; |
|
|
|
|
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
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.