Completed
Push — master ( 2f0aca...1e79f6 )
by Sebastian
03:02
created

PHPUnit::getCoverage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 0
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 Symfony\Component\Process\Process;
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
    public function __construct($pathToPHPUnit)
39
    {
40
        $this->phpUnit = $pathToPHPUnit;
41
    }
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
    public function getCoverage()
51
    {
52
        $process = new Process($this->phpUnit . ' --coverage-text|grep Classes|cut -d " " -f 4|cut -d "%" -f 1');
53
        $process->run();
54
        $output = $process->getOutput();
55
        if (!$process->isSuccessful() || empty($output)) {
56
            throw new RuntimeException('error while executing PHPUnit: ' . $process->getErrorOutput());
57
        }
58
        return (float) $output;
59
    }
60
}
61