Skip to content

Commit 0b49676

Browse files
committed
Add tests
1 parent e5a73c3 commit 0b49676

14 files changed

+721
-8
lines changed

composer.json

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
"description": "Tool to show code coverage metrics, measured by PHP Unit in Cobertura format, in console and CI/CD pipeline",
44
"keywords": [
55
"test",
6-
"coverage",
7-
"metrics",
86
"phpunit",
7+
"metrics",
8+
"coverage",
99
"cobertura",
10+
"ccn",
11+
"crap",
1012
"console"
1113
],
1214
"homepage": "https://github.com/andrey-tech/phpunit-cobertura-formatter-php",
@@ -39,12 +41,9 @@
3941
"phpunit/phpunit": "^12.3.8",
4042
"slevomat/coding-standard": "^8.22",
4143
"squizlabs/php_codesniffer": "^3.13.4",
44+
"symfony/process": "^5.4 || ^6.4 || ^7.3",
4245
"vimeo/psalm": "^6.13.1"
4346
},
44-
"conflict": {
45-
},
46-
"suggest": {
47-
},
4847
"config": {
4948
"sort-packages": true,
5049
"allow-plugins": {
@@ -56,6 +55,11 @@
5655
"AndreyTech\\": "src/"
5756
}
5857
},
58+
"autoload-dev": {
59+
"psr-4": {
60+
"Test\\": "tests/"
61+
}
62+
},
5963
"bin": [
6064
"bin/phpunit-cobertura-formatter"
6165
],
@@ -64,13 +68,15 @@
6468
"cs": "phpcs -s -p --report=full --standard=phpcs.xml.dist",
6569
"psalm": "psalm --long-progress --output-format=compact --config=psalm.xml.dist",
6670
"phpmd": "phpmd src ansi phpmd.xml.dist --suffixes php --ignore-violations-on-exit",
67-
"pdepend": "pdepend --summary-xml=.summary.xml --suffix=php src && pdepend-summary-formatter --init && pdepend-summary-formatter .summary.xml --config-file=pdepend-summary-formatter.yml.dist --ignore-red-metrics-on-exit --ignore-yellow-metrics-on-exit",
71+
"pdepend": "pdepend --summary-xml=.summary.xml --suffix=php src && pdepend-summary-formatter --init && pdepend-summary-formatter .summary.xml --config-file=pdepend-summary-formatter.yml.dist --ignore-yellow-metrics-on-exit",
72+
"test": "phpunit --configuration=phpunit.xml.dist --colors=always",
6873
"check": [
6974
"@lint",
7075
"@cs",
7176
"@psalm",
7277
"@phpmd",
73-
"@pdepend"
78+
"@pdepend",
79+
"@test"
7480
]
7581
}
7682
}

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<file>bin/</file>
1010
<file>src/</file>
11+
<file>tests/</file>
1112

1213
<arg name="basepath" value="."/>
1314
<arg name="colors"/>

phpunit.xml.dist

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.3/phpunit.xsd"
5+
bootstrap="tests/autoload.php"
6+
backupGlobals="false"
7+
cacheResult="false"
8+
colors="true"
9+
>
10+
<php>
11+
<ini name="error_reporting" value="-1" />
12+
</php>
13+
14+
<testsuites>
15+
<testsuite name="unit">
16+
<directory>tests</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<source>
21+
<include>
22+
<directory suffix=".php">src</directory>
23+
</include>
24+
</source>
25+
26+
</phpunit>

psalm.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<projectFiles>
1414
<directory name="bin"/>
1515
<directory name="src"/>
16+
<directory name="tests"/>
1617

1718
<ignoreFiles>
1819
<directory name="vendor"/>

