EntityTest   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 338
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 28
eloc 154
c 9
b 0
f 0
dl 0
loc 338
rs 10

28 Methods

Rating   Name   Duplication   Size   Complexity  
A testItUseInterface() 0 5 1
A testItInitializeWithDateTimeImmutableAttribute() 0 10 1
A testItInitializeWithMbStringAttribute() 0 10 1
A testItInitializeWithNullAttribute() 0 10 1
A testItSetCamelCaseAttribute() 0 9 1
A testItSetPascalCaseAttribute() 0 9 1
A testItInitializeWithEmptyAttributes() 0 14 1
A testItInitializeWithCamelCaseAttribute() 0 10 1
A testItSetUpperCaseAttribute() 0 9 1
A testItSetAttribute() 0 9 1
A testItInitializeStdClassEmptyAttribute() 0 10 1
A testItSetSnakeCaseAttribute() 0 9 1
A testItInitializeClosureAttribute() 0 12 1
A testItInitializeWithKeyArrayAttribute() 0 10 1
A testItInitializeWithIntAttribute() 0 10 1
A testItInitializeStdClassAttribute() 0 12 1
A testItInitializeWithArrayAttribute() 0 10 1
A testItInitializeWithDateTimeAttribute() 0 10 1
A testItInitializeClassAttribute() 0 10 1
A testItInitializeWithoutAttributes() 0 14 1
A testItInitializeWithDateTimeZoneAttribute() 0 10 1
A testItInitializeWithFloatAttribute() 0 10 1
A testItInitializeWithBoolFalseAttribute() 0 10 1
A testItInitializeWithBoolTrueAttribute() 0 10 1
A testItGetAsString() 0 10 1
A testItInitializeWithEmptyArrayAttribute() 0 10 1
A testItInitializeWithStringAttribute() 0 10 1
A testItInitializeWithDateIntervalAttribute() 0 10 1
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([
32
            'foo' => null,
33
            'bar' => null,
34
            'fooBar' => null,
35
            'FooBar' => null,
36
            'FOOBAR' => null,
37
        ], $entity->toArray());
38
        $this->assertEquals(\json_encode([]), $entity);
39
    }
40
41
    public function testItInitializeWithEmptyAttributes(): void
42
    {
43
        $entity = new EntityMock([]);
44
45
        $this->assertSame(null, $entity->foo());
46
        $this->assertSame(null, $entity->bar());
47
        $this->assertSame([
48
            'foo' => null,
49
            'bar' => null,
50
            'fooBar' => null,
51
            'FooBar' => null,
52
            'FOOBAR' => null,
53
        ], $entity->toArray());
54
        $this->assertEquals(\json_encode([]), (string)$entity);
55
    }
56
57
    public function testItInitializeWithBoolTrueAttribute(): void
