|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DocbookCS\Tests\Unit\Fix; |
| 6 | + |
| 7 | +use DocbookCS\Fix\Fix; |
| 8 | +use DocbookCS\Fix\FixApplier; |
| 9 | +use DocbookCS\Fix\Fixer\FileEmptyLastLineFixer; |
| 10 | +use DocbookCS\Fix\FixPlan; |
| 11 | +use DocbookCS\Fix\FixResult; |
| 12 | +use DocbookCS\Sniff\FileEmptyLastLineSniffer; |
| 13 | +use DocbookCS\Source\File; |
| 14 | +use DocbookCS\Violation\SourceRange; |
| 15 | +use DocbookCS\Violation\Violation; |
| 16 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 17 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 18 | +use PHPUnit\Framework\Attributes\Test; |
| 19 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +#[ |
| 23 | + CoversClass(FileEmptyLastLineFixer::class), |
| 24 | + CoversClass(FileEmptyLastLineSniffer::class), |
| 25 | + CoversClass(Fix::class), |
| 26 | + CoversClass(FixApplier::class), |
| 27 | + CoversClass(FixResult::class), |
| 28 | + // |
| 29 | + UsesClass(File::class), |
| 30 | + UsesClass(FixPlan::class), |
| 31 | + UsesClass(SourceRange::class), |
| 32 | + UsesClass(Violation::class), |
| 33 | +] |
| 34 | +final class FileEmptyLastLineFixerTest extends TestCase |
| 35 | +{ |
| 36 | + #[Test] |
| 37 | + public function itTreatsTheCanonicalEndingAsANoOp(): void |
| 38 | + { |
| 39 | + $content = "<root/>\n"; |
| 40 | + $source = new File('file.xml', $content); |
| 41 | + $range = new SourceRange(1, strlen($content) - 1, strlen($content), "\n"); |
| 42 | + $violation = new Violation( |
| 43 | + FileEmptyLastLineSniffer::getCode(), |
| 44 | + $source->path, |
| 45 | + 'violation.', |
| 46 | + [$range], |
| 47 | + ); |
| 48 | + |
| 49 | + $fix = new FileEmptyLastLineFixer()->process($violation); |
| 50 | + $result = new FixApplier()->apply($source, [$fix]); |
| 51 | + |
| 52 | + self::assertSame($content, $result->file->content); |
| 53 | + self::assertSame(0, $result->applied); |
| 54 | + self::assertSame(1, $result->skipped); |
| 55 | + } |
| 56 | + |
| 57 | + #[Test, DataProvider('nonCompliantEndings')] |
| 58 | + public function itLeavesExactlyOneLfEmptyLastLine(string $content, string $expected): void |
| 59 | + { |
| 60 | + $source = new File('file.xml', $content); |
| 61 | + $document = new \DOMDocument(); |
| 62 | + $document->loadXML($content); |
| 63 | + $sniffer = new FileEmptyLastLineSniffer(); |
| 64 | + $violation = $sniffer->process($document, $source)[0]; |
| 65 | + |
| 66 | + $fix = new FileEmptyLastLineFixer()->process($violation); |
| 67 | + $result = new FixApplier()->apply($source, [$fix]); |
| 68 | + |
| 69 | + self::assertSame($expected, $result->file->content); |
| 70 | + self::assertSame(1, $result->applied); |
| 71 | + self::assertSame([], $sniffer->process($document, $result->file)); |
| 72 | + } |
| 73 | + |
| 74 | + /** @return iterable<string, array{string, string}> */ |
| 75 | + public static function nonCompliantEndings(): iterable |
| 76 | + { |
| 77 | + yield 'missing ending' => ['<root/>', "<root/>\n"]; |
| 78 | + yield 'after line feed content' => ["<root>\n</root>", "<root>\n</root>\n"]; |
| 79 | + yield 'after carriage return and line feed content' => ["<root>\r\n</root>", "<root>\r\n</root>\n"]; |
| 80 | + yield 'after carriage return content' => ["<root>\r</root>", "<root>\r</root>\n"]; |
| 81 | + yield 'extra line feed' => ["<root/>\n\n", "<root/>\n"]; |
| 82 | + yield 'carriage return and line feed' => ["<root/>\r\n", "<root/>\n"]; |
| 83 | + yield 'carriage return' => ["<root/>\r", "<root/>\n"]; |
| 84 | + yield 'extra carriage return and line feed' => ["<root/>\r\n\r\n", "<root/>\n"]; |
| 85 | + yield 'extra carriage return' => ["<root/>\r\r", "<root/>\n"]; |
| 86 | + yield 'multiple mixed extra endings' => ["<root/>\r\n\n\r", "<root/>\n"]; |
| 87 | + } |
| 88 | +} |
0 commit comments