Browse Source

Add schema.json

Dariusz Ruminski 8 years ago
parent
commit
1b2cf9d8bd

+ 1 - 0
composer.json

@@ -28,6 +28,7 @@
     },
     "require-dev": {
         "gecko-packages/gecko-php-unit": "^2.0",
+        "justinrainbow/json-schema": "^5.0",
         "phpunit/phpunit": "^4.5|^5",
         "satooshi/php-coveralls": "^1.0"
     },

+ 47 - 0
doc/schema.json

@@ -0,0 +1,47 @@
+{
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type": "object",
+    "properties": {
+        "files": {
+            "type": "array",
+            "uniqueItems": true,
+            "items": {
+                "type": "object",
+                "properties": {
+                    "name": {
+                        "type": "string"
+                    },
+                    "appliedFixers": {
+                        "type": "array",
+                        "uniqueItems": true,
+                        "items": {
+                            "type": "string"
+                        }
+                    }
+                },
+                "required": [
+                    "name"
+                ]
+            }
+        },
+        "time": {
+            "type": "object",
+            "properties": {
+                "total": {
+                    "type": "number"
+                }
+            },
+            "required": [
+                "total"
+            ]
+        },
+        "memory": {
+            "type": "number"
+        }
+    },
+    "required": [
+        "files",
+        "time",
+        "memory"
+    ]
+}

+ 120 - 92
tests/Report/JsonReporterTest.php

@@ -17,6 +17,7 @@ use PhpCsFixer\Report\ReportSummary;
 
 /**
  * @author Boris Gorbylev <ekho@ekho.name>
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  *
  * @internal
  */
@@ -40,7 +41,7 @@ final class JsonReporterTest extends \PHPUnit_Framework_TestCase
 
     public function testGenerateNoErrors()
     {
-        $expectedJson = <<<'JSON'
+        $expectedReport = <<<'JSON'
 {
     "files": [
     ],
@@ -51,24 +52,24 @@ final class JsonReporterTest extends \PHPUnit_Framework_TestCase
 }
 JSON;
 
-        $this->assertJsonStringEqualsJsonString(
-            $expectedJson,
-            $this->reporter->generate(
-                new ReportSummary(
-                    array(),
-                    0,
-                    0,
-                    false,
-                    false,
-                    false
-                )
+        $actualReport = $this->reporter->generate(
+            new ReportSummary(
+                array(),
+                0,
+                0,
+                false,
+                false,
+                false
             )
         );
+
+        $this->assertJsonSchema($actualReport);
+        $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
     }
 
     public function testGenerateSimple()
     {
-        $expectedJson = <<<'JSON'
+        $expectedReport = <<<'JSON'
 {
     "files": [
         {
@@ -82,28 +83,28 @@ JSON;
 }
 JSON;
 
-        $this->assertJsonStringEqualsJsonString(
-            $expectedJson,
-            $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'),
                     ),
-                    5 * 1000,
-                    2 * 1024 * 1024,
-                    false,
-                    false,
-                    false
-                )
+                ),
+                5 * 1000,
+                2 * 1024 * 1024,
+                false,
+                false,
+                false
             )
         );
+
+        $this->assertJsonSchema($actualReport);
+        $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
     }
 
     public function testGenerateWithDiff()
     {
-        $expectedJson = <<<'JSON'
+        $expectedReport = <<<'JSON'
 {
     "files": [
         {
@@ -118,29 +119,29 @@ JSON;
 }
 JSON;
 
-        $this->assertJsonStringEqualsJsonString(
-            $expectedJson,
-            $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 ;)',
                     ),
-                    5 * 1000,
-                    2 * 1024 * 1024,
-                    false,
-                    false,
-                    false
-                )
+                ),
+                5 * 1000,
+                2 * 1024 * 1024,
+                false,
+                false,
+                false
             )
         );
