-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFixerInputValidationTest.php
More file actions
141 lines (123 loc) · 5.16 KB
/
Copy pathFixerInputValidationTest.php
File metadata and controls
141 lines (123 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
namespace DocbookCS\Tests\Unit\Fix;
use DocbookCS\Fix\Fixer\AttributeOrderFixer;
use DocbookCS\Fix\Fixer\ExceptionNameFixer;
use DocbookCS\Fix\Fixer\FileEmptyLastLineFixer;
use DocbookCS\Fix\Fixer\Fixer;
use DocbookCS\Fix\Fixer\MixedIndentationFixer;
use DocbookCS\Fix\Fixer\SimparaFixer;
use DocbookCS\Fix\Fixer\TrailingWhitespaceFixer;
use DocbookCS\Fix\FixerException;
use DocbookCS\Violation\SourceRange;
use DocbookCS\Violation\Violation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[
CoversClass(AttributeOrderFixer::class),
CoversClass(ExceptionNameFixer::class),
CoversClass(FileEmptyLastLineFixer::class),
CoversClass(FixerException::class),
CoversClass(MixedIndentationFixer::class),
CoversClass(SimparaFixer::class),
CoversClass(TrailingWhitespaceFixer::class),
//
UsesClass(SourceRange::class),
UsesClass(Violation::class),
]
final class FixerInputValidationTest extends TestCase
{
/** @param non-empty-list<SourceRange> $ranges */
#[Test, DataProvider('missingContent')]
public function itRejectsAffectedRangesWithoutSourceContent(Fixer $fixer, array $ranges): void
{
$this->expectException(FixerException::class);
$this->expectExceptionMessageIs('Fixers require affected source ranges with source content.');
$fixer->process($this->violation($ranges));
}
/** @return iterable<string, array{Fixer, non-empty-list<SourceRange>}> */
public static function missingContent(): iterable
{
yield 'attribute order' => [new AttributeOrderFixer(), [new SourceRange(1, 0, 4)]];
yield 'exception name' => [new ExceptionNameFixer(), [
new SourceRange(1, 0, 9),
new SourceRange(1, 10, 19),
]];
yield 'file empty last line' => [new FileEmptyLastLineFixer(), [new SourceRange(1, 0, 4)]];
yield 'mixed indentation' => [new MixedIndentationFixer(), [new SourceRange(1, 0, 2)]];
yield 'simpara' => [new SimparaFixer(), [
new SourceRange(1, 0, 4),
new SourceRange(1, 5, 9),
]];
yield 'trailing whitespace' => [new TrailingWhitespaceFixer(), [new SourceRange(1, 0, 2)]];
}
/** @param non-empty-list<SourceRange> $ranges */
#[Test, DataProvider('invalidContent')]
public function itRejectsInvalidSourceContent(Fixer $fixer, array $ranges): void
{
$this->expectException(FixerException::class);
$m = 'Cannot fix violation T.Sniff in f.xml on line 1 because its source content is not valid fixer input.';
$this->expectExceptionMessageIs($m);
$fixer->process($this->violation($ranges));
}
/** @return iterable<string, array{Fixer, non-empty-list<SourceRange>}> */
public static function invalidContent(): iterable
{
yield 'attribute order' => [new AttributeOrderFixer(), [new SourceRange(1, 0, 6, '<tag/>')]];
yield 'exception name' => [new ExceptionNameFixer(), [
new SourceRange(1, 0, 5, 'class'),
new SourceRange(1, 6, 11, 'class'),
]];
yield 'file empty last line with mixed content' => [
new FileEmptyLastLineFixer(),
[new SourceRange(1, 0, 9, "text\ntext")],
];
yield 'file empty last line with empty range' => [
new FileEmptyLastLineFixer(),
[new SourceRange(1, 0, 0, '')],
];
yield 'mixed indentation' => [new MixedIndentationFixer(), [new SourceRange(1, 0, 2, ' ')]];
yield 'simpara' => [new SimparaFixer(), [
new SourceRange(1, 0, 4, 'span'),
new SourceRange(1, 5, 9, 'span'),
]];
yield 'trailing whitespace' => [new TrailingWhitespaceFixer(), [new SourceRange(1, 0, 4, 'text')]];
}
#[Test, DataProvider('pairedElementFixers')]
public function itRejectsIncompletePairedElementRanges(Fixer $fixer): void
{
$this->expectException(FixerException::class);
$fixer->process($this->violation([new SourceRange(1, 0, 4, 'para')]));
}
/** @return iterable<string, array{Fixer}> */
public static function pairedElementFixers(): iterable
{
yield 'exception name' => [new ExceptionNameFixer()];
yield 'simpara' => [new SimparaFixer()];
}
#[Test]
public function itRejectsMalformedAttributeOrderInput(): void
{
$this->expectException(FixerException::class);
new AttributeOrderFixer()->process($this->violation([
new SourceRange(1, 0, 9, 'not a tag'),
]));
}
#[Test]
public function itRejectsAttributeOrderInputThatIsAlreadyOrdered(): void
{
$content = '<tag xml:id="id" xmlns="urn:test">';
$this->expectException(FixerException::class);
new AttributeOrderFixer()->process($this->violation([
new SourceRange(1, 0, strlen($content), $content),
]));
}
/** @param non-empty-list<SourceRange> $ranges */
private function violation(array $ranges): Violation
{
return new Violation('T.Sniff', 'f.xml', 'violation.', $ranges);
}
}