|
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\Context\Initializer; |
|
13
|
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
|
15
|
|
|
use Behat\Behat\Context\Initializer\ContextInitializer; |
|
16
|
|
|
use Behat\Behat\EventDispatcher\Event\ScenarioTested; |
|
17
|
|
|
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument; |
|
18
|
|
|
use Gorghoa\ScenarioStateBehatExtension\Context\ScenarioStateAwareContext; |
|
19
|
|
|
use Gorghoa\ScenarioStateBehatExtension\ScenarioState; |
|
20
|
|
|
use Gorghoa\ScenarioStateBehatExtension\ScenarioStateInterface; |
|
21
|
|
|
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument; |
|
22
|
|
|
use Gorghoa\StepArgumentInjectorBehatExtension\Argument\StepArgumentHolder; |
|
23
|
|
|
use Gorghoa\StepArgumentInjectorBehatExtension\Exception\RejectedAnnotationException; |
|
24
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @author Rodrigue Villetard <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class ScenarioStateInitializer implements ContextInitializer, EventSubscriberInterface, StepArgumentHolder |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var ScenarioStateInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $store; |
|
35
|
|
|
|
|
36
|
2 |
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
2 |
|
$this->clearStore(); |
|
39
|
2 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public static function getSubscribedEvents() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
1 |
|
ScenarioTested::AFTER => ['clearStore'], |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function initializeContext(Context $context) |
|
55
|
|
|
{ |
|
56
|
1 |
|
if (!$context instanceof ScenarioStateAwareContext) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
$context->setScenarioState($this->store); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function clearStore() |
|
64
|
|
|
{ |
|
65
|
2 |
|
$this->store = new ScenarioState(); |
|
66
|
2 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return ScenarioStateInterface |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function getStore() |
|
72
|
|
|
{ |
|
73
|
2 |
|
return $this->store; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Test if this service should handle specific argument injection. |
|
78
|
|
|
* |
|
79
|
|
|
* @param StepInjectorArgument $annotation |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
public function doesHandleStepArgument(StepInjectorArgument $annotation) |
|
84
|
|
|
{ |
|
85
|
|
|
return $annotation instanceof ScenarioStateArgument && $this->store->hasStateFragment($annotation->getName()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get an argument value to inject. |
|
90
|
|
|
* |
|
91
|
|
|
* @param StepInjectorArgument $annotation |
|
92
|
|
|
* |
|
93
|
|
|
* @throws RejectedAnnotationException |
|
94
|
|
|
* |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getStepArgumentValueFor(StepInjectorArgument $annotation) |
|
98
|
|
|
{ |
|
99
|
|
|
if (!($annotation instanceof ScenarioStateArgument)) { |
|
100
|
|
|
$class = get_class($annotation); |
|
101
|
|
|
throw new RejectedAnnotationException("$class not handled by ScenarioStateBehatExtension"); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->store->getStateFragment($annotation->getName()); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|