|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PeterFox\PhpStanToonFormatter\Tests; |
| 4 | + |
| 5 | +use HelgeSverre\Toon\Toon; |
| 6 | +use PHPStan\Analyser\Error; |
| 7 | +use PHPStan\Command\AnalysisResult; |
| 8 | +use PHPStan\Command\Output; |
| 9 | +use PeterFox\PhpStanToonFormatter\ToonErrorFormatter; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class ToonErrorFormatterTest extends TestCase |
| 13 | +{ |
| 14 | + public function testFormatErrors(): void |
| 15 | + { |
| 16 | + $fileSpecificError = new Error( |
| 17 | + 'Error message', |
| 18 | + 'file.php', |
| 19 | + 10, |
| 20 | + true, |
| 21 | + null, |
| 22 | + null, |
| 23 | + 'Tip message', |
| 24 | + null, |
| 25 | + null, |
| 26 | + 'error.identifier' |
| 27 | + ); |
| 28 | + |
| 29 | + $analysisResult = new AnalysisResult( |
| 30 | + [$fileSpecificError], |
| 31 | + ['Global error'], |
| 32 | + [], |
| 33 | + [], |
| 34 | + [], |
| 35 | + false, |
| 36 | + null, |
| 37 | + true, |
| 38 | + 0, |
| 39 | + false, |
| 40 | + [] |
| 41 | + ); |
| 42 | + $output = $this->createMock(Output::class); |
| 43 | + |
| 44 | + $expectedData = [ |
| 45 | + 'totals' => [ |
| 46 | + 'errors' => 1, |
| 47 | + 'file_errors' => 1, |
| 48 | + ], |
| 49 | + 'files' => [ |
| 50 | + 'file.php' => [ |
| 51 | + 'errors' => 1, |
| 52 | + 'messages' => [ |
| 53 | + [ |
| 54 | + 'message' => 'Error message', |
| 55 | + 'line' => 10, |
| 56 | + 'ignorable' => true, |
| 57 | + 'tip' => 'Tip message', |
| 58 | + 'identifier' => 'error.identifier', |
| 59 | + ], |
| 60 | + ], |
| 61 | + ], |
| 62 | + ], |
| 63 | + 'errors' => ['Global error'], |
| 64 | + ]; |
| 65 | + |
| 66 | + $output->expects($this->once()) |
| 67 | + ->method('writeRaw') |
| 68 | + ->with(Toon::encode($expectedData)); |
| 69 | + |
| 70 | + $formatter = new ToonErrorFormatter(); |
| 71 | + $exitCode = $formatter->formatErrors($analysisResult, $output); |
| 72 | + |
| 73 | + $this->assertSame(1, $exitCode); |
| 74 | + } |
| 75 | + |
| 76 | + public function testFormatErrorsWithNoErrors(): void |
| 77 | + { |
| 78 | + $analysisResult = new AnalysisResult( |
| 79 | + [], |
| 80 | + [], |
| 81 | + [], |
| 82 | + [], |
| 83 | + [], |
| 84 | + false, |
| 85 | + null, |
| 86 | + true, |
| 87 | + 0, |
| 88 | + false, |
| 89 | + [] |
| 90 | + ); |
| 91 | + $output = $this->createMock(Output::class); |
| 92 | + |
| 93 | + $expectedData = [ |
| 94 | + 'totals' => [ |
| 95 | + 'errors' => 0, |
| 96 | + 'file_errors' => 0, |
| 97 | + ], |
| 98 | + 'files' => [], |
| 99 | + 'errors' => [], |
| 100 | + ]; |
| 101 | + |
| 102 | + $output->expects($this->once()) |
| 103 | + ->method('writeRaw') |
| 104 | + ->with(Toon::encode($expectedData)); |
| 105 | + |
| 106 | + $formatter = new ToonErrorFormatter(); |
| 107 | + $exitCode = $formatter->formatErrors($analysisResult, $output); |
| 108 | + |
| 109 | + $this->assertSame(0, $exitCode); |
| 110 | + } |
| 111 | +} |
0 commit comments