CoverageListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 57
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getSubscribedEvents() 0 6 1
A afterSuite() 0 3 1
A afterExample() 0 14 1
A beforeExample() 0 10 1
1
<?php
2
3
/*
4
 * This file is part of the doyo/code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <https://itstoni.com>
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
declare(strict_types=1);
13
14
namespace Doyo\PhpSpec\CodeCoverage\Listener;
15
16
use Doyo\Bridge\CodeCoverage\CodeCoverageInterface;
17
use Doyo\Bridge\CodeCoverage\TestCase;
18
use PhpSpec\Event\ExampleEvent;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21
class CoverageListener implements EventSubscriberInterface
22
{
23
    /**
24
     * @var CodeCoverageInterface
25
     */
26
    private $coverage;
27
28
    /**
29
     * CoverageListener constructor.
30
     *
31
     * @param CodeCoverageInterface $coverage
32
     */
33 5
    public function __construct(CodeCoverageInterface $coverage)
34
    {
35 5
        $this->coverage = $coverage;
36
    }
37
38 1
    public static function getSubscribedEvents()
39
    {
40
        return [
41 1
            'beforeExample' => ['beforeExample', -10],
42
            'afterExample'  => ['afterExample', -10],
43
            'afterSuite'    => ['afterSuite', -10],
44
        ];
45
    }
46
47 2
    public function beforeExample(ExampleEvent $exampleEvent)
48
    {
49 2
        $example = $exampleEvent->getExample();
50 2
        $name    = strtr('%spec%:%example%', [
51 2
            '%spec%'    => $example->getSpecification()->getTitle(),
52 2
            '%example%' => $example->getTitle(),
53
        ]);
54 2
        $testCase = new TestCase($name);
55
56 2
        $this->coverage->start($testCase);
57
    }
58
59 101
    public function afterExample(ExampleEvent $exampleEvent)
60
    {
61 101
        $result = $exampleEvent->getResult();
62
        $map    = [
63 101
            ExampleEvent::PASSED  => TestCase::RESULT_PASSED,
64 101
            ExampleEvent::SKIPPED => TestCase::RESULT_SKIPPED,
65 101
            ExampleEvent::FAILED  => TestCase::RESULT_FAILED,
66 101
            ExampleEvent::BROKEN  => TestCase::RESULT_ERROR,
67 101
            ExampleEvent::PENDING => TestCase::RESULT_SKIPPED,
68
        ];
69
70 101
        $result = $map[$result];
71 101
        $this->coverage->setResult($result);
72 101
        $this->coverage->stop();
73
    }
74
75 2
    public function afterSuite()
76
    {
77 2
        $this->coverage->complete();
78
    }
79
}
80