-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathMarkdownTest.php
More file actions
351 lines (305 loc) · 15.1 KB
/
MarkdownTest.php
File metadata and controls
351 lines (305 loc) · 15.1 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* Tests the Markdown documentation generation.
*
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\Generators;
use PHP_CodeSniffer\Exceptions\GeneratorException;
use PHP_CodeSniffer\Generators\Markdown;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHP_CodeSniffer\Tests\Core\Generators\Fixtures\MarkdownDouble;
use PHPUnit\Framework\TestCase;
/**
* Test the Markdown documentation generation.
*
* @covers \PHP_CodeSniffer\Generators\Markdown
* @group Windows
*/
final class MarkdownTest extends TestCase
{
/**
* Verify that an XML doc which isn't valid documentation yields an Exception to warn devs.
*
* @return void
*/
public function testGeneratingInvalidDocsResultsInException()
{
// Set up the ruleset.
$standard = __DIR__.'/NoValidDocsTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);
$this->expectException(GeneratorException::class);
$this->expectExceptionMessage('Missing top-level <documentation> element in XML documentation file');
$generator = new Markdown($ruleset);
$generator->generate();
}//end testGeneratingInvalidDocsResultsInException()
/**
* Test the generated docs.
*
* @param string $standard The standard to use for the test.
* @param string $pathToExpected Path to a file containing the expected function output.
*
* @dataProvider dataDocs
*
* @return void
*/
public function testDocs($standard, $pathToExpected)
{
// Set up the ruleset.
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);
$expected = file_get_contents($pathToExpected);
$this->assertNotFalse($expected, 'Output expectation file could not be found');
// Make the test OS independent.
$expected = str_replace("\n", PHP_EOL, $expected);
$this->expectOutputString($expected);
$generator = new MarkdownDouble($ruleset);
$generator->generate();
}//end testDocs()
/**
* Data provider.
*
* @return array<string, array<string, string>>
*/
public static function dataDocs()
{
return [
'Standard without docs' => [
'standard' => __DIR__.'/NoDocsTest.xml',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputEmpty.txt',
],
'Standard with one doc file' => [
'standard' => __DIR__.'/OneDocTest.xml',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputOneDoc.md',
],
'Standard with multiple doc files' => [
'standard' => __DIR__.'/StructureDocsTest.xml',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputStructureDocs.md',
],
];
}//end dataDocs()
/**
* Test the generated docs for the handling of specific parts of the documentation.
*
* @param string $sniffs The specific fixture sniffs to verify the docs for.
* @param string $pathToExpected Path to a file containing the expected function output.
*
* @dataProvider dataDocSpecifics
*
* @return void
*/
public function testDocSpecifics($sniffs, $pathToExpected)
{
// Set up the ruleset.
$standard = __DIR__.'/AllValidDocsTest.xml';
$config = new ConfigDouble(["--standard=$standard", "--sniffs=$sniffs"]);
$ruleset = new Ruleset($config);
$expected = file_get_contents($pathToExpected);
$this->assertNotFalse($expected, 'Output expectation file could not be found');
// Make the test OS independent.
$expected = str_replace("\n", PHP_EOL, $expected);
$this->expectOutputString($expected);
$generator = new MarkdownDouble($ruleset);
$generator->generate();
}//end testDocSpecifics()
/**
* Data provider.
*
* @return array<string, array<string, string>>
*/
public static function dataDocSpecifics()
{
return [
'Documentation title: case' => [
'sniffs' => 'StandardWithDocs.Content.DocumentationTitleCase',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputDocumentationTitleCase.md',
],
'Documentation title: length' => [
'sniffs' => 'StandardWithDocs.Content.DocumentationTitleLength',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputDocumentationTitleLength.md',
],
'Documentation title: fallback to file name' => [
'sniffs' => 'StandardWithDocs.Content.DocumentationTitlePCREFallback',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputDocumentationTitlePCREFallback.md',
],
'Standard Element: blank line handling' => [
'sniffs' => 'StandardWithDocs.Content.StandardBlankLines',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputStandardBlankLines.md',
],
'Standard Element: encoding of special characters' => [
'sniffs' => 'StandardWithDocs.Content.StandardEncoding',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputStandardEncoding.md',
],
'Standard Element: indent handling' => [
'sniffs' => 'StandardWithDocs.Content.StandardIndent',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputStandardIndent.md',
],
'Standard Element: line wrapping' => [
'sniffs' => 'StandardWithDocs.Content.StandardLineWrapping',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputStandardLineWrapping.md',
],
'Code Title: line wrapping' => [
'sniffs' => 'StandardWithDocs.Content.CodeTitleLineWrapping',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputCodeTitleLineWrapping.md',
],
'Code Title: whitespace handling' => [
'sniffs' => 'StandardWithDocs.Content.CodeTitleWhitespace',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputCodeTitleWhitespace.md',
],
'Code Comparison: blank line handling' => [
'sniffs' => 'StandardWithDocs.Content.CodeComparisonBlankLines',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputCodeComparisonBlankLines.md',
],
'Code Comparison: different block lengths' => [
'sniffs' => 'StandardWithDocs.Content.CodeComparisonBlockLength',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputCodeComparisonBlockLength.md',
],
'Code Comparison: encoding of special characters' => [
'sniffs' => 'StandardWithDocs.Content.CodeComparisonEncoding',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputCodeComparisonEncoding.md',
],
'Code Comparison: line length handling' => [
'sniffs' => 'StandardWithDocs.Content.CodeComparisonLineLength',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputCodeComparisonLineLength.md',
],
'Unsupported: <code> element at the wrong level' => [
'sniffs' => 'StandardWithDocs.Unsupported.ElementAtWrongLevel',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputEmpty.txt',
],
'Unsupported: one correct elm, one at wrong level' => [
'sniffs' => 'StandardWithDocs.Unsupported.OneElmAtWrongLevel',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputUnsupportedOneElmAtWrongLevel.md',
],
'Unsupported: superfluous code element' => [
'sniffs' => 'StandardWithDocs.Unsupported.SuperfluousCodeElement',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputUnsupportedSuperfluousCodeElement.md',
],
'Unsupported: unknown element' => [
'sniffs' => 'StandardWithDocs.Unsupported.UnknownElement',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputEmpty.txt',
],
'Invalid: code comparison mismatched code elms' => [
'sniffs' => 'StandardWithDocs.Invalid.CodeComparisonMismatchedCodeElms',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputInvalidCodeComparisonMismatchedCodeElms.md',
],
'Invalid: code comparison only has one code elm' => [
'sniffs' => 'StandardWithDocs.Invalid.CodeComparisonMissingCodeElm',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputInvalidCodeComparisonMissingCodeElm.md',
],
'Invalid: code elements have no content' => [
'sniffs' => 'StandardWithDocs.Invalid.CodeComparisonNoCode',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.md',
],
'Invalid: code comparison element has no content' => [
'sniffs' => 'StandardWithDocs.Invalid.CodeComparisonNoContent',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputInvalidCodeComparisonNoContent.md',
],
'Invalid: code comparison two code elms, one empty' => [
'sniffs' => 'StandardWithDocs.Invalid.CodeComparisonOneEmptyCodeElm',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputInvalidCodeComparisonOneEmptyCodeElm.md',
],
'Invalid: code comparison two empty code elms' => [
'sniffs' => 'StandardWithDocs.Invalid.CodeComparisonTwoEmptyCodeElms',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.md',
],
'Invalid: code title attributes are empty' => [
'sniffs' => 'StandardWithDocs.Invalid.CodeTitleEmpty',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputInvalidCodeTitleEmpty.md',
],
'Invalid: code title attributes missing' => [
'sniffs' => 'StandardWithDocs.Invalid.CodeTitleMissing',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputInvalidCodeTitleMissing.md',
],
'Invalid: documentation title attribute is empty' => [
'sniffs' => 'StandardWithDocs.Invalid.DocumentationTitleEmpty',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputInvalidDocumentationTitleEmpty.md',
],
'Invalid: documentation title attribute missing' => [
'sniffs' => 'StandardWithDocs.Invalid.DocumentationTitleMissing',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputInvalidDocumentationTitleMissing.md',
],
'Invalid: standard element has no content' => [
'sniffs' => 'StandardWithDocs.Invalid.StandardNoContent',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputInvalidStandardNoContent.md',
],
];
}//end dataDocSpecifics()
/**
* Test the generated footer.
*
* @return void
*/
public function testFooter()
{
// Set up the ruleset.
$standard = __DIR__.'/OneDocTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);
$regex = '`^\RDocumentation generated on [A-Z][a-z]{2}, [0-9]{2} [A-Z][a-z]{2} 20[0-9]{2} [0-2][0-9](?::[0-5][0-9]){2} [+-][0-9]{4}';
$regex .= ' by \[PHP_CodeSniffer [3-9]\.[0-9]+.[0-9]+\]\(https://github\.com/PHPCSStandards/PHP_CodeSniffer\)\R$`';
$generator = new MarkdownDouble($ruleset);
$footer = $generator->getRealFooter();
if (method_exists($this, 'assertMatchesRegularExpression') === true) {
$this->assertMatchesRegularExpression($regex, $footer);
} else {
// PHPUnit < 9.1.0.
$this->assertRegExp($regex, $footer);
}
}//end testFooter()
/**
* Safeguard that the footer logic doesn't permanently change the error level.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @return void
*/
public function testFooterResetsErrorReportingToOriginalSetting()
{
$expected = error_reporting();
// Set up the ruleset.
$standard = __DIR__.'/OneDocTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);
$generator = new MarkdownDouble($ruleset);
$generator->getRealFooter();
$this->assertSame($expected, error_reporting());
}//end testFooterResetsErrorReportingToOriginalSetting()
/**
* Safeguard that users won't see a PHP warning about the timezone not being set when calling date().
*
* The warning we don't want to see is:
* "date(): It is not safe to rely on the system's timezone settings. You are *required* to use
* the date.timezone setting or the date_default_timezone_set() function. In case you used any of
* those methods and you are still getting this warning, you most likely misspelled the timezone
* identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select
* your timezone."
*
* JRF: Based on my tests, the warning only occurs on PHP < 7.0, but never a bad thing to safeguard this
* on a wider range of PHP versions.
*
* Note: as of PHP 8.2, PHP no longer accepts an empty string as timezone and will use `UTC` instead,
* so the warning on calling date() in the code itself would not display anyway.
*
* @requires PHP < 8.2
*
* @doesNotPerformAssertions
*
* @return void
*/
public function testFooterDoesntThrowWarningOnMissingTimezone()
{
$originalIni = @ini_set('date.timezone', '');
// Set up the ruleset.
$standard = __DIR__.'/OneDocTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);
$generator = new MarkdownDouble($ruleset);
$generator->getRealFooter();
// Reset the timezone to its original state.
ini_set('date.timezone', $originalIni);
}//end testFooterDoesntThrowWarningOnMissingTimezone()
}//end class