Skip to content

Generators: add initial set of tests #671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .markdownlint-cli2.yaml
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ showFound: true
ignores:
- "node_modules/"
- "vendor/"
- "tests/Core/Generators/Expectations/"

# Disable inline config comments.
noInlineConfig: true
7 changes: 6 additions & 1 deletion src/Generators/HTML.php
Original file line number Diff line number Diff line change
@@ -7,7 +7,9 @@
* to each sniff.
*
* @author Greg Sherwood <[email protected]>
* @author Juliette Reinders Folmer <[email protected]>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

@@ -133,7 +135,7 @@ protected function printHeader()
echo '<html>'.PHP_EOL;
echo ' <head>'.PHP_EOL;
echo " <title>$standard Coding Standards</title>".PHP_EOL;
echo ' '.self::STYLESHEET.PHP_EOL;
echo ' '.str_replace("\n", PHP_EOL, self::STYLESHEET).PHP_EOL;
echo ' </head>'.PHP_EOL;
echo ' <body>'.PHP_EOL;
echo " <h1>$standard Coding Standards</h1>".PHP_EOL;
@@ -226,6 +228,9 @@ protected function printTextBlock(DOMNode $node)
$content = trim($node->nodeValue);
$content = htmlspecialchars($content);

// Use the correct line endings based on the OS.
$content = str_replace("\n", PHP_EOL, $content);

// Allow em tags only.
$content = str_replace('&lt;em&gt;', '<em>', $content);
$content = str_replace('&lt;/em&gt;', '</em>', $content);
9 changes: 7 additions & 2 deletions src/Generators/Markdown.php
Original file line number Diff line number Diff line change
@@ -3,7 +3,9 @@
* A doc generator that outputs documentation in Markdown format.
*
* @author Stefano Kowalke <[email protected]>
* @author Juliette Reinders Folmer <[email protected]>
* @copyright 2014 Arroba IT
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

@@ -111,6 +113,9 @@ protected function printTextBlock(DOMNode $node)
$content = trim($node->nodeValue);
$content = htmlspecialchars($content);

// Use the correct line endings based on the OS.
$content = str_replace("\n", PHP_EOL, $content);

$content = str_replace('&lt;em&gt;', '*', $content);
$content = str_replace('&lt;/em&gt;', '*', $content);

@@ -132,13 +137,13 @@ protected function printCodeComparisonBlock(DOMNode $node)

$firstTitle = $codeBlocks->item(0)->getAttribute('title');
$first = trim($codeBlocks->item(0)->nodeValue);
$first = str_replace("\n", "\n ", $first);
$first = str_replace("\n", PHP_EOL.' ', $first);
$first = str_replace('<em>', '', $first);
$first = str_replace('</em>', '', $first);

$secondTitle = $codeBlocks->item(1)->getAttribute('title');
$second = trim($codeBlocks->item(1)->nodeValue);
$second = str_replace("\n", "\n ", $second);
$second = str_replace("\n", PHP_EOL.' ', $second);
$second = str_replace('<em>', '', $second);
$second = str_replace('</em>', '', $second);

Empty file.
78 changes: 78 additions & 0 deletions tests/Core/Generators/Expectations/ExpectedOutputNoDocs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<html>
<head>
<title>GeneratorTest Coding Standards</title>
<style>
body {
background-color: #FFFFFF;
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
color: #000000;
}

h1 {
color: #666666;
font-size: 20px;
font-weight: bold;
margin-top: 0px;
background-color: #E6E7E8;
padding: 20px;
border: 1px solid #BBBBBB;
}

h2 {
color: #00A5E3;
font-size: 16px;
font-weight: normal;
margin-top: 50px;
}

.code-comparison {
width: 100%;
}

.code-comparison td {
border: 1px solid #CCCCCC;
}

.code-comparison-title, .code-comparison-code {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #000000;
vertical-align: top;
padding: 4px;
width: 50%;
background-color: #F1F1F1;
line-height: 15px;
}

.code-comparison-code {
font-family: Courier;
background-color: #F9F9F9;
}

.code-comparison-highlight {
background-color: #DDF1F7;
border: 1px solid #00A5E3;
line-height: 15px;
}

.tag-line {
text-align: center;
width: 100%;
margin-top: 30px;
font-size: 12px;
}

.tag-line a {
color: #000000;
}
</style>
</head>
<body>
<h1>GeneratorTest Coding Standards</h1>
<h2>Table of Contents</h2>
<ul class="toc">
</ul>
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
</body>
</html>
2 changes: 2 additions & 0 deletions tests/Core/Generators/Expectations/ExpectedOutputNoDocs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# GeneratorTest Coding Standard
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)
82 changes: 82 additions & 0 deletions tests/Core/Generators/Expectations/ExpectedOutputOneDoc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<html>
<head>
<title>GeneratorTest Coding Standards</title>
<style>
body {
background-color: #FFFFFF;
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
color: #000000;
}

h1 {
color: #666666;
font-size: 20px;
font-weight: bold;
margin-top: 0px;
background-color: #E6E7E8;
padding: 20px;
border: 1px solid #BBBBBB;
}

h2 {
color: #00A5E3;
font-size: 16px;
font-weight: normal;
margin-top: 50px;
}

