1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FlexPHP\Entities\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use FlexPHP\Entities\EntityInterface; |
6
|
|
|
use FlexPHP\Entities\Tests\Mocks\EntityMock; |
7
|
|
|
use FlexPHP\Entities\Tests\TestCase; |
8
|
|
|
|
9
|
|
|
class EntityTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testItUseInterface() |
12
|
|
|
{ |
13
|
|
|
$entity = new EntityMock(); |
14
|
|
|
|
15
|
|
|
$this->assertInstanceOf(EntityInterface::class, $entity); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testItInitializeWithoutAttributes() |
19
|
|
|
{ |
20
|
|
|
$entity = new EntityMock(); |
21
|
|
|
|
22
|
|
|
$this->assertSame(null, $entity->foo()); |
23
|
|
|
$this->assertSame(null, $entity->bar()); |
24
|
|
|
$this->assertSame([], $entity->toArray()); |
25
|
|
|
$this->assertEquals(json_encode([]), $entity); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testItInitializeWithEmptyAttributes() |
29
|
|
|
{ |
30
|
|
|
$entity = new EntityMock([]); |
31
|
|
|
|
32
|
|
|
$this->assertSame(null, $entity->foo()); |
33
|
|
|
$this->assertSame(null, $entity->bar()); |
34
|
|
|
$this->assertSame([], $entity->toArray()); |
35
|
|
|
$this->assertEquals(json_encode([]), (string)$entity); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testItInitializeWithBoolTrueAttribute() |
39
|
|
|
{ |
40
|
|
|
$foo = true; |
41
|
|
|
|
42
|
|
|
$entity = new EntityMock([ |
43
|
|
|
'foo' => $foo, |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
$this->assertSame($foo, $entity->foo()); |
47
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testItInitializeWithBoolFalseAttribute() |
51
|
|
|
{ |
52
|
|
|
$foo = false; |
53
|
|
|
|
54
|
|
|
$entity = new EntityMock([ |
55
|
|
|
'foo' => $foo, |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
$this->assertSame($foo, $entity->foo()); |
59
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testItInitializeWithNullAttribute() |
63
|
|
|
{ |
64
|
|
|
$foo = null; |
65
|
|
|
|
66
|
|
|
$entity = new EntityMock([ |
67
|
|
|
'foo' => $foo, |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
$this->assertSame($foo, $entity->foo()); |
71
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testItInitializeWithStringAttribute() |
75
|
|
|
{ |
76
|
|
|
$foo = (string)'bar'; |
77
|
|
|
|
78
|
|
|
$entity = new EntityMock([ |
79
|
|
|
'foo' => $foo, |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
$this->assertSame($foo, $entity->foo()); |
83
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testItInitializeWithIntAttribute() |
87
|
|
|
{ |
88
|
|
|
$foo = (int)rand(1, 9); |
89
|
|
|
|
90
|
|
|
$entity = new EntityMock([ |
91
|
|
|
'foo' => $foo, |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$this->assertSame($foo, $entity->foo()); |
95
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testItInitializeWithFloatAttribute() |
99
|
|
|
{ |
100
|
|
|
$foo = microtime(true); |
101
|
|
|
|
102
|
|
|
$entity = new EntityMock([ |
103
|
|
|
'foo' => $foo, |
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
$this->assertSame($foo, $entity->foo()); |
107
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testItInitializeWithEmptyArrayAttribute() |
111
|
|
|
{ |
112
|
|
|
$foo = []; |
113
|
|
|
|
114
|
|
|
$entity = new EntityMock([ |
115
|
|
|
'foo' => $foo, |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
$this->assertSame($foo, $entity->foo()); |
119
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testItInitializeWithArrayAttribute() |
123
|
|
|
{ |
124
|
|
|
$foo = ['foo', 'bar']; |
125
|
|
|
|
126
|
|
|
$entity = new EntityMock([ |
127
|
|
|
'foo' => $foo, |
128
|
|
|
]); |
129
|
|
|
|
130
|
|
|
$this->assertSame($foo, $entity->foo()); |
131
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testItInitializeWithKeyArrayAttribute() |
135
|
|
|
{ |
136
|
|
|
$foo = ['foo' => 'bar']; |
137
|
|
|
|
138
|
|
|
$entity = new EntityMock([ |
139
|
|
|
'foo' => $foo, |
140
|
|
|
]); |
141
|
|
|
|
142
|
|
|
$this->assertSame($foo, $entity->foo()); |
143
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testItInitializeWithCamelCaseAttribute() |
147
|
|
|
{ |
148
|
|
|
$fooBar = 'fooBar'; |
149
|
|
|
|
150
|
|
|
$entity = new EntityMock([ |
151
|
|
|
'fooBar' => $fooBar, |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
$this->assertSame($fooBar, $entity->fooBar()); |
155
|
|
|
$this->assertSame($fooBar, $entity->toArray()['fooBar']); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testItSetAttribute() |
159
|
|
|
{ |
160
|
|
|
$foo = 'foo'; |
161
|
|
|
|
162
|
|
|
$entity = new EntityMock(); |
163
|
|
|
$entity->foo($foo); |
164
|
|
|
|
165
|
|
|
$this->assertSame($foo, $entity->foo()); |
166
|
|
|
$this->assertSame($foo, $entity->toArray()['foo']); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testItSetCamelCaseAttribute() |
170
|
|
|
{ |
171
|
|
|
$fooBar = 'fooBar'; |
172
|
|
|
|
173
|
|
|
$entity = new EntityMock(); |
174
|
|
|
$entity->fooBar($fooBar); |
175
|
|
|
|
176
|
|
|
$this->assertSame($fooBar, $entity->fooBar()); |
177
|
|
|
$this->assertSame($fooBar, $entity->toArray()['fooBar']); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testItSetAttributeInChain() |
181
|
|
|
{ |
182
|
|
|
$foo = 'foo'; |
183
|
|
|
$bar = 1; |
184
|
|
|
$fooBar = true; |
185
|
|
|
|
186
|
|
|
$entity = new EntityMock(); |
187
|
|
|
$entity->foo($foo)->bar($bar)->fooBar($fooBar); |
188
|
|
|
|
189
|
|
|
$this->assertSame($foo, $entity->foo()); |
190
|
|
|
$this->assertSame($bar, $entity->bar()); |
191
|
|
|
$this->assertSame($fooBar, $entity->fooBar()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testItGetAsString() |
195
|
|
|
{ |
196
|
|
|
$foo = 'foo'; |
197
|
|
|
$bar = 'bar'; |
198
|
|
|
|
199
|
|
|
$entity = new EntityMock(); |
200
|
|
|
$entity->foo($foo)->bar($bar); |
201
|
|
|
|
202
|
|
|
$this->assertEquals(json_encode(compact('foo', 'bar')), $entity); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|