Passed
Push — master ( 92d243...5a69a1 )
by Eric
12:07
created

testNonConsoleInvalidCloverNoRootElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of PHPUnit Coverage Check.
7
 *
8
 * (c) Eric Sizemore <[email protected]>
9
 * (c) Richard Regeer <[email protected]>
10
 *
11
 * This source file is subject to the MIT license. For the full copyright,
12
 * license information, and credits/acknowledgements, please view the LICENSE
13
 * and README files that were distributed with this source code.
14
 */
15
16
namespace Esi\CoverageCheck\Tests;
17
18
use Esi\CoverageCheck\CoverageCheck;
19
use Esi\CoverageCheck\Utils;
20
use InvalidArgumentException;
21
use PHPUnit\Framework\Attributes\CoversClass;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CoversClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use PHPUnit\Framework\TestCase;
23
use RuntimeException;
24
25
/**
26
 * @internal
27
 */
28
#[CoversClass(CoverageCheck::class)]
29
#[CoversClass(Utils::class)]
30
class CoverageCheckTest extends TestCase
31
{
32
    private CoverageCheck $coverageCheck;
33
34
    /**
35
     * @var string[]
36
     */
37
    private static array $fixtures;
38
39
    #[\Override]
40
    protected function setUp(): void
41
    {
42
        $this->coverageCheck = new CoverageCheck();
43
        self::$fixtures      = [
44
            'valid'        => \dirname(__FILE__, 2) . '/fixtures/clover.xml',
45
            'notexist'     => \dirname(__FILE__, 2) . '/fixtures/clovr.xml',
46
            'empty'        => \dirname(__FILE__, 2) . '/fixtures/empty.xml',
47
            'invalid_root' => \dirname(__FILE__, 2) . '/fixtures/invalid_root_element.xml',
48
            'invalid_xml'  => \dirname(__FILE__, 2) . '/fixtures/invalid_xml.xml',
49
            'no_children'  => \dirname(__FILE__, 2) . '/fixtures/no_children.xml',
50
            'no_metrics'   => \dirname(__FILE__, 2) . '/fixtures/no_project_metrics.xml',
51
        ];
52
    }
53
54
    public function testGetSetCloverFile(): void
55
    {
56
        $this->coverageCheck->setCloverFile(self::$fixtures['valid']);
57
        self::assertSame(self::$fixtures['valid'], $this->coverageCheck->getCloverFile());
58
    }
59
60
    public function testGetSetOnlyPercentage(): void
61
    {
62
        $this->coverageCheck->setOnlyPercentage(true);
63
        self::assertTrue($this->coverageCheck->getOnlyPercentage());
64
65
        $this->coverageCheck->setOnlyPercentage();
66
        self::assertFalse($this->coverageCheck->getOnlyPercentage());
67
    }
68
69
    public function testGetSetThreshold(): void
70
    {
71
        $this->coverageCheck->setThreshold(100);
72
        self::assertSame(100, $this->coverageCheck->getThreshold());
73
    }
74
75
    public function testGetSetThresholdInvalid(): void
76
    {
77
        $this->expectException(InvalidArgumentException::class);
78
        $this->coverageCheck->setThreshold(101);
79
    }
80
81
    public function testNonConsoleCallInvalid(): void
82
    {
83
        $results = $this->coverageCheck->nonConsoleCall(self::$fixtures['valid']);
84
        self::assertSame('[ERROR] Total code coverage is 90.32% which is below the accepted 100%', $results);
85
    }
86
87
    public function testNonConsoleCallInvalidOnlyPercentage(): void
88
    {
89
        $results = $this->coverageCheck->nonConsoleCall(self::$fixtures['valid'], 100, true);
90
        self::assertSame('90.32%', $results);
91
    }
92
93
    public function testNonConsoleCallValid(): void
94
    {
95
        $results = $this->coverageCheck->nonConsoleCall(self::$fixtures['valid'], 90);
96
        self::assertSame('[OK] Total code coverage is 90.32%', $results);
97
    }
98
99
    public function testNonConsoleCallValidOnlyPercentage(): void
100
    {
101
        $results = $this->coverageCheck->nonConsoleCall(self::$fixtures['valid'], 90, true);
102
        self::assertSame('90.32%', $results);
103
    }
104
105
    public function testNonConsoleInvalidCloverNoChildren(): void
106
    {
107
        $this->expectException(RuntimeException::class);
108
        $this->coverageCheck->nonConsoleCall(self::$fixtures['no_children'], 90);
109
    }
110
111
    public function testNonConsoleInvalidCloverNoProjectMetrics(): void
112
    {
113
        $this->expectException(RuntimeException::class);
114
        $this->coverageCheck->nonConsoleCall(self::$fixtures['no_metrics'], 90);
115
    }
116
117
    public function testNonConsoleInvalidCloverNoRootElement(): void
118
    {
119
        $this->expectException(RuntimeException::class);
120
        $this->coverageCheck->nonConsoleCall(self::$fixtures['invalid_root'], 90);
121
    }
122
123
    public function testNonConsoleNotEnoughCode(): void
124
    {
125
        $results = $this->coverageCheck->nonConsoleCall(self::$fixtures['empty'], 90);
126
        self::assertSame('[ERROR] Insufficient data for calculation. Please add more code.', $results);
127
    }
128
129
    public function testParseXmlErrors(): void
130
    {
131
        $this->expectException(RuntimeException::class);
132
133
        $xml = (string) file_get_contents(self::$fixtures['invalid_xml']);
134
        Utils::parseXml($xml);
135
    }
136
137
    public function testSetCloverFileThatDoesNotExist(): void
138
    {
139
        $this->expectException(InvalidArgumentException::class);
140
        $this->coverageCheck->setCloverFile(self::$fixtures['notexist']);
141
    }
142
}
143