|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPCSDevTools, tools for PHP_CodeSniffer sniff developers. |
| 4 | + * |
| 5 | + * @package PHPCSDevTools |
| 6 | + * @copyright 2019 PHPCSDevTools Contributors |
| 7 | + * @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
| 8 | + * @link https://github.com/PHPCSStandards/PHPCSDevTools |
| 9 | + */ |
| 10 | + |
| 11 | +namespace PHPCSDevTools\Tests\DocsXsd; |
| 12 | + |
| 13 | +use PHPCSDevTools\Tests\IOTestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test the Docs XSD feature. |
| 17 | + * |
| 18 | + * @coversNothing |
| 19 | + * |
| 20 | + * @phpcs:disable Squiz.Arrays.ArrayDeclaration.DoubleArrowNotAligned -- If needed, fix once replaced by better sniff. |
| 21 | + */ |
| 22 | +final class DocsXsdTest extends IOTestCase |
| 23 | +{ |
| 24 | + |
| 25 | + /** |
| 26 | + * Command that is being run during the test |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + const COMMAND = 'xmllint --noout --schema DocsXsd/phpcsdocs.xsd Tests/Fixtures/DocsXsd/%s'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Verify that the valid XSD doesn't throw errors |
| 34 | + * |
| 35 | + * Successful xmllint command will have the exit code 0, and contain the 'validates' |
| 36 | + * message in the stderr part of the result. |
| 37 | + * |
| 38 | + * @dataProvider dataValidXsd |
| 39 | + * |
| 40 | + * @param string $fixtureFile The name of the fixture file in the fixture directory. |
| 41 | + * |
| 42 | + * @return void |
| 43 | + */ |
| 44 | + public function testXsdValidationPassedWithValidXml($fixtureFile) |
| 45 | + { |
| 46 | + $command = \sprintf(self::COMMAND, $fixtureFile); |
| 47 | + $result = $this->executeCliCommand($command); |
| 48 | + |
| 49 | + $this->assertSame('', $result['stdout'], 'Unexpected output in stdout'); |
| 50 | + $this->assertStringContainsString( |
| 51 | + "Tests/Fixtures/DocsXsd/{$fixtureFile} validates", |
| 52 | + $result['stderr'], |
| 53 | + 'Unexpected output in stderr' |
| 54 | + ); |
| 55 | + $this->assertSame(0, $result['exitcode'], 'Exit code does not match 0'); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Data provider for valid test cases. |
| 60 | + * |
| 61 | + * @return array |
| 62 | + */ |
| 63 | + public function dataValidXsd() |
| 64 | + { |
| 65 | + return [ |
| 66 | + 'Valid docs example with single standard in the file' => [ |
| 67 | + 'fixtureFile' => 'ValidSingleStandard.xml', |
| 68 | + ], |
| 69 | + 'Valid docs example with multiple standards in the file' => [ |
| 70 | + 'fixtureFile' => 'ValidMultipleStandard.xml', |
| 71 | + ], |
| 72 | + 'Valid docs example with multiple arbitrary attributes on the <documentation> element' => [ |
| 73 | + 'fixtureFile' => 'ValidDocumentationWithAdditionalAttributes.xml', |
| 74 | + ], |
| 75 | + 'Valid docs example with multiple arbitrary attributes on the <standard> element' => [ |
| 76 | + 'fixtureFile' => 'ValidDocumentationWithAdditionalAttributesOnStandardElement.xml', |
| 77 | + ], |
| 78 | + 'Valid docs example with multiple code examples' => [ |
| 79 | + 'fixtureFile' => 'ValidMultipleCodeExamples.xml', |
| 80 | + ], |
| 81 | + 'Valid docs example without code comparison element in the sequence group' => [ |
| 82 | + 'fixtureFile' => 'ValidEmptyCodeComparisonElement.xml', |
| 83 | + ], |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Verify that an invalid PHPCS docs XML file will throw the correct validation errors |
| 89 | + * |
| 90 | + * @dataProvider dataInvalidXsd |
| 91 | + * |
| 92 | + * @param string $fixtureFile The name of the fixture file in the fixture directory. |
| 93 | + * @param string $expectedStdOut Expected output from the xmllint command. |
| 94 | + * @param string $expectedStdErr Expected validation error from the xmllint command. |
| 95 | + * |
| 96 | + * @return void |
| 97 | + */ |
| 98 | + public function testXsdValidationFailsForInvalidXml($fixtureFile, $expectedStdOut, $expectedStdErr) |
| 99 | + { |
| 100 | + $command = \sprintf(self::COMMAND, $fixtureFile); |
| 101 | + $result = $this->executeCliCommand($command); |
| 102 | + |
| 103 | + $this->assertSame($expectedStdOut, $result['stdout'], 'Unexpected output in stdout'); |
| 104 | + $this->assertStringContainsString($expectedStdErr, $result['stderr'], 'Unexpected output in stderr'); |
| 105 | + $this->assertGreaterThan(0, $result['exitcode'], 'Exit code does not match 0'); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Data provider for invalid test cases. |
| 110 | + * |
| 111 | + * @return array |
| 112 | + */ |
| 113 | + public function dataInvalidXsd() |
| 114 | + { |
| 115 | + return [ |
| 116 | + 'Title attribute too long on <documentation> element' => [ |
| 117 | + 'fixtureFile' => 'InvalidTitleTooLong.xml', |
| 118 | + 'expectedStdOut' => '', |
| 119 | + 'expectedStdErr' => "this exceeds the allowed maximum length of '58'", |
| 120 | + ], |
| 121 | + 'Documentation root element missing' => [ |
| 122 | + 'fixtureFile' => 'InvalidMissingDocumentationRoot.xml', |
| 123 | + 'expectedStdOut' => '', |
| 124 | + 'expectedStdErr' => "Element 'standard': No matching global declaration available for the validation root", |
| 125 | + ], |
| 126 | + 'Missing title attribute in the <documentation> root element' => [ |
| 127 | + 'fixtureFile' => 'InvalidMissingDocumentationTitleAttribute.xml', |
| 128 | + 'expectedStdOut' => '', |
| 129 | + 'expectedStdErr' => "Element 'documentation': The attribute 'title' is required but missing.", |
| 130 | + ], |
| 131 | + 'Missing standard element in the sequence group' => [ |
| 132 | + 'fixtureFile' => 'InvalidMissingStandardElement.xml', |
| 133 | + 'expectedStdOut' => '', |
| 134 | + 'expectedStdErr' => "Element 'code_comparison': This element is not expected. Expected is ( standard ).", |
| 135 | + ], |
| 136 | + 'Missing sequence group' => [ |
| 137 | + 'fixtureFile' => 'InvalidMissingRuleGroup.xml', |
| 138 | + 'expectedStdOut' => '', |
| 139 | + 'expectedStdErr' => "Element 'documentation': Missing child element(s). Expected is ( standard )", |
| 140 | + ], |
| 141 | + 'More than two code blocks in one comparison group' => [ |
| 142 | + 'fixtureFile' => 'InvalidMoreThanTwoCodeBlocksInComparison.xml', |
| 143 | + 'expectedStdOut' => '', |
| 144 | + 'expectedStdErr' => "Schemas validity error : Element 'code': This element is not expected.", |
| 145 | + ], |
| 146 | + 'Less than two code blocks in one comparison group' => [ |
| 147 | + 'fixtureFile' => 'InvalidLessThanTwoCodeBlocksInComparison.xml', |
| 148 | + 'expectedStdOut' => '', |
| 149 | + 'expectedStdErr' => "Element 'code_comparison': Missing child element(s). Expected is ( code ).", |
| 150 | + ], |
| 151 | + '<code> element missing title attribute' => [ |
| 152 | + 'fixtureFile' => 'InvalidCodeElementMissingTitle.xml', |
| 153 | + 'expectedStdOut' => '', |
| 154 | + 'expectedStdErr' => "Element 'code': The attribute 'title' is required but missing.", |
| 155 | + ], |
| 156 | + 'Documentation contains non standard element' => [ |
| 157 | + 'fixtureFile' => 'InvalidContainsNonStandardElements.xml', |
| 158 | + 'expectedStdOut' => '', |
| 159 | + 'expectedStdErr' => "Element 'rule': This element is not expected. Expected is ( standard )", |
| 160 | + ], |
| 161 | + '<standard> element contains non CDATA content' => [ |
| 162 | + 'fixtureFile' => 'InvalidStandardContainsElements.xml', |
| 163 | + 'expectedStdOut' => '', |
| 164 | + 'expectedStdErr' => "Element 'standard': Element content is not allowed, because the content type is a simple type definition.", |
| 165 | + ], |
| 166 | + 'Wrong order of <code_comparison> element (before <standard> element)' => [ |
| 167 | + 'fixtureFile' => 'InvalidWrongOrderOfElements.xml', |
| 168 | + 'expectedStdOut' => '', |
| 169 | + 'expectedStdErr' => "Element 'code_comparison': This element is not expected. Expected is ( standard ).", |
| 170 | + ], |
| 171 | + 'Empty <code_comparison> element' => [ |
| 172 | + 'fixtureFile' => 'InvalidEmptyCodeComparisonElement.xml', |
| 173 | + 'expectedStdOut' => '', |
| 174 | + 'expectedStdErr' => "Element 'code_comparison': Missing child element(s). Expected is ( code ).", |
| 175 | + ], |
| 176 | + '<code_comparison> element contains non <code> elements' => [ |
| 177 | + 'fixtureFile' => 'InvalidCodeComparisonElementContainsNonCodeElements.xml', |
| 178 | + 'expectedStdOut' => '', |
| 179 | + 'expectedStdErr' => "Element 'sniff': This element is not expected. Expected is ( code ).", |
| 180 | + ], |
| 181 | + 'Empty title attribute in the <documentation> root element' => [ |
| 182 | + 'fixtureFile' => 'InvalidEmptyDocumentationTitleAttribute.xml', |
| 183 | + 'expectedStdOut' => '', |
| 184 | + 'expectedStdErr' => "Element 'documentation', attribute 'title': [facet 'minLength'] The value '' has a length of '0'; this underruns the allowed minimum length of '1'.", |
| 185 | + ], |
| 186 | + 'Title attribute in the <documentation> root element has wrong type (will throw parser error)' => [ |
| 187 | + 'fixtureFile' => 'InvalidDocumentationTitleAttributeType.xml', |
| 188 | + 'expectedStdOut' => '', |
| 189 | + 'expectedStdErr' => "parser error : AttValue: \" or ' expected", |
| 190 | + ], |
| 191 | + 'Multiple title attributes in the <documentation> root element (will throw parser error)' => [ |
| 192 | + 'fixtureFile' => 'InvalidDocumentationMultipleTitleAttributes.xml', |
| 193 | + 'expectedStdOut' => '', |
| 194 | + 'expectedStdErr' => 'parser error : Attribute title redefined', |
| 195 | + ], |
| 196 | + '<code> element empty title attribute' => [ |
| 197 | + 'fixtureFile' => 'InvalidCodeElementEmptyTitle.xml', |
| 198 | + 'expectedStdOut' => '', |
| 199 | + 'expectedStdErr' => "Element 'code', attribute 'title': [facet 'minLength'] The value '' has a length of '0'; this underruns the allowed minimum length of '1'.", |
| 200 | + ], |
| 201 | + '<code> element has has wrong type in the title attribute (will throw parser error)' => [ |
| 202 | + 'fixtureFile' => 'InvalidCodeElementTitleWrongType.xml', |
| 203 | + 'expectedStdOut' => '', |
| 204 | + 'expectedStdErr' => "parser error : AttValue: \" or ' expected", |
| 205 | + ], |
| 206 | + '<code> element has has multiple title attributes (will throw parser error)' => [ |
| 207 | + 'fixtureFile' => 'InvalidCodeElementMultipleTitleAttributes.xml', |
| 208 | + 'expectedStdOut' => '', |
| 209 | + 'expectedStdErr' => 'Attribute title redefined', |
| 210 | + ], |
| 211 | + '<code> element contains non string elements' => [ |
| 212 | + 'fixtureFile' => 'InvalidCodeElementContainsNonStringElements.xml', |
| 213 | + 'expectedStdOut' => '', |
| 214 | + 'expectedStdErr' => "Element 'code': Element content is not allowed, because the content type is a simple type definition.", |
| 215 | + ], |
| 216 | + ]; |
| 217 | + } |
| 218 | +} |
0 commit comments