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\Context\ScenarioStateAwareContext; |
18
|
|
|
use Gorghoa\ScenarioStateBehatExtension\ScenarioState; |
19
|
|
|
use Gorghoa\ScenarioStateBehatExtension\ScenarioStateInterface; |
20
|
|
|
use Gorghoa\StepArgumentInjectorBehatExtension\Argument\StepArgumentHolder; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Rodrigue Villetard <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ScenarioStateInitializer implements ContextInitializer, EventSubscriberInterface, StepArgumentHolder |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ScenarioStateInterface |
30
|
|
|
*/ |
31
|
|
|
private $store; |
32
|
|
|
|
33
|
2 |
|
public function __construct() |
34
|
|
|
{ |
35
|
2 |
|
$this->clearStore(); |
36
|
2 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
1 |
|
public static function getSubscribedEvents() |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
1 |
|
ScenarioTested::AFTER => ['clearStore'], |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
1 |
|
public function initializeContext(Context $context) |
52
|
|
|
{ |
53
|
1 |
|
if (!$context instanceof ScenarioStateAwareContext) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$context->setScenarioState($this->store); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
2 |
|
public function clearStore() |
61
|
|
|
{ |
62
|
2 |
|
$this->store = new ScenarioState(); |
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return ScenarioStateInterface |
67
|
|
|
*/ |
68
|
2 |
|
public function getStore() |
69
|
|
|
{ |
70
|
2 |
|
return $this->store; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function hasStepArgumentFor($key) |
74
|
|
|
{ |
75
|
|
|
return $this->store->hasStateFragment($key); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getStepArgumentFor($key) |
79
|
|
|
{ |
80
|
|
|
return $this->store->getStateFragment($key); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|