From 8b8aa2e4b11fb60ddea76fae6d2a6b5fbcffa8f8 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 4 Nov 2024 00:03:59 +0100 Subject: [PATCH] Generators: add tests for handling documentation titles This adds dedicated tests for specific issues which can be encountered with the `title` attribute for `` elements. This initial set of tests for this documents the current behaviour. This behaviour may not always be the desired behaviour, in which case, this will be fixed in follow-up commits. --- tests/Core/Generators/AllValidDocsTest.xml | 10 +++ .../ExpectedOutputDocumentationTitleCase.html | 78 +++++++++++++++++++ .../ExpectedOutputDocumentationTitleCase.md | 6 ++ .../ExpectedOutputDocumentationTitleCase.txt | 7 ++ ...xpectedOutputDocumentationTitleLength.html | 78 +++++++++++++++++++ .../ExpectedOutputDocumentationTitleLength.md | 6 ++ ...ExpectedOutputDocumentationTitleLength.txt | 7 ++ .../DocumentationTitleCaseStandard.xml | 7 ++ .../DocumentationTitleLengthStandard.xml | 7 ++ .../Content/DocumentationTitleCaseSniff.php | 12 +++ .../Content/DocumentationTitleLengthSniff.php | 12 +++ tests/Core/Generators/HTMLTest.php | 61 +++++++++++++++ tests/Core/Generators/MarkdownTest.php | 61 +++++++++++++++ tests/Core/Generators/TextTest.php | 61 +++++++++++++++ 14 files changed, 413 insertions(+) create mode 100644 tests/Core/Generators/AllValidDocsTest.xml create mode 100644 tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.html create mode 100644 tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.md create mode 100644 tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.txt create mode 100644 tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.html create mode 100644 tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.md create mode 100644 tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.txt create mode 100644 tests/Core/Generators/Fixtures/StandardWithDocs/Docs/Content/DocumentationTitleCaseStandard.xml create mode 100644 tests/Core/Generators/Fixtures/StandardWithDocs/Docs/Content/DocumentationTitleLengthStandard.xml create mode 100644 tests/Core/Generators/Fixtures/StandardWithDocs/Sniffs/Content/DocumentationTitleCaseSniff.php create mode 100644 tests/Core/Generators/Fixtures/StandardWithDocs/Sniffs/Content/DocumentationTitleLengthSniff.php diff --git a/tests/Core/Generators/AllValidDocsTest.xml b/tests/Core/Generators/AllValidDocsTest.xml new file mode 100644 index 0000000000..71a0c7f7a5 --- /dev/null +++ b/tests/Core/Generators/AllValidDocsTest.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.html b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.html new file mode 100644 index 0000000000..98184e1e85 --- /dev/null +++ b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.html @@ -0,0 +1,78 @@ + + + GeneratorTest Coding Standards + + + +

GeneratorTest Coding Standards

+ +

lowercase title

+

This is a standard block.

+
+ + diff --git a/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.md b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.md new file mode 100644 index 0000000000..d0464a4efa --- /dev/null +++ b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.md @@ -0,0 +1,6 @@ +# GeneratorTest Coding Standard + +## lowercase title +This is a standard block. + +Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer) diff --git a/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.txt b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.txt new file mode 100644 index 0000000000..c762a01d96 --- /dev/null +++ b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleCase.txt @@ -0,0 +1,7 @@ + +-------------------------------------------------- +| GENERATORTEST CODING STANDARD: LOWERCASE TITLE | +-------------------------------------------------- + +This is a standard block. + diff --git a/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.html b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.html new file mode 100644 index 0000000000..e94beac1ca --- /dev/null +++ b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.html @@ -0,0 +1,78 @@ + + + GeneratorTest Coding Standards + + + +

GeneratorTest Coding Standards

+ +

This is a very very very very very very very very very very very long title

+

This is a standard block.

