1+ <?php
2+
3+ namespace Swaggest \JsonSchema \Tests \PHPUnit \Ref ;
4+
5+
6+ use Swaggest \JsonSchema \Context ;
7+ use Swaggest \JsonSchema \InvalidValue ;
8+ use Swaggest \JsonSchema \RemoteRef \Preloaded ;
9+ use Swaggest \JsonSchema \Schema ;
10+
11+ class FileResolverTest extends \PHPUnit_Framework_TestCase
12+ {
13+ public function testFileResolver ()
14+ {
15+ $ refProvider = new Preloaded ();
16+ $ refProvider ->setSchemaData (
17+ 'file://baseTypes.json ' ,
18+ json_decode (<<<'JSON'
19+ {
20+ "stringFromOutside": {
21+ "type": "string"
22+ }
23+ }
24+ JSON
25+ )
26+ );
27+
28+ $ schemaData = json_decode (<<<'JSON'
29+ {
30+ "$schema": "http://json-schema.org/schema#",
31+ "type": "object",
32+ "properties": {
33+ "sample": { "$ref": "file://baseTypes.json#/stringFromOutside" }
34+ },
35+ "required": ["sample"],
36+ "additionalProperties": false
37+ }
38+ JSON
39+ );
40+ $ options = new Context ();
41+ $ options ->remoteRefProvider = $ refProvider ;
42+ $ schema = Schema::import ($ schemaData , $ options );
43+
44+ $ schema ->in (json_decode ('{"sample": "some-string"} ' )); // no exception for string
45+ try {
46+ $ schema ->in (json_decode ('{"sample": 1} ' )); // exception for int
47+ $ this ->fail ('Exception expected ' );
48+ } catch (InvalidValue $ exception ) {
49+ $ expected = <<<'TEXT'
50+ Swaggest\JsonSchema\Exception\Error Object
51+ (
52+ [error] => String expected, 1 received
53+ [schemaPointers] => Array
54+ (
55+ [0] => /properties/sample/$ref
56+ [1] => file://baseTypes.json#/stringFromOutside
57+ )
58+
59+ [dataPointer] => /sample
60+ [processingPath] => #->properties:sample->$ref[file~2//baseTypes.json#/stringFromOutside]
61+ [subErrors] =>
62+ )
63+
64+ TEXT;
65+
66+ $ this ->assertSame ($ expected , print_r ($ exception ->inspect (), 1 ));
67+ }
68+ }
69+
70+ }
0 commit comments