.code-comparison {
width: 100%;
}

.code-comparison td {
border: 1px solid #CCCCCC;
}

.code-comparison-title, .code-comparison-code {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #000000;
vertical-align: top;
padding: 4px;
width: 50%;
background-color: #F1F1F1;
line-height: 15px;
}

.code-comparison-code {
font-family: Courier;
background-color: #F9F9F9;
}

.code-comparison-highlight {
background-color: #DDF1F7;
border: 1px solid #00A5E3;
line-height: 15px;
}

.tag-line {
text-align: center;
width: 100%;
margin-top: 30px;
font-size: 12px;
}

.tag-line a {
color: #000000;
}
</style>
</head>
<body>
<h1>GeneratorTest Coding Standards</h1>
<h2>Table of Contents</h2>
<ul class="toc">
<li><a href="#One-Standard-Block,-No-Code">One Standard Block, No Code</a></li>
</ul>
<a name="One-Standard-Block,-No-Code" />
<h2>One Standard Block, No Code</h2>
<p class="text">Documentation contains one standard block and no code comparison.</p>
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
</body>
</html>
5 changes: 5 additions & 0 deletions tests/Core/Generators/Expectations/ExpectedOutputOneDoc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# GeneratorTest Coding Standard

## One Standard Block, No Code
Documentation contains one standard block and no code comparison.
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)
7 changes: 7 additions & 0 deletions tests/Core/Generators/Expectations/ExpectedOutputOneDoc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

--------------------------------------------------------------
| GENERATORTEST CODING STANDARD: ONE STANDARD BLOCK, NO CODE |
--------------------------------------------------------------

Documentation contains one standard block and no code comparison.

191 changes: 191 additions & 0 deletions tests/Core/Generators/Expectations/ExpectedOutputStructureDocs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<html>
<head>
<title>GeneratorTest Coding Standards</title>
<style>
body {
background-color: #FFFFFF;
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
color: #000000;
}

h1 {
color: #666666;
font-size: 20px;
font-weight: bold;
margin-top: 0px;
background-color: #E6E7E8;
padding: 20px;
border: 1px solid #BBBBBB;
}

h2 {
color: #00A5E3;
font-size: 16px;
font-weight: normal;
margin-top: 50px;
}

.code-comparison {
width: 100%;
}

.code-comparison td {
border: 1px solid #CCCCCC;
}

.code-comparison-title, .code-comparison-code {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #000000;
vertical-align: top;
padding: 4px;
width: 50%;
background-color: #F1F1F1;
line-height: 15px;
}

.code-comparison-code {
font-family: Courier;
background-color: #F9F9F9;
}

.code-comparison-highlight {
background-color: #DDF1F7;
border: 1px solid #00A5E3;
line-height: 15px;
}

.tag-line {
text-align: center;
width: 100%;
margin-top: 30px;
font-size: 12px;
}

