123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Report;
- use PhpCsFixer\Report\JsonReporter;
- /**
- * @author Boris Gorbylev <ekho@ekho.name>
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Report\JsonReporter
- */
- final class JsonReporterTest extends AbstractReporterTestCase
- {
- public function createSimpleReport()
- {
- return <<<'JSON'
- {
- "files": [
- {
- "name": "someFile.php"
- }
- ],
- "time": {
- "total": 0
- },
- "memory": 0
- }
- JSON;
- }
- public function createWithDiffReport()
- {
- return <<<'JSON'
- {
- "files": [
- {
- "name": "someFile.php",
- "diff": "this text is a diff ;)"
- }
- ],
- "time": {
- "total": 0
- },
- "memory": 0
- }
- JSON;
- }
- public function createWithAppliedFixersReport()
- {
- return <<<'JSON'
- {
- "files": [
- {
- "name": "someFile.php",
- "appliedFixers":["some_fixer_name_here_1", "some_fixer_name_here_2"]
- }
- ],
- "time": {
- "total": 0
- },
- "memory": 0
- }
- JSON;
- }
- public function createWithTimeAndMemoryReport()
- {
- return <<<'JSON'
- {
- "files": [
- {
- "name": "someFile.php"
- }
- ],
- "memory": 2.5,
- "time": {
- "total": 1.234
- }
- }
- JSON;
- }
- public function createComplexReport()
- {
- return <<<'JSON'
- {
- "files": [
- {
- "name": "someFile.php",
- "appliedFixers":["some_fixer_name_here_1", "some_fixer_name_here_2"],
- "diff": "this text is a diff ;)"
- },
- {
- "name": "anotherFile.php",
- "appliedFixers":["another_fixer_name_here"],
- "diff": "another diff here ;)"
- }
- ],
- "memory": 2.5,
- "time": {
- "total": 1.234
- }
- }
- JSON;
- }
- protected function createReporter()
- {
- return new JsonReporter();
- }
- protected function getFormat()
- {
- return 'json';
- }
- protected function createNoErrorReport()
- {
- return <<<'JSON'
- {
- "files": [
- ],
- "time": {
- "total": 0
- },
- "memory": 0
- }
- JSON;
- }
- protected function assertFormat($expected, $input)
- {
- $this->assertJsonSchema($input);
- $this->assertJsonStringEqualsJsonString($expected, $input);
- }
- /**
- * @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()
- )
- )
- );
- }
- }
|