|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of FlexPHP. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Freddie Gar <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace FlexPHP\Entities\Tests\Unit; |
|
11
|
|
|
|
|
12
|
|
|
use FlexPHP\Entities\EntityInterface; |
|
13
|
|
|
use FlexPHP\Entities\Tests\Mocks\EntityMock; |
|
14
|
|
|
use FlexPHP\Entities\Tests\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
class EntityTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function testItUseInterface(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$entity = new EntityMock(); |
|
21
|
|
|
|
|
22
|
|
|
$this->assertInstanceOf(EntityInterface::class, $entity); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testItInitializeWithoutAttributes(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$entity = new EntityMock(); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertSame(null, $entity->foo()); |
|
30
|
|
|
$this->assertSame(null, $entity->bar()); |
|
31
|
|
|
$this->assertSame([], $entity->toArray()); |
|
32
|
|
|
$this->assertEquals(\json_encode([]), $entity); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testItInitializeWithEmptyAttributes(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$entity = new EntityMock([]); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertSame(null, $entity->foo()); |
|
40
|
|
|
$this->assertSame(null, $entity->bar()); |
|
41
|
|
|
$this->assertSame([], $entity->toArray()); |
|
42
|
|
|
$this->assertEquals(\json_encode([]), (string)$entity); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testItInitializeWithBoolTrueAttribute(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$foo = true; |
|
48
|
|
|
|
|
49
|
|
|
$entity = new EntityMock([ |
|
50
|
|
|
'foo' => $foo, |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
54
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testItInitializeWithBoolFalseAttribute(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$foo = false; |
|
60
|
|
|
|
|
61
|
|
|
$entity = new EntityMock([ |
|
62
|
|
|
'foo' => $foo, |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
66
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testItInitializeWithNullAttribute(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$foo = null; |
|
72
|
|
|
|
|
73
|
|
|
$entity = new EntityMock([ |
|
74
|
|
|
'foo' => $foo, |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
78
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testItInitializeWithStringAttribute(): void |
|
82
|
|
|
{ |
|
83
|
|
|
$foo = (string)'bar'; |
|
84
|
|
|
|
|
85
|
|
|
$entity = new EntityMock([ |
|
86
|
|
|
'foo' => $foo, |
|
87
|
|
|
]); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
90
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testItInitializeWithMbStringAttribute(): void |
|
94
|
|
|
{ |
|
95
|
|
|
$foo = (string)'漢字はユニコード'; |
|
96
|
|
|
|
|
97
|
|
|
$entity = new EntityMock([ |
|
98
|
|
|
'foo' => $foo, |
|
99
|
|
|
]); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
102
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testItInitializeWithIntAttribute(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$foo = (int)\rand(1, 9); |
|
108
|
|
|
|
|
109
|
|
|
$entity = new EntityMock([ |
|
110
|
|
|
'foo' => $foo, |
|
111
|
|
|
]); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
114
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testItInitializeWithFloatAttribute(): void |
|
118
|
|
|
{ |
|
119
|
|
|
$foo = \microtime(true); |
|
120
|
|
|
|
|
121
|
|
|
$entity = new EntityMock([ |
|
122
|
|
|
'foo' => $foo, |
|
123
|
|
|
]); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
126
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testItInitializeWithEmptyArrayAttribute(): void |
|
130
|
|
|
{ |
|
131
|
|
|
$foo = []; |
|
132
|
|
|
|
|
133
|
|
|
$entity = new EntityMock([ |
|
134
|
|
|
'foo' => $foo, |
|
135
|
|
|
]); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
138
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testItInitializeWithArrayAttribute(): void |
|
142
|
|
|
{ |
|
143
|
|
|
$foo = ['foo', 'bar']; |
|
144
|
|
|
|
|
145
|
|
|
$entity = new EntityMock([ |
|
146
|
|
|
'foo' => $foo, |
|
147
|
|
|
]); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
150
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testItInitializeWithKeyArrayAttribute(): void |
|
154
|
|
|
{ |
|
155
|
|
|
$foo = ['foo' => 'bar']; |
|
156
|
|
|
|
|
157
|
|
|
$entity = new EntityMock([ |
|
158
|
|
|
'foo' => $foo, |
|
159
|
|
|
]); |
|
160
|
|
|
|
|
161
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
162
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function testItInitializeStdClassEmptyAttribute(): void |
|
166
|
|
|
{ |
|
167
|
|
|
$foo = new \stdClass; |
|
168
|
|
|
|
|
169
|
|
|
$entity = new EntityMock([ |
|
170
|
|
|
'foo' => $foo, |
|
171
|
|
|
]); |
|
172
|
|
|
|
|
173
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
174
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testItInitializeStdClassAttribute(): void |
|
178
|
|
|
{ |
|
179
|
|
|
$foo = new \stdClass; |
|
180
|
|
|
$foo->bar = 'bar'; |
|
181
|
|
|
$foo->baz = 'baz'; |
|
182
|
|
|
|
|
183
|
|
|
$entity = new EntityMock([ |
|
184
|
|
|
'foo' => $foo, |
|
185
|
|
|
]); |
|
186
|
|
|
|
|
187
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
188
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function testItInitializeClosureAttribute(): void |
|
192
|
|
|
{ |
|
193
|
|
|
$foo = function () { |
|
194
|
|
|
return \microtime(); |
|
195
|
|
|
}; |
|
196
|
|
|
|
|
197
|
|
|
$entity = new EntityMock([ |
|
198
|
|
|
'foo' => $foo, |
|
199
|
|
|
]); |
|
200
|
|
|
|
|
201
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
202
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function testItInitializeClassAttribute(): void |
|
206
|
|
|
{ |
|
207
|
|
|
$foo = new \Exception('Test'); |
|
208
|
|
|
|
|
209
|
|
|
$entity = new EntityMock([ |
|
210
|
|
|
'foo' => $foo, |
|
211
|
|
|
]); |
|
212
|
|
|
|
|
213
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
214
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function testItInitializeWithDateTimeAttribute(): void |
|
218
|
|
|
{ |
|
219
|
|
|
$foo = new \DateTime(); |
|
220
|
|
|
|
|
221
|
|
|
$entity = new EntityMock([ |
|
222
|
|
|
'foo' => $foo, |
|
223
|
|
|
]); |
|
224
|
|
|
|
|
225
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
226
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function testItInitializeWithDateTimeImmutableAttribute(): void |
|
230
|
|
|
{ |
|
231
|
|
|
$foo = new \DateTimeImmutable(); |
|
232
|
|
|
|
|
233
|
|
|
$entity = new EntityMock([ |
|
234
|
|
|
'foo' => $foo, |
|
235
|
|
|
]); |
|
236
|
|
|
|
|
237
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
238
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public function testItInitializeWithDateIntervalAttribute(): void |
|
242
|
|
|
{ |
|
243
|
|
|
$foo = new \DateInterval('P1M'); |
|
244
|
|
|
|
|
245
|
|
|
$entity = new EntityMock([ |
|
246
|
|
|
'foo' => $foo, |
|
247
|
|
|
]); |
|
248
|
|
|
|
|
249
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
250
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
public function testItInitializeWithDateTimeZoneAttribute(): void |
|
254
|
|
|
{ |
|
255
|
|
|
$foo = new \DateTimeZone('UTC'); |
|
256
|
|
|
|
|
257
|
|
|
$entity = new EntityMock([ |
|
258
|
|
|
'foo' => $foo, |
|
259
|
|
|
]); |
|
260
|
|
|
|
|
261
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
262
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
public function testItInitializeWithCamelCaseAttribute(): void |
|
266
|
|
|
{ |
|
267
|
|
|
$fooBar = 'fooBar'; |
|
268
|
|
|
|
|
269
|
|
|
$entity = new EntityMock([ |
|
270
|
|
|
'fooBar' => $fooBar, |
|
271
|
|
|
]); |
|
272
|
|
|
|
|
273
|
|
|
$this->assertSame($fooBar, $entity->fooBar()); |
|
274
|
|
|
$this->assertSame($fooBar, $entity->toArray()['fooBar']); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
public function testItSetAttribute(): void |
|
278
|
|
|
{ |
|
279
|
|
|
$foo = 'foo'; |
|
280
|
|
|
|
|
281
|
|
|
$entity = new EntityMock(); |
|
282
|
|
|
$entity->foo($foo); |
|
283
|
|
|
|
|
284
|
|
|
$this->assertSame($foo, $entity->foo()); |
|
285
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
public function testItSetCamelCaseAttribute(): void |
|
289
|
|
|
{ |
|
290
|
|
|
$fooBar = 'fooBar'; |
|
291
|
|
|
|
|
292
|
|
|
$entity = new EntityMock(); |
|
293
|
|
|
$entity->fooBar($fooBar); |
|
294
|
|
|
|
|
295
|
|
|
$this->assertSame($fooBar, $entity->fooBar()); |
|
296
|
|
|
$this->assertSame($fooBar, $entity->toArray()['fooBar']); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
public function testItSetPascalCaseAttribute(): void |
|
300
|
|
|
{ |
|
301
|
|
|
$FooBar = 'FooBar'; |
|
302
|
|
|
|
|
303
|
|
|
$entity = new EntityMock(); |
|
304
|
|
|
$entity->FooBar($FooBar); |
|
305
|
|
|
|
|
306
|
|
|
$this->assertSame($FooBar, $entity->FooBar()); |
|
307
|
|
|
$this->assertSame($FooBar, $entity->toArray()['fooBar']); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
public function testItSetUpperCaseAttribute(): void |
|
311
|
|
|
{ |
|
312
|
|
|
$FOOBAR = 'FOOBAR'; |
|
313
|
|
|
|
|
314
|
|
|
$entity = new EntityMock(); |
|
315
|
|
|
$entity->FOOBAR($FOOBAR); |
|
316
|
|
|
|
|
317
|
|
|
$this->assertSame($FOOBAR, $entity->FOOBAR()); |
|
318
|
|
|
$this->assertSame($FOOBAR, $entity->toArray()['fOOBAR']); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
public function testItGetAsString(): void |
|
322
|
|
|
{ |
|
323
|
|
|
$foo = 'foo'; |
|
324
|
|
|
$bar = 'bar'; |
|
325
|
|
|
|
|
326
|
|
|
$entity = new EntityMock(); |
|
327
|
|
|
$entity->foo($foo); |
|
328
|
|
|
$entity->bar($bar); |
|
329
|
|
|
|
|
330
|
|
|
$this->assertEquals(\json_encode(\compact('foo', 'bar')), $entity); |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|