forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityTest.php
More file actions
173 lines (127 loc) · 3.42 KB
/
Copy pathEntityTest.php
File metadata and controls
173 lines (127 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php namespace CodeIgniter;
use CodeIgniter\Entity;
class EntityTest extends \CIUnitTestCase
{
public function testSimpleSetAndGet()
{
$entity = $this->getEntity();
$entity->foo = 'to wong';
$this->assertEquals('to wong', $entity->foo);
}
public function testGetterSetters()
{
$entity = $this->getEntity();
$entity->bar = 'thanks';
$this->assertEquals('bar:thanks:bar', $entity->bar);
}
public function testUnsetResetsToDefaultValue()
{
$entity = $this->getEntity();
$this->assertEquals('sumfin', $entity->default);
$entity->default = 'else';
$this->assertEquals('else', $entity->default);
unset($entity->default);
$this->assertEquals('sumfin', $entity->default);
}
public function testIssetWorksLikeTraditionalIsset()
{
$entity = $this->getEntity();
$this->assertTrue(isset($entity->default));
$this->assertFalse(isset($entity->foo));
}
public function testFill()
{
$entity = $this->getEntity();
$entity->fill([
'foo' => 123,
'bar' => 234,
'baz' => 4556
]);
$this->assertEquals(123, $entity->foo);
$this->assertEquals('bar:234:bar', $entity->bar);
$this->assertTrue(! isset($entity->baz));
}
public function testDataMappingConvertsOriginalName()
{
$entity = $this->getMappedEntity();
$entity->bar = 'made it';
// Check mapped field
$this->assertEquals('made it', $entity->foo);
// Should also get from original name
// since Model's would be looking for the original name
$this->assertEquals('made it', $entity->bar);
// But it shouldn't actually set a class property for the original name...
$this->expectException(\ReflectionException::class);
$this->getPrivateProperty($entity, 'bar');
}
public function testDataMappingWorksWithCustomSettersAndGetters()
{
$entity = $this->getMappedEntity();
// Will map to "simple"
$entity->orig = 'first';
$this->assertEquals('oo:first:oo', $entity->simple);
$entity->simple = 'second';
$this->assertEquals('oo:second:oo', $entity->simple);
}
public function testIssetWorksWithMapping()
{
$entity = $this->getMappedEntity();
// maps to 'foo'
$entity->bar = 'here';
$this->assertTrue(isset($entity->foo));
$this->assertFalse(isset($entity->bar));
}
public function testUnsetWorksWithMapping()
{
$entity = $this->getMappedEntity();
// maps to 'foo'
$entity->bar = 'here';
// doesn't work on original name
unset($entity->bar);
$this->assertEquals('here', $entity->bar);
$this->assertEquals('here', $entity->foo);
// does work on mapped field
unset($entity->foo);
$this->assertNull($entity->foo);
$this->assertNull($entity->bar);
}
protected function getEntity()
{
return new class extends Entity
{
protected $foo;
protected $bar;
protected $default = 'sumfin';
public function setBar($value)
{
$this->bar = "bar:{$value}";
return $this;
}
public function getBar()
{
return "{$this->bar}:bar";
}
};
}
protected function getMappedEntity()
{
return new class extends Entity
{
protected $foo;
protected $simple;
// 'bar' is db column, 'foo' is internal representation
protected $datamap = [
'bar' => 'foo',
'orig' => 'simple'
];
protected function setSimple(string $val)
{
$this->simple = 'oo:'.$val;
}
protected function getSimple()
{
return $this->simple.':oo';
}
};
}
}