1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fwk\Db; |
4
|
|
|
|
5
|
|
|
class User 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\Email' |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
$this->metas = new Relations\One2Many( |
26
|
|
|
'id', |
27
|
|
|
'user_id', |
28
|
|
|
'fwkdb_test_users_metas' |
29
|
|
|
); |
30
|
|
|
$this->metas->setReference('name'); |
31
|
|
|
|
32
|
|
|
$this->phone = new Relations\One2One('phone_id', 'id', 'fwkdb_test_phones'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
class Email extends \stdClass |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Test class for Accessor. |
42
|
|
|
* Generated by PHPUnit on 2012-05-27 at 17:46:42. |
43
|
|
|
*/ |
44
|
|
|
class BasicEntityRelationsTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* @var \Fwk\Db\Connection |
49
|
|
|
*/ |
50
|
|
|
protected $connection; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Sets up the fixture, for example, opens a network connection. |
54
|
|
|
* This method is called before a test is executed. |
55
|
|
|
*/ |
56
|
|
|
protected function setUp() |
57
|
|
|
{ |
58
|
|
|
$this->connection = new Connection(array( |
59
|
|
|
'memory' => true, |
60
|
|
|
'driver' => 'pdo_sqlite' |
61
|
|
|
)); |
62
|
|
|
|
63
|
|
|
\FwkDbTestUtil::createTestDb($this->connection); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function tearDown() |
67
|
|
|
{ |
68
|
|
|
\FwkDbTestUtil::dropTestDb($this->connection); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testM2MSave() |
72
|
|
|
{ |
73
|
|
|
$u = new User; |
74
|
|
|
$u->username = "joebar"; |
75
|
|
|
|
76
|
|
|
$e = new Email(); |
77
|
|
|
$e->email = "[email protected]"; |
78
|
|
|
$e->verified = 1; |
79
|
|
|
|
80
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
81
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_emails')->finder()->all())); |
82
|
|
|
|
83
|
|
|
$u->emails[] = $e; |
84
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
85
|
|
|
|
86
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
87
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_emails')->finder()->all())); |
88
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users_emails')->finder()->all())); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testM2MRemoveChild() |
92
|
|
|
{ |
93
|
|
|
$u = new User; |
94
|
|
|
$u->username = "joebar"; |
95
|
|
|
|
96
|
|
|
$e = new Email(); |
97
|
|
|
$e->email = "[email protected]"; |
98
|
|
|
$e->verified = 1; |
99
|
|
|
|
100
|
|
|
$u->emails[] = $e; |
101
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
102
|
|
|
|
103
|
|
|
$user = $this->connection->table('fwkdb_test_users')->finder('Fwk\Db\User')->one(1); |
104
|
|
|
$this->assertNotNull($user); |
105
|
|
|
$this->assertInstanceOf('\Fwk\Db\Email', $user->emails[0]); |
106
|
|
|
|
107
|
|
|
unset($user->emails[0]); |
108
|
|
|
$this->connection->table('fwkdb_test_users')->save($user); |
109
|
|
|
|
110
|
|
|
$this->assertNull($user->emails[0]); |
111
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_emails')->finder()->all())); |
112
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users_emails')->finder()->all())); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testO2MSave() |
116
|
|
|
{ |
117
|
|
|
$u = new User; |
118
|
|
|
$u->username = "joebar"; |
119
|
|
|
|
120
|
|
|
$m = new \stdClass; |
121
|
|
|
$m->name = "param"; |
122
|
|
|
$m->value = "value"; |
123
|
|
|
|
124
|
|
|
$u->metas[] = $m; |
125
|
|
|
|
126
|
|
|
$this->assertEquals($m, $u->metas['param']); // test the reference |
127
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
128
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users_metas')->finder()->all())); |
129
|
|
|
|
130
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
131
|
|
|
|
132
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
133
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users_metas')->finder()->all())); |
134
|
|
|
$this->assertEquals($u->id, $u->metas['param']->user_id); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testO2MRemoveChild() |
138
|
|
|
{ |
139
|
|
|
$u = new User; |
140
|
|
|
$u->username = "joebar"; |
141
|
|
|
|
142
|
|
|
$m = new \stdClass; |
143
|
|
|
$m->name = "param"; |
144
|
|
|
$m->value = "value"; |
145
|
|
|
|
146
|
|
|
$u->metas[] = $m; |
147
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
148
|
|
|
|
149
|
|
|
$user = $this->connection->table('fwkdb_test_users')->finder('Fwk\Db\User')->one(1); |
150
|
|
|
$this->assertNotNull($user); |
151
|
|
|
$this->assertInstanceOf('\stdClass', $user->metas['param']); |
152
|
|
|
|
153
|
|
|
unset($user->metas['param']); |
154
|
|
|
$this->connection->table('fwkdb_test_users')->save($user); |
155
|
|
|
|
156
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
157
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users_metas')->finder()->all())); |
158
|
|
|
$this->assertNull($user->metas['param']); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testO2OSave() |
162
|
|
|
{ |
163
|
|
|
$u = new User; |
164
|
|
|
$u->username = "joebar"; |
165
|
|
|
|
166
|
|
|
$m = new \stdClass; |
167
|
|
|
$m->number = "0467010506"; |
168
|
|
|
|
169
|
|
|
$u->phone->set($m); |
170
|
|
|
|
171
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
172
|
|
|
$this->assertEquals(0, count($this->connection->table('fwkdb_test_phones')->finder()->all())); |
173
|
|
|
|
174
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
175
|
|
|
|
176
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
177
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_phones')->finder()->all())); |
178
|
|
|
$this->assertEquals($m->id, $u->phone_id); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testO2ORemoveChild() |
182
|
|
|
{ |
183
|
|
|
$u = new User; |
184
|
|
|
$u->username = "joebar"; |
185
|
|
|
|
186
|
|
|
$m = new \stdClass; |
187
|
|
|
$m->number = "0467010506"; |
188
|
|
|
|
189
|
|
|
$u->phone->set($m); |
190
|
|
|
|
191
|
|
|
$this->connection->table('fwkdb_test_users')->save($u); |
192
|
|
|
|
193
|
|
|
$user = $this->connection->table('fwkdb_test_users')->finder('Fwk\Db\User')->one(1); |
194
|
|
|
$this->assertNotNull($user); |
195
|
|
|
$this->assertInstanceOf('\stdClass', $user->phone->get()); |
196
|
|
|
|
197
|
|
|
$user->phone->set(); |
198
|
|
|
$this->connection->table('fwkdb_test_users')->save($user); |
199
|
|
|
|
200
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all())); |
201
|
|
|
$this->assertEquals(1, count($this->connection->table('fwkdb_test_phones')->finder()->all())); |
202
|
|
|
$this->assertNull($user->phone_id); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
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.