.tag-line a {
color: #000000;
}
</style>
</head>
<body>
<h1>GeneratorTest Coding Standards</h1>
<h2>Table of Contents</h2>
<ul class="toc">
<li><a href="#No-Content">No Content</a></li>
<li><a href="#Code-Comparison-Only,-Missing-Standard-Block">Code Comparison Only, Missing Standard Block</a></li>
<li><a href="#One-Standard-Block,-Code-Comparison">One Standard Block, Code Comparison</a></li>
<li><a href="#One-Standard-Block,-No-Code">One Standard Block, No Code</a></li>
<li><a href="#One-Standard-Block,-Two-Code-Comparisons">One Standard Block, Two Code Comparisons</a></li>
<li><a href="#Two-Standard-Blocks,-No-Code">Two Standard Blocks, No Code</a></li>
<li><a href="#Two-Standard-Blocks,-One-Code-Comparison">Two Standard Blocks, One Code Comparison</a></li>
<li><a href="#Two-Standard-Blocks,-Three-Code-Comparisons">Two Standard Blocks, Three Code Comparisons</a></li>
</ul>
<a name="No-Content" />
<h2>No Content</h2>
<a name="Code-Comparison-Only,-Missing-Standard-Block" />
<h2>Code Comparison Only, Missing Standard Block</h2>
<table class="code-comparison">
<tr>
<td class="code-comparison-title">Valid: Lorem ipsum dolor sit amet.</td>
<td class="code-comparison-title">Invalid: Maecenas non rutrum dolor.</td>
</tr>
<tr>
<td class="code-comparison-code"><span class="code-comparison-highlight">class&nbsp;Code</span>&nbsp;{}</td>
<td class="code-comparison-code"><span class="code-comparison-highlight">class&nbsp;Comparison</span>&nbsp;{}</td>
</tr>
</table>
<a name="One-Standard-Block,-Code-Comparison" />
<h2>One Standard Block, Code Comparison</h2>
<p class="text">Documentation contains one standard block and one code comparison.</p>
<table class="code-comparison">
<tr>
<td class="code-comparison-title">Valid: Lorem ipsum dolor sit amet.</td>
<td class="code-comparison-title">Invalid: Maecenas non rutrum dolor.</td>
</tr>
<tr>
<td class="code-comparison-code"><span class="code-comparison-highlight">class&nbsp;Code</span>&nbsp;{}</td>
<td class="code-comparison-code"><span class="code-comparison-highlight">class&nbsp;Comparison</span>&nbsp;{}</td>
</tr>
</table>
<a name="One-Standard-Block,-No-Code" />
<h2>One Standard Block, No Code</h2>
<p class="text">Documentation contains one standard block and no code comparison.</p>
<a name="One-Standard-Block,-Two-Code-Comparisons" />
<h2>One Standard Block, Two Code Comparisons</h2>
<p class="text">Documentation contains one standard block and two code comparisons.</p>
<table class="code-comparison">
<tr>
<td class="code-comparison-title">Valid: Etiam commodo magna at vestibulum blandit.</td>
<td class="code-comparison-title">Invalid: Vivamus lacinia ante velit.</td>
</tr>
<tr>
<td class="code-comparison-code"><span class="code-comparison-highlight">class&nbsp;Code</span>&nbsp;{}</td>
<td class="code-comparison-code"><span class="code-comparison-highlight">class&nbsp;Comparison</span>&nbsp;{}</td>
</tr>
</table>
<table class="code-comparison">
<tr>
<td class="code-comparison-title">Valid: Pellentesque nisi neque.</td>
<td class="code-comparison-title">Invalid: Mauris dictum metus quis maximus pharetra.</td>
</tr>
<tr>
<td class="code-comparison-code"><span class="code-comparison-highlight">$one</span>&nbsp;=&nbsp;10;</td>
<td class="code-comparison-code"><span class="code-comparison-highlight">$a</span>&nbsp;=&nbsp;10;</td>
</tr>
</table>
<a name="Two-Standard-Blocks,-No-Code" />
<h2>Two Standard Blocks, No Code</h2>
<p class="text">This is standard block one.</p>
<p class="text">This is standard block two.</p>
<a name="Two-Standard-Blocks,-One-Code-Comparison" />
<h2>Two Standard Blocks, One Code Comparison</h2>
<p class="text">This is standard block one.</p>
<table class="code-comparison">
<tr>
<td class="code-comparison-title">Valid: Vestibulum et orci condimentum.</td>
<td class="code-comparison-title">Invalid: Donec in nisl ut tortor convallis interdum.</td>
</tr>
<tr>
<td class="code-comparison-code">class&nbsp;<span class="code-comparison-highlight">Code</span>&nbsp;{}</td>
<td class="code-comparison-code">class&nbsp;<span class="code-comparison-highlight">Comparison</span>&nbsp;{}</td>
</tr>
</table>
<p class="text">This is standard block two.</p>
<a name="Two-Standard-Blocks,-Three-Code-Comparisons" />
<h2>Two Standard Blocks, Three Code Comparisons</h2>
<p class="text">This is standard block one.</p>
<table class="code-comparison">
<tr>
<td class="code-comparison-title">Valid: Vestibulum et orci condimentum.</td>
<td class="code-comparison-title">Invalid: Donec in nisl ut tortor convallis interdum.</td>
</tr>
<tr>
<td class="code-comparison-code"><span class="code-comparison-highlight">class&nbsp;Code</span>&nbsp;{}</td>
<td class="code-comparison-code"><span class="code-comparison-highlight">class&nbsp;Comparison</span>&nbsp;{}</td>
</tr>
</table>
<p class="text">This is standard block two.</p>
<table class="code-comparison">
<tr>
<td class="code-comparison-title">Valid: Pellentesque nisi neque.</td>
<td class="code-comparison-title">Invalid: Mauris dictum metus quis maximus pharetra.</td>
</tr>
<tr>
<td class="code-comparison-code"><span class="code-comparison-highlight">$one</span>&nbsp;=&nbsp;10;</td>
<td class="code-comparison-code"><span class="code-comparison-highlight">$a</span>&nbsp;=&nbsp;10;</td>
</tr>
</table>
<table class="code-comparison">
<tr>
<td class="code-comparison-title">Valid: Quisque sagittis nisi vitae.</td>
<td class="code-comparison-title">Invalid: Morbi ac libero vitae lorem.</td>
</tr>
<tr>
<td class="code-comparison-code"><span class="code-comparison-highlight">echo</span>&nbsp;$foo;</td>
<td class="code-comparison-code"><span class="code-comparison-highlight">print</span>&nbsp;$foo;</td>
</tr>
</table>
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
</body>
</html>
171 changes: 171 additions & 0 deletions tests/Core/Generators/Expectations/ExpectedOutputStructureDocs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# GeneratorTest Coding Standard

## No Content

## Code Comparison Only, Missing Standard Block
<table>
<tr>
<th>Valid: Lorem ipsum dolor sit amet.</th>
<th>Invalid: Maecenas non rutrum dolor.</th>
</tr>
<tr>
<td>

class Code {}

</td>
<td>

class Comparison {}

</td>
</tr>
</table>

## One Standard Block, Code Comparison
Documentation contains one standard block and one code comparison.
<table>
<tr>
<th>Valid: Lorem ipsum dolor sit amet.</th>
<th>Invalid: Maecenas non rutrum dolor.</th>
</tr>
<tr>
<td>

