Skip to content

Commit 8b39d66

Browse files
authored
Merge pull request #716 from PHPCSStandards/feature/generators-add-tests-documentation-titles
Generators: add tests for handling documentation titles
2 parents fa96dd3 + 8b8aa2e commit 8b39d66

14 files changed

+413
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="GeneratorTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<config name="installed_paths" value="./tests/Core/Generators/Fixtures/"/>
5+
6+
<rule ref="StandardWithDocs">
7+
<exclude name="StandardWithDocs.Structure.NoDocumentationElement"/>
8+
</rule>
9+
10+
</ruleset>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<html>
2+
<head>
3+
<title>GeneratorTest Coding Standards</title>
4+
<style>
5+
body {
6+
background-color: #FFFFFF;
7+
font-size: 14px;
8+
font-family: Arial, Helvetica, sans-serif;
9+
color: #000000;
10+
}
11+
12+
h1 {
13+
color: #666666;
14+
font-size: 20px;
15+
font-weight: bold;
16+
margin-top: 0px;
17+
background-color: #E6E7E8;
18+
padding: 20px;
19+
border: 1px solid #BBBBBB;
20+
}
21+
22+
h2 {
23+
color: #00A5E3;
24+
font-size: 16px;
25+
font-weight: normal;
26+
margin-top: 50px;
27+
}
28+
29+
.code-comparison {
30+
width: 100%;
31+
}
32+
33+
.code-comparison td {
34+
border: 1px solid #CCCCCC;
35+
}
36+
37+
.code-comparison-title, .code-comparison-code {
38+
font-family: Arial, Helvetica, sans-serif;
39+
font-size: 12px;
40+
color: #000000;
41+
vertical-align: top;
42+
padding: 4px;
43+
width: 50%;
44+
background-color: #F1F1F1;
45+
line-height: 15px;
46+
}
47+
48+
.code-comparison-code {
49+
font-family: Courier;
50+
background-color: #F9F9F9;
51+
}
52+
53+
.code-comparison-highlight {
54+
background-color: #DDF1F7;
55+
border: 1px solid #00A5E3;
56+
line-height: 15px;
57+
}
58+
59+
.tag-line {
60+
text-align: center;
61+
width: 100%;
62+
margin-top: 30px;
63+
font-size: 12px;
64+
}
65+
66+
.tag-line a {
67+
color: #000000;
68+
}
69+
</style>
70+
</head>
71+
<body>
72+
<h1>GeneratorTest Coding Standards</h1>
73+
<a name="lowercase-title" />
74+
<h2>lowercase title</h2>
75+
<p class="text">This is a standard block.</p>
76+
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
77+
</body>
78+
</html>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# GeneratorTest Coding Standard
2+
3+
## lowercase title
4+
This is a standard block.
5+
6+
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
--------------------------------------------------
3+
| GENERATORTEST CODING STANDARD: LOWERCASE TITLE |
4+
--------------------------------------------------
5+
6+
This is a standard block.
7+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<html>
2+
<head>
3+
<title>GeneratorTest Coding Standards</title>
4+
<style>
5+
body {
6+
background-color: #FFFFFF;
7+
font-size: 14px;
8+
font-family: Arial, Helvetica, sans-serif;
9+
color: #000000;
10+
}
11+
12+
h1 {
13+
color: #666666;
14+
font-size: 20px;
15+
font-weight: bold;
16+
margin-top: 0px;
17+
background-color: #E6E7E8;
18+
padding: 20px;
19+
border: 1px solid #BBBBBB;
20+
}
21+
22+
h2 {
23+
color: #00A5E3;
24+
font-size: 16px;
25+
font-weight: normal;
26+
margin-top: 50px;
27+
}
28+
29+
.code-comparison {
30+
width: 100%;
31+
}
32+
33+
.code-comparison td {
34+
border: 1px solid #CCCCCC;
35+
}
36+
37+
.code-comparison-title, .code-comparison-code {
38+
font-family: Arial, Helvetica, sans-serif;
39+
font-size: 12px;
40+
color: #000000;
41+
vertical-align: top;
42+
padding: 4px;
43+
width: 50%;
44+
background-color: #F1F1F1;
45+
line-height: 15px;
46+
}
47+
48+
.code-comparison-code {
49+
font-family: Courier;
50+
background-color: #F9F9F9;
51+
}
52+
53+
.code-comparison-highlight {
54+
background-color: #DDF1F7;
55+
border: 1px solid #00A5E3;
56+
line-height: 15px;
57+
}
58+
59+
.tag-line {
60+
text-align: center;
61+
width: 100%;
62+
margin-top: 30px;
63+
font-size: 12px;
64+
}
65+
66+
.tag-line a {
67+
color: #000000;
68+
}
69+
</style>
70+
</head>
71+
<body>
72+
<h1>GeneratorTest Coding Standards</h1>
73+
<a name="This-is-a-very-very-very-very-very-very-very-very-very-very-very-long-title" />
74+
<h2>This is a very very very very very very very very very very very long title</h2>
75+
<p class="text">This is a standard block.</p>
76+
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
77+
</body>
78+
</html>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# GeneratorTest Coding Standard
2+
3+
## This is a very very very very very very very very very very very long title
4+
This is a standard block.
5+
6+
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
--------------------------------------------------------------------------------------------------------------
3+
| GENERATORTEST CODING STANDARD: THIS IS A VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY LONG TITLE |
4+
--------------------------------------------------------------------------------------------------------------
5+
6+
This is a standard block.
7+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<documentation title="lowercase title">
2+
<standard>
3+
<![CDATA[
4+
This is a standard block.
5+
]]>
6+
</standard>
7+
</documentation>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<documentation title="This is a very very very very very very very very very very very long title">
2+
<standard>
3+
<![CDATA[
4+
This is a standard block.
5+
]]>
6+
</standard>
7+
</documentation>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
6+
*/
7+
8+
namespace Fixtures\StandardWithDocs\Sniffs\Content;
9+
10+
use Fixtures\StandardWithDocs\Sniffs\DummySniff;
11+
12+
final class DocumentationTitleCaseSniff extends DummySniff {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
6+
*/
7+
8+
namespace Fixtures\StandardWithDocs\Sniffs\Content;
9+
10+
use Fixtures\StandardWithDocs\Sniffs\DummySniff;
11+
12+
final class DocumentationTitleLengthSniff extends DummySniff {}

tests/Core/Generators/HTMLTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,67 @@ public static function dataDocs()
7777
}//end dataDocs()
7878

7979

80+
/**
81+
* Test the generated docs for the handling of specific parts of the documentation.
82+
*
83+
* @param string $sniffs The specific fixture sniffs to verify the docs for.
84+
* @param string $pathToExpected Path to a file containing the expected function output.
85+
*
86+
* @dataProvider dataDocSpecifics
87+
*
88+
* @return void
89+
*/
90+
public function testDocSpecifics($sniffs, $pathToExpected)
91+
{
92+
// Set up the ruleset.
93+
$standard = __DIR__.'/AllValidDocsTest.xml';
94+
$config = new ConfigDouble(["--standard=$standard", "--sniffs=$sniffs"]);
95+
$ruleset = new Ruleset($config);
96+
97+
// In tests, the `--sniffs` setting doesn't work out of the box.
98+
$sniffParts = explode('.', $sniffs);
99+
$sniffFile = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.$sniffParts[0].DIRECTORY_SEPARATOR;
100+
$sniffFile .= 'Sniffs'.DIRECTORY_SEPARATOR.$sniffParts[1].DIRECTORY_SEPARATOR.$sniffParts[2].'Sniff.php';
101+
102+
$sniffParts = array_map('strtolower', $sniffParts);
103+
$sniffName = $sniffParts[0].'\sniffs\\'.$sniffParts[1].'\\'.$sniffParts[2].'sniff';
104+
$restrictions = [$sniffName => true];
105+
$ruleset->registerSniffs([$sniffFile], $restrictions, []);
106+
107+
$expected = file_get_contents($pathToExpected);
108+
$this->assertNotFalse($expected, 'Output expectation file could not be found');
109+
110+
// Make the test OS independent.
111+
$expected = str_replace("\n", PHP_EOL, $expected);
112+
$this->expectOutputString($expected);
113+
114+
$generator = new HTMLDouble($ruleset);
115+
$generator->generate();
116+
117+
}//end testDocSpecifics()
118+
119+
120+
/**
121+
* Data provider.
122+
*
123+
* @return array<string, array<string, string>>
124+
*/
125+
public static function dataDocSpecifics()
126+
{
127+
return [
128+
'Documentation title: case' => [
129+
'sniffs' => 'StandardWithDocs.Content.DocumentationTitleCase',
130+
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputDocumentationTitleCase.html',
131+
],
132+
'Documentation title: length' => [
133+
'sniffs' => 'StandardWithDocs.Content.DocumentationTitleLength',
134+
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputDocumentationTitleLength.html',
135+
],
136+
];
137+
138+
}//end dataDocSpecifics()
139+
140+
80141
/**
81142
* Test the generated footer.
82143
*

tests/Core/Generators/MarkdownTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,67 @@ public static function dataDocs()
7777
}//end dataDocs()
7878

7979

80+
/**
81+
* Test the generated docs for the handling of specific parts of the documentation.
82+
*
83+
* @param string $sniffs The specific fixture sniffs to verify the docs for.
84+
* @param string $pathToExpected Path to a file containing the expected function output.
85+
*
86+
* @dataProvider dataDocSpecifics
87+
*
88+
* @return void
89+
*/
90+
public function testDocSpecifics($sniffs, $pathToExpected)
91+
{
92+
// Set up the ruleset.
93+
$standard = __DIR__.'/AllValidDocsTest.xml';
94+
$config = new ConfigDouble(["--standard=$standard", "--sniffs=$sniffs"]);
95+
$ruleset = new Ruleset($config);
96+
97+
// In tests, the `--sniffs` setting doesn't work out of the box.
98+
$sniffParts = explode('.', $sniffs);
99+
$sniffFile = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.$sniffParts[0].DIRECTORY_SEPARATOR;
100+
$sniffFile .= 'Sniffs'.DIRECTORY_SEPARATOR.$sniffParts[1].DIRECTORY_SEPARATOR.$sniffParts[2].'Sniff.php';
101+
102+
$sniffParts = array_map('strtolower', $sniffParts);
103+
$sniffName = $sniffParts[0].'\sniffs\\'.$sniffParts[1].'\\'.$sniffParts[2].'sniff';
104+
$restrictions = [$sniffName => true];
105+
$ruleset->registerSniffs([$sniffFile], $restrictions, []);
106+
107+
$expected = file_get_contents($pathToExpected);
108+
$this->assertNotFalse($expected, 'Output expectation file could not be found');
109+
110+
// Make the test OS independent.
111+
$expected = str_replace("\n", PHP_EOL, $expected);
112+
$this->expectOutputString($expected);
113+
114+
$generator = new MarkdownDouble($ruleset);
115+
$generator->generate();
116+
117+
}//end testDocSpecifics()
118+
119+
120+
/**
121+
* Data provider.
122+
*
123+
* @return array<string, array<string, string>>
124+
*/
125+
public static function dataDocSpecifics()
126+
{
127+
return [
128+
'Documentation title: case' => [
129+
'sniffs' => 'StandardWithDocs.Content.DocumentationTitleCase',
130+
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputDocumentationTitleCase.md',
131+
],
132+
'Documentation title: length' => [
133+
'sniffs' => 'StandardWithDocs.Content.DocumentationTitleLength',
134+
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputDocumentationTitleLength.md',
135+
],
136+
];
137+
138+
}//end dataDocSpecifics()
139+
140+
80141
/**
81142
* Test the generated footer.
82143
*

0 commit comments

Comments
 (0)