+
+ + diff --git a/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.md b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.md new file mode 100644 index 0000000000..29f5972a7c --- /dev/null +++ b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.md @@ -0,0 +1,6 @@ +# GeneratorTest Coding Standard + +## This is a very very very very very very very very very very very long title +This is a standard block. + +Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer) diff --git a/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.txt b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.txt new file mode 100644 index 0000000000..2787b8cdfe --- /dev/null +++ b/tests/Core/Generators/Expectations/ExpectedOutputDocumentationTitleLength.txt @@ -0,0 +1,7 @@ + +-------------------------------------------------------------------------------------------------------------- +| GENERATORTEST CODING STANDARD: THIS IS A VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY LONG TITLE | +-------------------------------------------------------------------------------------------------------------- + +This is a standard block. + diff --git a/tests/Core/Generators/Fixtures/StandardWithDocs/Docs/Content/DocumentationTitleCaseStandard.xml b/tests/Core/Generators/Fixtures/StandardWithDocs/Docs/Content/DocumentationTitleCaseStandard.xml new file mode 100644 index 0000000000..a4078e3dee --- /dev/null +++ b/tests/Core/Generators/Fixtures/StandardWithDocs/Docs/Content/DocumentationTitleCaseStandard.xml @@ -0,0 +1,7 @@ + + + + + diff --git a/tests/Core/Generators/Fixtures/StandardWithDocs/Docs/Content/DocumentationTitleLengthStandard.xml b/tests/Core/Generators/Fixtures/StandardWithDocs/Docs/Content/DocumentationTitleLengthStandard.xml new file mode 100644 index 0000000000..2448927640 --- /dev/null +++ b/tests/Core/Generators/Fixtures/StandardWithDocs/Docs/Content/DocumentationTitleLengthStandard.xml @@ -0,0 +1,7 @@ + + + + + diff --git a/tests/Core/Generators/Fixtures/StandardWithDocs/Sniffs/Content/DocumentationTitleCaseSniff.php b/tests/Core/Generators/Fixtures/StandardWithDocs/Sniffs/Content/DocumentationTitleCaseSniff.php new file mode 100644 index 0000000000..09ffdac68e --- /dev/null +++ b/tests/Core/Generators/Fixtures/StandardWithDocs/Sniffs/Content/DocumentationTitleCaseSniff.php @@ -0,0 +1,12 @@ + true]; + $ruleset->registerSniffs([$sniffFile], $restrictions, []); + + $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 testDocSpecifics() + + + /** + * Data provider. + * + * @return array> + */ + public static function dataDocSpecifics() + { + return [ + 'Documentation title: case' => [ + 'sniffs' => 'StandardWithDocs.Content.DocumentationTitleCase', + 'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputDocumentationTitleCase.html', + ], + 'Documentation title: length' => [ + 'sniffs' => 'StandardWithDocs.Content.DocumentationTitleLength', + 'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputDocumentationTitleLength.html', + ], + ]; + + }//end dataDocSpecifics() + + /** * Test the generated footer. * diff --git a/tests/Core/Generators/MarkdownTest.php b/tests/Core/Generators/MarkdownTest.php index 0610678dbe..b7ba2f5fb1 100644 --- a/tests/Core/Generators/MarkdownTest.php +++ b/tests/Core/Generators/MarkdownTest.php @@ -77,6 +77,67 @@ public static function dataDocs() }//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); + + // In tests, the `--sniffs` setting doesn't work out of the box. + $sniffParts = explode('.', $sniffs); + $sniffFile = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.$sniffParts[0].DIRECTORY_SEPARATOR; + $sniffFile .= 'Sniffs'.DIRECTORY_SEPARATOR.$sniffParts[1].DIRECTORY_SEPARATOR.$sniffParts[2].'Sniff.php'; + + $sniffParts = array_map('strtolower', $sniffParts); + $sniffName = $sniffParts[0].'\sniffs\\'.$sniffParts[1].'\\'.$sniffParts[2].'sniff'; + $restrictions = [$sniffName => true]; + $ruleset->registerSniffs([$sniffFile], $restrictions, []); + + $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> + */ + 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', + ], + ]; + + }//end dataDocSpecifics() + + /** * Test the generated footer. * diff --git a/tests/Core/Generators/TextTest.php b/tests/Core/Generators/TextTest.php index 40544557f2..f571aff71d 100644 --- a/tests/Core/Generators/TextTest.php +++ b/tests/Core/Generators/TextTest.php @@ -77,4 +77,65 @@ public static function dataDocs() }//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); + + // In tests, the `--sniffs` setting doesn't work out of the box. + $sniffParts = explode('.', $sniffs); + $sniffFile = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.$sniffParts[0].DIRECTORY_SEPARATOR; + $sniffFile .= 'Sniffs'.DIRECTORY_SEPARATOR.$sniffParts[1].DIRECTORY_SEPARATOR.$sniffParts[2].'Sniff.php'; + + $sniffParts = array_map('strtolower', $sniffParts); + $sniffName = $sniffParts[0].'\sniffs\\'.$sniffParts[1].'\\'.$sniffParts[2].'sniff'; + $restrictions = [$sniffName => true]; + $ruleset->registerSniffs([$sniffFile], $restrictions, []); + + $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 testDocSpecifics() + + + /** + * Data provider. + * + * @return array> + */ + public static function dataDocSpecifics() + { + return [ + 'Documentation title: case' => [ + 'sniffs' => 'StandardWithDocs.Content.DocumentationTitleCase', + 'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputDocumentationTitleCase.txt', + ], + 'Documentation title: length' => [ + 'sniffs' => 'StandardWithDocs.Content.DocumentationTitleLength', + 'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputDocumentationTitleLength.txt', + ], + ]; + + }//end dataDocSpecifics() + + }//end class