TestCoverage   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 88
ccs 20
cts 20
cp 1
rs 10
c 1
b 0
f 0
wmc 6

4 Methods

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