class Code {}

</td>
<td>

class Comparison {}

</td>
</tr>
</table>

## One Standard Block, No Code
Documentation contains one standard block and no code comparison.

## One Standard Block, Two Code Comparisons
Documentation contains one standard block and two code comparisons.
<table>
<tr>
<th>Valid: Etiam commodo magna at vestibulum blandit.</th>
<th>Invalid: Vivamus lacinia ante velit.</th>
</tr>
<tr>
<td>

class Code {}

</td>
<td>

class Comparison {}

</td>
</tr>
</table>
<table>
<tr>
<th>Valid: Pellentesque nisi neque.</th>
<th>Invalid: Mauris dictum metus quis maximus pharetra.</th>
</tr>
<tr>
<td>

$one = 10;

</td>
<td>

$a = 10;

</td>
</tr>
</table>

## Two Standard Blocks, No Code
This is standard block one.
This is standard block two.

## Two Standard Blocks, One Code Comparison
This is standard block one.
<table>
<tr>
<th>Valid: Vestibulum et orci condimentum.</th>
<th>Invalid: Donec in nisl ut tortor convallis interdum.</th>
</tr>
<tr>
<td>

class Code {}

</td>
<td>

class Comparison {}

</td>
</tr>
</table>
This is standard block two.

## Two Standard Blocks, Three Code Comparisons
This is standard block one.
<table>
<tr>
<th>Valid: Vestibulum et orci condimentum.</th>
<th>Invalid: Donec in nisl ut tortor convallis interdum.</th>
</tr>
<tr>
<td>

class Code {}

</td>
<td>

class Comparison {}

</td>
</tr>
</table>
This is standard block two.
<table>
<tr>
<th>Valid: Pellentesque nisi neque.</th>
<th>Invalid: Mauris dictum metus quis maximus pharetra.</th>
</tr>
<tr>
<td>

$one = 10;

</td>
<td>

$a = 10;

</td>
</tr>
</table>
<table>
<tr>
<th>Valid: Quisque sagittis nisi vitae.</th>
<th>Invalid: Morbi ac libero vitae lorem.</th>
</tr>
<tr>
<td>

echo $foo;

</td>
<td>

print $foo;

</td>
</tr>
</table>
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)
111 changes: 111 additions & 0 deletions tests/Core/Generators/Expectations/ExpectedOutputStructureDocs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@

---------------------------------------------
| GENERATORTEST CODING STANDARD: NO CONTENT |
---------------------------------------------


-------------------------------------------------------------------------------
| GENERATORTEST CODING STANDARD: CODE COMPARISON ONLY, MISSING STANDARD BLOCK |
-------------------------------------------------------------------------------

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Lorem ipsum dolor sit amet. | Invalid: Maecenas non rutrum dolor. |
----------------------------------------------------------------------------------------------------
| class Code {} | class Comparison {} |
----------------------------------------------------------------------------------------------------


----------------------------------------------------------------------
| GENERATORTEST CODING STANDARD: ONE STANDARD BLOCK, CODE COMPARISON |
----------------------------------------------------------------------

Documentation contains one standard block and one code comparison.

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Lorem ipsum dolor sit amet. | Invalid: Maecenas non rutrum dolor. |
----------------------------------------------------------------------------------------------------
| class Code {} | class Comparison {} |
----------------------------------------------------------------------------------------------------


--------------------------------------------------------------
| GENERATORTEST CODING STANDARD: ONE STANDARD BLOCK, NO CODE |
--------------------------------------------------------------

Documentation contains one standard block and no code comparison.


---------------------------------------------------------------------------
| GENERATORTEST CODING STANDARD: ONE STANDARD BLOCK, TWO CODE COMPARISONS |
---------------------------------------------------------------------------

Documentation contains one standard block and two code comparisons.

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Etiam commodo magna at vestibulum | Invalid: Vivamus lacinia ante velit. |
| blandit. | |
----------------------------------------------------------------------------------------------------
| class Code {} | class Comparison {} |
----------------------------------------------------------------------------------------------------

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Pellentesque nisi neque. | Invalid: Mauris dictum metus quis maximus |
| | pharetra. |
----------------------------------------------------------------------------------------------------
| $one = 10; | $a = 10; |
----------------------------------------------------------------------------------------------------


---------------------------------------------------------------
| GENERATORTEST CODING STANDARD: TWO STANDARD BLOCKS, NO CODE |
---------------------------------------------------------------

This is standard block one.

This is standard block two.


---------------------------------------------------------------------------
| GENERATORTEST CODING STANDARD: TWO STANDARD BLOCKS, ONE CODE COMPARISON |
---------------------------------------------------------------------------

This is standard block one.

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Vestibulum et orci condimentum. | Invalid: Donec in nisl ut tortor convallis |
| | interdum. |
----------------------------------------------------------------------------------------------------
| class Code {} | class Comparison {} |
----------------------------------------------------------------------------------------------------

This is standard block two.


------------------------------------------------------------------------------
| GENERATORTEST CODING STANDARD: TWO STANDARD BLOCKS, THREE CODE COMPARISONS |
------------------------------------------------------------------------------

