|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Spiral\CodeStyle\Tests\Acceptance; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +final class StyleTest extends TestCase |
| 10 | +{ |
| 11 | + private const CS_STATUSES = [ |
| 12 | + 1 => 'General error (or PHP minimal requirement not matched).', |
| 13 | + 4 => 'Some files have invalid syntax (only in dry-run mode).', |
| 14 | + 8 => 'Some files need fixing (only in dry-run mode).', |
| 15 | + 16 => 'Configuration error of the application.', |
| 16 | + 32 => 'Configuration error of a Fixer.', |
| 17 | + 64 => 'Exception raised within the application.', |
| 18 | + ]; |
| 19 | + |
| 20 | + public function testStyled(): void |
| 21 | + { |
| 22 | + $dir = \realpath(__DIR__ . '/Stub/Styled'); |
| 23 | + $command = "php vendor/bin/php-cs-fixer fix --dry-run --diff --format=json $dir"; |
| 24 | + |
| 25 | + \exec($command, $output, $status); |
| 26 | + |
| 27 | + \in_array($status, [0, 8]) or $this->fail(\sprintf( |
| 28 | + "php-cs-fixer failed: %s. \n %s\n\n%s", |
| 29 | + $status, |
| 30 | + \implode('\n ', $this->describeFailCommand($status)), |
| 31 | + \implode("\n", $output), |
| 32 | + )); |
| 33 | + |
| 34 | + /** |
| 35 | + * @var array{ |
| 36 | + * about: non-empty-string, |
| 37 | + * files: list<array{ |
| 38 | + * name: non-empty-string, |
| 39 | + * diff: non-empty-string, |
| 40 | + * }>, |
| 41 | + * time: array{total: float}, |
| 42 | + * memory: positive-int |
| 43 | + * } $result |
| 44 | + */ |
| 45 | + $result = \json_decode(\implode('', $output), true, 512, \JSON_THROW_ON_ERROR); |
| 46 | + |
| 47 | + // No files to fix |
| 48 | + if ($result['files'] === []) { |
| 49 | + $this->assertTrue(true); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $this->fail(\sprintf( |
| 54 | + "Some nominal stub files were changed: \n\n%s", |
| 55 | + \implode("\n", \array_map( |
| 56 | + static fn(array $file) => $file['diff'], |
| 57 | + $result['files'], |
| 58 | + )), |
| 59 | + )); |
| 60 | + } |
| 61 | + |
| 62 | + private function describeFailCommand(int $code): array |
| 63 | + { |
| 64 | + $result = []; |
| 65 | + foreach (self::CS_STATUSES as $mask => $description) { |
| 66 | + $code & $mask and $result[] = $description; |
| 67 | + } |
| 68 | + |
| 69 | + return $result; |
| 70 | + } |
| 71 | +} |
0 commit comments