Browse Source

Add xml.xsd

Dariusz Ruminski 8 years ago
parent
commit
6819c984d4
3 changed files with 199 additions and 147 deletions
  1. 83 0
      doc/xml.xsd
  2. 20 59
      tests/Report/JunitReporterTest.php
  3. 96 88
      tests/Report/XmlReporterTest.php

+ 83 - 0
doc/xml.xsd

@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
+    <xs:element name="applied_fixer">
+        <xs:complexType>
+            <xs:simpleContent>
+                <xs:extension base="xs:string">
+                    <xs:attribute type="xs:string" name="name"/>
+                </xs:extension>
+            </xs:simpleContent>
+        </xs:complexType>
+    </xs:element>
+
+    <xs:element name="applied_fixers">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element ref="applied_fixer" maxOccurs="unbounded" minOccurs="0"/>
+            </xs:sequence>
+        </xs:complexType>
+    </xs:element>
+
+    <xs:element name="diff" type="xs:string"/>
+
+    <xs:element name="file">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element ref="applied_fixers" maxOccurs="1" minOccurs="0"/>
+                <xs:element ref="diff" maxOccurs="1" minOccurs="0"/>
+            </xs:sequence>
+            <xs:attribute type="xs:byte" name="id" use="optional"/>
+            <xs:attribute type="xs:string" name="name" use="optional"/>
+        </xs:complexType>
+    </xs:element>
+
+    <xs:element name="total">
+        <xs:complexType>
+            <xs:simpleContent>
+                <xs:extension base="xs:string">
+                    <xs:attribute type="xs:float" name="value"/>
+                </xs:extension>
+            </xs:simpleContent>
+        </xs:complexType>
+    </xs:element>
+
+    <xs:element name="files">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element ref="file" maxOccurs="unbounded" minOccurs="0"/>
+            </xs:sequence>
+        </xs:complexType>
+    </xs:element>
+
+    <xs:element name="time">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element ref="total"/>
+            </xs:sequence>
+            <xs:attribute type="xs:string" name="unit"/>
+        </xs:complexType>
+    </xs:element>
+
+    <xs:element name="memory">
+        <xs:complexType>
+            <xs:simpleContent>
+                <xs:extension base="xs:string">
+                    <xs:attribute type="xs:float" name="value"/>
+                    <xs:attribute type="xs:string" name="unit"/>
+                </xs:extension>
+            </xs:simpleContent>
+        </xs:complexType>
+    </xs:element>
+
+    <xs:element name="report">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element ref="files"/>
+                <xs:element ref="time" maxOccurs="1" minOccurs="0"/>
+                <xs:element ref="memory" maxOccurs="1" minOccurs="0"/>
+            </xs:sequence>
+        </xs:complexType>
+    </xs:element>
+
+</xs:schema>

+ 20 - 59
tests/Report/JunitReporterTest.php

@@ -12,6 +12,7 @@
 
 
 namespace PhpCsFixer\Tests\Report;
 namespace PhpCsFixer\Tests\Report;
 
 
+use GeckoPackages\PHPUnit\Constraints\XML\XMLMatchesXSDConstraint;
 use PhpCsFixer\Report\JunitReporter;
 use PhpCsFixer\Report\JunitReporter;
 use PhpCsFixer\Report\ReportSummary;
 use PhpCsFixer\Report\ReportSummary;
 
 