This is standard block one.

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Vestibulum et orci condimentum. | Invalid: Donec in nisl ut tortor convallis |
| | interdum. |
----------------------------------------------------------------------------------------------------
| class Code {} | class Comparison {} |
----------------------------------------------------------------------------------------------------

This is standard block two.

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Pellentesque nisi neque. | Invalid: Mauris dictum metus quis maximus |
| | pharetra. |
----------------------------------------------------------------------------------------------------
| $one = 10; | $a = 10; |
----------------------------------------------------------------------------------------------------

----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: Quisque sagittis nisi vitae. | Invalid: Morbi ac libero vitae lorem. |
----------------------------------------------------------------------------------------------------
| echo $foo; | print $foo; |
----------------------------------------------------------------------------------------------------

40 changes: 40 additions & 0 deletions tests/Core/Generators/Fixtures/HTMLDouble.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Test double for the HTML doc generator.
*
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Generators\Fixtures;

use PHP_CodeSniffer\Generators\HTML;

class HTMLDouble extends HTML
{

/**
* Print the footer of the HTML page without the date or version nr to make the expectation fixtures stable.
*
* @return void
*/
protected function printFooter()
{
echo ' <div class="tag-line">';
echo 'Documentation generated on #REDACTED#';
echo ' by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a>';
echo '</div>'.PHP_EOL;
echo ' </body>'.PHP_EOL;
echo '</html>'.PHP_EOL;
}

/**
* Print the _real_ footer of the HTML page.
*
* @return void
*/
public function printRealFooter()
{
parent::printFooter();
}
}
36 changes: 36 additions & 0 deletions tests/Core/Generators/Fixtures/MarkdownDouble.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Test double for the Markdown doc generator.
*
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Generators\Fixtures;

use PHP_CodeSniffer\Generators\Markdown;

class MarkdownDouble extends Markdown
{

/**
* Print the markdown footer without the date or version nr to make the expectation fixtures stable.
*
* @return void
*/
protected function printFooter()
{
echo 'Documentation generated on *REDACTED*';
echo ' by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)'.PHP_EOL;
}

/**
* Print the _real_ footer of the markdown page.
*
* @return void
*/
public function printRealFooter()
{
parent::printFooter();
}
}
28 changes: 28 additions & 0 deletions tests/Core/Generators/Fixtures/MockGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Mock generator class.
*
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Generators\Fixtures;

use DOMNode;
use PHP_CodeSniffer\Generators\Generator;

