|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Mapado\RestClientSdk\Tests\Units\Model; |
|
6
|
|
|
|
|
7
|
|
|
use atoum; |
|
8
|
|
|
use Mapado\RestClientSdk\Mapping; |
|
9
|
|
|
use Mapado\RestClientSdk\Mapping\Attribute; |
|
10
|
|
|
use Mapado\RestClientSdk\Mapping\ClassMetadata; |
|
11
|
|
|
use Mapado\RestClientSdk\Model\Serializer; |
|
|
|
|
|
|
12
|
|
|
use Mapado\RestClientSdk\UnitOfWork; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class ModelHydrator |
|
16
|
|
|
* |
|
17
|
|
|
* @author Julien Deniau <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ModelHydrator extends atoum |
|
20
|
|
|
{ |
|
21
|
|
|
private $sdk; |
|
22
|
|
|
|
|
23
|
|
|
private $unitOfWork; |
|
24
|
|
|
|
|
25
|
|
|
public function beforeTestMethod($method) |
|
26
|
|
|
{ |
|
27
|
|
|
$fooMetadata = new ClassMetadata( |
|
28
|
|
|
'foo', |
|
29
|
|
|
'Acme\Foo', |
|
30
|
|
|
'' |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
|
|
$mapping = new Mapping('/v1'); |
|
34
|
|
|
$mapping->setMapping([ |
|
35
|
|
|
$fooMetadata, |
|
36
|
|
|
]); |
|
37
|
|
|
|
|
38
|
|
|
$this->unitOfWork = new UnitOfWork($mapping); |
|
39
|
|
|
|
|
40
|
|
|
$this->mockGenerator->orphanize('__construct'); |
|
41
|
|
|
$this->mockGenerator->shuntParentClassCalls(); |
|
42
|
|
|
$this->sdk = new \mock\Mapado\RestClientSdk\SdkClient(); |
|
|
|
|
|
|
43
|
|
|
$this->calling($this->sdk)->getMapping = $mapping; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* testConvertId |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testConvertId() |
|
50
|
|
|
{ |
|
51
|
|
|
$this |
|
52
|
|
|
->given($this->newTestedInstance($this->sdk)) |
|
53
|
|
|
->then |
|
54
|
|
|
->string($this->testedInstance->convertId(2, 'Acme\Foo')) |
|
55
|
|
|
->isEqualTo('/v1/foo/2') |
|
56
|
|
|
->string($this->testedInstance->convertId('/v1/foo/2', 'Acme\Foo')) |
|
57
|
|
|
->isEqualTo('/v1/foo/2') |
|
58
|
|
|
; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* testConvertIdWithoutMappingPrefix |
|
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, $this->unitOfWork); |
|
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, $this->unitOfWork); |
|
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:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: