1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mapado\RestClientSdk\Tests\Units\Model; |
4
|
|
|
|
5
|
|
|
use atoum; |
6
|
|
|
use DateTime; |
7
|
|
|
use Mapado\RestClientSdk\Mapping; |
8
|
|
|
use Mapado\RestClientSdk\Mapping\Attribute; |
9
|
|
|
use Mapado\RestClientSdk\Mapping\ClassMetadata; |
10
|
|
|
use Mapado\RestClientSdk\Mapping\Relation; |
11
|
|
|
use Mapado\RestClientSdk\Model\Serializer; |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ModelHydrator |
15
|
|
|
* @author Julien Deniau <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class ModelHydrator extends atoum |
18
|
|
|
{ |
19
|
|
|
private $sdk; |
20
|
|
|
|
21
|
|
|
public function beforeTestMethod($method) |
22
|
|
|
{ |
23
|
|
|
$fooMetadata = new ClassMetadata( |
24
|
|
|
'foo', |
25
|
|
|
'Acme\Foo', |
26
|
|
|
'' |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
$mapping = new Mapping('/v1'); |
30
|
|
|
$mapping->setMapping([ |
31
|
|
|
$fooMetadata, |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$this->mockGenerator->orphanize('__construct'); |
35
|
|
|
$this->mockGenerator->shuntParentClassCalls(); |
36
|
|
|
$this->sdk = new \mock\Mapado\RestClientSdk\SdkClient; |
37
|
|
|
$this->calling($this->sdk)->getMapping = $mapping; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* testConvertId |
42
|
|
|
* |
43
|
|
|
* @access public |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function testConvertId() |
47
|
|
|
{ |
48
|
|
|
$this |
49
|
|
|
->given($this->newTestedInstance($this->sdk)) |
50
|
|
|
->then |
51
|
|
|
->string($this->testedInstance->convertId(2, 'Acme\Foo')) |
52
|
|
|
->isEqualTo('/v1/foo/2') |
53
|
|
|
->string($this->testedInstance->convertId('/v1/foo/2', 'Acme\Foo')) |
54
|
|
|
->isEqualTo('/v1/foo/2') |
55
|
|
|
; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* testConvertIdWithoutMappingPrefix |
60
|
|
|
* |
61
|
|
|
* @access public |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function testConvertIdWithoutMappingPrefix() |
65
|
|
|
{ |
66
|
|
|
$fooMetadata = new ClassMetadata( |
67
|
|
|
'foo', |
68
|
|
|
'Acme\Foo', |
69
|
|
|
'' |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$mapping = new Mapping(); |
73
|
|
|
$mapping->setMapping([ |
74
|
|
|
$fooMetadata, |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
$this->mockGenerator->orphanize('__construct'); |
78
|
|
|
$this->mockGenerator->shuntParentClassCalls(); |
79
|
|
|
$sdk = new \mock\Mapado\RestClientSdk\SdkClient; |
80
|
|
|
$this->calling($sdk)->getMapping = $mapping; |
81
|
|
|
$this |
82
|
|
|
->given($this->newTestedInstance($sdk)) |
83
|
|
|
->then |
84
|
|
|
->string($this->testedInstance->convertId(2, 'Acme\Foo')) |
85
|
|
|
->isEqualTo('/foo/2') |
86
|
|
|
->string($this->testedInstance->convertId('/foo/2', 'Acme\Foo')) |
87
|
|
|
->isEqualTo('/foo/2') |
88
|
|
|
; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testHydrateJsonLdItem() |
92
|
|
|
{ |
93
|
|
|
$productMetadata = new ClassMetadata( |
94
|
|
|
'product', |
95
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Product', |
96
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository' |
97
|
|
|
); |
98
|
|
|
$productMetadata->setAttributeList([ |
99
|
|
|
new Attribute('@id', 'id', 'string', true), |
100
|
|
|
new Attribute('value', 'value', 'string'), |
101
|
|
|
new Attribute('currency', 'currency', 'string'), |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
$mapping = new Mapping(); |
105
|
|
|
$mapping->setMapping([ |
106
|
|
|
$productMetadata, |
107
|
|
|
]); |
108
|
|
|
|
109
|
|
|
$this->mockGenerator->orphanize('__construct'); |
110
|
|
|
$this->mockGenerator->shuntParentClassCalls(); |
111
|
|
|
$sdk = new \mock\Mapado\RestClientSdk\SdkClient; |
112
|
|
|
$this->calling($sdk)->getMapping = $mapping; |
113
|
|
|
$this->calling($sdk)->getSerializer = new Serializer($mapping); |
114
|
|
|
|
115
|
|
|
$this |
116
|
|
|
->given($this->newTestedInstance($sdk)) |
117
|
|
|
// test one json-ld entity |
118
|
|
|
->and($productArray = json_decode(file_get_contents(__DIR__ . '/../../data/product.json-ld.json'), true)) |
119
|
|
|
->then |
120
|
|
|
->object($product = $this->testedInstance->hydrate($productArray, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Product')) |
121
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Product') |
122
|
|
|
->string($product->getId()) |
123
|
|
|
->isEqualTo('/products/1') |
124
|
|
|
|
125
|
|
|
// test a json-ld list |
126
|
|
|
->and($productListArray = json_decode(file_get_contents(__DIR__ . '/../../data/productList.json-ld.json'), true)) |
127
|
|
|
->then |
128
|
|
|
->object($productList = $this->testedInstance->hydrateList($productListArray, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Product')) |
129
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Collection\HydraPaginatedCollection') |
130
|
|
|
->integer($productList->getTotalItems()) |
131
|
|
|
->isEqualTo(2) |
132
|
|
|
|
133
|
|
|
->object($product = $productList[0]) |
134
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Product') |
135
|
|
|
->string($product->getId()) |
136
|
|
|
->isEqualTo('/products/1') |
137
|
|
|
->string($productList[1]->getId()) |
138
|
|
|
->isEqualTo('/products/2') |
139
|
|
|
; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testHydrateHalItem() |
143
|
|
|
{ |
144
|
|
|
$orderMetadata = new ClassMetadata( |
145
|
|
|
'order', |
146
|
|
|
'Mapado\RestClientSdk\Tests\Model\Hal\Order', |
147
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository' |
148
|
|
|
); |
149
|
|
|
$orderMetadata->setAttributeList([ |
150
|
|
|
new Attribute('_links.self.href', 'id', 'string', true), |
151
|
|
|
new Attribute('total', 'total', 'float'), |
152
|
|
|
new Attribute('currency', 'currency', 'string'), |
153
|
|
|
new Attribute('status', 'status', 'string'), |
154
|
|
|
]); |
155
|
|
|
|
156
|
|
|
$mapping = new Mapping(); |
157
|
|
|
$mapping->setConfig([ |
158
|
|
|
'collectionKey' => '_embedded.ea:order', |
159
|
|
|
]); |
160
|
|
|
$mapping->setMapping([ $orderMetadata ]); |
161
|
|
|
|
162
|
|
|
$this->mockGenerator->orphanize('__construct'); |
163
|
|
|
$this->mockGenerator->shuntParentClassCalls(); |
164
|
|
|
$sdk = new \mock\Mapado\RestClientSdk\SdkClient; |
165
|
|
|
$this->calling($sdk)->getMapping = $mapping; |
166
|
|
|
$this->calling($sdk)->getSerializer = new Serializer($mapping); |
167
|
|
|
|
168
|
|
|
$this |
169
|
|
|
->given($this->newTestedInstance($sdk)) |
170
|
|
|
// test one hal entity |
171
|
|
|
->and($orderArray = json_decode(file_get_contents(__DIR__ . '/../../data/order.hal.json'), true)) |
172
|
|
|
->then |
173
|
|
|
->object($order = $this->testedInstance->hydrate($orderArray, 'Mapado\RestClientSdk\Tests\Model\Hal\Order')) |
174
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Hal\Order') |
175
|
|
|
->string($order->getStatus()) |
176
|
|
|
->isEqualTo('shipped') |
177
|
|
|
->string($order->getId()) |
178
|
|
|
->isEqualTo('/orders/123') |
179
|
|
|
|
180
|
|
|
// test a json-ld list |
181
|
|
|
->and($orderListArray = json_decode(file_get_contents(__DIR__ . '/../../data/orderList.hal.json'), true)) |
182
|
|
|
->then |
183
|
|
|
->object($orderList = $this->testedInstance->hydrateList($orderListArray, 'Mapado\RestClientSdk\Tests\Model\Hal\Order')) |
184
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Collection\HalCollection') |
185
|
|
|
->integer($orderList->getTotalItems()) |
186
|
|
|
->isEqualTo(2) |
187
|
|
|
|
188
|
|
|
->object($order = $orderList[0]) |
189
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Hal\Order') |
190
|
|
|
->string($order->getId()) |
191
|
|
|
->isEqualTo('/orders/123') |
192
|
|
|
->string($orderList[1]->getId()) |
193
|
|
|
->isEqualTo('/orders/124') |
194
|
|
|
; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: