Passed
Pull Request — develop (#8)
by ANTHONIUS
04:34
created

Processor::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the doyo/behat-coverage-extension project.
5
 *
6
 * (c) Anthonius Munthi <[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
declare(strict_types=1);
13
14
namespace Doyo\Behat\Coverage\Bridge\CodeCoverage;
15
16
use Doyo\Behat\Coverage\Bridge\Exception\ProcessorException;
17
use SebastianBergmann\CodeCoverage\CodeCoverage;
18
use SebastianBergmann\CodeCoverage\Driver\Driver;
19
use SebastianBergmann\CodeCoverage\Filter;
20
21
/**
22
 * Provide bridge to PHP Code Coverage.
23
 *
24
 * @method        append(array $data, $id = null, $append = true, $linesToBeCovered = [], array $linesToBeUsed = [], $ignoreForceCoversAnnotation = false)
25
 * @method        setAddUncoveredFilesFromWhitelist(bool $flag)
26
 * @method        clear()
27
 * @method array  getTests()
28
 * @method Driver getDriver()
29
 * @method Filter filter()
30
 * @method array  stop(bool $append = true, $linesToBeCovered = [], array $linesToBeUsed = [], bool $ignoreForceCoversAnnotation = false)
31
 * @method void setData(array $data)
32
 * @method array getData(bool $raw = false)
33
 */
34
class Processor
35
{
36
    /**
37
     * @var CodeCoverage
38
     */
39
    private $codeCoverage;
40
41
    /**
42
     * @var TestCase[]
43
     */
44
    private $testCases = [];
45
46
    private $completed = false;
47
48 26
    public function __construct($driver = null, $filter = null)
49
    {
50 26
        $codeCoverage       = new CodeCoverage($driver, $filter);
51 26
        $this->codeCoverage = $codeCoverage;
52
    }
53
54 1
    public function setCodeCoverage(CodeCoverage $codeCoverage)
55
    {
56 1
        $this->codeCoverage = $codeCoverage;
57
    }
58
59
    /**
60
     * @return CodeCoverage
61
     */
62 7
    public function getCodeCoverage()
63
    {
64 7
        return $this->codeCoverage;
65
    }
66
67 6
    public function addTestCase(TestCase $testCase)
68
    {
69 6
        $this->testCases[$testCase->getName()] = $testCase;
70
    }
71
72 6
    public function complete()
73
    {
74 6
        $coverage  = $this->codeCoverage;
75 6
        $testCases = $this->testCases;
76 6
        $tests     = $coverage->getTests();
77
78 6
        foreach ($testCases as $testCase) {
79 6
            $name                   = $testCase->getName();
80 6
            $tests[$name]['status'] = $testCase->getResult();
81
        }
82
83 6
        $coverage->setTests($tests);
84 6
        $this->completed = true;
85
    }
86
87 7
    public function start(TestCase $testCase, $clear = false)
88
    {
89 7
        $this->codeCoverage->start($testCase->getName(), $clear);
90
    }
91
92 5
    public function updateCoverage($coverage)
93
    {
94 5
        $aggregate = $this->getData();
95
96 5
        foreach ($coverage as $class => $counts) {
97 4
            if (!isset($this->coverage[$class])) {
0 ignored issues
show
Bug Best Practice introduced by
The property coverage does not exist on Doyo\Behat\Coverage\Bridge\CodeCoverage\Processor. Did you maybe forget to declare it?
Loading history...
98 4
                $aggregate[$class] = $counts;
99 4
                continue;
100
            }
101
102
            foreach ($counts as $line => $status) {
103
                $status                   = !$status ? -1 : ($status > 1 ? 1 : $status);
104
                $aggregate[$class][$line] = $status;
105
            }
106
        }
107
108 5
        $this->setData($aggregate);
109
    }
110
111 9
    public function __call($name, $arguments)
112
    {
113 9
        $codeCoverage = $this->codeCoverage;
114 9
        if (method_exists($codeCoverage, $name)) {
115 8
            return \call_user_func_array([$codeCoverage, $name], $arguments);
116
        }
117 1
        throw new ProcessorException('Method name: '.$name.' not supported.');
118
    }
119
}
120