@@ -22,12 +23,24 @@ use PhpCsFixer\Report\ReportSummary;
  */
  */
 final class JunitReporterTest extends \PHPUnit_Framework_TestCase
 final class JunitReporterTest extends \PHPUnit_Framework_TestCase
 {
 {
-    /** @var JunitReporter */
+    /**
+     * @var JunitReporter
+     */
     private $reporter;
     private $reporter;
 
 
+    /**
+     * JUnit XML schema from Jenkins.
+     *
+     * @var string
+     *
+     * @see https://github.com/jenkinsci/xunit-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
+     */
+    private $xsd;
+
     protected function setUp()
     protected function setUp()
     {
     {
         $this->reporter = new JunitReporter();
         $this->reporter = new JunitReporter();
+        $this->xsd = file_get_contents(__DIR__.'/../../doc/junit-10.xsd');
     }
     }
 
 
     /**
     /**
@@ -60,7 +73,7 @@ XML;
             )
             )
         );
         );
 
 
-        $this->assertJunitXmlSchema($actualReport);
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
 
 
@@ -92,7 +105,7 @@ XML;
             )
             )
         );
         );
 
 
-        $this->assertJunitXmlSchema($actualReport);
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
 
 
@@ -130,7 +143,7 @@ XML;
             )
             )
         );
         );
 
 
-        $this->assertJunitXmlSchema($actualReport);
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
 
 
@@ -165,7 +178,7 @@ XML;
             )
             )
         );
         );
 
 
-        $this->assertJunitXmlSchema($actualReport);
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
 
 
@@ -197,7 +210,7 @@ XML;
             )
             )
         );
         );
 
 
-        $this->assertJunitXmlSchema($actualReport);
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
 
 
@@ -252,59 +265,7 @@ XML;
             )
             )
         );
         );
 
 
-        $this->assertJunitXmlSchema($actualReport);
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
         $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
-
-    /**
-     * Validates generated xml report with schema.
-     * Uses JUnit XML schema from Jenkins.
-     *
-     * @see https://github.com/jenkinsci/xunit-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
-     *
-     * @param string $xml
-     */
-    private function assertJunitXmlSchema($xml)
-    {
-        $xsdPath = __DIR__.'/../../doc/junit-10.xsd';
-
-        static $errorLevels = array(
-            LIBXML_ERR_WARNING => 'Warning',
-            LIBXML_ERR_ERROR => 'Error',
-            LIBXML_ERR_FATAL => 'Fatal Error',
-        );
-
-        $internal = libxml_use_internal_errors(true);
-
-        $dom = new \DOMDocument();
-
-        $loaded = $dom->loadXML($xml);
-        if (true !== $loaded) {
-            libxml_use_internal_errors($internal);
-            $this->fail(sprintf('XML loading failed, expected "true", got "%s".', var_export($loaded, true)));
-        }
-
-        $dom->schemaValidate($xsdPath);
-
-        $errors = array();
-        foreach (libxml_get_errors() as $error) {
-            $errors[] = sprintf(
-                '%s #%s: %s (%s:%s)',
-                isset($errorLevels[$error->level]) ? $errorLevels[$error->level] : null,
-                $error->code,
-                trim($error->message),
-                $error->file,
-                $error->line
-            );
-        }
-
-        $errors = implode(PHP_EOL, $errors);
-
-        libxml_clear_errors();
-        libxml_use_internal_errors($internal);
-
-        if (strlen($errors) > 0) {
-            $this->fail('Actual xml does not match schema: '.PHP_EOL.$errors);
-        }
-    }
 }
 }

+ 96 - 88
tests/Report/XmlReporterTest.php

@@ -12,6 +12,7 @@
 
 
 namespace PhpCsFixer\Tests\Report;
 namespace PhpCsFixer\Tests\Report;
 
 
+use GeckoPackages\PHPUnit\Constraints\XML\XMLMatchesXSDConstraint;
 use PhpCsFixer\Report\ReportSummary;
 use PhpCsFixer\Report\ReportSummary;
 use PhpCsFixer\Report\XmlReporter;
 use PhpCsFixer\Report\XmlReporter;
 
 
@@ -25,9 +26,15 @@ final class XmlReporterTest extends \PHPUnit_Framework_TestCase
     /** @var XmlReporter */
     /** @var XmlReporter */
     private $reporter;
     private $reporter;
 
 
