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

TestCoverage::verifyCoverage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 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\Action;
11
12
use sebastianfeldmann\CaptainHook\Config;
13
use sebastianfeldmann\CaptainHook\Console\IO;
14
use sebastianfeldmann\CaptainHook\Exception\ActionFailed;
15
use sebastianfeldmann\CaptainHook\Git\Repository;
16
use sebastianfeldmann\CaptainHook\Hook\Action;
17
use sebastianfeldmann\CaptainHook\Hook\PHP\CoverageResolver;
18
19
/**
20
 * Class TestCoverage
21
 *
22
 * @package CaptainHook
23
 * @author  Sebastian Feldmann <[email protected]>
24
 * @link    https://github.com/sebastianfeldmann/captainhook
25
 * @since   Class available since Release 1.2.0
26
 */
27
class TestCoverage implements Action
28
{
29
    /**
30
     * Clover XML file
31
     *
32
     * @var string
33
     */
34
    private $cloverXmlFile;
35
36
    /**
37
     * Path to PHPUnit
38
     *
39
     * @var string
40
     */
41
    private $phpUnit;
42
43
    /**
44
     * Minimum coverage in percent
45
     *
46
     * @var float
47
     */
48
    private $minCoverage;
49
50
    /**
51
     * Executes the action.
52
     *
53
     * @param  \sebastianfeldmann\CaptainHook\Config         $config
54
     * @param  \sebastianfeldmann\CaptainHook\Console\IO     $io
55
     * @param  \sebastianfeldmann\CaptainHook\Git\Repository $repository
56
     * @param  \sebastianfeldmann\CaptainHook\Config\Action  $action
57
     * @throws \Exception
58
     */
59
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action)
60
    {
61
        $io->write('checking coverage:', true, IO::VERBOSE);
62
        $this->handleOptions($action->getOptions());
63
64
        $coverageResolver = $this->getCoverageResolver();
65
        $coverage         = $coverageResolver->getCoverage();
66
67
        $this->verifyCoverage($coverage);
68
        $io->write('<info>test coverage: ' . $coverage . '%</info>');
69
    }
70
71
    /**
72
     * Setup local properties with given options.
73
     *
74
     * @param  \sebastianfeldmann\CaptainHook\Config\Options $options
75
     * @throws \RuntimeException
76
     */
77
    protected function handleOptions(Config\Options $options)
78
    {
79
        $this->cloverXmlFile = $options->get('cloverXml');
80
        $this->phpUnit       = $options->get('phpUnit', 'phpunit');
81
        $this->minCoverage   = $options->get('minCoverage', 80);
82
    }
83
84
    /**
85
     * Return the adequate coverage resolver.
86
     *
87
     * @return \sebastianfeldmann\CaptainHook\Hook\PHP\CoverageResolver
88
     */
89
    protected function getCoverageResolver()
90
    {
91
        // if clover xml is configured use it to read coverage data
92
        if (null !== $this->cloverXmlFile) {
93
            return new CoverageResolver\CloverXML($this->cloverXmlFile);
94
        }
95
96
        // no clover xml so use phpunit to get current test coverage
97
        return new CoverageResolver\PHPUnit($this->phpUnit);
98
    }
99
100
    /**
101
     * Check if current coverage is high enough.
102
     *
103
     * @param  float $coverage
104
     * @throws \sebastianfeldmann\CaptainHook\Exception\ActionFailed
105
     */
106
    protected function verifyCoverage($coverage)
107
    {
108
        if ($coverage < $this->minCoverage) {
109
            throw ActionFailed::withMessage(
110
                'Test coverage to low!' . PHP_EOL .
111
                'Current coverage is at ' . $coverage . '% but should be at least ' . $this->minCoverage . '%'
112
            );
113
        }
114
    }
115
}
116