-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathReferenceSqlTest.php
More file actions
702 lines (589 loc) · 29.1 KB
/
ReferenceSqlTest.php
File metadata and controls
702 lines (589 loc) · 29.1 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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<?php
declare(strict_types=1);
namespace Atk4\Data\Tests;
use Atk4\Data\Model;
use Atk4\Data\Schema\TestCase;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
/**
* Tests that condition is applied when traversing hasMany
* also that the original model can be re-loaded with a different
* value without making any condition stick.
*/
class ReferenceSqlTest extends TestCase
{
public function testBasic(): void
{
$this->setDb([
'user' => [
1 => ['id' => 1, 'name' => 'John'],
2 => ['id' => 2, 'name' => 'Peter'],
3 => ['id' => 3, 'name' => 'Joe'],
], 'order' => [
['amount' => '20', 'user_id' => 1],
['amount' => '15', 'user_id' => 2],
['amount' => '5', 'user_id' => 1],
['amount' => '3', 'user_id' => 1],
['amount' => '8', 'user_id' => 3],
],
]);
$u = (new Model($this->db, ['table' => 'user']))->addFields(['name']);
$o = (new Model($this->db, ['table' => 'order']))->addFields(['amount', 'user_id']);
$u->hasMany('Orders', ['model' => $o]);
$oo = $u->load(1)->ref('Orders');
$ooo = $oo->tryLoad(1);
$this->assertEquals(20, $ooo->get('amount'));
$ooo = $oo->tryLoad(2);
$this->assertNull($ooo->get('amount'));
$ooo = $oo->tryLoad(3);
$this->assertEquals(5, $ooo->get('amount'));
$oo = $u->load(2)->ref('Orders');
$ooo = $oo->tryLoad(1);
$this->assertNull($ooo->get('amount'));
$ooo = $oo->tryLoad(2);
$this->assertEquals(15, $ooo->get('amount'));
$ooo = $oo->tryLoad(3);
$this->assertNull($ooo->get('amount'));
$oo = $u->addCondition('id', '>', '1')->ref('Orders');
$this->assertSameSql(
'select "id", "amount", "user_id" from "order" "_O_7442e29d7d53" where "user_id" in (select "id" from "user" where "id" > :a)',
$oo->action('select')->render()[0]
);
}
/**
* Tests to make sure refLink properly generates field links.
*/
public function testLink(): void
{
$u = (new Model($this->db, ['table' => 'user']))->addFields(['name']);
$o = (new Model($this->db, ['table' => 'order']))->addFields(['amount', 'user_id']);
$u->hasMany('Orders', ['model' => $o]);
$this->assertSameSql(
'select "id", "amount", "user_id" from "order" "_O_7442e29d7d53" where "user_id" = "user"."id"',
$u->refLink('Orders')->action('select')->render()[0]
);
}
public function testBasic2(): void
{
$this->setDb([
'user' => [
['name' => 'John', 'currency' => 'EUR'],
['name' => 'Peter', 'currency' => 'GBP'],
['name' => 'Joe', 'currency' => 'EUR'],
], 'currency' => [
['currency' => 'EUR', 'name' => 'Euro'],
['currency' => 'USD', 'name' => 'Dollar'],
['currency' => 'GBP', 'name' => 'Pound'],
],
]);
$u = (new Model($this->db, ['table' => 'user']))->addFields(['name', 'currency']);
$c = (new Model($this->db, ['table' => 'currency']))->addFields(['currency', 'name']);
$u->hasMany('cur', ['model' => $c, 'our_field' => 'currency', 'their_field' => 'currency']);
$cc = $u->load(1)->ref('cur');
$cc = $cc->tryLoadOne();
$this->assertSame('Euro', $cc->get('name'));
$cc = $u->load(2)->ref('cur');
$cc = $cc->tryLoadOne();
$this->assertSame('Pound', $cc->get('name'));
}
public function testLink2(): void
{
$u = (new Model($this->db, ['table' => 'user']))->addFields(['name', 'currency_code']);
$c = (new Model($this->db, ['table' => 'currency']))->addFields(['code', 'name']);
$u->hasMany('cur', ['model' => $c, 'our_field' => 'currency_code', 'their_field' => 'code']);
$this->assertSameSql(
'select "id", "code", "name" from "currency" "_c_b5fddf1ef601" where "code" = "user"."currency_code"',
$u->refLink('cur')->action('select')->render()[0]
);
}
/**
* Tests that condition defined on the parent model is retained when traversing
* through hasMany.
*/
public function testBasicOne(): void
{
$this->setDb([
'user' => [
1 => ['id' => 1, 'name' => 'John'],
2 => ['id' => 2, 'name' => 'Peter'],
3 => ['id' => 3, 'name' => 'Joe'],
], 'order' => [
['amount' => '20', 'user_id' => 1],
['amount' => '15', 'user_id' => 2],
['amount' => '5', 'user_id' => 1],
['amount' => '3', 'user_id' => 1],
['amount' => '8', 'user_id' => 3],
],
]);
$u = (new Model($this->db, ['table' => 'user']))->addFields(['name']);
$o = (new Model($this->db, ['table' => 'order']))->addFields(['amount']);
$o->hasOne('user_id', ['model' => $u]);
$this->assertSame('John', $o->load(1)->ref('user_id')->get('name'));
$this->assertSame('Peter', $o->load(2)->ref('user_id')->get('name'));
$this->assertSame('John', $o->load(3)->ref('user_id')->get('name'));
$this->assertSame('Joe', $o->load(5)->ref('user_id')->get('name'));
$o->addCondition('amount', '>', 6);
$o->addCondition('amount', '<', 9);
$this->assertSameSql(
'select "id", "name" from "user" "_u_e8701ad48ba0" where "id" in (select "user_id" from "order" where ("amount" > :a and "amount" < :b))',
$o->ref('user_id')->action('select')->render()[0]
);
}
/**
* Tests Join::addField's ability to create expressions from foreign fields.
*/
public function testAddOneField(): void
{
$this->setDb([
'user' => [
1 => ['id' => 1, 'name' => 'John', 'date' => '2001-01-02'],
2 => ['id' => 2, 'name' => 'Peter', 'date' => '2004-08-20'],
3 => ['id' => 3, 'name' => 'Joe', 'date' => '2005-08-20'],
], 'order' => [
['amount' => '20', 'user_id' => 1],
['amount' => '15', 'user_id' => 2],
['amount' => '5', 'user_id' => 1],
['amount' => '3', 'user_id' => 1],
['amount' => '8', 'user_id' => 3],
],
]);
$u = (new Model($this->db, ['table' => 'user']))->addFields(['name', 'date' => ['type' => 'date']]);
$o = (new Model($this->db, ['table' => 'order']))->addFields(['amount']);
$o->hasOne('user_id', ['model' => $u])->addFields(['username' => 'name', ['date', 'type' => 'date']]);
$this->assertSame('John', $o->load(1)->get('username'));
$this->assertEquals(new \DateTime('2001-01-02'), $o->load(1)->get('date'));
$this->assertSame('Peter', $o->load(2)->get('username'));
$this->assertSame('John', $o->load(3)->get('username'));
$this->assertSame('Joe', $o->load(5)->get('username'));
// few more tests
$o = (new Model($this->db, ['table' => 'order']))->addFields(['amount']);
$o->hasOne('user_id', ['model' => $u])->addFields(['username' => 'name', 'thedate' => ['date', 'type' => 'date']]);
$this->assertSame('John', $o->load(1)->get('username'));
$this->assertEquals(new \DateTime('2001-01-02'), $o->load(1)->get('thedate'));
$o = (new Model($this->db, ['table' => 'order']))->addFields(['amount']);
$o->hasOne('user_id', ['model' => $u])->addFields(['date'], ['type' => 'date']);
$this->assertEquals(new \DateTime('2001-01-02'), $o->load(1)->get('date'));
}
public function testRelatedExpression(): void
{
$vat = 0.23;
$this->setDb([
'invoice' => [
1 => ['id' => 1, 'ref_no' => 'INV203'],
2 => ['id' => 2, 'ref_no' => 'INV204'],
3 => ['id' => 3, 'ref_no' => 'INV205'],
], 'invoice_line' => [
['total_net' => ($n = 10), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1)), 'invoice_id' => 1],
['total_net' => ($n = 30), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1)), 'invoice_id' => 1],
['total_net' => ($n = 100), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1)), 'invoice_id' => 2],
['total_net' => ($n = 25), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1)), 'invoice_id' => 3],
['total_net' => ($n = 25), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1)), 'invoice_id' => 3],
],
]);
$i = (new Model($this->db, ['table' => 'invoice']))->addFields(['ref_no']);
$l = (new Model($this->db, ['table' => 'invoice_line']))->addFields(['invoice_id', 'total_net', 'total_vat', 'total_gross']);
$i->hasMany('line', ['model' => $l]);
$i->addExpression('total_net', $i->refLink('line')->action('fx', ['sum', 'total_net']));
$this->assertSameSql(
'select "id", "ref_no", (select sum("total_net") from "invoice_line" "_l_6438c669e0d0" where "invoice_id" = "invoice"."id") "total_net" from "invoice"',
$i->action('select')->render()[0]
);
}
public function testAggregateHasMany(): void
{
$vat = 0.23;
$this->setDb([
'invoice' => [
1 => ['id' => 1, 'ref_no' => 'INV203'],
2 => ['id' => 2, 'ref_no' => 'INV204'],
3 => ['id' => 3, 'ref_no' => 'INV205'],
], 'invoice_line' => [
['total_net' => ($n = 10), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1)), 'invoice_id' => 1],
['total_net' => ($n = 30), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1)), 'invoice_id' => 1],
['total_net' => ($n = 100), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1)), 'invoice_id' => 2],
['total_net' => ($n = 25), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1)), 'invoice_id' => 3],
['total_net' => ($n = 25), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1)), 'invoice_id' => 3],
],
]);
$i = (new Model($this->db, ['table' => 'invoice']))->addFields(['ref_no']);
$l = (new Model($this->db, ['table' => 'invoice_line']))->addFields([
'invoice_id',
'total_net' => ['type' => 'atk4_money'],
'total_vat' => ['type' => 'atk4_money'],
'total_gross' => ['type' => 'atk4_money'],
]);
$i->hasMany('line', ['model' => $l])
->addFields([
'total_vat' => ['aggregate' => 'sum', 'type' => 'atk4_money'],
'total_net' => ['aggregate' => 'sum'],
'total_gross' => ['aggregate' => 'sum'],
]);
$i = $i->load('1');
// type was set explicitly
$this->assertSame('atk4_money', $i->getField('total_vat')->type);
// type was not set and is not inherited
$this->assertNull($i->getField('total_net')->type);
$this->assertEquals(40, $i->get('total_net'));
$this->assertEquals(9.2, $i->get('total_vat'));
$this->assertEquals(49.2, $i->get('total_gross'));
$i->ref('line')->import([
['total_net' => ($n = 1), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1))],
['total_net' => ($n = 2), 'total_vat' => ($n * $vat), 'total_gross' => ($n * ($vat + 1))],
]);
$i->reload();
$this->assertEquals($n = 43, $i->get('total_net'));
$this->assertEquals($n * $vat, $i->get('total_vat'));
$this->assertEquals($n * ($vat + 1), $i->get('total_gross'));
$i->ref('line')->import([
['total_net' => null, 'total_vat' => null, 'total_gross' => 1],
]);
$i->reload();
$this->assertEquals($n = 43, $i->get('total_net'));
$this->assertEquals($n * $vat, $i->get('total_vat'));
$this->assertEquals($n * ($vat + 1) + 1, $i->get('total_gross'));
}
public function testOtherAggregates(): void
{
$vat = 0.23;
$this->setDb([
'list' => [
1 => ['id' => 1, 'name' => 'Meat'],
2 => ['id' => 2, 'name' => 'Veg'],
3 => ['id' => 3, 'name' => 'Fruit'],
], 'item' => [
['name' => 'Apple', 'code' => 'ABC', 'list_id' => 3],
['name' => 'Banana', 'code' => 'DEF', 'list_id' => 3],
['name' => 'Pork', 'code' => 'GHI', 'list_id' => 1],
['name' => 'Chicken', 'code' => null, 'list_id' => 1],
['name' => 'Pear', 'code' => null, 'list_id' => 3],
],
]);
$buildLengthSqlFx = function (string $v): string {
return ($this->getDatabasePlatform() instanceof SQLServerPlatform ? 'LEN' : 'LENGTH') . '(' . $v . ')';
};
$buildSumWithIntegerCastSqlFx = function (string $v): string {
if ($this->getDatabasePlatform() instanceof PostgreSQLPlatform
|| $this->getDatabasePlatform() instanceof SQLServerPlatform) {
$v = 'CAST(' . $v . ' AS INT)';
}
return 'SUM(' . $v . ')';
};
$l = (new Model($this->db, ['table' => 'list']))->addFields(['name']);
$i = (new Model($this->db, ['table' => 'item']))->addFields(['list_id', 'name', 'code']);
$l->hasMany('Items', ['model' => $i])
->addFields([
'items_name' => ['aggregate' => 'count', 'field' => 'name'],
'items_code' => ['aggregate' => 'count', 'field' => 'code'], // counts only not-null values
'items_star' => ['aggregate' => 'count'], // no field set, counts all rows with count(*)
'items_c:' => ['concat' => '::', 'field' => 'name'],
'items_c-' => ['aggregate' => $i->dsql()->groupConcat($i->expr('[name]'), '-')],
'len' => ['aggregate' => $i->expr($buildSumWithIntegerCastSqlFx($buildLengthSqlFx('[name]')))], // TODO cast should be implicit when using "aggregate", sandpit http://sqlfiddle.com/#!17/0d2c0/3
'len2' => ['expr' => $buildSumWithIntegerCastSqlFx($buildLengthSqlFx('[name]'))],
'chicken5' => ['expr' => $buildSumWithIntegerCastSqlFx('[]'), 'args' => ['5']],
]);
$ll = $l->load(1);
$this->assertEquals(2, $ll->get('items_name')); // 2 not-null values
$this->assertEquals(1, $ll->get('items_code')); // only 1 not-null value
$this->assertEquals(2, $ll->get('items_star')); // 2 rows in total
$this->assertSame($ll->get('items_c:') === 'Pork::Chicken' ? 'Pork::Chicken' : 'Chicken::Pork', $ll->get('items_c:'));
$this->assertSame($ll->get('items_c-') === 'Pork-Chicken' ? 'Pork-Chicken' : 'Chicken-Pork', $ll->get('items_c-'));
$this->assertEquals(strlen('Chicken') + strlen('Pork'), $ll->get('len'));
$this->assertEquals(strlen('Chicken') + strlen('Pork'), $ll->get('len2'));
$this->assertEquals(10, $ll->get('chicken5'));
$ll = $l->load(2);
$this->assertEquals(0, $ll->get('items_name'));
$this->assertEquals(0, $ll->get('items_code'));
$this->assertEquals(0, $ll->get('items_star'));
$this->assertEquals('', $ll->get('items_c:'));
$this->assertEquals('', $ll->get('items_c-'));
$this->assertNull($ll->get('len'));
$this->assertNull($ll->get('len2'));
$this->assertNull($ll->get('chicken5'));
}
public function testReferenceHasOneTraversing(): void
{
$this->setDb([
'user' => [
['name' => 'Vinny', 'company_id' => 1],
['name' => 'Zoe', 'company_id' => 2],
],
'company' => [
['name' => 'Vinny Company'],
['name' => 'Zoe Company'],
],
'order' => [
['company_id' => 1, 'description' => 'Vinny Company Order 1', 'amount' => 50.0],
['company_id' => 2, 'description' => 'Zoe Company Order', 'amount' => 10.0],
['company_id' => 1, 'description' => 'Vinny Company Order 2', 'amount' => 15.0],
],
]);
$user = (new Model($this->db, ['table' => 'user']))->addFields(['name', 'company_id']);
$company = (new Model($this->db, ['table' => 'company']))->addFields(['name']);
$user->hasOne('Company', ['model' => $company, 'our_field' => 'company_id', 'their_field' => 'id']);
$order = new Model($this->db, ['table' => 'order']);
$order->addField('company_id');
$order->addField('description');
$order->addField('amount', ['default' => 20, 'type' => 'float']);
$company->hasMany('Orders', ['model' => $order]);
$user = $user->load(1);
$firstUserOrders = $user->ref('Company')->ref('Orders');
$firstUserOrders->setOrder('id');
$this->assertEquals([
['id' => '1', 'company_id' => 1, 'description' => 'Vinny Company Order 1', 'amount' => 50.0],
['id' => '3', 'company_id' => 1, 'description' => 'Vinny Company Order 2', 'amount' => 15.0],
], $firstUserOrders->export());
$user->unload();
$this->assertEquals([
['id' => '1', 'company_id' => 1, 'description' => 'Vinny Company Order 1', 'amount' => 50.0],
['id' => '3', 'company_id' => 1, 'description' => 'Vinny Company Order 2', 'amount' => 15.0],
], $firstUserOrders->export());
$this->assertEquals([
['id' => '1', 'company_id' => 1, 'description' => 'Vinny Company Order 1', 'amount' => 50.0],
['id' => '2', 'company_id' => 2, 'description' => 'Zoe Company Order', 'amount' => 10.0],
['id' => '3', 'company_id' => 1, 'description' => 'Vinny Company Order 2', 'amount' => 15.0],
], $user->ref('Company')->ref('Orders')->setOrder('id')->export());
}
public function testReferenceHook(): void
{
$this->setDb([
'user' => [
['name' => 'John', 'contact_id' => 2],
['name' => 'Peter', 'contact_id' => null],
['name' => 'Joe', 'contact_id' => 3],
], 'contact' => [
['address' => 'Sue contact'],
['address' => 'John contact'],
['address' => 'Joe contact'],
],
]);
$u = (new Model($this->db, ['table' => 'user']))->addFields(['name']);
$c = (new Model($this->db, ['table' => 'contact']))->addFields(['address']);
$u->hasOne('contact_id', ['model' => $c])
->addField('address');
$uu = $u->load(1);
$this->assertSame('John contact', $uu->get('address'));
$this->assertSame('John contact', $uu->ref('contact_id')->get('address'));
$uu = $u->load(2);
$this->assertNull($uu->get('address'));
$this->assertNull($uu->get('contact_id'));
$this->assertNull($uu->ref('contact_id')->get('address'));
$uu = $u->load(3);
$this->assertSame('Joe contact', $uu->get('address'));
$this->assertSame('Joe contact', $uu->ref('contact_id')->get('address'));
$uu = $u->load(2);
$uu->ref('contact_id')->save(['address' => 'Peters new contact']);
$this->assertNotNull($uu->get('contact_id'));
$this->assertSame('Peters new contact', $uu->ref('contact_id')->get('address'));
$uu->save()->reload();
$this->assertSame('Peters new contact', $uu->ref('contact_id')->get('address'));
$this->assertSame('Peters new contact', $uu->get('address'));
}
/**
* test case hasOne::our_key == owner::id_field.
*/
public function testIdFieldReferenceOurFieldCase(): void
{
$this->setDb([
'player' => [
['name' => 'John'],
['name' => 'Messi'],
['name' => 'Ronaldo'],
],
'stadium' => [
['name' => 'Sue bernabeu', 'player_id' => 3],
['name' => 'John camp', 'player_id' => 1],
],
]);
$p = (new Model($this->db, ['table' => 'player']))->addFields(['name']);
$s = (new Model($this->db, ['table' => 'stadium']));
$s->addFields(['name']);
$s->hasOne('player_id', ['model' => $p]);
$p->hasOne('Stadium', ['model' => $s, 'our_field' => 'id', 'their_field' => 'player_id']);
$p = $p->load(2);
$p->ref('Stadium')->import([['name' => 'Nou camp nou']]);
$this->assertSame('Nou camp nou', $p->ref('Stadium')->get('name'));
$this->assertSame(2, $p->ref('Stadium')->get('player_id'));
}
public function testModelProperty(): void
{
$user = new Model($this->db, ['table' => 'user']);
$user->hasMany('Orders', ['model' => [Model::class, 'table' => 'order'], 'their_field' => 'id']);
$o = $user->ref('Orders');
$this->assertSame('order', $o->table);
}
/**
* Few tests to test Reference\HasOneSql addTitle() method.
*/
public function testAddTitle(): void
{
$this->setDb([
'user' => [
1 => ['id' => 1, 'name' => 'John'],
], 'order' => [
['amount' => '20', 'user_id' => 1],
['amount' => '15', 'user_id' => 2],
],
]);
$u = (new Model($this->db, ['table' => 'user']))->addFields(['name']);
$o = (new Model($this->db, ['table' => 'order']))->addFields(['amount']);
// by default not set
$o->hasOne('user_id', ['model' => $u]);
$this->assertSame($o->getField('user_id')->isVisible(), true);
$o->getRef('user_id')->addTitle();
$this->assertTrue($o->hasField('user'));
$this->assertSame($o->getField('user')->isVisible(), true);
$this->assertSame($o->getField('user_id')->isVisible(), false);
// if it is set manually then it will not be changed
$o = (new Model($this->db, ['table' => 'order']))->addFields(['amount']);
$o->hasOne('user_id', ['model' => $u]);
$o->getField('user_id')->ui['visible'] = true;
$o->getRef('user_id')->addTitle();
$this->assertSame($o->getField('user_id')->isVisible(), true);
}
/**
* Tests that if we change hasOne->addTitle() field value then it will also update
* link field value when saved.
*/
public function testHasOneTitleSet(): void
{
$dbData = [
'user' => [
1 => ['id' => 1, 'name' => 'John', 'last_name' => 'Doe'],
2 => ['id' => 2, 'name' => 'Peter', 'last_name' => 'Foo'],
3 => ['id' => 3, 'name' => 'Goofy', 'last_name' => 'Goo'],
], 'order' => [
1 => ['id' => 1, 'user_id' => 1],
2 => ['id' => 2, 'user_id' => 2],
3 => ['id' => 3, 'user_id' => 1],
],
];
// restore DB
$this->setDb($dbData);
// with default title_field='name'
$u = (new Model($this->db, ['table' => 'user']))->addFields(['name', 'last_name']);
$o = (new Model($this->db, ['table' => 'order']));
$o->hasOne('user_id', ['model' => $u])->addTitle();
// change order user by changing title_field value
$o = $o->load(1);
$o->set('user', 'Peter');
$this->assertEquals(1, $o->get('user_id'));
$o->save();
$this->assertEquals(2, $o->get('user_id')); // user_id changed to Peters ID
$o->reload();
$this->assertEquals(2, $o->get('user_id')); // and it's really saved like that
// restore DB
$this->setDb($dbData);
// with custom title_field='last_name'
$u = (new Model($this->db, ['table' => 'user', 'title_field' => 'last_name']))->addFields(['name', 'last_name']);
$o = (new Model($this->db, ['table' => 'order']));
$o->hasOne('user_id', ['model' => $u])->addTitle();
// change order user by changing title_field value
$o = $o->load(1);
$o->set('user', 'Foo');
$this->assertEquals(1, $o->get('user_id'));
$o->save();
$this->assertEquals(2, $o->get('user_id')); // user_id changed to Peters ID
$o->reload();
$this->assertEquals(2, $o->get('user_id')); // and it's really saved like that
// restore DB
$this->setDb($dbData);
// with custom title_field='last_name' and custom link name
$u = (new Model($this->db, ['table' => 'user', 'title_field' => 'last_name']))->addFields(['name', 'last_name']);
$o = (new Model($this->db, ['table' => 'order']));
$o->hasOne('my_user', ['model' => $u, 'our_field' => 'user_id'])->addTitle();
// change order user by changing ref field value
$o = $o->load(1);
$o->set('my_user', 'Foo');
$this->assertEquals(1, $o->get('user_id'));
$o->save();
$this->assertEquals(2, $o->get('user_id')); // user_id changed to Peters ID
$o->reload();
$this->assertEquals(2, $o->get('user_id')); // and it's really saved like that
// restore DB
$this->setDb($dbData);
// with custom title_field='last_name' and custom link name
$u = (new Model($this->db, ['table' => 'user', 'title_field' => 'last_name']))->addFields(['name', 'last_name']);
$o = (new Model($this->db, ['table' => 'order']));
$o->hasOne('my_user', ['model' => $u, 'our_field' => 'user_id'])->addTitle();
// change order user by changing ref field value
$o = $o->load(1);
$o->set('my_user', 'Foo'); // user_id=2
$o->set('user_id', 3); // user_id=3 (this will take precedence)
$this->assertEquals(3, $o->get('user_id'));
$o->save();
$this->assertEquals(3, $o->get('user_id')); // user_id changed to Goofy ID
$o->reload();
$this->assertEquals(3, $o->get('user_id')); // and it's really saved like that
}
/**
* Tests that if we change hasOne->addTitle() field value then it will also update
* link field value when saved.
*/
public function testHasOneReferenceCaption(): void
{
// restore DB
$this->setDb([
'user' => [
1 => ['id' => 1, 'name' => 'John', 'last_name' => 'Doe'],
2 => ['id' => 2, 'name' => 'Peter', 'last_name' => 'Foo'],
3 => ['id' => 3, 'name' => 'Goofy', 'last_name' => 'Goo'],
],
'order' => [
1 => ['id' => 1, 'user_id' => 1],
2 => ['id' => 2, 'user_id' => 2],
3 => ['id' => 3, 'user_id' => 1],
],
]);
$u = (new Model($this->db, ['table' => 'user', 'title_field' => 'last_name']))->addFields(['name', 'last_name']);
// Test : Now the caption is null and is generated from field name
$this->assertSame('Last Name', $u->getField('last_name')->getCaption());
$u->getField('last_name')->caption = 'Surname';
// Test : Now the caption is not null and the value is returned
$this->assertSame('Surname', $u->getField('last_name')->getCaption());
$o = (new Model($this->db, ['table' => 'order']));
$order_user_ref = $o->hasOne('my_user', ['model' => $u, 'our_field' => 'user_id']);
$order_user_ref->addField('user_last_name', 'last_name');
$referenced_caption = $o->getField('user_last_name')->getCaption();
// Test : $field->caption for the field 'last_name' is defined in referenced model (User)
// When Order add field from Referenced model User
// caption will be passed to Order field user_last_name
$this->assertSame('Surname', $referenced_caption);
}
/**
* HasOne: Test if field type is taken from referenced Model if not set in HasOne::addField().
* Test if field type is set in HasOne::addField(), it has priority.
*/
public function testHasOneReferenceType(): void
{
// restore DB
$this->setDb(
[
'user' => [
1 => [
'id' => 1,
'name' => 'John',
'last_name' => 'Doe',
'some_number' => 3,
'some_other_number' => 4,
],
],
'order' => [
1 => ['id' => 1, 'user_id' => 1],
],
]
);
$user = (new Model($this->db, ['table' => 'user']))->addFields(
['name', 'last_name', 'some_number', 'some_other_number']
);
$user->getField('some_number')->type = 'integer';
$user->getField('some_other_number')->type = 'integer';
$order = (new Model($this->db, ['table' => 'order']));
$order_UserRef = $order->hasOne('my_user', ['model' => $user, 'our_field' => 'user_id']);
// no type set in defaults, should pull type integer from user model
$order_UserRef->addField('some_number');
$this->assertSame('integer', $order->getField('some_number')->type);
// set type in defaults, this should have higher priority than type set in Model
$order_UserRef->addField('some_other_number', null, ['type' => 'string']);
$this->assertSame('string', $order->getField('some_other_number')->type);
}
}