1+ <?php
2+
3+ /**
4+ * integration test for JsonSchema\Validator
5+ *
6+ * run tests against the example schema hierarchy in /tests/fixtures/
7+ *
8+ * This file is part of the JsonSchema package.
9+ *
10+ * For the full copyright and license information, please view the LICENSE
11+ * file that was distributed with this source code.
12+ */
13+
14+ namespace JsonSchema ;
15+
16+ class ValidatorTest extends \PHPUnit_Framework_TestCase
17+ {
18+ function fixturePath ($ fileName ) {
19+ return dirname (__DIR__ ) .'/fixtures/ ' . $ fileName .'.json ' ;
20+ }
21+ function fixture ($ fileName ) {
22+ return json_decode (file_get_contents ($ this ->fixturePath ($ fileName )));
23+ }
24+
25+ function setUp (){
26+ $ resolver = new RefResolver ();
27+ $ this ->schemaUri = 'file:// ' . $ this ->fixturePath ('Employee ' );
28+ $ retriever = new Uri \UriRetriever ;
29+ $ this ->schema = $ retriever ->retrieve ($ this ->schemaUri );
30+
31+ $ refResolver = new RefResolver ($ retriever );
32+ $ refResolver ->resolve ($ this ->schema , $ this ->schemaUri );
33+
34+ $ this ->data = $ this ->fixture ('employee_instance ' );
35+ }
36+
37+ function assertValid ($ data ) {
38+ $ validator = new Validator ();
39+ $ validator ->check ($ data , $ this ->schema );
40+ $ this ->assertEquals (array (), $ validator ->getErrors (), print_r ($ validator ->getErrors (), true ));
41+ $ this ->assertTrue ($ validator ->isValid ());
42+ }
43+
44+ function assertNotValid ($ data , $ errors ) {
45+ $ validator = new Validator ();
46+ $ validator ->check ($ data , $ this ->schema );
47+ $ this ->assertEquals ($ errors , $ validator ->getErrors (), print_r ($ validator ->getErrors (), true ));
48+ $ this ->assertFalse ($ validator ->isValid ());
49+ }
50+
51+ function testResolvedSchema () {
52+ $ expectedResolvedSchema = $ this ->fixture ('resolved_Employee_schema ' );
53+ $ expectedResolvedSchema ->id = $ this ->schemaUri ;
54+ $ this ->assertEquals ($ expectedResolvedSchema , $ this ->schema , 'expected resolved schema ' );
55+ }
56+
57+ function testEmployeeShouldBeValidate () {
58+ $ this ->assertValid ($ this ->data );
59+ }
60+
61+ function testEmployeeMissingOfficeAddress () {
62+ unset($ this ->data ->person ->office_address );
63+
64+ $ this ->assertNotValid (
65+ $ this ->data
66+ ,array (array (
67+ 'property ' => 'person.office_address '
68+ ,'message ' => 'is missing and it is required '
69+ ))
70+ );
71+ }
72+ }
0 commit comments