NamespaceTest.php
137 lines
| 1 | <?php |
| 2 | namespace namespacetest; |
| 3 | require_once __DIR__ . '/Unit.php'; |
| 4 | require_once __DIR__ . '/UnitData.php'; |
| 5 | require_once __DIR__ . '/model/User.php'; |
| 6 | require_once __DIR__ . '/model/Group.php'; |
| 7 | require_once __DIR__ . '/model/UserList.php'; |
| 8 | require_once __DIR__ . '/../othernamespace/Programmers.php'; |
| 9 | require_once __DIR__ . '/../othernamespace/Foo.php'; |
| 10 | |
| 11 | use apimatic\jsonmapper\JsonMapper; |
| 12 | use apimatic\jsonmapper\JsonMapperException; |
| 13 | use namespacetest\model\User; |
| 14 | use othernamespace\Programmers; |
| 15 | |
| 16 | /** |
| 17 | * @covers \apimatic\jsonmapper\JsonMapper |
| 18 | * @covers \apimatic\jsonmapper\TypeCombination |
| 19 | * @covers \apimatic\jsonmapper\JsonMapperException |
| 20 | */ |
| 21 | class NamespaceTest extends \PHPUnit\Framework\TestCase |
| 22 | { |
| 23 | public function testMapArrayNamespace() |
| 24 | { |
| 25 | $mapper = new JsonMapper(); |
| 26 | $json = '{"data":[{"value":"1.2"}]}'; |
| 27 | $res = $mapper->map(json_decode($json), new UnitData()); |
| 28 | $this->assertInstanceOf('\namespacetest\UnitData', $res); |
| 29 | $this->assertInstanceOf('\namespacetest\Unit', $res->data[0]); |
| 30 | } |
| 31 | |
| 32 | public function testMapSimpleArrayNamespace() |
| 33 | { |
| 34 | $mapper = new JsonMapper(); |
| 35 | $json = '{"units":[{"value":"1.2"}]}'; |
| 36 | $res = $mapper->map(json_decode($json), new UnitData()); |
| 37 | $this->assertInstanceOf('\namespacetest\UnitData', $res); |
| 38 | $this->assertInstanceOf('\namespacetest\Unit', $res->units[0]); |
| 39 | } |
| 40 | |
| 41 | public function testMapSimpleStringArrayNamespace() |
| 42 | { |
| 43 | $mapper = new JsonMapper(); |
| 44 | $json = '{"messages":["message 1", "message 2"]}'; |
| 45 | $res = $mapper->map(json_decode($json), new UnitData()); |
| 46 | $this->assertInstanceOf('\namespacetest\UnitData', $res); |
| 47 | $this->assertNotNull($res->messages); |
| 48 | $this->assertCount(2, $res->messages); |
| 49 | } |
| 50 | |
| 51 | public function testMapChildClassNamespace() |
| 52 | { |
| 53 | $mapper = new JsonMapper(); |
| 54 | $json = '{"user":{"name": "John Smith"}}'; |
| 55 | $res = $mapper->map(json_decode($json), new UnitData()); |
| 56 | $this->assertInstanceOf('\namespacetest\UnitData', $res); |
| 57 | $this->assertInstanceOf('\namespacetest\model\User', $res->user); |
| 58 | } |
| 59 | |
| 60 | public function testMapChildClassConstructorNamespace() |
| 61 | { |
| 62 | $mapper = new JsonMapper(); |
| 63 | $json = '{"user":"John Smith"}'; |
| 64 | $res = $mapper->map(json_decode($json), new UnitData()); |
| 65 | $this->assertInstanceOf('\namespacetest\UnitData', $res); |
| 66 | $this->assertInstanceOf('\namespacetest\model\User', $res->user); |
| 67 | } |
| 68 | |
| 69 | public function testMapChildObjectArrayNamespace() |
| 70 | { |
| 71 | $mapper = new JsonMapper(); |
| 72 | $json = '{"data":null,"user":{"name": "John Smith"}}'; |
| 73 | /* @var \namespacetest\UnitData $res */ |
| 74 | $res = $mapper->map(json_decode($json), new UnitData()); |
| 75 | $this->assertNull($res->data); |
| 76 | $this->assertInstanceOf('\namespacetest\model\User', $res->user); |
| 77 | } |
| 78 | |
| 79 | public function testMapEmpty() |
| 80 | { |
| 81 | $this->expectException(JsonMapperException::class); |
| 82 | $this->expectExceptionMessage("Empty type at property 'namespacetest\UnitData::\$empty'"); |
| 83 | $mapper = new JsonMapper(); |
| 84 | $json = '{"empty":{}}'; |
| 85 | /* @var \namespacetest\UnitData $res */ |
| 86 | $res = $mapper->map(json_decode($json), new UnitData()); |
| 87 | } |
| 88 | |
| 89 | public function testMapCustomArraObjectWithChildType() |
| 90 | { |
| 91 | $mapper = new JsonMapper(); |
| 92 | $json = '{"users":[{"user":"John Smith"}]}'; |
| 93 | $res = $mapper->map(json_decode($json), new UnitData()); |
| 94 | $this->assertInstanceOf('\namespacetest\UnitData', $res); |
| 95 | $this->assertInstanceOf('\namespacetest\model\UserList', $res->users); |
| 96 | $this->assertInstanceOf('\namespacetest\model\User', $res->users[0]); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Test a setter method with a namespaced type hint that |
| 101 | * is within another namespace than the object itself. |
| 102 | */ |
| 103 | public function testSetterNamespacedTypeHint() |
| 104 | { |
| 105 | $mapper = new JsonMapper(); |
| 106 | $json = '{"namespacedTypeHint":"Foo"}'; |
| 107 | $res = $mapper->map(json_decode($json), new UnitData()); |
| 108 | $this->assertInstanceOf('\namespacetest\UnitData', $res); |
| 109 | $this->assertInstanceOf( |
| 110 | '\othernamespace\Foo', $res->internalData['namespacedTypeHint'] |
| 111 | ); |
| 112 | $this->assertEquals( |
| 113 | 'Foo', $res->internalData['namespacedTypeHint']->name |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Test a setter method with a namespaced type hint that |
| 119 | * is within another namespace than the object itself. |
| 120 | */ |
| 121 | public function testParentInDifferentNamespace() |
| 122 | { |
| 123 | $mapper = new JsonMapper(); |
| 124 | $json = '{"language":"PHP","languageUser":{"name":"phpUser"},"lead":{"name":"phpLead"},"users":[{"name":"member1"},{"name":"member2"}]}'; |
| 125 | $res = $mapper->mapClass(json_decode($json), Programmers::class); |
| 126 | $this->assertInstanceOf(Programmers::class, $res); |
| 127 | $this->assertEquals('PHP', $res->language); |
| 128 | $this->assertInstanceOf(User::class, $res->languageUser); |
| 129 | $this->assertEquals('phpUser', $res->languageUser->name); |
| 130 | $this->assertInstanceOf(User::class, $res->lead); |
| 131 | $this->assertEquals('phpLead', $res->lead->name); |
| 132 | $this->assertTrue(is_array($res->getUsers())); |
| 133 | $this->assertEquals('member1', $res->getUsers()[0]->name); |
| 134 | $this->assertEquals('member2', $res->getUsers()[1]->name); |
| 135 | } |
| 136 | } |
| 137 |