Completed
Pull Request — 1.5 (#758)
by Paweł
11:13
created

FixturesHookListener   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 118
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 12 1
A setFixtureService() 0 4 1
A beforeExercise() 0 5 1
A beforeFeature() 0 8 2
A afterFeature() 0 9 2
A beforeScenario() 0 9 2
A afterScenario() 0 9 2
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)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
    {
123
        if ('scenario' !== $this->lifetime) {
124
            return;
125
        }
126
127
        $this->fixtureService
128
            ->flush();
129
    }
130
}
131