|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fwk\Db; |
|
4
|
|
|
|
|
5
|
|
|
class User7 extends \stdClass |
|
6
|
|
|
{ |
|
7
|
|
|
public $emails; |
|
8
|
|
|
|
|
9
|
|
|
public $metas; |
|
10
|
|
|
|
|
11
|
|
|
public $phone; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
|
|
$this->emails = new Relations\Many2Many( |
|
16
|
|
|
'id', |
|
17
|
|
|
'user_id', |
|
18
|
|
|
'fwkdb_test_emails', |
|
19
|
|
|
'fwkdb_test_users_emails', |
|
20
|
|
|
'id', |
|
21
|
|
|
'email_id', |
|
22
|
|
|
'Fwk\Db\Email7' |
|
23
|
|
|
); |
|
24
|
|
|
$this->emails->setFetchMode(RelationInterface::FETCH_EAGER); |
|
25
|
|
|
$this->emails->setOrderBy('email'); |
|
26
|
|
|
|
|
27
|
|
|
$this->metas = new Relations\One2Many( |
|
28
|
|
|
'id', |
|
29
|
|
|
'user_id', |
|
30
|
|
|
'fwkdb_test_users_metas' |
|
31
|
|
|
); |
|
32
|
|
|
$this->metas->setReference('name'); |
|
33
|
|
|
$this->metas->setFetchMode(RelationInterface::FETCH_EAGER); |
|
34
|
|
|
|
|
35
|
|
|
$this->phone = new Relations\One2One('phone_id', 'id', 'fwkdb_test_phones'); |
|
36
|
|
|
$this->phone->setFetchMode(RelationInterface::FETCH_EAGER); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
class Email7 extends \stdClass |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Test class for Accessor. |
|
46
|
|
|
* Generated by PHPUnit on 2012-05-27 at 17:46:42. |
|
47
|
|
|
*/ |
|
48
|
|
|
class BasicEntityEagerRelationsTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
/** |
|
51
|
|
|
* |
|
52
|
|
|
* @var \Fwk\Db\Connection |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $connection; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
58
|
|
|
* This method is called before a test is executed. |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function setUp() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->connection = new Connection(array( |
|
63
|
|
|
'memory' => true, |
|
64
|
|
|
'driver' => 'pdo_sqlite' |
|
65
|
|
|
)); |
|
66
|
|
|
|
|
67
|
|
|
\FwkDbTestUtil::createTestDb($this->connection); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function tearDown() |
|
71
|
|
|
{ |
|
72
|
|
|
\FwkDbTestUtil::dropTestDb($this->connection); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testM2MSave() |
|
76
|
|
|
{ |
|
77
|
|
|
$u = new User7; |
|
78
|
|
|
$u->username = "joebar"; |
|
79
|
|
|
|
|
80
|
|
|
$e = new Email7(); |
|
81
|
|
|
$e->email = "[email protected]"; |
|
82
|
|
|
$e->verified = 1; |
|
83
|
|
|
|
|
84
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
|
85
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_emails')->finder()->all())); |
|
86
|
|
|
|
|
87
|
|
|
$u->emails[] = $e; |
|
88
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
|
91
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_emails')->finder()->all())); |
|
92
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users_emails')->finder()->all())); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testM2MRemoveChild() |
|
96
|
|
|
{ |
|
97
|
|
|
$u = new User7; |
|
98
|
|
|
$u->username = "joebar"; |
|
99
|
|
|
|
|
100
|
|
|
$e = new Email7(); |
|
101
|
|
|
$e->email = "[email protected]"; |
|
102
|
|
|
$e->verified = 1; |
|
103
|
|
|
|
|
104
|
|
|
$u->emails[] = $e; |
|
105
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
|
106
|
|
|
|
|
107
|
|
|
$user = $this->connection->table('fwkdb_test_users')->finder('Fwk\Db\User')->one(1); |
|
108
|
|
|
$this->assertNotNull($user); |
|
109
|
|
|
$this->assertInstanceOf('\Fwk\Db\Email', $user->emails[0]); |
|
110
|
|
|
|
|
111
|
|
|
unset($user->emails[0]); |
|
112
|
|
|
$this->connection->table('fwkdb_test_users')->save($user); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertNull($user->emails[0]); |
|
115
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_emails')->finder()->all())); |
|
116
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users_emails')->finder()->all())); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testO2MSave() |
|
120
|
|
|
{ |
|
121
|
|
|
$u = new User7; |
|
122
|
|
|
$u->username = "joebar"; |
|
123
|
|
|
|
|
124
|
|
|
$m = new \stdClass; |
|
125
|
|
|
$m->name = "param"; |
|
126
|
|
|
$m->value = "value"; |
|
127
|
|
|
|
|
128
|
|
|
$u->metas[] = $m; |
|
129
|
|
|
|
|
130
|
|
|
$this->assertEquals($m, $u->metas['param']); // test the reference |
|
131
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
|
132
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users_metas')->finder()->all())); |
|
133
|
|
|
|
|
134
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
|
135
|
|
|
|
|
136
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
|
137
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users_metas')->finder()->all())); |
|
138
|
|
|
$this->assertEquals($u->id, $u->metas['param']->user_id); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testO2MRemoveChild() |
|
142
|
|
|
{ |
|
143
|
|
|
$u = new User7; |
|
144
|
|
|
$u->username = "joebar"; |
|
145
|
|
|
|
|
146
|
|
|
$m = new \stdClass; |
|
147
|
|
|
$m->name = "param"; |
|
148
|
|
|
$m->value = "value"; |
|
149
|
|
|
|
|
150
|
|
|
$u->metas[] = $m; |
|
151
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
|
152
|
|
|
|
|
153
|
|
|
$user = $this->connection->table('fwkdb_test_users')->finder('Fwk\Db\User')->one(1); |
|
154
|
|
|
$this->assertNotNull($user); |
|
155
|
|
|
$this->assertInstanceOf('\stdClass', $user->metas['param']); |
|
156
|
|
|
|
|
157
|
|
|
unset($user->metas['param']); |
|
158
|
|
|
$this->connection->table('fwkdb_test_users')->save($user); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
|
161
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users_metas')->finder()->all())); |
|
162
|
|
|
$this->assertNull($user->metas['param']); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function testO2OSave() |
|
166
|
|
|
{ |
|
167
|
|
|
$u = new User7; |
|
168
|
|
|
$u->username = "joebar"; |
|
169
|
|
|
|
|
170
|
|
|
$m = new \stdClass; |
|
171
|
|
|
$m->number = "0467010506"; |
|
172
|
|
|
|
|
173
|
|
|
$u->phone->set($m); |
|
174
|
|
|
|
|
175
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
|
176
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_phones')->finder()->all())); |
|
177
|
|
|
|
|
178
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
|
179
|
|
|
|
|
180
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
|
181
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_phones')->finder()->all())); |
|
182
|
|
|
$this->assertEquals($m->id, $u->phone_id); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function testO2ORemoveChild() |
|
186
|
|
|
{ |
|
187
|
|
|
$u = new User7; |
|
188
|
|
|
$u->username = "joebar"; |
|
189
|
|
|
|
|
190
|
|
|
$m = new \stdClass; |
|
191
|
|
|
$m->number = "0467010506"; |
|
192
|
|
|
|
|
193
|
|
|
$u->phone->set($m); |
|
194
|
|
|
|
|
195
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
|
196
|
|
|
|
|
197
|
|
|
$user = $this->connection->table('fwkdb_test_users')->finder('Fwk\Db\User')->one(1); |
|
198
|
|
|
$this->assertNotNull($user); |
|
199
|
|
|
$this->assertInstanceOf('\stdClass', $user->phone->get()); |
|
200
|
|
|
|
|
201
|
|
|
$user->phone->set(); |
|
202
|
|
|
$this->connection->table('fwkdb_test_users')->save($user); |
|
203
|
|
|
|
|
204
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
|
205
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_phones')->finder()->all())); |
|
206
|
|
|
$this->assertNull($user->phone_id); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
protected function insertBulk(Connection $db) |
|
210
|
|
|
{ |
|
211
|
|
|
$users = array(); |
|
212
|
|
|
for($x = 0; $x < 10; $x++) { |
|
213
|
|
|
$u = new User7; |
|
214
|
|
|
$u->username = "test". $x; |
|
215
|
|
|
|
|
216
|
|
|
$m = new \stdClass; |
|
217
|
|
|
$m->number = "046701050". $x; |
|
218
|
|
|
$u->phone->set($m); |
|
219
|
|
|
|
|
220
|
|
|
for ($y = 0; $y < 5; $y++) { |
|
221
|
|
|
$me = new \stdClass; |
|
222
|
|
|
$me->name = "param". $y; |
|
223
|
|
|
$me->value = "value". $x; |
|
224
|
|
|
$u->metas[] = $me; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$users[] = $u; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
$db->table('fwkdb_test_users')->save($users); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @medium |
|
235
|
|
|
*/ |
|
236
|
|
|
public function testSelectEager() |
|
237
|
|
|
{ |
|
238
|
|
|
$this->insertBulk($this->connection); |
|
239
|
|
|
$this->assertEquals(10, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
|
240
|
|
|
|
|
241
|
|
|
$user1 = $this->connection->table('fwkdb_test_users')->finder('Fwk\Db\User7')->one(1); |
|
242
|
|
|
$this->assertTrue(is_object($user1)); |
|
243
|
|
|
$this->assertEquals(5, count($user1->metas)); |
|
244
|
|
|
$this->assertEquals(1, count($user1->phone)); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.