-
Notifications
You must be signed in to change notification settings - Fork 46
/
LookupSqlTest.php
401 lines (350 loc) · 13.5 KB
/
LookupSqlTest.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
declare(strict_types=1);
namespace Atk4\Data\Tests;
use Atk4\Data\Model;
use Atk4\Data\Schema\TestCase;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
/**
* You can lookup country by name or by code. We will also try looking up country by
* multiple fields (e.g. code and is_eu) to see if those work are used as AND conditions. For instance
* is_eu = true, code = 'US', should not be able to lookup the country.
*
* Users is a reference. You can specify it as an array containing import data for and that will be inserted
* recursively.
*
* We also introduced user_names field, which will concatenate all user names for said country. It can also be
* used when importing, simply provide a comma-separated string of user names and they will be CREATED for you.
*/
class LCountry extends Model
{
public $table = 'country';
protected function init(): void
{
parent::init();
$this->addField('name');
$this->addField('code');
$this->addField('is_eu', ['type' => 'boolean', 'default' => false]);
$this->hasMany('Users', ['model' => [LUser::class]])
->addField('user_names', ['field' => 'name', 'concat' => ', ']);
}
}
/**
* User has one country and may have friends. Friend is a many-to-many relationship between users.
*
* When importing users, you should be able to specify country using 'country_id' or using some of the
* lookup fields: 'country', 'country_code' or 'is_eu'.
*
* If ID is not specified (unlike specifying null!) we will rely on the lookup fields to try and find
* a country. If multiple lookup fields are set, we should find a country that matches them all. If country
* cannot be found then null should be set for country_id.
*
* Friends is many-to-many relationship. We have 'friend_names' field which may be similar to the one we had
* for a country. However specifying friend_names as a comma-separated value will not create any friends.
* Instead it will look up existing records and will create "Friend" record for all of them.
*
* Like before Friends can also be specified as an array.
*/
class LUser extends Model
{
public $table = 'user';
protected function init(): void
{
parent::init();
$this->addField('name');
$this->addField('is_vip', ['type' => 'boolean', 'default' => false]);
$this->hasOne('country_id', ['model' => [LCountry::class]])
->addFields(['country_code' => 'code', 'is_eu'])
->addTitle();
$this->hasMany('Friends', ['model' => [LFriend::class]])
->addField('friend_names', ['field' => 'friend_name', 'concat' => '; ']);
}
}
/**
* Friend is a many-to-many binder that connects User with another User.
*
* In our case, however, we want each friendship to be reciprocal. If John
* is a friend of Sue, then Sue must be a friend of John.
*
* To implement that, we include insert / delete handlers which would create
* a reverse record.
*
* The challenge here is to make sure that those handlers are executed automatically
* while importing User and Friends.
*/
class LFriend extends Model
{
public $table = 'friend';
public ?string $titleField = 'friend_name';
protected bool $skipReverse = false;
protected function init(): void
{
parent::init();
$this->hasOne('user_id', ['model' => [LUser::class]])
->addField('my_name', 'name');
$this->hasOne('friend_id', ['model' => [LUser::class]])
->addField('friend_name', 'name');
// add/remove reverse friendships
$this->onHookShort(self::HOOK_AFTER_INSERT, function () {
if ($this->skipReverse) {
return;
}
$c = $this->getModel()->createEntity();
$c->skipReverse = true;
// $c->insert([
// 'user_id' => $this->get('friend_id'),
// 'friend_id' => $this->get('user_id'),
// ]);
});
$this->onHookShort(Model::HOOK_BEFORE_DELETE, function () {
if ($this->skipReverse) {
return;
}
// $c = $this->getModel()->loadBy([
// 'user_id' => $this->get('friend_id'),
// 'friend_id' => $this->get('user_id'),
// ]);
// $c->skipReverse = true;
// $c->delete();
});
}
}
class LookupSqlTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$country = new LCountry($this->db);
$this->createMigrator($country)->create();
$user = new LUser($this->db);
$this->createMigrator($user)->create();
$friend = new LFriend($this->db);
$this->createMigrator($friend)->create();
$this->createMigrator()->createForeignKey($user->getReference('country_id'));
$this->createMigrator()->createForeignKey($friend->getReference('user_id'));
$this->createMigrator()->createForeignKey($friend->getReference('friend_id'));
}
public function testImportCountriesBasic(): void
{
$c = new LCountry($this->db);
$results = [];
// should be OK, will set country name, rest of fields will be null
$c->createEntity()->saveAndUnload(['name' => 'Canada']);
// adds another country, but with more fields
$c->createEntity()->saveAndUnload(['name' => 'Latvia', 'code' => 'LV', 'is_eu' => true]);
// setting field prior will affect save()
$cc = $c->createEntity();
$cc->set('is_eu', true);
$cc->save(['name' => 'Estonia', 'code' => 'ES']);
// is_eu will NOT BLEED into this record, because insert() does not make use of current model values.
$c->insert(['name' => 'Korea', 'code' => 'KR']);
// is_eu will NOT BLEED into Japan or Russia, because import() treats all records individually
$c->import([
['name' => 'Japan', 'code' => 'JP'],
['name' => 'Lithuania', 'code' => 'LT', 'is_eu' => true],
['name' => 'Russia', 'code' => 'RU'],
]);
static::assertSameExportUnordered([
'country' => [
1 => [
'id' => 1,
'name' => 'Canada',
'code' => null,
'is_eu' => '0',
],
2 => [
'id' => 2,
'name' => 'Latvia',
'code' => 'LV',
'is_eu' => '1',
],
3 => [
'id' => 3,
'name' => 'Estonia',
'code' => 'ES',
'is_eu' => '1',
],
4 => [
'id' => 4,
'name' => 'Korea',
'code' => 'KR',
'is_eu' => '0',
],
5 => [
'id' => 5,
'name' => 'Japan',
'code' => 'JP',
'is_eu' => '0',
],
6 => [
'id' => 6,
'name' => 'Lithuania',
'code' => 'LT',
'is_eu' => '1',
],
7 => [
'id' => 7,
'name' => 'Russia',
'code' => 'RU',
'is_eu' => '0',
],
],
], $this->getDb(['country']));
}
public function testImportInternationalUsers(): void
{
$c = new LCountry($this->db);
$c->insert(['name' => 'Canada', 'Users' => [['name' => 'Alain'], ['name' => 'Duncan', 'is_vip' => true]]]);
$c->insert(['name' => 'Latvia', 'Users' => [['name' => 'imants'], ['name' => 'juris']]]);
static::assertSameExportUnordered([
'country' => [
1 => [
'id' => 1,
'name' => 'Canada',
'code' => null,
'is_eu' => '0',
],
2 => [
'id' => 2,
'name' => 'Latvia',
'code' => null,
'is_eu' => '0',
],
],
'user' => [
1 => [
'id' => 1,
'name' => 'Alain',
'is_vip' => '0',
'country_id' => 1,
],
2 => [
'id' => 2,
'name' => 'Duncan',
'is_vip' => '1',
'country_id' => 1,
],
3 => [
'id' => 3,
'name' => 'imants',
'is_vip' => '0',
'country_id' => 2,
],
4 => [
'id' => 4,
'name' => 'juris',
'is_vip' => '0',
'country_id' => 2,
],
],
], $this->getDb(['country', 'user']));
}
public function testImportByLookup(): void
{
$c = new LCountry($this->db);
$c->import([
['name' => 'Canada', 'code' => 'CA'],
['name' => 'Latvia', 'code' => 'LV', 'is_eu' => true],
['name' => 'Japan', 'code' => 'JP'],
['name' => 'Lithuania', 'code' => 'LT', 'is_eu' => true],
['name' => 'Russia', 'code' => 'RU'],
]);
$u = new LUser($this->db);
static::assertTrue($u->getField('country_id')->isEditable());
static::assertFalse($u->getField('country')->isEditable());
static::assertFalse($u->getField('country_code')->isEditable());
$u->import([
['name' => 'Alain', 'country_code' => 'CA'],
['name' => 'Imants', 'country_code' => 'LV'],
// ['name' => 'Romans', 'country_code' => 'UK'], // country code does not exist
]);
static::assertSameExportUnordered([
'country' => [
1 => [
'id' => 1,
'name' => 'Canada',
'code' => 'CA',
'is_eu' => '0',
],
2 => [
'id' => 2,
'name' => 'Latvia',
'code' => 'LV',
'is_eu' => '1',
],
3 => [
'id' => 3,
'name' => 'Japan',
'code' => 'JP',
'is_eu' => '0',
],
4 => [
'id' => 4,
'name' => 'Lithuania',
'code' => 'LT',
'is_eu' => '1',
],
5 => [
'id' => 5,
'name' => 'Russia',
'code' => 'RU',
'is_eu' => '0',
],
],
'user' => [
1 => [
'id' => 1,
'name' => 'Alain',
'is_vip' => '0',
'country_id' => 1,
],
2 => [
'id' => 2,
'name' => 'Imants',
'is_vip' => '0',
'country_id' => 2,
],
],
], $this->getDb(['country', 'user']));
}
public function testImportInternationalFriends(): void
{
$c = new LCountry($this->db);
$c->insert(['name' => 'Canada', 'Users' => [['name' => 'Alain'], ['name' => 'Duncan', 'is_vip' => true]]]);
$c->insert(['name' => 'Latvia', 'Users' => [['name' => 'imants'], ['name' => 'juris']]]);
static::assertSame('imants, juris', $c->loadBy('name', 'Latvia')->get('user_names'));
if ($this->getDatabasePlatform() instanceof SQLServerPlatform) {
static::markTestIncomplete('TODO MSSQL: Cannot perform an aggregate function on an expression containing an aggregate or a subquery');
}
$user1 = $c->ref('Users')->loadBy('name', 'Duncan');
$user2 = $c->loadBy('name', 'Latvia')->ref('Users')->loadBy('name', 'imants');
$user3 = $user2->getModel()->loadBy('name', 'juris');
$user2->ref('Friends')->import([
['friend_id' => $user1->getId()],
['friend_id' => $user3->getId()],
]);
static::assertNull($user1->get('friend_names'));
static::assertNull($user2->get('friend_names'));
static::assertNull($user3->get('friend_names'));
$user1->reload();
$user2->reload();
$user3->reload();
static::assertNull($user1->get('friend_names'));
static::assertSame('Duncan; juris', $user2->get('friend_names'));
static::assertNull($user3->get('friend_names'));
/* TODO - that's left for hasMTM implementation..., to be coming later
// Specifying hasMany here will perform input
$c->insert(['Canada', 'Users' => ['Alain', ['Duncan', 'is_vip' => true]]]);
// Inserting Users into Latvia can also specify Friends. In this case Friend name will be looked up
$c->insert(['Latvia', 'Users' => ['Imants', ['Juris', 'friend_names' => 'Alain, Imants' TODO convert to array, we never split implicitly ]]]);
// Inserting This time explicitly specify friend attributes
$c->insert(['UK', 'Users' => [
['Romans', 'Friends' => [
['friend_id' => 1],
['friend_name' => 'Juris'],
'Alain',
]],
]]);
// BTW - Alain should have 3 friends here
*/
}
}