|
| 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