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