|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace tests\Libero\CodingStandard; |
| 4 | + |
| 5 | +use LogicException; |
| 6 | +use ParseError; |
| 7 | +use PHP_CodeSniffer\Config; |
| 8 | +use PHP_CodeSniffer\Files\DummyFile; |
| 9 | +use PHP_CodeSniffer\Files\File; |
| 10 | +use PHP_CodeSniffer\Runner; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Symfony\Component\Finder\Finder; |
| 13 | +use function array_combine; |
| 14 | +use function array_filter; |
| 15 | +use function array_map; |
| 16 | +use function explode; |
| 17 | +use function Functional\flatten; |
| 18 | +use function ini_get; |
| 19 | +use function preg_match_all; |
| 20 | +use function sort; |
| 21 | +use function strpos; |
| 22 | +use function token_get_all; |
| 23 | +use const TOKEN_PARSE; |
| 24 | + |
| 25 | +final class RulesetTests extends TestCase |
| 26 | +{ |
| 27 | + /** @var Runner */ |
| 28 | + private static $codeSniffer; |
| 29 | + |
| 30 | + public static function setUpBeforeClass() |
| 31 | + { |
| 32 | + self::$codeSniffer = new Runner(); |
| 33 | + self::$codeSniffer->config = new Config(['--standard=Libero']); |
| 34 | + self::$codeSniffer->init(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @test |
| 39 | + * @dataProvider cases |
| 40 | + */ |
| 41 | + public function it_finds_and_fixes_violations( |
| 42 | + string $filename, |
| 43 | + string $contents, |
| 44 | + string $fixed, |
| 45 | + array $messages, |
| 46 | + ?string $description |
| 47 | + ) : void { |
| 48 | + $file = $this->createFile($filename, $contents); |
| 49 | + $actual = flatten($this->getMessages($file)); |
| 50 | + |
| 51 | + sort($actual); |
| 52 | + sort($messages); |
| 53 | + |
| 54 | + $this->assertSame($messages, $actual, $description); |
| 55 | + $this->assertSame($fixed, $file->fixer->getContents()); |
| 56 | + } |
| 57 | + |
| 58 | + public function cases() : iterable |
| 59 | + { |
| 60 | + $files = Finder::create()->files()->in(__DIR__.'/cases'); |
| 61 | + |
| 62 | + foreach ($files as $file) { |
| 63 | + preg_match_all('~(?:---)?([A-Z]+)---\s+([\s\S]+?)\n---~', $file->getContents(), $matches); |
| 64 | + |
| 65 | + $parts = array_combine(array_map('strtolower', $matches[1]), $matches[2]); |
| 66 | + |
| 67 | + if (isset($parts['messages'])) { |
| 68 | + $parts['messages'] = array_filter(explode("\n", $parts['messages'])); |
| 69 | + } |
| 70 | + |
| 71 | + if (empty($parts['contents'])) { |
| 72 | + throw new LogicException("Couldn't find contents in {$file->getRelativePathname()}"); |
| 73 | + } elseif (empty($parts['fixed']) && empty($parts['messages'])) { |
| 74 | + throw new LogicException("Expected one of fixed or messages in {$file->getRelativePathname()}"); |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + token_get_all($parts['contents'], TOKEN_PARSE); |
| 79 | + } catch (ParseError $exception) { |
| 80 | + $message = "Failed to parse content in {$file->getRelativePathname()}: {$exception->getMessage()}"; |
| 81 | + throw new LogicException($message, 0, $exception); |
| 82 | + } |
| 83 | + |
| 84 | + if (!empty($parts['fixed'])) { |
| 85 | + try { |
| 86 | + token_get_all($parts['fixed'], TOKEN_PARSE); |
| 87 | + } catch (ParseError $exception) { |
| 88 | + $message = "Failed to parse fixed in {$file->getRelativePathname()}: {$exception->getMessage()}"; |
| 89 | + throw new LogicException($message, 0, $exception); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + yield $file->getRelativePathname() => [ |
| 94 | + $parts['filename'] ?? 'test.php', |
| 95 | + $parts['contents'], |
| 96 | + $parts['fixed'] ?? $parts['contents'], |
| 97 | + $parts['messages'] ?? [], |
| 98 | + $parts['description'] ?? null, |
| 99 | + ]; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private function createFile(string $filename, string $content) : File |
| 104 | + { |
| 105 | + if (!ini_get('short_open_tag') && false === strpos($content, '<?php')) { |
| 106 | + $this->markTestSkipped('short_open_tag option is disabled'); |
| 107 | + } |
| 108 | + |
| 109 | + $file = new DummyFile( |
| 110 | + "phpcs_input_file:${filename}\n{$content}", |
| 111 | + self::$codeSniffer->ruleset, |
| 112 | + self::$codeSniffer->config |
| 113 | + ); |
| 114 | + |
| 115 | + $file->process(); |
| 116 | + |
| 117 | + $file->fixer->fixFile(); |
| 118 | + |
| 119 | + $file = new DummyFile( |
| 120 | + "phpcs_input_file:${filename}\n{$file->fixer->getContents()}", |
| 121 | + self::$codeSniffer->ruleset, |
| 122 | + self::$codeSniffer->config |
| 123 | + ); |
| 124 | + |
| 125 | + $file->process(); |
| 126 | + |
| 127 | + return $file; |
| 128 | + } |
| 129 | + |
| 130 | + private function getMessages(File $file) : iterable |
| 131 | + { |
| 132 | + foreach ([$file->getErrors(), $file->getWarnings()] as $messages) { |
| 133 | + foreach ($messages as $line => $lineMessages) { |
| 134 | + foreach ($lineMessages as $column => $columnMessages) { |
| 135 | + foreach ($columnMessages as $data) { |
| 136 | + yield "{$line}:{$column} {$data['source']}"; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments