Skip to content
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

[TASK] Validate guides.xml files against scheme #666

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"webmozart/assert": "^1.10"
},
"require-dev": {
"ext-dom": "*",
"ext-libxml": "*",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"doctrine/coding-standard": "^12.0",
"fakerphp/faker": "^1.23",
Expand Down
23 changes: 13 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions packages/guides-cli/resources/schema/guides.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
<xsd:schema
targetNamespace="https://www.phpdoc.org/guides"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="https://www.phpdoc.org/guides"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should publish the xsd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First we should make it work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't publish the XSD on an uri (but instead reference a local file like done in the examples in this PR). You need proper version management when publishing the XSD's, something you get from free when you recommend people to use the XSD installed in their vendor directory.

(XML namespaces being a "uri" has no relation to the web)

version="3.0"
elementFormDefault="qualified"
>
<xsd:element name="guides" type="guides"/>

<xsd:complexType name="guides">
<xsd:choice maxOccurs="unbounded">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="project" type="project" minOccurs="0" maxOccurs="1"/>
<xsd:element name="base-template-path" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="theme" type="theme" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="extension" type="extension" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="output-format" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="inventory" type="inventory" minOccurs="1" maxOccurs="unbounded"/>
<xsd:element name="template" type="template" minOccurs="1" maxOccurs="unbounded"/>
<xsd:element name="inventory" type="inventory" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="template" type="template" minOccurs="0" maxOccurs="unbounded"/>
</xsd:choice>

<xsd:attribute name="input" type="xsd:string"/>
Expand All @@ -26,6 +27,7 @@
<xsd:attribute name="show-progress" type="xsd:string"/>
<xsd:attribute name="theme" type="xsd:string"/>
<xsd:attribute name="default-code-language" type="xsd:string"/>
<xsd:attribute name="links-are-relative" type="xsd:string"/>

</xsd:complexType>

Expand Down
64 changes: 64 additions & 0 deletions tests/Integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace phpDocumentor\Guides\Integration;

use DOMDocument;
use phpDocumentor\Guides\ApplicationTestCase;
use phpDocumentor\Guides\Cli\Command\Run;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -21,7 +22,11 @@
use function file_exists;
use function file_get_contents;
use function implode;
use function libxml_clear_errors;
use function libxml_get_errors;
use function libxml_use_internal_errors;
use function setlocale;
use function sprintf;
use function str_ends_with;
use function str_replace;
use function system;
Expand Down Expand Up @@ -167,6 +172,65 @@ public static function getTestsForLatex(): array
return self::getTestsForDirectory(__DIR__ . '/tests-latex');
}

#[DataProvider('getTestsWithGuidesXmlForDirectoryTest')]
public function testXmlValidation(string $xmlFile): void
{
$xsdFile = 'packages/guides-cli/resources/schema/guides.xsd';
libxml_use_internal_errors(true);
// Create a DOMDocument for XML validation
$dom = new DOMDocument();
$dom->load($xmlFile);

// Validate against XSD schema
$isValid = $dom->schemaValidate($xsdFile);
$errorString = '';

if (!$isValid) {
$errors = libxml_get_errors();

foreach ($errors as $error) {
$errorString .= sprintf("Validation Error at line %s: %s\n", $error->line, $error->message);
}

libxml_clear_errors();
}

self::assertTrue($isValid, 'XML of ' . $xmlFile . ' does not validate against the schema: ' . $errorString);
}

/** @return mixed[] */
public static function getTestsWithGuidesXmlForDirectoryTest(): array
{
return self::getTestsWithGuidesXmlForSubDirectoryTest('tests/Integration/tests');
}

/** @return mixed[] */
private static function getTestsWithGuidesXmlForSubDirectoryTest(string $directory = 'tests'): array
{
$finder = new SymfonyFinder();
$finder
->directories()
->in($directory)
->depth('== 0');

$tests = [];

foreach ($finder as $dir) {
if (!file_exists($dir->getPathname() . '/input')) {
$tests = array_merge($tests, self::getTestsWithGuidesXmlForSubDirectoryTest($dir->getPathname()));
continue;
}

if (!file_exists($dir->getPathname() . '/input/guides.xml')) {
continue;
}

$tests[$dir->getRelativePathname()] = [$dir->getPathname() . '/input/guides.xml'];
}

return $tests;
}

/** @return mixed[] */
private static function getTestsForDirectory(string $directory): array
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd">
<theme>bootstrap</theme>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
theme="bootstrap"
>
<extension class="phpDocumentor\Guides\Bootstrap"/>
</guides>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd">
<theme>bootstrap</theme>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
theme="bootstrap"
>
<extension class="phpDocumentor\Guides\Bootstrap"/>
</guides>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd">
<theme>bootstrap</theme>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
theme="bootstrap"
>
<extension class="phpDocumentor\Guides\Bootstrap"/>
</guides>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd">
<theme>bootstrap</theme>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
theme="bootstrap"
>
<extension class="phpDocumentor\Guides\Bootstrap"/>
</guides>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd">
<theme>bootstrap</theme>

xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
theme="bootstrap"
>
<extension class="phpDocumentor\Guides\Bootstrap"/>
</guides>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document Title - Title</title>

</head>
<body>
<div class="section" id="document-title">
<h1>Document Title</h1>

<p>Lorem Ipsum Dolor.</p>
</div>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
>
<project title="Title"/>
</guides>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
==============
Document Title
==============

Lorem Ipsum Dolor.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document Title</title>

</head>
<body>
<div class="section" id="document-title">
<h1>Document Title</h1>

<p>Lorem Ipsum Dolor.</p>
<pre><code class="language-php">$some = &#039;PHP code&#039;;</code></pre>
</div>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
default-code-language="php"
>
</guides>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
==============
Document Title
==============

Lorem Ipsum Dolor.

::

$some = 'PHP code';
2 changes: 1 addition & 1 deletion tests/Integration/tests/input-file/input/guides.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
input-file="README.rst"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
input-format="md"
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/tests/markdown/code-md/input/guides.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
input-format="md"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
input-format="md"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
input-format="md"
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/tests/markdown/html-md/input/guides.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
input-format="md"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
input-format="md"
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/tests/markdown/image-md/input/guides.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
input-format="md"
Expand Down
Loading