Skip to content

Commit

Permalink
Add example for #136
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Nov 14, 2021
1 parent 7d9628d commit 22eadfe
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
35 changes: 35 additions & 0 deletions tests/src/Helper/RefPatchedResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Swaggest\JsonSchema\Tests\Helper;

use Swaggest\JsonSchema\RemoteRefProvider;

class RefPatchedResolver implements RemoteRefProvider
{
private $resolver;

private $patches;

public function __construct(RemoteRefProvider $resolver) {
$this->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);
}
}
}
46 changes: 44 additions & 2 deletions tests/src/PHPUnit/Ref/FileResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -22,7 +64,7 @@ public function testFileResolver()
}
}
JSON
)
)
);

$schemaData = json_decode(<<<'JSON'
Expand All @@ -36,7 +78,7 @@ public function testFileResolver()
"additionalProperties": false
}
JSON
);
);
$options = new Context();
$options->remoteRefProvider = $refProvider;
$schema = Schema::import($schemaData, $options);
Expand Down

0 comments on commit 22eadfe

Please sign in to comment.