+    /**
+     * @var string
+     */
+    private $xsd;
+
     protected function setUp()
     protected function setUp()
     {
     {
         $this->reporter = new XmlReporter();
         $this->reporter = new XmlReporter();
+        $this->xsd = file_get_contents(__DIR__.'/../../doc/xml.xsd');
     }
     }
 
 
     /**
     /**
@@ -47,19 +54,19 @@ final class XmlReporterTest extends \PHPUnit_Framework_TestCase
 </report>
 </report>
 XML;
 XML;
 
 
-        $this->assertXmlStringEqualsXmlString(
-            $expectedReport,
-            $this->reporter->generate(
-                new ReportSummary(
-                    array(),
-                    0,
-                    0,
-                    false,
-                    false,
-                    false
-                )
+        $actualReport = $this->reporter->generate(
+            new ReportSummary(
+                array(),
+                0,
+                0,
+                false,
+                false,
+                false
             )
             )
         );
         );
+
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
 
 
     public function testGenerateSimple()
     public function testGenerateSimple()
@@ -73,23 +80,23 @@ XML;
 </report>
 </report>
 XML;
 XML;
 
 
-        $this->assertXmlStringEqualsXmlString(
-            $expectedReport,
-            $this->reporter->generate(
-                new ReportSummary(
-                    array(
-                        'someFile.php' => array(
-                            'appliedFixers' => array('some_fixer_name_here'),
-                        ),
+        $actualReport = $this->reporter->generate(
+            new ReportSummary(
+                array(
+                    'someFile.php' => array(
+                        'appliedFixers' => array('some_fixer_name_here'),
                     ),
                     ),
-                    0,
-                    0,
-                    false,
-                    false,
-                    false
-                )
+                ),
+                0,
+                0,
+                false,
+                false,
+                false
             )
             )
         );
         );
+
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
 
 
     public function testGenerateWithDiff()
     public function testGenerateWithDiff()
@@ -105,24 +112,24 @@ XML;
 </report>
 </report>
 XML;
 XML;
 
 
-        $this->assertXmlStringEqualsXmlString(
-            $expectedReport,
-            $this->reporter->generate(
-                new ReportSummary(
-                    array(
-                        'someFile.php' => array(
-                            'appliedFixers' => array('some_fixer_name_here'),
-                            'diff' => 'this text is a diff ;)',
-                        ),
+        $actualReport = $this->reporter->generate(
+            new ReportSummary(
+                array(
+                    'someFile.php' => array(
+                        'appliedFixers' => array('some_fixer_name_here'),
+                        'diff' => 'this text is a diff ;)',
                     ),
                     ),
-                    0,
-                    0,
-                    false,
-                    false,
-                    false
-                )
+                ),
+                0,
+                0,
+                false,
+                false,
+                false
             )
             )
         );
         );
+
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
 
 
     public function testGenerateWithAppliedFixers()
     public function testGenerateWithAppliedFixers()
@@ -140,23 +147,23 @@ XML;
 </report>
 </report>
 XML;
 XML;
 
 
-        $this->assertXmlStringEqualsXmlString(
-            $expectedReport,
-            $this->reporter->generate(
-                new ReportSummary(
-                    array(
-                        'someFile.php' => array(
-                            'appliedFixers' => array('some_fixer_name_here'),
-                        ),
+        $actualReport = $this->reporter->generate(
+            new ReportSummary(
+                array(
+                    'someFile.php' => array(
+                        'appliedFixers' => array('some_fixer_name_here'),
                     ),
                     ),
-                    0,
-                    0,
-                    true,
-                    false,
-                    false
-                )
+                ),
+                0,
+                0,
+                true,
+                false,
+                false
             )
             )
         );
         );
+
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
 
 
     public function testGenerateWithTimeAndMemory()
     public function testGenerateWithTimeAndMemory()
@@ -174,23 +181,23 @@ XML;
 </report>
 </report>
 XML;
 XML;
 
 
-        $this->assertXmlStringEqualsXmlString(
-            $expectedReport,
-            $this->reporter->generate(
-                new ReportSummary(
-                    array(
-                        'someFile.php' => array(
-                            'appliedFixers' => array('some_fixer_name_here'),
-                        ),
+        $actualReport = $this->reporter->generate(
+            new ReportSummary(
+                array(
+                    'someFile.php' => array(
+                        'appliedFixers' => array('some_fixer_name_here'),
                     ),
                     ),
-                    1234,
-                    2.5 * 1024 * 1024,
-                    false,
-                    false,
-                    false
-                )
+                ),
+                1234,
+                2.5 * 1024 * 1024,
+                false,
+                false,
+                false
             )
             )
         );
         );
+
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
 
 
     public function testGenerateComplex()
     public function testGenerateComplex()
@@ -207,7 +214,8 @@ XML;
     </file>
     </file>
     <file id="2" name="anotherFile.php">
     <file id="2" name="anotherFile.php">
       <applied_fixers>
       <applied_fixers>
-        <applied_fixer name="another_fixer_name_here"/>
+        <applied_fixer name="another_fixer_name_here_1"/>
+        <applied_fixer name="another_fixer_name_here_2"/>
       </applied_fixers>
       </applied_fixers>
       <diff>another diff here ;)</diff>
       <diff>another diff here ;)</diff>
     </file>
     </file>
@@ -219,27 +227,27 @@ XML;
 </report>
 </report>
 XML;
 XML;
 
 
-        $this->assertXmlStringEqualsXmlString(
-            $expectedReport,
-            $this->reporter->generate(
-                new ReportSummary(
-                    array(
-                        'someFile.php' => array(
-                            'appliedFixers' => array('some_fixer_name_here'),
-                            'diff' => 'this text is a diff ;)',
-                        ),
-                        'anotherFile.php' => array(
-                            'appliedFixers' => array('another_fixer_name_here'),
-                            'diff' => 'another diff here ;)',
-                        ),
+        $actualReport = $this->reporter->generate(
+            new ReportSummary(
+                array(
+                    'someFile.php' => array(
+                        'appliedFixers' => array('some_fixer_name_here'),
+                        'diff' => 'this text is a diff ;)',
                     ),
                     ),
-                    1234,
-                    2.5 * 1024 * 1024,
-                    true,
-                    false,
-                    false
-                )
+                    'anotherFile.php' => array(
+                        'appliedFixers' => array('another_fixer_name_here_1', 'another_fixer_name_here_2'),
+                        'diff' => 'another diff here ;)',
+                    ),
+                ),
+                1234,
+                2.5 * 1024 * 1024,
+                true,
+                false,
+                false
             )
             )
         );
         );
+
+        $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
     }
 }
 }