|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Arkitect\Tests\Unit\CLI\Printer; |
| 5 | + |
| 6 | +use Arkitect\CLI\Printer\GitlabPrinter; |
| 7 | +use Arkitect\Rules\Violation; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class GitlabPrinterTest extends TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Test the `print` method returns a valid JSON string |
| 14 | + * with violations details when violations exist. |
| 15 | + */ |
| 16 | + public function test_print_with_violations(): void |
| 17 | + { |
| 18 | + $violation1 = $this->createMock(Violation::class); |
| 19 | + $violation1->method('getFqcn')->willReturn('Some\\ExampleClass'); |
| 20 | + $violation1->method('getError')->willReturn('Some error message'); |
| 21 | + $violation1->method('getLine')->willReturn(42); |
| 22 | + |
| 23 | + $violation2 = $this->createMock(Violation::class); |
| 24 | + $violation2->method('getFqcn')->willReturn('Another\\ExampleClass'); |
| 25 | + $violation2->method('getError')->willReturn('Another error message'); |
| 26 | + $violation2->method('getLine')->willReturn(null); |
| 27 | + |
| 28 | + $violationsCollection = [ |
| 29 | + 'RuleA' => [$violation1], |
| 30 | + 'RuleB' => [$violation2], |
| 31 | + ]; |
| 32 | + |
| 33 | + $printer = new GitlabPrinter(); |
| 34 | + |
| 35 | + $result = $printer->print($violationsCollection); |
| 36 | + $decodedResult = json_decode($result, true); |
| 37 | + |
| 38 | + $this->assertIsString($result, 'Result should be a string'); |
| 39 | + $this->assertJson($result, 'Result should be a valid JSON string'); |
| 40 | + $this->assertCount(2, $decodedResult, 'Result should contain two violations'); |
| 41 | + |
| 42 | + $this->assertSame('Some error message', $decodedResult[0]['description']); |
| 43 | + $this->assertSame('RuleA.some-error-message', $decodedResult[0]['check_name']); |
| 44 | + $this->assertSame(hash('sha256', 'RuleA.some-error-message'), $decodedResult[0]['fingerprint']); |
| 45 | + $this->assertSame('major', $decodedResult[0]['severity']); |
| 46 | + $this->assertSame(__DIR__.'/GitlabPrinterTest.php', $decodedResult[0]['location']['path']); |
| 47 | + $this->assertSame(42, $decodedResult[0]['lines']['begin']); |
| 48 | + |
| 49 | + $this->assertSame('Another error message', $decodedResult[1]['description']); |
| 50 | + $this->assertSame('RuleB.another-error-message', $decodedResult[1]['check_name']); |
| 51 | + $this->assertSame(hash('sha256', 'RuleB.another-error-message'), $decodedResult[1]['fingerprint']); |
| 52 | + $this->assertSame('major', $decodedResult[1]['severity']); |
| 53 | + $this->assertSame(__DIR__.'/GitlabPrinterTest.php', $decodedResult[1]['location']['path']); |
| 54 | + $this->assertSame(1, $decodedResult[1]['lines']['begin']); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Test the `print` method returns an empty JSON array |
| 59 | + * when no violations are provided. |
| 60 | + */ |
| 61 | + public function test_print_with_no_violations(): void |
| 62 | + { |
| 63 | + $violationsCollection = []; |
| 64 | + |
| 65 | + $printer = new GitlabPrinter(); |
| 66 | + |
| 67 | + $result = $printer->print($violationsCollection); |
| 68 | + |
| 69 | + $this->assertIsString($result, 'Result should be a string'); |
| 70 | + $this->assertJson($result, 'Result should be a valid JSON string'); |
| 71 | + |
| 72 | + $decodedResult = json_decode($result, true); |
| 73 | + $this->assertEmpty($decodedResult, 'Result should be an empty array'); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +namespace Some; |
| 78 | + |
| 79 | +class ExampleClass |
| 80 | +{ |
| 81 | +} |
| 82 | + |
| 83 | +namespace Another; |
| 84 | + |
| 85 | +class ExampleClass |
| 86 | +{ |
| 87 | +} |
0 commit comments