+
+        $this->assertJsonSchema($actualReport);
+        $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
     }
 
     public function testGenerateWithAppliedFixers()
     {
-        $expectedJson = <<<'JSON'
+        $expectedReport = <<<'JSON'
 {
     "files": [
         {
@@ -155,28 +156,28 @@ JSON;
 }
 JSON;
 
-        $this->assertJsonStringEqualsJsonString(
-            $expectedJson,
-            $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'),
                     ),
-                    5 * 1000,
-                    2 * 1024 * 1024,
-                    true,
-                    false,
-                    false
-                )
+                ),
+                5 * 1000,
+                2 * 1024 * 1024,
+                true,
+                false,
+                false
             )
         );
+
+        $this->assertJsonSchema($actualReport);
+        $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
     }
 
     public function testGenerateWithTimeAndMemory()
     {
-        $expectedJson = <<<'JSON'
+        $expectedReport = <<<'JSON'
 {
     "files": [
         {
@@ -190,28 +191,28 @@ JSON;
 }
 JSON;
 
-        $this->assertJsonStringEqualsJsonString(
-            $expectedJson,
-            $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->assertJsonSchema($actualReport);
+        $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
     }
 
     public function testGenerateComplex()
     {
-        $expectedJson = <<<'JSON'
+        $expectedReport = <<<'JSON'
 {
     "files": [
         {
@@ -232,25 +233,52 @@ JSON;
 }
 JSON;
 
-        $this->assertJsonStringEqualsJsonString(
-            $expectedJson,
-            $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 ;)',
+                    ),
+                    'anotherFile.php' => array(
+                        'appliedFixers' => array('another_fixer_name_here'),
+                        'diff' => 'another diff here ;)',
                     ),
-                    1234,
-                    2.5 * 1024 * 1024,
-                    true,
-                    true,
-                    true
+                ),
+                1234,
+                2.5 * 1024 * 1024,
+                true,
+                true,
+                true
+            )
+        );
+
+        $this->assertJsonSchema($actualReport);
+        $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
+    }
+
+    /**
+     * @param string $json
+     */
+    private function assertJsonSchema($json)
+    {
+        $jsonPath = __DIR__.'/../../doc/schema.json';
+
+        $data = json_decode($json);
+
+        $validator = new \JsonSchema\Validator();
+        $validator->validate(
+            $data,
+            (object) array('$ref' => 'file://'.realpath($jsonPath))
+        );
+
+        $this->assertTrue(
+            $validator->isValid(),
+            implode(
+                "\n",
+                array_map(
+                    function (array $item) { return sprintf('Property `%s`: %s.', $item['property'], $item['message']); },
+                    $validator->getErrors()
                 )
             )
         );

+ 24 - 24
tests/Report/JunitReporterTest.php

@@ -40,7 +40,7 @@ final class JunitReporterTest extends \PHPUnit_Framework_TestCase
 
     public function testGenerateNoErrors()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0" encoding="UTF-8"?>
 <testsuites>
   <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="0" errors="0">
@@ -49,7 +49,7 @@ final class JunitReporterTest extends \PHPUnit_Framework_TestCase
 </testsuites>
 XML;
 
-        $actualXml = $this->reporter->generate(
+        $actualReport = $this->reporter->generate(
             new ReportSummary(
                 array(),
                 0,
@@ -60,13 +60,13 @@ XML;
             )
         );
 
-        $this->assertJunitXmlSchema($actualXml);
-        $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
+        $this->assertJunitXmlSchema($actualReport);
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
 
     public function testGenerateSimple()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0" encoding="UTF-8"?>
 <testsuites>
   <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0">
@@ -77,7 +77,7 @@ XML;
 </testsuites>
 XML;
 
-        $actualXml = $this->reporter->generate(
+        $actualReport = $this->reporter->generate(
             new ReportSummary(
                 array(
                     'someFile.php' => array(
@@ -92,13 +92,13 @@ XML;
             )
         );
 
-        $this->assertJunitXmlSchema($actualXml);
-        $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
+        $this->assertJunitXmlSchema($actualReport);
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
 
     public function testGenerateWithDiff()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0" encoding="UTF-8"?>
 <testsuites>
   <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0">
@@ -114,7 +114,7 @@ this text is a diff ;)]]></failure>
 </testsuites>
 XML;
 
-        $actualXml = $this->reporter->generate(
+        $actualReport = $this->reporter->generate(
             new ReportSummary(
                 array(
                     'someFile.php' => array(
@@ -130,13 +130,13 @@ XML;
             )
         );
 
-        $this->assertJunitXmlSchema($actualXml);
-        $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
+        $this->assertJunitXmlSchema($actualReport);
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
 
     public function testGenerateWithAppliedFixers()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0" encoding="UTF-8"?>
 <testsuites>
   <testsuite name="PHP CS Fixer" tests="1" assertions="2" failures="2" errors="0">
@@ -150,7 +150,7 @@ XML;
 </testsuites>
 XML;
 
-        $actualXml = $this->reporter->generate(
+        $actualReport = $this->reporter->generate(
             new ReportSummary(
                 array(
                     'someFile.php' => array(
@@ -165,13 +165,13 @@ XML;
             )
         );
 
-        $this->assertJunitXmlSchema($actualXml);
-        $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
+        $this->assertJunitXmlSchema($actualReport);
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
 
     public function testGenerateWithTimeAndMemory()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0" encoding="UTF-8"?>
 <testsuites>
   <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0" time="1.234">
@@ -182,7 +182,7 @@ XML;
 </testsuites>
 XML;
 
-        $actualXml = $this->reporter->generate(
+        $actualReport = $this->reporter->generate(
             new ReportSummary(
                 array(
                     'someFile.php' => array(
@@ -197,13 +197,13 @@ XML;
             )
         );
 
-        $this->assertJunitXmlSchema($actualXml);
-        $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
+        $this->assertJunitXmlSchema($actualReport);
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
 
     public function testGenerateComplex()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0"?>
 <testsuites>
   <testsuite assertions="3" errors="0" failures="3" name="PHP CS Fixer" tests="2" time="1.234">
@@ -232,7 +232,7 @@ another diff here ;)</failure>
 </testsuites>
 XML;
 
-        $actualXml = $this->reporter->generate(
+        $actualReport = $this->reporter->generate(
             new ReportSummary(
                 array(
                     'someFile.php' => array(
@@ -252,8 +252,8 @@ XML;
             )
         );
 
-        $this->assertJunitXmlSchema($actualXml);
-        $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
+        $this->assertJunitXmlSchema($actualReport);
+        $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
     }
 
     /**

+ 12 - 12
tests/Report/TextReporterTest.php

@@ -40,11 +40,11 @@ final class TextReporterTest extends \PHPUnit_Framework_TestCase
 
     public function testGenerateNoErrors()
     {
-        $expectedText = <<<'TEXT'
+        $expectedReport = <<<'TEXT'
 TEXT;
 
         $this->assertSame(
-            $expectedText,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(),
@@ -60,14 +60,14 @@ TEXT;
 
     public function testGenerateSimple()
     {
-        $expectedText = str_replace("\n", PHP_EOL, <<<'TEXT'
+        $expectedReport = str_replace("\n", PHP_EOL, <<<'TEXT'
    1) someFile.php
 
 TEXT
         );
 
         $this->assertSame(
-            $expectedText,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(
@@ -87,7 +87,7 @@ TEXT
 
     public function testGenerateWithDiff()
     {
-        $expectedText = str_replace("\n", PHP_EOL, <<<'TEXT'
+        $expectedReport = str_replace("\n", PHP_EOL, <<<'TEXT'
    1) someFile.php
       ---------- begin diff ----------
 this text is a diff ;)
@@ -98,7 +98,7 @@ TEXT
         );
 
         $this->assertSame(
-            $expectedText,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(
@@ -119,14 +119,14 @@ TEXT
 
     public function testGenerateWithAppliedFixers()
     {
-        $expectedText = str_replace("\n", PHP_EOL, <<<'TEXT'
+        $expectedReport = str_replace("\n", PHP_EOL, <<<'TEXT'
    1) someFile.php (some_fixer_name_here)
 
 TEXT
         );
 
         $this->assertSame(
-            $expectedText,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(
@@ -146,7 +146,7 @@ TEXT
 
     public function testGenerateWithTimeAndMemory()
     {
-        $expectedText = str_replace("\n", PHP_EOL, <<<'TEXT'
+        $expectedReport = str_replace("\n", PHP_EOL, <<<'TEXT'
    1) someFile.php
 
 Fixed all files in 1.234 seconds, 2.500 MB memory used
@@ -155,7 +155,7 @@ TEXT
         );
 
         $this->assertSame(
-            $expectedText,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(
@@ -175,7 +175,7 @@ TEXT
 
     public function testGenerateComplexWithDecoratedOutput()
     {
-        $expectedText = str_replace("\n", PHP_EOL, <<<'TEXT'
+        $expectedReport = str_replace("\n", PHP_EOL, <<<'TEXT'
    1) someFile.php (<comment>some_fixer_name_here</comment>)
 <comment>      ---------- begin diff ----------</comment>
 this text is a diff ;)
@@ -193,7 +193,7 @@ TEXT
         );
 
         $this->assertSame(
-            $expectedText,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(

+ 12 - 12
tests/Report/XmlReporterTest.php

@@ -40,7 +40,7 @@ final class XmlReporterTest extends \PHPUnit_Framework_TestCase
 
     public function testGenerateNoErrors()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0" encoding="UTF-8"?>
 <report>
   <files />
@@ -48,7 +48,7 @@ final class XmlReporterTest extends \PHPUnit_Framework_TestCase
 XML;
 
         $this->assertXmlStringEqualsXmlString(
-            $expectedXml,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(),
@@ -64,7 +64,7 @@ XML;
 
     public function testGenerateSimple()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0" encoding="UTF-8"?>
 <report>
   <files>
@@ -74,7 +74,7 @@ XML;
 XML;
 
         $this->assertXmlStringEqualsXmlString(
-            $expectedXml,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(
@@ -94,7 +94,7 @@ XML;
 
     public function testGenerateWithDiff()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0" encoding="UTF-8"?>
 <report>
   <files>
@@ -106,7 +106,7 @@ XML;
 XML;
 
         $this->assertXmlStringEqualsXmlString(
-            $expectedXml,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(
@@ -127,7 +127,7 @@ XML;
 
     public function testGenerateWithAppliedFixers()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0" encoding="UTF-8"?>
 <report>
   <files>
@@ -141,7 +141,7 @@ XML;
 XML;
 
         $this->assertXmlStringEqualsXmlString(
-            $expectedXml,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(
@@ -161,7 +161,7 @@ XML;
 
     public function testGenerateWithTimeAndMemory()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0" encoding="UTF-8"?>
 <report>
   <files>
@@ -175,7 +175,7 @@ XML;
 XML;
 
         $this->assertXmlStringEqualsXmlString(
-            $expectedXml,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(
@@ -195,7 +195,7 @@ XML;
 
     public function testGenerateComplex()
     {
-        $expectedXml = <<<'XML'
+        $expectedReport = <<<'XML'
 <?xml version="1.0" encoding="UTF-8"?>
 <report>
   <files>
@@ -220,7 +220,7 @@ XML;
 XML;
 
         $this->assertXmlStringEqualsXmlString(
-            $expectedXml,
+            $expectedReport,
             $this->reporter->generate(
                 new ReportSummary(
                     array(