From 22eadfeb4a31bfc61073b7c2102b212e07266d22 Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Sun, 14 Nov 2021 16:11:33 +0100 Subject: [PATCH] Add example for #136 --- tests/src/Helper/RefPatchedResolver.php | 35 ++++++++++++++++ tests/src/PHPUnit/Ref/FileResolverTest.php | 46 +++++++++++++++++++++- 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 tests/src/Helper/RefPatchedResolver.php diff --git a/tests/src/Helper/RefPatchedResolver.php b/tests/src/Helper/RefPatchedResolver.php new file mode 100644 index 0000000..50ebcfa --- /dev/null +++ b/tests/src/Helper/RefPatchedResolver.php @@ -0,0 +1,35 @@ +resolver = $resolver; + } + + public function addPatch($url, $func) { + $this->patches[$url] = $func; + } + + public function getSchemaData($url) + { + $data = $this->resolver->getSchemaData($url); + + if ($data === false) { + return $data; + } + + if (isset($this->patches[$url])) { + $patch = $this->patches[$url]; + + $patch($data); + } + } +} \ No newline at end of file diff --git a/tests/src/PHPUnit/Ref/FileResolverTest.php b/tests/src/PHPUnit/Ref/FileResolverTest.php index f584aba..919e645 100644 --- a/tests/src/PHPUnit/Ref/FileResolverTest.php +++ b/tests/src/PHPUnit/Ref/FileResolverTest.php @@ -7,9 +7,51 @@ use Swaggest\JsonSchema\InvalidValue; use Swaggest\JsonSchema\RemoteRef\Preloaded; use Swaggest\JsonSchema\Schema; +use Swaggest\JsonSchema\Tests\Helper\RefPatchedResolver; class FileResolverTest extends \PHPUnit_Framework_TestCase { + public function testPatchSchema() + { + $refProvider = new Preloaded(); + $refProvider->setSchemaData( + 'https://example.com/string.json', + json_decode(<<<'JSON' +{"type": "string"} +JSON + ) + ); + $refProvider->setSchemaData( + 'https://example.com/integer.json', + json_decode(<<<'JSON' +{"type": "integer"} +JSON + ) + ); + + $refProvider = new RefPatchedResolver($refProvider); + $refProvider->addPatch('https://example.com/string.json', function (\stdClass $schemaData) { + $schemaData->title = 'This is a string'; + }); + + + $schemaData = json_decode(<<<'JSON' +{ + "properties": { + "s": {"$ref":"https://example.com/string.json"}, + "i": {"$ref":"https://example.com/integer.json"} + } +} +JSON + ); + + $context = new Context($refProvider); + $schema = Schema::import($schemaData, $context); + + $this->assertEquals('This is a string', $schema->getProperties()['s']->title); + $this->assertEquals('', $schema->getProperties()['i']->title); + } + public function testFileResolver() { $refProvider = new Preloaded(); @@ -22,7 +64,7 @@ public function testFileResolver() } } JSON -) + ) ); $schemaData = json_decode(<<<'JSON' @@ -36,7 +78,7 @@ public function testFileResolver() "additionalProperties": false } JSON -); + ); $options = new Context(); $options->remoteRefProvider = $refProvider; $schema = Schema::import($schemaData, $options);