33namespace Swaggest \JsonSchema \Tests \PHPUnit \ClassStructure ;
44
55
6+ use Swaggest \JsonSchema \Structure \Composition ;
7+ use Swaggest \JsonSchema \Tests \Helper \LevelThreeClass ;
68use Swaggest \JsonSchema \Tests \Helper \NestedStructure ;
79use Swaggest \JsonSchema \Tests \Helper \SampleStructure ;
810
911class NestedTest extends \PHPUnit_Framework_TestCase
1012{
1113 public function testClassStructure ()
1214 {
13- $ schema = NestedStructure::schema ();
1415 $ data = json_decode (<<<JSON
1516{
1617 "ownString": "aaa",
1718 "ownMagicInt": 1,
18- "native": true
19+ "native": true,
20+ "propOne": "bbb"
1921}
2022JSON
21- );
23+ );
24+ $ object = NestedStructure::import ($ data );
25+ $ this ->assertSame ('aaa ' , $ object ->ownString );
26+ $ this ->assertSame (true , $ object ->sampleNested ->native );
27+ $ this ->assertSame ('bbb ' , $ object ->sampleNested ->propOne );
28+
29+ $ data2 = NestedStructure::export ($ object );
30+ $ this ->assertEquals ((array )$ data , (array )$ data2 );
31+
32+ $ object ->sampleNested ->propOne = 'ccc ' ;
33+ $ this ->assertSame ('ccc ' , $ object ->propOne );
34+ }
35+
36+
37+ public function testDynamic ()
38+ {
39+ $ schema = new Composition (SampleStructure::schema (), LevelThreeClass::schema ());
40+
41+ $ data = json_decode (<<<JSON
42+ {
43+ "ownString": "aaa",
44+ "ownMagicInt": 1,
45+ "native": true,
46+ "propOne": "bbb",
47+ "level3": 3
48+ }
49+ JSON
50+ );
51+
2252 $ object = $ schema ->import ($ data );
53+
2354 $ this ->assertSame ('aaa ' , $ object ->ownString );
24- $ this ->assertSame (true , $ object ->getNested (SampleStructure::class)->native );
55+ $ this ->assertSame (1 , $ object ->ownMagicInt );
56+ $ this ->assertSame (3 , $ object ->level3 ); // flat accessor
57+ $ this ->assertSame (true , $ object ->native );
58+
59+ $ sample = SampleStructure::pick ($ object );
60+ $ l3 = LevelThreeClass::pick ($ object );
61+
62+ $ this ->assertSame ('bbb ' , $ sample ->propOne );
63+ $ this ->assertSame (true , $ sample ->native );
64+
65+ $ this ->assertSame (3 , $ l3 ->level3 );
66+
67+ $ l3 ->level3 = 2 ;
68+ $ this ->assertSame (2 , $ object ->level3 ); // flat accessor
69+
70+ $ object ->level3 = 5 ;
71+ $ this ->assertSame (5 , $ l3 ->level3 );
72+
73+ $ sample ->propTwo = 8 ;
74+
75+ $ data2 = $ schema ->export ($ object );
76+ $ this ->assertEquals (array (
77+ 'ownString ' => 'aaa ' ,
78+ 'ownMagicInt ' => 1 ,
79+ 'native ' => true ,
80+ 'propOne ' => 'bbb ' ,
81+ 'level3 ' => 5 ,
82+ 'propTwo ' => 8 ,
83+ ), (array )$ data2 );
2584 }
2685
2786}
0 commit comments