58
    {
59
        $foo = true;
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 testItInitializeWithBoolFalseAttribute(): void
70
    {
71
        $foo = false;
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 testItInitializeWithNullAttribute(): void
82
    {
83
        $foo = null;
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 testItInitializeWithStringAttribute(): void
94
    {
95
        $foo = (string)'bar';
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 testItInitializeWithMbStringAttribute(): void
106
    {
107
        $foo = (string)'漢字はユニコード';
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 testItInitializeWithIntAttribute(): void
118
    {
119
        $foo = (int)\rand(1, 9);
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 testItInitializeWithFloatAttribute(): void
130
    {
131
        $foo = \microtime(true);
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 testItInitializeWithEmptyArrayAttribute(): void
142
    {
143
        $foo = [];
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 testItInitializeWithArrayAttribute(): 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 testItInitializeWithKeyArrayAttribute(): void
166
    {
167
        $foo = ['foo' => 'bar'];
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 testItInitializeStdClassEmptyAttribute(): void
178
    {
179
        $foo = new \stdClass;
180
181
        $entity = new EntityMock([
182
            'foo' => $foo,
183
        ]);
184
185
        $this->assertSame($foo, $entity->foo());
186
        $this->assertSame($foo, $entity->toArray()['foo']);
187
    }
188
189
    public function testItInitializeStdClassAttribute(): void
190
    {
191
        $foo = new \stdClass;
192
        $foo->bar = 'bar';
193
        $foo->baz = 'baz';
194
195
        $entity = new EntityMock([
196
            'foo' => $foo,
197
        ]);
198
199
        $this->assertSame($foo, $entity->foo());
200
        $this->assertSame($foo, $entity->toArray()['foo']);
201
    }
202
203
    public function testItInitializeClosureAttribute(): void
204
    {
205
        $foo = function () {
206
            return \microtime();
207
        };
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 testItInitializeClassAttribute(): void
218
    {
219
        $foo = new \Exception('Test');
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 testItInitializeWithDateTimeAttribute(): void
230
    {
231
        $foo = new \DateTime();
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 testItInitializeWithDateTimeImmutableAttribute(): void
242
    {
243
        $foo = new \DateTimeImmutable();
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 testItInitializeWithDateIntervalAttribute(): void
254
    {
255
        $foo = new \DateInterval('P1M');
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 testItInitializeWithDateTimeZoneAttribute(): void
266
    {
267
        $foo = new \DateTimeZone('UTC');
268
269
        $entity = new EntityMock([
270
            'foo' => $foo,
271
        ]);
272
273
        $this->assertSame($foo, $entity->foo());
274
        $this->assertSame($foo, $entity->toArray()['foo']);
275
    }
276
277
    public function testItInitializeWithCamelCaseAttribute(): void
278
    {
279
        $fooBar = 'fooBar';
280
281
        $entity = new EntityMock([
282
            'fooBar' => $fooBar,
283
        ]);
284
285
        $this->assertSame($fooBar, $entity->fooBar());
286
        $this->assertSame($fooBar, $entity->toArray()['fooBar']);
287
    }
288
289
    public function testItSetAttribute(): void
290
    {
291
        $foo = 'foo';
292
293
        $entity = new EntityMock();
294
        $entity->foo($foo);
295
296
        $this->assertSame($foo, $entity->foo());
297
        $this->assertSame($foo, $entity->toArray()['foo']);
298
    }
299
300
    public function testItSetCamelCaseAttribute(): void
301
    {
302
        $fooBar = 'fooBar';
303
304
        $entity = new EntityMock();
305
        $entity->fooBar($fooBar);
306
307
        $this->assertSame($fooBar, $entity->fooBar());
308
        $this->assertSame($fooBar, $entity->toArray()['fooBar']);
309
    }
310
311
    public function testItSetPascalCaseAttribute(): void
312
    {
313
        $FooBar = 'FooBar';
314
315
        $entity = new EntityMock();
316
        $entity->FooBar($FooBar);
317
318
        $this->assertSame($FooBar, $entity->FooBar());
319
        $this->assertSame($FooBar, $entity->toArray()['FooBar']);
320
    }
321
322
    public function testItSetUpperCaseAttribute(): void
323
    {
324
        $FOOBAR = 'FOOBAR';
325
326
        $entity = new EntityMock();
327
        $entity->FOOBAR($FOOBAR);
328
329
        $this->assertSame($FOOBAR, $entity->FOOBAR());
330
        $this->assertSame($FOOBAR, $entity->toArray()['FOOBAR']);
331
    }
332
333
    public function testItSetSnakeCaseAttribute(): void
334
    {
335
        $foo_bar = 'foo_bar';
336
337
        $entity = new EntityMock();
338
        $entity->foo_bar($foo_bar);
339
340
        $this->assertSame($foo_bar, $entity->foo_bar());
341
        $this->assertSame($foo_bar, $entity->toArray()['foo_bar']);
342
    }
343
344
    public function testItGetAsString(): void
345
    {
346
        $foo = 'foo';
347
        $bar = 'bar';
348
349
        $entity = new EntityMock();
350
        $entity->foo($foo);
351
        $entity->bar($bar);
352
353
        $this->assertEquals(\json_encode(\compact('foo', 'bar')), $entity);
354
    }
355
}
356