Completed
Push — master ( aa9657...bb9832 )
by Sebastian
05:21
created

PHPUnit::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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