-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityManagerTest.php
More file actions
1358 lines (1171 loc) · 59.1 KB
/
Copy pathEntityManagerTest.php
File metadata and controls
1358 lines (1171 loc) · 59.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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Tests\Icinga\Module\Notifications\Common;
use DateTime;
use DateTimeInterface;
use Exception;
use Icinga\Module\Notifications\Common\Collection;
use Icinga\Module\Notifications\Common\EntityManager;
use Icinga\Module\Notifications\Common\Model;
use LogicException;
use PDO;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\Badge;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\Charm;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\Flag;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\Gadget;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\GadgetBadge;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\GadgetTag;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\Pairing;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\RecordingConnection;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\Stamped;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\StampedNote;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\Sticker;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\Tag;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\TickingEntityManager;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\Trinket;
use Tests\Icinga\Module\Notifications\Lib\EntityManager\Workshop;
use ipl\Stdlib\Filter;
/**
* End-to-end tests for the {@see EntityManager} write path.
*
* Each test runs against a fresh in-memory sqlite schema (see {@see self::setUp()}) through a
* {@see RecordingConnection}, so an assertion can inspect both the resulting rows and the exact set of
* writes issued. The EntityManager is a {@see TickingEntityManager}, whose clock returns 1s, 2s, 3s, …
* on successive `changed_at` stamps, so those stamps can be asserted by value.
*
* The fixtures model the relation shapes the real schema uses: HasMany ({@see Workshop}->gadget),
* BelongsTo ({@see Gadget}->workshop), a plain many-to-many ({@see Gadget}->sticker) and a soft-delete
* many-to-many through a junction model ({@see Gadget}->tag via {@see GadgetTag}), plus models exercising
* compound keys ({@see Pairing}), binary keys ({@see Trinket}/{@see Charm}) and column behaviors
* ({@see Flag}, {@see Stamped}).
*/
class EntityManagerTest extends TestCase
{
/** @var RecordingConnection The sqlite-backed connection recording every write of the test */
protected $db;
/**
* Create the in-memory schema and reset the deterministic clock before each test
*/
protected function setUp(): void
{
$this->db = $this->connect();
TickingEntityManager::$tick = 0;
}
/**
* Open a fresh in-memory sqlite connection and create the fixture schema on it
*
* @param array<int, mixed> $pdoOptions Extra PDO options, e.g. to reproduce driver quirks
*
* @return RecordingConnection
*/
protected function connect(array $pdoOptions = []): RecordingConnection
{
$config = ['db' => 'sqlite', 'dbname' => ':memory:'];
if ($pdoOptions) {
$config['options'] = $pdoOptions;
}
$db = new RecordingConnection($config);
$db->exec(
'CREATE TABLE workshop (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NOT NULL);'
. 'CREATE TABLE gadget (id INTEGER PRIMARY KEY AUTOINCREMENT, workshop_id INTEGER, name VARCHAR NOT NULL);'
. 'CREATE TABLE sticker (id INTEGER PRIMARY KEY AUTOINCREMENT, label VARCHAR NOT NULL);'
. 'CREATE TABLE gadget_sticker (gadget_id INTEGER NOT NULL, sticker_id INTEGER NOT NULL);'
. 'CREATE TABLE flag (id INTEGER PRIMARY KEY AUTOINCREMENT,
label VARCHAR NOT NULL, enabled VARCHAR NOT NULL);'
. 'CREATE TABLE stamped (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NOT NULL, changed_at INTEGER);'
. 'CREATE TABLE stamped_note ('
. 'id INTEGER PRIMARY KEY AUTOINCREMENT, stamped_id INTEGER NOT NULL, text VARCHAR NOT NULL,'
. " changed_at INTEGER, deleted VARCHAR NOT NULL DEFAULT 'n');"
. 'CREATE TABLE trinket (id BLOB PRIMARY KEY, name VARCHAR NOT NULL);'
. 'CREATE TABLE charm (id INTEGER PRIMARY KEY AUTOINCREMENT,
trinket_id BLOB NOT NULL, label VARCHAR NOT NULL);'
. 'CREATE TABLE "values" ('
. '"order" INTEGER NOT NULL, "group" INTEGER NOT NULL, "insert" VARCHAR NOT NULL,'
. ' PRIMARY KEY ("order", "group"));'
. 'CREATE TABLE tag (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NOT NULL);'
. 'CREATE TABLE gadget_tag ('
. 'gadget_id INTEGER NOT NULL, tag_id INTEGER NOT NULL,'
. " changed_at INTEGER NOT NULL, deleted VARCHAR NOT NULL DEFAULT 'n',"
. ' PRIMARY KEY (gadget_id, tag_id));'
. 'CREATE TABLE badge (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NOT NULL);'
. 'CREATE TABLE gadget_badge ('
. 'id INTEGER PRIMARY KEY AUTOINCREMENT,'
. ' gadget_id INTEGER NOT NULL, badge_id INTEGER NOT NULL,'
. " changed_at INTEGER NOT NULL, deleted VARCHAR NOT NULL DEFAULT 'n');"
);
return $db;
}
/**
* Get a fresh EntityManager bound to the test connection, with a deterministic clock
*
* @return EntityManager
*/
protected function em(): EntityManager
{
return new TickingEntityManager($this->db);
}
/**
* Run a SELECT and return its rows as plain associative arrays
*
* @param string $sql
*
* @return array<int, array<string, mixed>>
*/
protected function rows(string $sql): array
{
return $this->db->prepexec($sql)->fetchAll(PDO::FETCH_ASSOC);
}
/**
* Get the recorded writes (insert/update/delete) that targeted the given table
*
* Handy for asserting that the EntityManager left a table completely untouched.
*
* @param string $table
*
* @return list<array<string, mixed>>
*/
protected function writesTo(string $table): array
{
return array_values(array_filter($this->db->calls, fn (array $c): bool => $c['table'] === $table));
}
/**
* Build and persist a workshop with the given name
*
* @param string $name
*
* @return Workshop
*/
protected function savedWorkshop(string $name = 'Acme'): Workshop
{
$workshop = (new Workshop())->setNew();
$workshop->name = $name;
$this->em()->save($workshop);
return $workshop;
}
// -----------------------------------------------------------------------------------------------------
// Single-row lifecycle: insert, update, no-op, delete
// -----------------------------------------------------------------------------------------------------
public function testInsertAssignsGeneratedKeyMarksModelLoadedAndEmitsExactlyOneInsert()
{
$workshop = (new Workshop())->setNew();
$this->assertTrue(
$workshop->isNew(),
'A model flagged with setNew() should report as new before its first save'
);
$workshop->name = 'Acme';
$this->em()->save($workshop);
$this->assertFalse($workshop->isNew(), 'A saved model should no longer be new');
$this->assertFalse($workshop->isModified(), 'A saved model should carry no pending changes');
$this->assertSame(1, $workshop->id, 'The generated primary key should be written back to the model');
$this->assertSame(
[['method' => 'insert', 'table' => 'workshop', 'data' => ['name' => 'Acme']]],
$this->db->calls,
'Inserting a new model should emit exactly one INSERT for that row and nothing else'
);
$this->assertSame([['id' => 1, 'name' => 'Acme']], $this->rows('SELECT * FROM workshop'));
}
public function testUpdateWritesOnlyTheChangedColumnScopedByPrimaryKey()
{
$gadget = (new Gadget())->setNew();
$gadget->workshop_id = 5;
$gadget->name = 'Spanner';
$this->em()->save($gadget);
$this->db->resetCalls();
$gadget->name = 'Wrench';
$this->assertSame(
['name' => true],
$gadget->getModifiedProperties(),
'Only the changed column should be tracked as modified'
);
$this->em()->save($gadget);
$this->assertFalse($gadget->isModified(), 'The model should be unmodified after an update');
$this->assertSame(
[[
'method' => 'update',
'table' => 'gadget',
'data' => ['name' => 'Wrench'],
'condition' => ['id = ?' => $gadget->id],
]],
$this->db->calls,
'The update should touch only the changed column and match the row by its primary key'
);
$this->assertSame(
[['workshop_id' => 5, 'name' => 'Wrench']],
$this->rows('SELECT workshop_id, name FROM gadget'),
'The unchanged column should be preserved'
);
}
public function testResavingAnUnchangedModelWhetherHydratedOrNotIssuesNoWrites()
{
$this->savedWorkshop('Acme');
$loaded = Workshop::on($this->db)->first()->setNew(false);
$this->assertNotNull($loaded);
$this->assertFalse($loaded->isNew(), 'A hydrated model should not be new');
$this->assertFalse($loaded->isModified(), 'A freshly hydrated model should have no changes');
$this->db->resetCalls();
$this->em()->save($loaded);
$this->assertSame([], $this->db->calls, 'Saving an unchanged hydrated model should issue no writes');
}
public function testHardDeleteRemovesTheRowClearsTheKeyAndAllowsAFreshReinsert()
{
$workshop = $this->savedWorkshop('Acme');
$oldId = $workshop->id;
// workshop has no `deleted` column, so this is a hard delete, not a soft delete.
$this->db->resetCalls();
$this->em()->save($workshop->delete());
$this->assertSame(
[['method' => 'delete', 'table' => 'workshop', 'condition' => ['id = ?' => $oldId]]],
$this->db->calls,
'A model without a deleted column should be removed with a single DELETE scoped by its primary key'
);
$this->assertSame([], $this->rows('SELECT * FROM workshop'), 'The row should be gone');
$this->assertTrue($workshop->isNew(), 'A deleted model should be treated as new again');
$this->assertFalse($workshop->isModified(), 'A deleted model should carry no pending changes');
$this->assertFalse($workshop->hasProperty('id'), 'The auto-increment key should be cleared on delete');
$workshop->name = 'Acme 2';
$this->em()->save($workshop);
$this->assertNotSame($oldId, $workshop->id, 'A save after delete should receive a fresh auto-increment id');
$this->assertSame([['name' => 'Acme 2']], $this->rows('SELECT name FROM workshop'));
}
public function testSoftDeleteKeepsTheRowMarksItDeletedAndRestampsChangedAt()
{
// gadget_tag carries a `deleted` column, so delete() keeps the row and flips deleted to 'y'
// rather than removing it, re-stamping changed_at in the process.
$link = (new GadgetTag())->setNew();
$link->gadget_id = 1;
$link->tag_id = 1;
$this->em()->save($link); // insert -> changed_at 1000
$this->em()->save($link->delete()); // soft-deleted -> changed_at 2000
$this->assertSame(
[['gadget_id' => 1, 'tag_id' => 1, 'deleted' => 'y', 'changed_at' => 2000]],
$this->rows('SELECT gadget_id, tag_id, deleted, changed_at FROM gadget_tag'),
'A model with a deleted column should be kept, marked deleted = y and re-stamped, not removed'
);
}
public function testDeletingANewModelThrowsAnException()
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Model is marked as new and cannot be deleted');
$workshop = (new Workshop())->setNew();
$workshop->name = 'Acme';
$this->em()->save($workshop->delete());
}
public function testChangingMutabilityThrowsAnException()
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Model is marked for deletion');
$link = (new GadgetTag())->setNew(false)->delete();
$link->setNew();
}
// -----------------------------------------------------------------------------------------------------
// Compound and binary primary keys
// -----------------------------------------------------------------------------------------------------
public function testCompoundKeyIsWrittenScopedAndClearedAcrossItsLifecycle()
{
// Two rows sharing `order` but differing in `group`: every WHERE has to include *both* key
// columns to target exactly one of them. The Pairing fixture's table (`values`), key columns
// (`order`/`group`) and plain column (`insert`) are all SQL reserved keywords, so this also
// proves the EntityManager quotes table names, INSERT/UPDATE columns and the compound key WHERE.
$a = (new Pairing())->setNew();
$a->order = 1;
$a->group = 1;
$a->insert = 'A';
$this->em()->save($a);
$b = (new Pairing())->setNew();
$b->order = 1;
$b->group = 2;
$b->insert = 'B';
$this->em()->save($b);
$this->assertFalse($b->isNew(), 'A saved compound-key model should no longer be new');
$this->assertFalse($b->isModified(), 'A saved compound-key model should carry no pending changes');
$this->assertSame(1, $b->order, 'order should not be overwritten by a lastInsertId fetch');
$this->assertSame(2, $b->group, 'group should not be overwritten by a lastInsertId fetch');
$this->db->resetCalls();
$b->insert = 'B2';
$this->em()->save($b);
$this->assertSame(
['order = ?' => 1, 'group = ?' => 2],
$this->db->calls[0]['condition'],
'Both key columns should scope the UPDATE'
);
$this->em()->save($b->delete());
$this->assertFalse($b->hasProperty('order'), 'Each part of a compound key should be cleared on delete');
$this->assertFalse($b->hasProperty('group'), 'Each part of a compound key should be cleared on delete');
$this->assertSame(
[['order' => 1, 'group' => 1, 'insert' => 'A']],
$this->rows('SELECT "order", "group", "insert" FROM "values"'),
'Only the row matching all key columns should be updated then deleted; the sibling stays intact'
);
}
public function testBinaryKeyRoundTripsThroughInsertUpdateDeleteAndCascade()
{
$id = hex2bin('deadbeefcafebabe1234567890abcdef');
$trinket = (new Trinket())->setNew();
$trinket->id = $id;
$trinket->name = 'Amulet';
$charm = (new Charm())->setNew();
$charm->label = 'rune';
$trinket->charm = Collection::create([$charm]);
$this->em()->save($trinket);
$this->assertSame($id, $trinket->id, 'The binary key should be unchanged on the model after insert');
$this->assertSame($id, $charm->trinket_id, 'The parent binary key should be copied into the child');
$this->assertSame(
[['id' => $id, 'name' => 'Amulet']],
$this->rows('SELECT id, name FROM trinket'),
'The binary key should be stored byte-for-byte'
);
$this->assertSame(
[['trinket_id' => $id, 'label' => 'rune']],
$this->rows('SELECT trinket_id, label FROM charm'),
'The child row should carry the parent binary key'
);
// Update and delete must both match the row by its binary primary key.
$trinket->name = 'Charm';
$this->em()->save($trinket);
$this->assertSame(
[['id' => $id, 'name' => 'Charm']],
$this->rows('SELECT id, name FROM trinket'),
'The UPDATE should match the row by its binary primary key'
);
$loaded = Trinket::on($this->db)->first()->setNew(false);
$this->em()->save($loaded->delete());
$this->assertSame(
[],
$this->rows('SELECT id FROM trinket'),
'The DELETE should match the row by its binary key'
);
$this->assertFalse(
$loaded->hasProperty('id'),
'The application-assigned binary key should be cleared on delete'
);
}
// -----------------------------------------------------------------------------------------------------
// Cascading a graph in one save()
// -----------------------------------------------------------------------------------------------------
public function testSavingADeepGraphCascadesEveryRelationInOneCall()
{
// A single cascading save covering, in one go:
// - HasMany: Workshop->gadget (the parent key is copied into each child)
// - BelongsToMany (hard): Gadget->sticker (linked through the plain gadget_sticker table)
// - BelongsToMany (soft): Gadget->tag (linked through the GadgetTag junction model)
// The sticker is shared between both gadgets, so its row is inserted once and linked twice.
$spanner = (new Gadget())->setNew();
$spanner->name = 'Spanner';
$wrench = (new Gadget())->setNew();
$wrench->name = 'Wrench';
$shared = (new Sticker())->setNew();
$shared->label = 'fragile';
$sharp = (new Tag())->setNew();
$sharp->name = 'sharp';
$spanner->sticker = Collection::create([$shared]);
$spanner->tag = Collection::create([$sharp]);
$wrench->sticker = Collection::create([$shared]);
$workshop = (new Workshop())->setNew();
$workshop->name = 'Acme';
$workshop->gadget = Collection::create([$spanner, $wrench]);
$this->em()->save($workshop);
$this->assertSame($workshop->id, $spanner->workshop_id, 'The parent key should be copied into each child');
$this->assertSame($workshop->id, $wrench->workshop_id, 'The parent key should be copied into each child');
$this->assertFalse($shared->isNew(), 'A cascaded many-to-many target should be persisted');
$this->assertFalse(
$shared->hasProperty('gadget_id'),
'A many-to-many target must not carry the junction foreign key as a stray property'
);
$this->assertSame('workshop', $this->db->calls[0]['table'], 'The parent should be inserted first');
$this->assertSame(
['insert'],
array_values(array_unique(array_column($this->db->calls, 'method'))),
'A fresh graph should be persisted purely with inserts'
);
$this->assertCount(2, $this->writesTo('gadget'), 'Both children should be inserted');
$this->assertCount(1, $this->writesTo('sticker'), 'The shared target should be inserted exactly once');
$this->assertCount(2, $this->writesTo('gadget_sticker'), 'Each link should be its own junction insert');
$this->assertCount(1, $this->writesTo('tag'), 'The tag target should be inserted');
$this->assertCount(1, $this->writesTo('gadget_tag'), 'The soft-delete junction row should be inserted');
$this->assertSame(
[['gadget_id' => $spanner->id], ['gadget_id' => $wrench->id]],
$this->rows('SELECT gadget_id FROM gadget_sticker ORDER BY gadget_id'),
'The shared sticker should be linked to both gadgets'
);
}
public function testBelongsToSavesTheParentFirstAndAssigningNullClearsTheForeignKey()
{
$gadget = (new Gadget())->setNew();
$gadget->name = 'Spanner';
$workshop = (new Workshop())->setNew();
$workshop->name = 'Acme';
$gadget->workshop = $workshop;
$this->em()->save($gadget);
$this->assertFalse($workshop->isNew(), 'The parent should be persisted before the model that references it');
$this->assertSame($workshop->id, $gadget->workshop_id, 'The parent key should be copied into the foreign key');
// Load fresh and clear the relation by assigning null. The property then holds null (not a lazy-loader
// closure), so saveGraph sees an explicit assignment and nulls the foreign key.
$loaded = Gadget::on($this->db)->first()->setNew(false);
$loaded->workshop = null;
$this->em()->save($loaded);
$this->assertSame(
[['name' => 'Spanner', 'workshop_id' => null]],
$this->rows('SELECT name, workshop_id FROM gadget'),
'Assigning null to a BelongsTo should null the foreign key on update'
);
}
// -----------------------------------------------------------------------------------------------------
// Many-to-many: the Collection API and its persistence semantics
// -----------------------------------------------------------------------------------------------------
public function testCollectionExposesTheStagedManyToManyMembership()
{
// Seed a gadget linked to two stickers, so reading the relation yields a lazily-loaded Collection.
$gadget = (new Gadget())->setNew();
$gadget->name = 'Spanner';
$fragile = (new Sticker())->setNew();
$fragile->label = 'fragile';
$heavy = (new Sticker())->setNew();
$heavy->label = 'heavy';
$gadget->sticker = Collection::create([$fragile, $heavy]);
$this->em()->save($gadget);
$loaded = Gadget::on($this->db)->first()->setNew(false);
$stickers = $loaded->sticker;
$this->assertInstanceOf(Collection::class, $stickers, 'A to-many relation should read back as a Collection');
$this->assertCount(2, $stickers, 'count() should reflect the two loaded members');
$this->assertFalse($stickers->isReplacing(), 'A freshly loaded collection should be in merge mode');
$this->assertFalse(
$stickers->hasPendingChanges(),
'A freshly loaded collection should have nothing to persist'
);
$this->assertFalse($loaded->isModified(), 'Reading a relation must not mark the model modified');
foreach ($stickers as $sticker) {
match ($sticker->label) {
'fragile' => $fragile = $sticker,
'heavy' => $heavy = $sticker
};
}
$shiny = (new Sticker())->setNew();
$shiny->label = 'shiny';
$stickers->attach($shiny);
$this->assertCount(3, $stickers, 'Attaching a new member should add it to the loaded base');
$this->assertTrue($stickers->hasPendingChanges(), 'A staged attach should count as a pending change');
$stickers->detach($fragile);
$this->assertCount(2, $stickers, 'A detached member should drop out of the staged view');
$this->assertContains(
$fragile,
$stickers->getDetachments(),
'The detached member should be staged for removal'
);
$stickers->attach($fragile);
$this->assertCount(3, $stickers, 'Re-attaching should restore the member');
$this->assertSame([], $stickers->getDetachments(), 'Re-attaching a member should cancel its pending detach');
$only = (new Sticker())->setNew();
$only->label = 'only';
$stickers->sync([$only]);
$this->assertTrue($stickers->isReplacing(), 'sync() puts the collection into replace mode');
$this->assertCount(1, $stickers, 'Replace mode should count exactly the synced members, ignoring the base');
$stickers->clearPendingChanges();
$this->assertFalse($stickers->hasPendingChanges(), 'clearPendingChanges() drops the staged writes');
$this->assertFalse($stickers->isReplacing(), 'clearPendingChanges() leaves merge mode behind');
$fresh = (new Workshop())->setNew();
$this->assertFalse(isset($fresh->gadget), 'A non-hydrated model should have no collections by default');
}
public function testManyToManyPersistenceIsAdditiveAndDeduplicatesLinks()
{
$gadget = (new Gadget())->setNew();
$gadget->name = 'Spanner';
$fragile = (new Sticker())->setNew();
$fragile->label = 'fragile';
$gadget->sticker = Collection::create([$fragile]);
$this->em()->save($gadget);
$this->assertFalse($fragile->isNew(), 'The target should be persisted');
$this->assertSame(
[['gadget_id' => $gadget->id, 'sticker_id' => $fragile->id]],
$this->rows('SELECT gadget_id, sticker_id FROM gadget_sticker'),
'A many-to-many assignment should write the junction row'
);
$loaded = Gadget::on($this->db)->first()->setNew(false);
$loaded->sticker = Collection::create([$fragile]);
$this->db->resetCalls();
$this->em()->save($loaded);
$this->assertSame(
[],
$this->writesTo('gadget_sticker'),
'Re-saving an unchanged link must issue no junction write'
);
$heavy = (new Sticker())->setNew();
$heavy->label = 'heavy';
$loaded = Gadget::on($this->db)->first()->setNew(false);
$loaded->sticker = Collection::create([$heavy]);
$this->em()->save($loaded);
$this->assertSame(
[['sticker_id' => $heavy->id]],
$this->rows('SELECT sticker_id FROM gadget_sticker ORDER BY sticker_id'),
'Overriding the default collection must reconcile the junction'
);
$loaded = Gadget::on($this->db)->first()->setNew(false);
$loaded->sticker = [];
$this->db->resetCalls();
$this->em()->save($loaded);
$this->assertCount(
1,
$this->writesTo('gadget_sticker'),
'A bare array assignment must reconcile the junction'
);
$this->assertCount(0, $this->rows('SELECT * FROM gadget_sticker'), 'Both links must be dropped');
}
public function testManyToManyReconciliationTreatsStringKeysAsEqualToIntKeys()
{
// Production drivers (MySQL/PgSQL) return keys as strings while persisted models hold them as ints,
// so sync must treat those as the same link — re-saving an unchanged relation must stay a no-op,
// not delete-and-reinsert. STRINGIFY_FETCHES reproduces that boundary; sqlite otherwise returns
// ints on both sides and never exercises it.
$db = $this->connect([PDO::ATTR_STRINGIFY_FETCHES => true]);
$gadget = (new Gadget())->setNew();
$gadget->name = 'Spanner';
$sticker = (new Sticker())->setNew();
$sticker->label = 'fragile';
$gadget->sticker = Collection::create([$sticker]);
(new TickingEntityManager($db))->save($gadget);
$loaded = Gadget::on($db)->first()->setNew(false);
$loaded->sticker = Collection::create([$sticker]);
$db->resetCalls();
(new TickingEntityManager($db))->save($loaded);
$this->assertSame(
[],
array_filter($db->calls, fn ($call) => $call['table'] === 'gadget_sticker'),
'An unchanged link must trigger no insert or delete even when keys come back as strings'
);
$this->assertSame(
[['gadget_id' => '1', 'sticker_id' => '1']],
$db->prepexec('SELECT gadget_id, sticker_id FROM gadget_sticker')->fetchAll(PDO::FETCH_ASSOC),
'The single link should be left intact'
);
}
public function testSoftDeleteJunctionDetachesRevivesAndLeavesUnchangedLinksUntouched()
{
// gadget->tag is linked through GadgetTag, which carries a `deleted` column: removals soft-delete
// and re-adds revive the tombstoned row instead of hard-deleting or duplicating it.
$gadget = (new Gadget())->setNew();
$gadget->name = 'Spanner';
$sharp = (new Tag())->setNew();
$sharp->name = 'sharp';
$heavy = (new Tag())->setNew();
$heavy->name = 'heavy';
$gadget->tag = Collection::create([$sharp, $heavy]);
$this->em()->save($gadget); // two links inserted -> changed_at 1000, 2000
$loaded = Gadget::on($this->db)->first()->setNew(false);
$loaded->tag = Collection::create([$sharp, $heavy]);
$this->db->resetCalls();
$this->em()->save($loaded);
$this->assertSame([], $this->writesTo('gadget_tag'), 'Re-saving an unchanged active link should write nothing');
$link = GadgetTag::on($this->db)->filter(
Filter::all(Filter::equal('gadget_id', $gadget->id), Filter::equal('tag_id', $heavy->id))
)->first()->setNew(false);
$this->em()->save($link->delete()); // soft-deleted -> changed_at 3000
$this->assertSame(
[
['tag_id' => $sharp->id, 'deleted' => 'n', 'changed_at' => 1000],
['tag_id' => $heavy->id, 'deleted' => 'y', 'changed_at' => 3000],
],
$this->rows('SELECT tag_id, deleted, changed_at FROM gadget_tag ORDER BY tag_id'),
'Detaching a soft-delete link should mark it deleted and re-stamp changed_at, leaving the other link alone'
);
$readded = Gadget::on($this->db)->first()->setNew(false);
$readded->tag = Collection::create([$sharp, $heavy]);
$this->em()->save($readded); // revived -> changed_at 4000
$this->assertSame(
[
['tag_id' => $sharp->id, 'deleted' => 'n', 'changed_at' => 1000],
['tag_id' => $heavy->id, 'deleted' => 'n', 'changed_at' => 4000],
],
$this->rows('SELECT tag_id, deleted, changed_at FROM gadget_tag ORDER BY tag_id'),
'Re-adding a detached link should revive the existing row'
);
}
public function testSoftDeleteJunctionWithSurrogateKeyRevivesAndDetachesThroughReconcile()
{
$gadget = (new Gadget())->setNew();
$gadget->name = 'Spanner';
$sharp = (new Badge())->setNew();
$sharp->name = 'sharp';
$heavy = (new Badge())->setNew();
$heavy->name = 'heavy';
$gadget->badge = Collection::create([$sharp, $heavy]);
$this->em()->save($gadget); // two links inserted -> changed_at 1000, 2000
$heavyLinkId = GadgetBadge::on($this->db)
->filter(Filter::all(Filter::equal('gadget_id', $gadget->id), Filter::equal('badge_id', $heavy->id)))
->first()
->id;
// Sync the membership down to just "sharp"; reconcile must soft-delete heavy's link scoped by its
// surrogate id, not by gadget_id/badge_id
$loaded = Gadget::on($this->db)->first()->setNew(false);
$loaded->badge = Collection::create([$sharp]);
$this->em()->save($loaded); // heavy soft-deleted -> changed_at 3000
$this->assertSame(
[
['badge_id' => $sharp->id, 'deleted' => 'n', 'changed_at' => 1000],
['badge_id' => $heavy->id, 'deleted' => 'y', 'changed_at' => 3000],
],
$this->rows('SELECT badge_id, deleted, changed_at FROM gadget_badge ORDER BY badge_id'),
'Removing a surrogate-keyed soft-delete link should mark it deleted and re-stamp changed_at'
);
// Re-add heavy; reconcile must revive the tombstoned row, again scoped by its surrogate id
$readded = Gadget::on($this->db)->first()->setNew(false);
$readded->badge = Collection::create([$sharp, $heavy]);
$this->em()->save($readded); // heavy revived -> changed_at 4000
$this->assertSame(
[
['badge_id' => $sharp->id, 'deleted' => 'n', 'changed_at' => 1000],
['badge_id' => $heavy->id, 'deleted' => 'n', 'changed_at' => 4000],
],
$this->rows('SELECT badge_id, deleted, changed_at FROM gadget_badge ORDER BY badge_id'),
'Re-adding a detached surrogate-keyed link should revive the existing row'
);
$this->assertSame(
[['id' => $heavyLinkId, 'c' => 1]],
$this->rows(
'SELECT id, COUNT(*) AS c FROM gadget_badge WHERE badge_id = ' . $heavy->id . ' GROUP BY id'
),
'Reviving must reuse the original junction row, keeping its surrogate id instead of inserting a duplicate'
);
}
public function testSoftDeleteHasManyChildrenAreSoftDeletedOnSyncAndDetach()
{
// stamped_note is a HasMany child carrying a `deleted` column, so removing a note keeps its row and
// marks deleted = 'y' rather than hard-deleting it.
$stamped = (new Stamped())->setNew();
$stamped->name = 'Manual';
$first = (new StampedNote())->setNew();
$first->text = 'first';
$second = (new StampedNote())->setNew();
$second->text = 'second';
$stamped->stamped_note = Collection::create([$first, $second]);
$this->em()->save($stamped);
$loaded = Stamped::on($this->db)->first()->setNew(false);
$loaded->stamped_note = (new Collection())->sync([]);
$this->em()->save($loaded);
$this->assertSame(
[
['text' => 'first', 'deleted' => 'y'],
['text' => 'second', 'deleted' => 'y'],
],
$this->rows('SELECT text, deleted FROM stamped_note ORDER BY id'),
'sync([]) should soft-delete every orphaned child rather than removing its row'
);
$third = (new StampedNote())->setNew();
$third->text = 'third';
$loaded->stamped_note = Collection::create([$third]);
$this->em()->save($loaded);
$this->assertSame(
'n',
$this->rows("SELECT deleted FROM stamped_note WHERE text = 'third'")[0]['deleted'],
'A newly attached child should be inserted active'
);
// Detaching soft-deletes the child rather than removing it, since a HasMany link is the child row itself.
$loaded->stamped_note = (new Collection())->detach($third);
$this->em()->save($loaded);
$this->assertSame(
'y',
$this->rows("SELECT deleted FROM stamped_note WHERE text = 'third'")[0]['deleted'],
'Detaching a soft-delete child should mark it deleted rather than removing its row'
);
}
// -----------------------------------------------------------------------------------------------------
// Column behaviors and the changed_at stamp
// -----------------------------------------------------------------------------------------------------
public function testColumnBehaviorConvertsValuesOnInsertAndUpdate()
{
$flag = (new Flag())->setNew();
$flag->label = 'shiny';
$flag->enabled = true;
$this->em()->save($flag);
$this->assertTrue($flag->enabled, 'The model value should remain in its PHP form after save');
$this->assertSame(
[['label' => 'shiny', 'enabled' => 'y']],
$this->rows('SELECT label, enabled FROM flag'),
'BoolCast should convert true to its database value on insert'
);
$flag->enabled = false;
$this->em()->save($flag);
$this->assertSame(
[['enabled' => 'n']],
$this->rows('SELECT enabled FROM flag'),
'BoolCast should convert the new value on update'
);
}
public function testChangedAtIsStampedOnEveryWriteButNotWhenNothingChanged()
{
$stamped = (new Stamped())->setNew();
$stamped->name = 'Widget';
$this->em()->save($stamped); // insert -> changed_at 1s
$this->assertInstanceOf(
DateTimeInterface::class,
$stamped->changed_at,
'The stamp should be a DateTime on the model'
);
$this->assertSame(1, $stamped->changed_at->getTimestamp(), 'The insert should stamp changed_at');
$this->assertSame(
[['name' => 'Widget', 'changed_at' => 1000]],
$this->rows('SELECT name, changed_at FROM stamped'),
'The DateTime should be converted to a millisecond timestamp on the way to the database'
);
$stamped->name = 'Gizmo';
$this->em()->save($stamped); // update -> changed_at 2s
$this->assertSame(2, $stamped->changed_at->getTimestamp(), 'An update should re-stamp changed_at');
$this->db->resetCalls();
$this->em()->save($stamped);
$this->assertSame(2, $stamped->changed_at->getTimestamp(), 'An unchanged save must not re-stamp changed_at');
$this->assertSame([], $this->writesTo('stamped'), 'An unchanged save must not write the row');
// Reassigning only a relation leaves the parent row's own data untouched, so it is neither
// re-stamped nor re-written.
$loaded = Stamped::on($this->db)->first()->setNew(false);
$loaded->stamped_note = new Collection();
$this->db->resetCalls();
$this->em()->save($loaded);
$this->assertSame(
[],
$this->writesTo('stamped'),
'A relation-only reassignment must not UPDATE the parent row'
);
}
// -----------------------------------------------------------------------------------------------------
// "Dumb" delete: no implicit cascade, explicit intent honoured
// -----------------------------------------------------------------------------------------------------
public function testDeletingAModelNeverCascadesToItsAssignedParentOrChildrenAndLeavesJunctionsIntact()
{
// A gadget linked to a sticker. Assign a brand-new BelongsTo parent, then delete the gadget. The
// parent is independent and unmodified, so it must not be persisted, and the existing junction row
// must be left in place (orphaned, not purged) — deletion does not touch junctions.
$gadget = (new Gadget())->setNew();
$gadget->name = 'Spanner';
$sticker = (new Sticker())->setNew();
$sticker->label = 'fragile';
$gadget->sticker = Collection::create([$sticker]);
$this->em()->save($gadget);
$gadgetId = $gadget->id;
$newParent = (new Workshop())->setNew();
$newParent->name = 'Acme';
/** @var Model $loadedGadget */
$loadedGadget = Gadget::on($this->db)->first()->setNew(false);
$loadedGadget->workshop = $newParent;
$this->db->resetCalls();
$this->em()->save($loadedGadget->delete());
$this->assertSame(
[['method' => 'delete', 'table' => 'gadget', 'condition' => ['id = ?' => $gadgetId]]],
$this->db->calls,
'A deleted model should issue only its own DELETE; an assigned parent must not be cascaded'
);
$this->assertTrue($newParent->isNew(), 'An assigned parent must not be persisted while deleting');
$this->assertSame(
[['gadget_id' => $gadgetId, 'sticker_id' => $sticker->id]],
$this->rows('SELECT gadget_id, sticker_id FROM gadget_sticker'),
'Deleting the owner must leave its existing junction rows untouched (orphaned)'
);
// A workshop with a brand-new assigned HasMany child, deleted in one save. The child is not marked
// for deletion, so it must not be persisted onto a foreign key that is about to vanish.
$workshop = $this->savedWorkshop('Globex');
$workshopId = $workshop->id;
$orphan = (new Gadget())->setNew();
$orphan->name = 'Orphan';
/** @var Model $loadedWorkshop */
$loadedWorkshop = Workshop::on($this->db)->filter(Filter::equal('name', 'Globex'))->first()->setNew(false);
$loadedWorkshop->gadget = Collection::create([$orphan]);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('should not carry attachments');
$this->em()->save($loadedWorkshop->delete());
}
public function testAssigningANewManyToManyTargetWhileDeletingTheOwnerStillLinksIt()
{
// The EntityManager is "dumb": an explicitly assigned many-to-many link is honoured even while the
// owner is being deleted. The target is persisted and its junction row written before the owner's
// DELETE, leaving the link orphaned. Removing a source's links is a separate, explicit operation.
$gadget = (new Gadget())->setNew();
$gadget->name = 'Spanner';
$this->em()->save($gadget);
$gadgetId = $gadget->id;
$late = (new Sticker())->setNew();
$late->label = 'late';
/** @var Model $loaded */
$loaded = Gadget::on($this->db)->first()->setNew(false);
$loaded->sticker = Collection::create([$late]);
$this->db->resetCalls();
$this->em()->save($loaded->delete());
$this->assertFalse($late->isNew(), 'The explicitly assigned target should still be persisted');
$this->assertSame(
['sticker', 'gadget_sticker', 'gadget'],
array_column($this->db->calls, 'table'),
'The link should be written before the owner is deleted'
);
$this->assertSame(
['insert', 'insert', 'delete'],
array_column($this->db->calls, 'method'),
'The target and its junction row should be inserted before the owner is deleted'
);
$this->assertSame([], $this->rows('SELECT * FROM gadget'), 'The owner itself should be gone');
$this->assertSame(
[['gadget_id' => $gadgetId, 'sticker_id' => $late->id]],
$this->rows('SELECT gadget_id, sticker_id FROM gadget_sticker'),
'The honoured link should remain behind as an orphaned junction row'
);
}
public function testExplicitDeletionsAndInPlaceEditsAcrossAGraphAreAllApplied()
{
// A gadget that owns a workshop (BelongsTo) and is linked to a tag through the soft-delete junction.
$gadget = (new Gadget())->setNew();
$gadget->name = 'Spanner';
$workshop = (new Workshop())->setNew();
$workshop->name = 'Acme';
$gadget->workshop = $workshop;
$tag = (new Tag())->setNew();
$tag->name = 'sharp';
$gadget->tag = Collection::create([$tag]);
$this->em()->save($gadget); // link inserted -> changed_at 1000
$gadgetId = $gadget->id;
$workshopId = $workshop->id;
$tagId = $tag->id;
// Detach the link out of band, soft-deleting the junction row.
$link = GadgetTag::on($this->db)->filter(Filter::equal('gadget_id', $gadget->id))->first()->setNew(false);
$this->em()->save($link->delete()); // soft-deleted -> changed_at 2000
// In one save: edit the (independent) parent in place, mark the target deleted and delete the gadget.
// The parent outlives the gadget, so its edit is still applied — after the gadget is gone. The target
// is explicitly deleted, so it is removed too. The soft-deleted junction row is left as a tombstone.
$loaded = Gadget::on($this->db)->with('workshop')->first()->setNew(false);
$loaded->workshop->setNew(false);
$loaded->workshop->name = 'Globex';
$loaded->tag = Collection::create([$tag->delete()]);
$this->em()->save($loaded->delete());
$this->assertSame([], $this->rows('SELECT * FROM gadget'), 'The explicitly deleted gadget should be gone');
$this->assertSame([], $this->rows('SELECT * FROM tag'), 'The explicitly deleted target should be gone');
$this->assertSame(
[['id' => $workshopId, 'name' => 'Globex']],
$this->rows('SELECT id, name FROM workshop'),
'An in-place edit to an independent parent should be applied even as its owner is deleted'
);
$this->assertSame(
[['gadget_id' => $gadgetId, 'tag_id' => $tagId, 'deleted' => 'y', 'changed_at' => 2000]],
$this->rows('SELECT gadget_id, tag_id, deleted, changed_at FROM gadget_tag'),
'The soft-deleted junction row should remain as an untouched tombstone'
);
}
public function testDeletingAModelAlsoDeletesAChildExplicitlyMarkedDeleted()
{
$workshop = (new Workshop())->setNew();
$workshop->name = 'Acme';
$gadget = (new Gadget())->setNew();
$gadget->name = 'Spanner';
$workshop->gadget = Collection::create([$gadget]);
$this->em()->save($workshop);
$this->assertNotEmpty($this->rows('SELECT * FROM gadget'), 'precondition: the child exists');
$loadedWorkshop = Workshop::on($this->db)->first()->setNew(false);
$loadedGadget = Gadget::on($this->db)->first()->setNew(false);
$loadedWorkshop->gadget = Collection::create([$loadedGadget->delete()]);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('should not carry attachments');
$this->em()->save($loadedWorkshop->delete());
}
public function testModifyDeletedRelation()
{
$stamped = (new Stamped())->setNew();
$stamped->name = 'Manual';
$note = (new StampedNote())->setNew();
$note->text = 'initial';