class MockGenerator extends Generator
{

/**
* Process the documentation for a single sniff.
*
* @param \DOMNode $doc The DOMNode object for the sniff.
*
* @return void
*/
protected function processSniff(DOMNode $doc)
{
echo $this->getTitle($doc), PHP_EOL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<documentation title="No Content">
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<something title="No Documentation Element">
</something>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<documentation title="Code Comparison Only, Missing Standard Block">
<code_comparison>
<code title="Valid: Lorem ipsum dolor sit amet.">
<![CDATA[
<em>class Code</em> {}
]]>
</code>
<code title="Invalid: Maecenas non rutrum dolor.">
<![CDATA[
<em>class Comparison</em> {}
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<documentation title="One Standard Block, Code Comparison">
<standard>
<![CDATA[
Documentation contains one standard block and one code comparison.
]]>
</standard>
<code_comparison>
<code title="Valid: Lorem ipsum dolor sit amet.">
<![CDATA[
<em>class Code</em> {}
]]>
</code>
<code title="Invalid: Maecenas non rutrum dolor.">
<![CDATA[
<em>class Comparison</em> {}
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<documentation title="One Standard Block, No Code">
<standard>
<![CDATA[
Documentation contains one standard block and no code comparison.
]]>
</standard>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<documentation title="One Standard Block, Two Code Comparisons">
<standard>
<![CDATA[
Documentation contains one standard block and two code comparisons.
]]>
</standard>
<code_comparison>
<code title="Valid: Etiam commodo magna at vestibulum blandit.">
<![CDATA[
<em>class Code</em> {}
]]>
</code>
<code title="Invalid: Vivamus lacinia ante velit.">
<![CDATA[
<em>class Comparison</em> {}
]]>
</code>
</code_comparison>
<code_comparison>
<code title="Valid: Pellentesque nisi neque.">
<![CDATA[
<em>$one</em> = 10;
]]>
</code>
<code title="Invalid: Mauris dictum metus quis maximus pharetra.">
<![CDATA[
<em>$a</em> = 10;
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<documentation title="Two Standard Blocks, No Code">
<standard>
<![CDATA[
This is standard block one.
]]>
</standard>
<standard>
<![CDATA[
This is standard block two.
]]>
</standard>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<documentation title="Two Standard Blocks, One Code Comparison">
<standard>
<![CDATA[
This is standard block one.
]]>
</standard>
<code_comparison>
<code title="Valid: Vestibulum et orci condimentum.">
<![CDATA[
class <em>Code</em> {}
]]>
</code>
<code title="Invalid: Donec in nisl ut tortor convallis interdum.">
<![CDATA[
class <em>Comparison</em> {}
]]>
</code>
</code_comparison>
<standard>
<![CDATA[
This is standard block two.
]]>
</standard>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<documentation title="Two Standard Blocks, Three Code Comparisons">
<standard>
<![CDATA[
This is standard block one.
]]>
</standard>
<code_comparison>
<code title="Valid: Vestibulum et orci condimentum.">
<![CDATA[
<em>class Code</em> {}
]]>
</code>
<code title="Invalid: Donec in nisl ut tortor convallis interdum.">
<![CDATA[
<em>class Comparison</em> {}
]]>
</code>
</code_comparison>
<standard>
<![CDATA[
This is standard block two.
]]>
</standard>
<code_comparison>
<code title="Valid: Pellentesque nisi neque.">
<![CDATA[
<em>$one</em> = 10;
]]>
</code>
<code title="Invalid: Mauris dictum metus quis maximus pharetra.">
<![CDATA[
<em>$a</em> = 10;
]]>
</code>
</code_comparison>
<code_comparison>
<code title="Valid: Quisque sagittis nisi vitae.">
<![CDATA[
<em>echo</em> $foo;
]]>
</code>
<code title="Invalid: Morbi ac libero vitae lorem.">
<![CDATA[
<em>print</em> $foo;
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Test fixture.
*
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
*/

namespace Fixtures\StandardWithDocs\Sniffs;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

abstract class DummySniff implements Sniff
{

public function register()
{
return [T_WHITESPACE];
}

public function process(File $phpcsFile, $stackPtr)
{
// Do something.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Test fixture.
*
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
*/

namespace Fixtures\StandardWithDocs\Sniffs\Structure;

use Fixtures\StandardWithDocs\Sniffs\DummySniff;

final class DocumentationMissingSniff extends DummySniff {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Test fixture.
*
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
*/

namespace Fixtures\StandardWithDocs\Sniffs\Structure;

use Fixtures\StandardWithDocs\Sniffs\DummySniff;

final class NoContentSniff extends DummySniff {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Test fixture.
*
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
*/

namespace Fixtures\StandardWithDocs\Sniffs\Structure;

use Fixtures\StandardWithDocs\Sniffs\DummySniff;

final class NoDocumentationElementSniff extends DummySniff {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Test fixture.
*
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
*/

namespace Fixtures\StandardWithDocs\Sniffs\Structure;

use Fixtures\StandardWithDocs\Sniffs\DummySniff;

final class OneCodeComparisonNoStandardSniff extends DummySniff {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Test fixture.
*
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
*/

namespace Fixtures\StandardWithDocs\Sniffs\Structure;

use Fixtures\StandardWithDocs\Sniffs\DummySniff;

final class OneStandardBlockCodeComparisonSniff extends DummySniff {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Test fixture.
*
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
*/

namespace Fixtures\StandardWithDocs\Sniffs\Structure;

use Fixtures\StandardWithDocs\Sniffs\DummySniff;

final class OneStandardBlockNoCodeSniff extends DummySniff {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Test fixture.
*
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
*/

namespace Fixtures\StandardWithDocs\Sniffs\Structure;

use Fixtures\StandardWithDocs\Sniffs\DummySniff;

final class OneStandardBlockTwoCodeComparisonsSniff extends DummySniff {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Test fixture.
*
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
*/

namespace Fixtures\StandardWithDocs\Sniffs\Structure;

use Fixtures\StandardWithDocs\Sniffs\DummySniff;

final class TwoStandardBlocksNoCodeSniff extends DummySniff {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Test fixture.
*
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
*/

namespace Fixtures\StandardWithDocs\Sniffs\Structure;

use Fixtures\StandardWithDocs\Sniffs\DummySniff;

final class TwoStandardBlocksOneCodeComparisonSniff extends DummySniff {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Test fixture.
*
* @see \PHP_CodeSniffer\Tests\Core\Generators\GeneratorTest
*/

namespace Fixtures\StandardWithDocs\Sniffs\Structure;

use Fixtures\StandardWithDocs\Sniffs\DummySniff;

final class TwoStandardBlocksThreeCodeComparisonsSniff extends DummySniff {}
4 changes: 4 additions & 0 deletions tests/Core/Generators/Fixtures/StandardWithDocs/ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="StandardWithDocs" namespace="Fixtures\StandardWithDocs" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">

</ruleset>
241 changes: 241 additions & 0 deletions tests/Core/Generators/GeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
<?php
/**
* Tests the functionality in the abstract Generator class.
*
* @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\Ruleset;
use PHP_CodeSniffer\Runner;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHP_CodeSniffer\Tests\Core\Generators\Fixtures\MockGenerator;
use PHPUnit\Framework\TestCase;

/**
* Tests the functionality in the abstract Generator class.
*
* @covers \PHP_CodeSniffer\Generators\Generator
* @group Windows
*/
final class GeneratorTest extends TestCase
{


/**
* Test the list of available documentation for a standard is generated correctly.
*
* @param string $standard The standard to use for the test.
* @param array<string> $expected The expected list of found docs.
*
* @dataProvider dataConstructor
*
* @return void
*/
public function testConstructor($standard, array $expected)
{
// Set up the ruleset.
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);

$generator = new MockGenerator($ruleset);
$this->assertSame($expected, $generator->docFiles);

}//end testConstructor()


/**
* Data provider.
*
* @return array<string, array<string, string|array<string>>>
*/
public static function dataConstructor()
{
$pathToDocsInFixture = __DIR__.DIRECTORY_SEPARATOR.'Fixtures';
$pathToDocsInFixture .= DIRECTORY_SEPARATOR.'StandardWithDocs';
$pathToDocsInFixture .= DIRECTORY_SEPARATOR.'Docs'.DIRECTORY_SEPARATOR;

return [
'Standard without docs' => [
'standard' => __DIR__.'/NoDocsTest.xml',
'expected' => [],
],
'Standard with an invalid doc file' => [
'standard' => __DIR__.'/NoValidDocsTest.xml',
'expected' => [
$pathToDocsInFixture.'Structure'.DIRECTORY_SEPARATOR.'NoDocumentationElementStandard.xml',
],
],
'Standard with one doc file' => [
'standard' => __DIR__.'/OneDocTest.xml',
'expected' => [
$pathToDocsInFixture.'Structure'.DIRECTORY_SEPARATOR.'OneStandardBlockNoCodeStandard.xml',
],
],
'Standard with multiple doc files' => [
'standard' => __DIR__.'/StructureDocsTest.xml',
'expected' => [
$pathToDocsInFixture.'Structure'.DIRECTORY_SEPARATOR.'NoContentStandard.xml',
$pathToDocsInFixture.'Structure'.DIRECTORY_SEPARATOR.'OneCodeComparisonNoStandardStandard.xml',
$pathToDocsInFixture.'Structure'.DIRECTORY_SEPARATOR.'OneStandardBlockCodeComparisonStandard.xml',
$pathToDocsInFixture.'Structure'.DIRECTORY_SEPARATOR.'OneStandardBlockNoCodeStandard.xml',
$pathToDocsInFixture.'Structure'.DIRECTORY_SEPARATOR.'OneStandardBlockTwoCodeComparisonsStandard.xml',
$pathToDocsInFixture.'Structure'.DIRECTORY_SEPARATOR.'TwoStandardBlocksNoCodeStandard.xml',
$pathToDocsInFixture.'Structure'.DIRECTORY_SEPARATOR.'TwoStandardBlocksOneCodeComparisonStandard.xml',
$pathToDocsInFixture.'Structure'.DIRECTORY_SEPARATOR.'TwoStandardBlocksThreeCodeComparisonsStandard.xml',
],
],
];

}//end dataConstructor()


/**
* Verify that an XML doc which isn't valid documentation yields an Exception to warn devs.
*
* This should not be hidden via defensive coding!
*
* @return void
*/
public function testGeneratingInvalidDocsResultsInException()
{
// Set up the ruleset.
$standard = __DIR__.'/NoValidDocsTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);

if (PHP_VERSION_ID >= 80000) {
$exception = 'TypeError';
$message = 'processSniff(): Argument #1 ($doc) must be of type DOMNode, null given';
} else if (PHP_VERSION_ID >= 70000) {
$exception = 'TypeError';
$message = 'processSniff() must be an instance of DOMNode, null given';
} else {
$exception = 'PHPUnit_Framework_Error';
$message = 'processSniff() must be an instance of DOMNode, null given';
}

if (method_exists($this, 'expectExceptionMessage') === true) {
// PHPUnit 5.2.0+.
$this->expectException($exception);
$this->expectExceptionMessage($message);
} else {
// Ancient PHPUnit.
$this->setExpectedException($exception, $message);
}

$generator = new MockGenerator($ruleset);
$generator->generate();

}//end testGeneratingInvalidDocsResultsInException()


/**
* Verify the wiring for the generate() function.
*
* @param string $standard The standard to use for the test.
* @param string $expected The expected function output.
*
* @dataProvider dataGeneratingDocs
*
* @return void
*/
public function testGeneratingDocs($standard, $expected)
{
// Set up the ruleset.
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);

$this->expectOutputString($expected);

$generator = new MockGenerator($ruleset);
$generator->generate();

}//end testGeneratingDocs()


/**
* Data provider.
*
* @return array<string, array<string, string>>
*/
public static function dataGeneratingDocs()
{
$multidocExpected = [];
$multidocExpected[] = 'No Content';
$multidocExpected[] = 'Code Comparison Only, Missing Standard Block';
$multidocExpected[] = 'One Standard Block, Code Comparison';
$multidocExpected[] = 'One Standard Block, No Code';
$multidocExpected[] = 'One Standard Block, Two Code Comparisons';
$multidocExpected[] = 'Two Standard Blocks, No Code';
$multidocExpected[] = 'Two Standard Blocks, One Code Comparison';
$multidocExpected[] = 'Two Standard Blocks, Three Code Comparisons';
$multidocExpected = implode(PHP_EOL, $multidocExpected).PHP_EOL;

return [
'Standard without docs' => [
'standard' => __DIR__.'/NoDocsTest.xml',
'expected' => '',
],
'Standard with one doc file' => [
'standard' => __DIR__.'/OneDocTest.xml',
'expected' => 'One Standard Block, No Code'.PHP_EOL,
],
'Standard with multiple doc files' => [
'standard' => __DIR__.'/StructureDocsTest.xml',
'expected' => $multidocExpected,
],
];

}//end dataGeneratingDocs()


/**
* Test that the documentation for each standard passed on the command-line is shown separately.
*
* @covers \PHP_CodeSniffer\Runner::runPHPCS
*
* @return void
*/
public function testGeneratorWillShowEachStandardSeparately()
{
$standard = __DIR__.'/OneDocTest.xml';
$_SERVER['argv'] = [
'phpcs',
'--generator=Text',
"--standard=$standard,PSR1",
'--report-width=80',
];

$regex = '`^
\R* # Optional blank line at the start.
(?:
(?P<delimiter>-+\R) # Line with dashes.
\|[ ]GENERATORTEST[ ]CODING[ ]STANDARD:[ ][^\|]+\|\R # Doc title line with prefix expected for first standard.
(?P>delimiter) # Line with dashes.
.+?\R{2} # Standard description.
) # Only expect this group once.
(?:
(?P>delimiter) # Line with dashes.
\|[ ]PSR1[ ]CODING[ ]STANDARD:[ ][^\|]+\|\R # Doc title line with prefix expected for second standard.
(?P>delimiter) # Line with dashes.
.+?\R+ # Standard description.
(?:
-+[ ]CODE[ ]COMPARISON[ ]-+\R # Code Comparison starter line with dashes.
(?:.+?(?P>delimiter)\R){2} # Arbitrary text followed by a delimiter line.
)* # Code comparison is optional and can exist multiple times.
\R+
){3,} # This complete group should occur at least three times.
`sx';

$this->expectOutputRegex($regex);

$runner = new Runner();
$runner->runPHPCS();

}//end testGeneratorWillShowEachStandardSeparately()


}//end class
104 changes: 104 additions & 0 deletions tests/Core/Generators/HTMLTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Tests the HTML 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\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHP_CodeSniffer\Tests\Core\Generators\Fixtures\HTMLDouble;
use PHPUnit\Framework\TestCase;

/**
* Test the HTML documentation generation.
*
* @covers \PHP_CodeSniffer\Generators\HTML
* @group Windows
*/
final class HTMLTest extends TestCase
{


/**
* 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 HTMLDouble($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/ExpectedOutputNoDocs.html',
],
'Standard with one doc file' => [
'standard' => __DIR__.'/OneDocTest.xml',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputOneDoc.html',
],
'Standard with multiple doc files' => [
'standard' => __DIR__.'/StructureDocsTest.xml',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputStructureDocs.html',
],
];

}//end dataDocs()


/**
* 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 = '`^ <div class="tag-line">';
$regex .= 'Documentation 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 <a href="https://github\.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer [3-9]\.[0-9]+.[0-9]+</a>';
$regex .= '</div>\R </body>\R</html>\R$`';
$this->expectOutputRegex($regex);

$generator = new HTMLDouble($ruleset);
$generator->printRealFooter();

}//end testFooter()


}//end class
102 changes: 102 additions & 0 deletions tests/Core/Generators/MarkdownTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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\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
{


/**
* 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/ExpectedOutputNoDocs.md',
],
'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 footer.
*
* @return void
*/
public function testFooter()
{
// Set up the ruleset.
$standard = __DIR__.'/OneDocTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);

$regex = '`^Documentation 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$`';
$this->expectOutputRegex($regex);

$generator = new MarkdownDouble($ruleset);
$generator->printRealFooter();

}//end testFooter()


}//end class
8 changes: 8 additions & 0 deletions tests/Core/Generators/NoDocsTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="GeneratorTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">

<config name="installed_paths" value="./tests/Core/Generators/Fixtures/"/>

<rule ref="StandardWithDocs.Structure.DocumentationMissing"/>

</ruleset>
8 changes: 8 additions & 0 deletions tests/Core/Generators/NoValidDocsTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="GeneratorTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">

<config name="installed_paths" value="./tests/Core/Generators/Fixtures/"/>

<rule ref="StandardWithDocs.Structure.NoDocumentationElement"/>

</ruleset>
8 changes: 8 additions & 0 deletions tests/Core/Generators/OneDocTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="GeneratorTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">

<config name="installed_paths" value="./tests/Core/Generators/Fixtures/"/>

<rule ref="StandardWithDocs.Structure.OneStandardBlockNoCode"/>

</ruleset>
10 changes: 10 additions & 0 deletions tests/Core/Generators/StructureDocsTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="GeneratorTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">

<config name="installed_paths" value="./tests/Core/Generators/Fixtures/"/>

<rule ref="StandardWithDocs.Structure">
<exclude name="StandardWithDocs.Structure.NoDocumentationElement"/>
</rule>

</ruleset>
80 changes: 80 additions & 0 deletions tests/Core/Generators/TextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Tests the Text 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\Generators\Text;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;

/**
* Test the Text documentation generation.
*
* @covers \PHP_CodeSniffer\Generators\Text
* @group Windows
*/
final class TextTest extends TestCase
{


/**
* 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 Text($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.txt',
],
'Standard with multiple doc files' => [
'standard' => __DIR__.'/StructureDocsTest.xml',
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputStructureDocs.txt',
],
];

}//end dataDocs()


}//end class