tests/Functional/ScriptTest.php

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
<?php
2+
3+
/**
4+
* @author andrey-tech
5+
* @copyright 2025 andrey-tech
6+
* @link https://github.com/andrey-tech/
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Test\Functional;
13+
14+
use Symfony\Component\Process\Process;
15+
16+
use function getcwd;
17+
use function realpath;
18+
use function sprintf;
19+
use function unlink;
20+
21+
final class ScriptTest extends TestCase
22+
{
23+
private const string SCRIPT_FILE = __DIR__ . '/../../bin/phpunit-cobertura-formatter';
24+
private const string CONFIG_FILE = __DIR__ . '/../../config/phpunit-cobertura-formatter.yml.dist';
25+
26+
public function testSuccessRed(): void
27+
{
28+
$process = $this->runProcess([
29+
sprintf('--config-file=%s', (string) realpath(self::CONFIG_FILE)),
30+
$this->getDataFileAbsolutePath('cobertura-red.xml')
31+
]);
32+
33+
$this->assertStringContainsString(
34+
$this->getDataFileContents('success-red.txt'),
35+
$process->getOutput()
36+
);
37+
38+
$this->assertStringContainsString('Exit code:', $process->getOutput());
39+
$this->assertStringContainsString('Time:', $process->getOutput());
40+
$this->assertStringContainsString('Memory:', $process->getOutput());
41+
42+
$this->assertSame(2, $process->getExitCode());
43+
$this->assertEmpty($process->getErrorOutput());
44+
}
45+
46+
public function testNoColorSuccessRed(): void
47+
{
48+
$process = $this->runProcess([
49+
'--no-color',
50+
sprintf('--config-file=%s', (string) realpath(self::CONFIG_FILE)),
51+
$this->getDataFileAbsolutePath('cobertura-red.xml')
52+
]);
53+
54+
$this->assertStringContainsString(
55+
$this->getDataFileContents('no-color-success-red.txt'),
56+
$process->getOutput()
57+
);
58+
59+
$this->assertStringContainsString('Exit code:', $process->getOutput());
60+
$this->assertStringContainsString('Time:', $process->getOutput());
61+
$this->assertStringContainsString('Memory:', $process->getOutput());
62+
63+
$this->assertSame(2, $process->getExitCode());
64+
$this->assertEmpty($process->getErrorOutput());
65+
}
66+
67+
public function testNoColorSuccessIgnoreRed(): void
68+
{
69+
$process = $this->runProcess([
70+
'--no-color',
71+
'--ignore-red-metrics-on-exit',
72+
sprintf('--config-file=%s', (string) realpath(self::CONFIG_FILE)),
73+
$this->getDataFileAbsolutePath('cobertura-red.xml')
74+
]);
75+
76+
$this->assertStringContainsString(
77+
$this->getDataFileContents('no-color-success-red.txt'),
78+
$process->getOutput()
79+
);
80+
81+
$this->assertStringContainsString('Exit code:', $process->getOutput());
82+
$this->assertStringContainsString('Time:', $process->getOutput());
83+
$this->assertStringContainsString('Memory:', $process->getOutput());
84+
85+
$this->assertSame(3, $process->getExitCode());
86+
$this->assertEmpty($process->getErrorOutput());
87+
}
88+
89+
public function testNoColorSuccessGreen(): void
90+
{
91+
$process = $this->runProcess([
92+
'--no-color',
93+
sprintf('--config-file=%s', (string) realpath(self::CONFIG_FILE)),
94+
$this->getDataFileAbsolutePath('cobertura-green.xml')
95+
]);
96+
97+
$this->assertStringContainsString(
98+
$this->getDataFileContents('no-color-success-green.txt'),
99+
$process->getOutput()
100+
);
101+
102+
$this->assertStringContainsString('Exit code:', $process->getOutput());
103+
$this->assertStringContainsString('Time:', $process->getOutput());
104+
$this->assertStringContainsString('Memory:', $process->getOutput());
105+
106+
$this->assertSame(0, $process->getExitCode());
107+
$this->assertEmpty($process->getErrorOutput());
108+
}
109+
110+
public function testNoColorSuccessFilterGreen(): void
111+
{
112+
$process = $this->runProcess([
113+
'--no-color',
114+
'--filter-class-name=Fixer$',
115+
sprintf('--config-file=%s', (string) realpath(self::CONFIG_FILE)),
116+
$this->getDataFileAbsolutePath('cobertura-green.xml')
117+
]);
118+
119+
$this->assertStringContainsString(
120+
$this->getDataFileContents('no-color-success-green.txt'),
121+
$process->getOutput()
122+
);
123+
124+
$this->assertStringContainsString('Exit code:', $process->getOutput());
125+
$this->assertStringContainsString('Time:', $process->getOutput());
126+
$this->assertStringContainsString('Memory:', $process->getOutput());
127+
128+
$this->assertSame(0, $process->getExitCode());
129+
$this->assertEmpty($process->getErrorOutput());
130+
}
131+
132+
public function testNoColorSuccessFilterEmptyGreen(): void
133+
{
134+
$process = $this->runProcess([
135+
'--no-color',
136+
'--filter-class-name=xxx',
137+
sprintf('--config-file=%s', (string) realpath(self::CONFIG_FILE)),
138+
$this->getDataFileAbsolutePath('cobertura-green.xml')
139+
]);
140+
141+
$this->assertStringNotContainsString(
142+
$this->getDataFileContents('no-color-success-green.txt'),
143+
$process->getOutput()
144+
);
145+
146+
$this->assertStringContainsString('Exit code:', $process->getOutput());
147+
$this->assertStringContainsString('Time:', $process->getOutput());
148+
$this->assertStringContainsString('Memory:', $process->getOutput());
149+
150+
$this->assertSame(0, $process->getExitCode());
151+
$this->assertEmpty($process->getErrorOutput());
152+
}
153+
154+
public function testNoColorSuccessYellow(): void
155+
{
156+
$process = $this->runProcess([
157+
'--no-color',
158+
sprintf('--config-file=%s', (string) realpath(self::CONFIG_FILE)),
159+
$this->getDataFileAbsolutePath('cobertura-yellow.xml')
160+
]);
161+
162+
$this->assertStringContainsString(
163+
$this->getDataFileContents('no-color-success-yellow.txt'),
164+
$process->getOutput()
165+
);
166+
167+
$this->assertStringContainsString('Exit code:', $process->getOutput());
168+
$this->assertStringContainsString('Time:', $process->getOutput());
169+
$this->assertStringContainsString('Memory:', $process->getOutput());
170+
171+
$this->assertSame(3, $process->getExitCode());
172+
$this->assertEmpty($process->getErrorOutput());
173+
}
174+
175+
public function testNoColorSuccessIgnoreYellow(): void
176+
{
177+
$process = $this->runProcess([
178+
'--no-color',
179+
'--ignore-yellow-metrics-on-exit',
180+
sprintf('--config-file=%s', (string) realpath(self::CONFIG_FILE)),
181+
$this->getDataFileAbsolutePath('cobertura-yellow.xml')
182+
]);
183+
184+
$this->assertStringContainsString(
185+
$this->getDataFileContents('no-color-success-yellow.txt'),
186+
$process->getOutput()
187+
);
188+
189+
$this->assertStringContainsString('Exit code:', $process->getOutput());
190+
$this->assertStringContainsString('Time:', $process->getOutput());
191+
$this->assertStringContainsString('Memory:', $process->getOutput());
192+
193+
$this->assertSame(0, $process->getExitCode());
194+
$this->assertEmpty($process->getErrorOutput());
195+
}
196+
197+
public function testNoCoberturaFileError(): void
198+
{
199+
$process = $this->runProcess();
200+
201+
$this->assertStringContainsString(
202+
'ERROR: Missing required argument: path to cobertura XML file.',
203+
$process->getOutput()
204+
);
205+
206+
$this->assertSame(1, $process->getExitCode());
207+
$this->assertEmpty($process->getErrorOutput());
208+
}
209+
210+
public function testInitSuccess(): void
211+
{
212+
$process = $this->runProcess([
213+
'--init',
214+
]);
215+
216+
unlink(
217+
(string) realpath((string) getcwd() . '/phpunit-cobertura-formatter.yml.dist')
218+
);
219+
220+
$this->assertStringContainsString(
221+
'Default config file created at',
222+
$process->getOutput()
223+
);
224+
225+
$this->assertSame(0, $process->getExitCode());
226+
$this->assertEmpty($process->getErrorOutput());
227+
}
228+
229+
/**
230+
* @param string[] $options
231+
*/
232+
private function runProcess(array $options = []): Process
233+
{
234+
$process = new Process([
235+
'php',
236+
self::SCRIPT_FILE,
237+
...$options
238+
]);
239+
240+
$process->setTimeout(60);
241+
$process->run();
242+
243+
return $process;
244+
}
245+
}

0 commit comments

Comments
 (0)