|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Inpsyde\CodingStandard\Tests; |
| 6 | + |
| 7 | +use RuntimeException; |
| 8 | + |
| 9 | +class E2eTest extends TestCase |
| 10 | +{ |
| 11 | + private string $testPackagePath = ''; |
| 12 | + private string $phpCsBinary = ''; |
| 13 | + |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + $libPath = (string) getenv('LIB_PATH'); |
| 17 | + $this->testPackagePath = $libPath . '/tests/e2e/fixtures/test-package'; |
| 18 | + $this->phpCsBinary = $libPath . '/vendor/bin/phpcs'; |
| 19 | + } |
| 20 | + |
| 21 | + public function testInpsydeAndTemplatesRulesets(): void |
| 22 | + { |
| 23 | + $output = []; |
| 24 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec |
| 25 | + exec( |
| 26 | + sprintf( |
| 27 | + 'cd %s && %s', |
| 28 | + $this->testPackagePath, |
| 29 | + $this->phpCsBinary |
| 30 | + ), |
| 31 | + $output |
| 32 | + ); |
| 33 | + |
| 34 | + /** @var array<string> $output> */ |
| 35 | + if ($output[0] !== 'EE 2 / 2 (100%)') { |
| 36 | + throw new RuntimeException(implode("\n", $output)); |
| 37 | + } |
| 38 | + |
| 39 | + $json = end($output); |
| 40 | + |
| 41 | + // phpcs:disable Inpsyde.CodeQuality.LineLength.TooLong |
| 42 | + $expectedMessages = [ |
| 43 | + 'index.php' => [ |
| 44 | + [ |
| 45 | + 'source' => 'Inpsyde.CodeQuality.NoElse.ElseFound', |
| 46 | + 'line' => 12, |
| 47 | + ], |
| 48 | + [ |
| 49 | + 'source' => 'WordPress.Security.EscapeOutput.OutputNotEscaped', |
| 50 | + 'line' => 13, |
| 51 | + ], |
| 52 | + ], |
| 53 | + 'template.php' => [ |
| 54 | + |
| 55 | + [ |
| 56 | + 'source' => 'WordPress.Security.EscapeOutput.OutputNotEscaped', |
| 57 | + 'line' => 12, |
| 58 | + ], |
| 59 | + [ |
| 60 | + 'source' => 'InpsydeTemplates.Formatting.TrailingSemicolon.Found', |
| 61 | + 'line' => 12, |
| 62 | + ], |
| 63 | + [ |
| 64 | + 'source' => 'Inpsyde.CodeQuality.DisableCallUserFunc.call_user_func_call_user_func', |
| 65 | + 'line' => 15, |
| 66 | + ], |
| 67 | + |
| 68 | + ], |
| 69 | + ]; |
| 70 | + // phpcs:enable Inpsyde.CodeQuality.LineLength.TooLong |
| 71 | + |
| 72 | + self::assertSame($expectedMessages, $this->phpCsMessages($json)); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @psalm-return array<string, list<array{source: string, line: positive-int}>> |
| 77 | + */ |
| 78 | + private function phpCsMessages(string $json): array |
| 79 | + { |
| 80 | + /** @var array{ |
| 81 | + * files: array<string, array{ |
| 82 | + * messages: list<array{ |
| 83 | + * source: string, |
| 84 | + * line: positive-int, |
| 85 | + * ... |
| 86 | + * }> |
| 87 | + * }> |
| 88 | + * } $data */ |
| 89 | + $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR); |
| 90 | + |
| 91 | + $result = []; |
| 92 | + foreach ($data['files'] as $fileName => $fileData) { |
| 93 | + $baseName = basename($fileName); |
| 94 | + $result[$baseName] = []; |
| 95 | + foreach ($fileData['messages'] as ['source' => $source, 'line' => $line]) { |
| 96 | + $result[$baseName][] = ['source' => $source, 'line' => $line]; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return $result; |
| 101 | + } |
| 102 | +} |
0 commit comments