Passed
Push — master ( 69774c...de7969 )
by Sebastian
02:46
created

PHPUnit::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[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
namespace CaptainHook\App\Hook\PHP\CoverageResolver;
13
14
use RuntimeException;
15
use CaptainHook\App\Hook\PHP\CoverageResolver;
16
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor;
17
18
/**
19
 * Class PHPUnit
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/captainhookphp/captainhook
24
 * @since   Class available since Release 1.2.0
25
 */
26
class PHPUnit implements CoverageResolver
27
{
28
    /**
29
     * Path to phpunit
30
     *
31
     * @var string
32
     */
33
    private $phpUnit;
34
35
    /**
36
     * PHPUnit constructor.
37
     *
38 3
     * @param string $pathToPHPUnit
39
     */
40 3
    public function __construct(string $pathToPHPUnit)
41 3
    {
42
        $this->phpUnit = $pathToPHPUnit;
43
    }
44
45
    /**
46
     * Run PHPUnit to calculate code coverage.
47
     * Shamelessly ripped from bruli/php-git-hooks.
48
     *
49
     * @author Pablo Braulio
50 3
     * @return float
51
     */
52 3
    public function getCoverage(): float
53 3
    {
54 3
        $processor = new Processor();
55 3
        $result    = $processor->run($this->phpUnit . ' --coverage-text|grep Classes|cut -d " " -f 4|cut -d "%" -f 1');
56 1
        $output    = $result->getStdOut();
57
        if (!$result->isSuccessful() || empty($output)) {
58 2
            throw new RuntimeException('Error while executing PHPUnit: ' . $result->getStdErr());
59
        }
60
        return (float) $output;
61
    }
62
}
63