|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Models; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Spiral\Models\DataEntity; |
|
16
|
|
|
|
|
17
|
|
|
class DataEntityTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function testSetter(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$entity = new DataEntity(); |
|
22
|
|
|
$entity->setField('abc', 123); |
|
23
|
|
|
$this->assertEquals(123, $entity->getField('abc')); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertTrue($entity->hasField('abc')); |
|
26
|
|
|
$this->assertFalse($entity->hasField('bce')); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testMagicProperties(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$entity = new DataEntity(); |
|
32
|
|
|
$entity->abc = 123; |
|
|
|
|
|
|
33
|
|
|
$this->assertEquals(123, $entity->abc); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
$this->assertTrue(isset($entity->abc)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testPackingSimple(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$entity = new DataEntity(['a' => 'b', 'c' => 10]); |
|
41
|
|
|
$this->assertSame(['a' => 'b', 'c' => 10], $entity->getValue()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testSerialize(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$data = ['a' => 123, 'b' => null, 'c' => 'test']; |
|
47
|
|
|
|
|
48
|
|
|
$entity = new DataEntity($data); |
|
49
|
|
|
$this->assertEquals($data, $entity->getValue()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testSetValue(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$data = ['a' => 123, 'b' => null, 'c' => 'test']; |
|
55
|
|
|
|
|
56
|
|
|
$entity = new PublicEntity($data); |
|
57
|
|
|
$this->assertEquals($data, $entity->getValue()); |
|
58
|
|
|
|
|
59
|
|
|
$entity = new PublicEntity(); |
|
60
|
|
|
$entity->setValue(['a' => 123]); |
|
61
|
|
|
$this->assertEquals(['a' => 123], $entity->getValue()); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertSame(['a'], $entity->getKeys()); |
|
64
|
|
|
$this->assertTrue(isset($entity->a)); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
unset($entity->a); |
|
67
|
|
|
$this->assertEquals([], $entity->getValue()); |
|
68
|
|
|
|
|
69
|
|
|
$entity['a'] = 90; |
|
70
|
|
|
$this->assertEquals(['a' => 90], $entity->getValue()); |
|
71
|
|
|
$this->assertSame(90, $entity['a']); |
|
72
|
|
|
$this->assertTrue(isset($entity['a'])); |
|
73
|
|
|
|
|
74
|
|
|
unset($entity['a']); |
|
75
|
|
|
$this->assertEquals([], $entity->getValue()); |
|
76
|
|
|
|
|
77
|
|
|
$entity['a'] = 90; |
|
78
|
|
|
foreach ($entity as $key => $value) { |
|
79
|
|
|
$this->assertSame('a', $key); |
|
80
|
|
|
$this->assertSame(90, $value); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->assertSame('a', $key); |
|
|
|
|
|
|
84
|
|
|
$this->assertSame(90, $value); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
$this->assertEquals(['a' => 90], $entity->toArray()); |
|
87
|
|
|
$this->assertEquals(['a' => 90], $entity->jsonSerialize()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testSecured(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$entity = new SecuredEntity(); |
|
93
|
|
|
$entity->setValue([ |
|
94
|
|
|
'name' => 'Antony', |
|
95
|
|
|
'id' => '900' |
|
96
|
|
|
]); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertEquals([], $entity->getValue()); |
|
99
|
|
|
|
|
100
|
|
|
$entity = new PartiallySecuredEntity(); |
|
101
|
|
|
$entity->setValue([ |
|
102
|
|
|
'name' => 'Antony', |
|
103
|
|
|
'id' => 900 |
|
104
|
|
|
]); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertEquals([ |
|
107
|
|
|
'id' => 900 |
|
108
|
|
|
], $entity->getValue()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testSetters(): void |
|
112
|
|
|
{ |
|
113
|
|
|
$entity = new FilteredEntity(); |
|
114
|
|
|
$entity->setValue([ |
|
115
|
|
|
'name' => 'Antony', |
|
116
|
|
|
'id' => '900' |
|
117
|
|
|
]); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertEquals([ |
|
120
|
|
|
'id' => 900 |
|
121
|
|
|
], $entity->getValue()); |
|
122
|
|
|
|
|
123
|
|
|
$entity->id = []; |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals([ |
|
126
|
|
|
'id' => 0 |
|
127
|
|
|
], $entity->getValue()); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testNullable(): void |
|
131
|
|
|
{ |
|
132
|
|
|
$entity = new NullableEntity(); |
|
133
|
|
|
$entity->setValue([ |
|
134
|
|
|
'name' => 'Antony', |
|
135
|
|
|
'id' => '900' |
|
136
|
|
|
]); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertEquals([ |
|
139
|
|
|
'name' => 'Antony', |
|
140
|
|
|
'id' => 900 |
|
141
|
|
|
], $entity->getValue()); |
|
142
|
|
|
|
|
143
|
|
|
// no filter |
|
144
|
|
|
$entity->name = null; |
|
|
|
|
|
|
145
|
|
|
$this->assertEquals([ |
|
146
|
|
|
'name' => null, |
|
147
|
|
|
'id' => 900 |
|
148
|
|
|
], $entity->getValue()); |
|
149
|
|
|
|
|
150
|
|
|
$entity->id = null; |
|
|
|
|
|
|
151
|
|
|
$this->assertEquals([ |
|
152
|
|
|
'name' => null, |
|
153
|
|
|
'id' => null |
|
154
|
|
|
], $entity->getValue()); |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
$entity = new FilteredEntity(); |
|
158
|
|
|
$entity->setValue([ |
|
159
|
|
|
'name' => 'Antony', |
|
160
|
|
|
'id' => null |
|
161
|
|
|
]); |
|
162
|
|
|
|
|
163
|
|
|
$this->assertEquals([ |
|
164
|
|
|
'id' => 0 |
|
165
|
|
|
], $entity->getValue()); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function testGetters(): void |
|
169
|
|
|
{ |
|
170
|
|
|
$entity = new GetEntity(['id' => []]); |
|
171
|
|
|
$this->assertSame(0, $entity->id); |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
$this->assertEquals([ |
|
174
|
|
|
'id' => 0 |
|
175
|
|
|
], $entity->getFields()); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|