|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SWP\Behat\Listener; |
|
6
|
|
|
|
|
7
|
|
|
use Behat\Behat\EventDispatcher\Event\ExampleTested; |
|
8
|
|
|
use Behat\Behat\EventDispatcher\Event\FeatureTested; |
|
9
|
|
|
use Behat\Behat\EventDispatcher\Event\ScenarioTested; |
|
10
|
|
|
use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
12
|
|
|
|
|
13
|
|
|
class FixturesHookListener implements EventSubscriberInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string feature|scenario |
|
17
|
|
|
*/ |
|
18
|
|
|
private $lifetime; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var object |
|
22
|
|
|
*/ |
|
23
|
|
|
private $fixtureService; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $lifetime |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($lifetime) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->lifetime = $lifetime; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function getSubscribedEvents() |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
ExerciseCompleted::BEFORE => 'beforeExercise', |
|
42
|
|
|
FeatureTested::BEFORE => 'beforeFeature', |
|
43
|
|
|
FeatureTested::AFTER => 'afterFeature', |
|
44
|
|
|
ExampleTested::BEFORE => 'beforeScenario', |
|
45
|
|
|
ScenarioTested::BEFORE => 'beforeScenario', |
|
46
|
|
|
ExampleTested::AFTER => 'afterScenario', |
|
47
|
|
|
ScenarioTested::AFTER => 'afterScenario', |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set fixture service. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \BehatExtension\DoctrineDataFixturesExtension\Service\FixtureService $service |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setFixtureService($service) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->fixtureService = $service; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Listens to "exercise.before" event. |
|
63
|
|
|
* |
|
64
|
|
|
* @param \Behat\Testwork\Tester\Event\ExerciseCompleted $event |
|
65
|
|
|
*/ |
|
66
|
|
|
public function beforeExercise(ExerciseCompleted $event) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
$this->fixtureService |
|
69
|
|
|
->cacheFixtures(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Listens to "feature.before" event. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \Behat\Behat\Tester\Event\FeatureTested $event |
|
76
|
|
|
*/ |
|
77
|
|
|
public function beforeFeature(FeatureTested $event) |
|
78
|
|
|
{ |
|
79
|
|
|
if ('feature' !== $this->lifetime) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->fixtureService->reloadFixtures(\in_array('disable-fixtures', $event->getFeature()->getTags())); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Listens to "feature.after" event. |
|
88
|
|
|
* |
|
89
|
|
|
* @param \Behat\Behat\Tester\Event\FeatureTested $event |
|
90
|
|
|
*/ |
|
91
|
|
|
public function afterFeature(FeatureTested $event) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
if ('feature' !== $this->lifetime) { |
|
94
|
|
|
return; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->fixtureService |
|
98
|
|
|
->flush(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Listens to "scenario.before" and "outline.example.before" event. |
|
103
|
|
|
* |
|
104
|
|
|
* @param \Behat\Behat\Tester\Event\AbstractScenarioTested $event |
|
105
|
|
|
*/ |
|
106
|
|
|
public function beforeScenario(ScenarioTested $event) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
if ('scenario' !== $this->lifetime) { |
|
109
|
|
|
return; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$this->fixtureService |
|
113
|
|
|
->reloadFixtures(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Listens to "scenario.after" and "outline.example.after" event. |
|
118
|
|
|
* |
|
119
|
|
|
* @param \Behat\Behat\Tester\Event\AbstractScenarioTested $event |
|
120
|
|
|
*/ |
|
121
|
|
|
public function afterScenario(ScenarioTested $event) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
if ('scenario' !== $this->lifetime) { |
|
124
|
|
|
return; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->fixtureService |
|
128
|
|
|
->flush(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.