1+ <?php
2+
3+ namespace JsonSchema \Tests \Uri ;
4+
5+ use JsonSchema \Uri \UriResolver ;
6+
7+ class UriResolverTest extends \PHPUnit_Framework_TestCase {
8+
9+ function testParseWithFragment () {
10+ $ resolver = new UriResolver ();
11+ $ this ->assertEquals (
12+ array ('scheme ' => 'file ' , 'authority ' => '' , 'path ' => '/the/directory ' , 'query ' =>'' , 'fragment ' => '/the/fragment ' )
13+ ,$ resolver ->parse ('file:///the/directory#/the/fragment ' )
14+ );
15+ }
16+
17+ function testParseWithQuery () {
18+ $ resolver = new UriResolver ();
19+ $ this ->assertEquals (
20+ array ('scheme ' => 'file ' , 'authority ' => '' , 'path ' => '/the/directory ' , 'query ' =>'the=query&key=val ' )
21+ ,$ resolver ->parse ('file:///the/directory?the=query&key=val ' )
22+ );
23+ }
24+
25+ function testParseWithHttp () {
26+ $ resolver = new UriResolver ();
27+ $ this ->assertEquals (
28+ array ('scheme ' => 'http ' , 'authority ' => 'localhost:1234 ' , 'path ' => '/the/directory ' )
29+ ,$ resolver ->parse ('http://localhost:1234/the/directory ' )
30+ );
31+ }
32+
33+ function testParseFragmentOnly () {
34+ $ resolver = new UriResolver ();
35+ $ this ->assertEquals (
36+ array ('scheme ' => '' , 'authority ' => '' , 'path ' => '' , 'query ' =>'' , 'fragment ' => '/theFragment ' )
37+ ,$ resolver ->parse ('#/theFragment ' )
38+ );
39+ }
40+
41+ function testResolveWithBothHttp () {
42+ $ resolver = new UriResolver ();
43+ $ this ->setExpectedException ('JsonSchema\Exception\UriResolverException ' , "Unable to resolve URI '/the/path' from base '' " );
44+ $ this ->assertEquals ('http://localhost:1234/the/path ' , $ resolver ->resolve ('/the/path ' , 'http://localhost:1234 ' ));
45+ }
46+
47+ function testResolveWithValidHttpBaseUri () {
48+ $ resolver = new UriResolver ();
49+
50+ $ this ->assertEquals ('http://localhost:1234/the/other/schema.json ' , $ resolver ->resolve ('/the/other/schema.json ' , 'http://localhost:1234/ ' ));
51+ $ this ->assertEquals ('http://localhost:1234/other/foo.json ' , $ resolver ->resolve ('/other/foo.json ' , 'http://localhost:1234/schema.json ' ));
52+ $ this ->assertEquals ('http://localhost:1234/foo.json#/fragment ' , $ resolver ->resolve ('/foo.json#/fragment ' , 'http://localhost:1234/schma.json ' ));
53+ $ this ->assertEquals ('http://localhost:1234/three/dirs/bar.json ' , $ resolver ->resolve ('bar.json ' , 'http://localhost:1234/three/dirs/schema.json ' ));
54+ $ this ->assertEquals ('http://localhost:1234/sourceSchema.json#/localTarget ' , $ resolver ->resolve ('#/localTarget ' , 'http://localhost:1234/sourceSchema.json#/sourceNode ' ));
55+ }
56+
57+ function testResolveWithValidFileBaseUri () {
58+ $ resolver = new UriResolver ();
59+ $ this ->assertEquals ('file:///base/uri/foo.json ' , $ resolver ->resolve ('foo.json ' , 'file:///base/uri/dir.json ' ));
60+ $ this ->assertEquals ('file:///other-dir/foo.json ' , $ resolver ->resolve ('file:///other-dir/foo.json ' , 'file:///base/uri/dir.json ' ));
61+
62+ }
63+
64+ function testCombineRelativePathWithBasePath () {
65+ $ this ->assertEquals ('/foo/bar/boo ' , UriResolver::combineRelativePathWithBasePath ('boo ' , '/foo/bar/ ' ));
66+ $ this ->assertEquals ('/foo/bar/ ' , UriResolver::combineRelativePathWithBasePath ('' , '/foo/bar/ ' ));
67+ $ this ->assertEquals ('/foo/bar ' , UriResolver::combineRelativePathWithBasePath ('' , '/foo/bar ' ));
68+ // ??? imho /boo
69+ $ this ->assertEquals ('/foo/bar/boo ' , UriResolver::combineRelativePathWithBasePath ('/boo ' , '/foo/bar/ ' ));
70+ $ this ->assertEquals ('/foo.json ' , UriResolver::combineRelativePathWithBasePath ('' , '/foo.json ' ));
71+
72+ }
73+ }
0 commit comments