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

TestCoverage::verifyCoverage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
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\Hook\Action;
16
use SebastianFeldmann\CaptainHook\Hook\PHP\CoverageResolver;
17
use SebastianFeldmann\Git\Repository;
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\Git\Repository             $repository
56
     * @param  \SebastianFeldmann\CaptainHook\Config\Action  $action
57
     * @throws \Exception
58
     */
59 3
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action)
60
    {
61 3
        $io->write('checking coverage:', true, IO::VERBOSE);
62 3
        $this->handleOptions($action->getOptions());
63
64 3
        $coverageResolver = $this->getCoverageResolver();
65 3
        $coverage         = $coverageResolver->getCoverage();
66
67 3
        $this->verifyCoverage($coverage);
68 2
        $io->write('<info>test coverage: ' . $coverage . '%</info>');
69 2
    }
70
71
    /**
72
     * Setup local properties with given options.
73
     *
74
     * @param  \SebastianFeldmann\CaptainHook\Config\Options $options
75
     * @throws \RuntimeException
76
     */
77 3
    protected function handleOptions(Config\Options $options)
78
    {
79 3
        $this->cloverXmlFile = $options->get('cloverXml');
80 3
        $this->phpUnit       = $options->get('phpUnit', 'phpunit');
81 3
        $this->minCoverage   = $options->get('minCoverage', 80);
82 3
    }
83
84
    /**
85
     * Return the adequate coverage resolver.
86
     *
87
     * @return \SebastianFeldmann\CaptainHook\Hook\PHP\CoverageResolver
88
     */
89 3
    protected function getCoverageResolver() : CoverageResolver
90
    {
91
        // if clover xml is configured use it to read coverage data
92 3
        if (null !== $this->cloverXmlFile) {
93 2
            return new CoverageResolver\CloverXML($this->cloverXmlFile);
94
        }
95
96
        // no clover xml so use phpunit to get current test coverage
97 1
        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 3
    protected function verifyCoverage($coverage)
107
    {
108 3
        if ($coverage < $this->minCoverage) {
109 1
            throw ActionFailed::withMessage(
110 1
                'Test coverage to low!' . PHP_EOL .
111 1
                'Current coverage is at ' . $coverage . '% but should be at least ' . $this->minCoverage . '%'
112
            );
113
        }
114 2
    }
115
}
116