Passed
Pull Request — master (#3)
by ANTHONIUS
04:17
created

CoverageListener::start()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doyo\Behat\CodeCoverage\Listener;
4
5
use Behat\Behat\EventDispatcher\Event\ExampleTested;
6
use Behat\Behat\EventDispatcher\Event\ScenarioTested;
7
use Behat\Behat\Hook\Scope\ScenarioScope;
8
use Behat\Testwork\EventDispatcher\Event\AfterTested;
9
use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted;
10
use Behat\Testwork\Tester\Result\TestResult;
11
use Doyo\Behat\Coverage\Event\CoverageEvent;
0 ignored issues
show
Bug introduced by
The type Doyo\Behat\Coverage\Event\CoverageEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Doyo\Bridge\CodeCoverage\CodeCoverageInterface;
13
use Doyo\Bridge\CodeCoverage\TestCase;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
16
class CoverageListener implements EventSubscriberInterface
17
{
18
    /**
19
     * @var bool
20
     */
21
    private $enabled = false;
22
23
    /**
24
     * @var CodeCoverageInterface
25
     */
26
    private $coverage;
27
28 5
    public function __construct(
29
        CodeCoverageInterface $coverage,
30
        bool $enabled
31
    )
32
    {
33 5
        $this->coverage = $coverage;
34 5
        $this->enabled = $enabled;
35
    }
36
37
    public static function getSubscribedEvents()
38
    {
39
        return [
40
            ExerciseCompleted::BEFORE => 'refresh',
41
            ScenarioTested::BEFORE    => 'start',
42
            ExampleTested::BEFORE     => 'start',
43
            ScenarioTested::AFTER     => 'stop',
44
            ExampleTested::AFTER      => 'stop',
45
            ExerciseCompleted::AFTER  => 'complete',
46
        ];
47
    }
48
49 1
    public function refresh()
50
    {
51 1
        if(!$this->enabled){
52
            return;
53
        }
54
55 1
        $this->coverage->refresh();
56
    }
57
58 1
    public function start(ScenarioTested $scenarioTested)
59
    {
60 1
        if(!$this->enabled){
61
            return;
62
        }
63
64 1
        $scenario   = $scenarioTested->getScenario();
65 1
        $id         = $scenarioTested->getFeature()->getFile().':'.$scenario->getLine();
66 1
        $testCase   = new TestCase($id);
67
68 1
        $this->coverage->start($testCase);
69
    }
70
71 15
    public function stop(AfterTested $tested)
72
    {
73 15
        if(!$this->enabled){
74
            return;
75
        }
76
77
        $map           = [
78 15
            TestResult::PASSED  => TestCase::RESULT_PASSED,
79 15
            TestResult::FAILED  => TestCase::RESULT_FAILED,
80 15
            TestResult::SKIPPED => TestCase::RESULT_SKIPPED,
81 15
            TestResult::PENDING => TestCase::RESULT_SKIPPED,
82
        ];
83 15
        $result = $map[$tested->getTestResult()->getResultCode()];
84 15
        $coverage = $this->coverage;
85 15
        $coverage->setResult($result);
86 15
        $coverage->stop();
87
    }
88
89 1
    public function complete()
90
    {
91 1
        if(!$this->enabled){
92
            return;
93
        }
94
95 1
        $this->coverage->complete();
96
    }
97
98
99
}
100