packages/ztd-query-core/tests/Unit/Shadow/Mutation/DataMutationTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Shadow\Mutation;
6
7use PHPUnit\Framework\Attributes\CoversNothing;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\TestCase;
10use Tests\Fake\CascadingShop;
11use ZtdQuery\Schema\TableDefinition;
12use ZtdQuery\Shadow\Mutation\Row\DeleteMutation;
13use ZtdQuery\Shadow\Mutation\Row\InsertMutation;
14use ZtdQuery\Shadow\Mutation\Row\MultiDeleteMutation;
15use ZtdQuery\Shadow\Mutation\Row\MultiUpdateMutation;
16use ZtdQuery\Shadow\Mutation\Row\ReplaceMutation;
17use ZtdQuery\Shadow\Mutation\Row\UpdateMutation;
18use ZtdQuery\Shadow\Mutation\ShadowMutation;
19use ZtdQuery\Shadow\Mutation\Table\CreateTableMutation;
20use ZtdQuery\Shadow\Mutation\Table\MultiTruncateMutation;
21use ZtdQuery\Shadow\Mutation\Table\SynchronizeMutation;
22use ZtdQuery\Shadow\Mutation\Table\TruncateMutation;
23use ZtdQuery\Shadow\Mutation\UpsertMutation;
24use ZtdQuery\Shadow\ReferentialIntegrityEnforcer;
25
26#[CoversNothing]
27final class DataMutationTest extends TestCase
28{
29 #[DataProvider('providerRowChangingMutation')]
30 public function testAMutationThatChangesRowsCarriesItsConsequencesToTheChildren(
31 ShadowMutation $mutation,
32 ): void {
33 $registry = CascadingShop::registry();
34 $before = CascadingShop::shadow();
35 $after = $before->snapshot();
36 $after->set('parents', []);
37
38 (new ReferentialIntegrityEnforcer($registry))->synchronize($before, $after, $mutation, [], 'SQL');
39
40 self::assertSame([], $after->get('children'));
41 }
42
43 /**
44 * @return iterable<string, array{ShadowMutation}>
45 */
46 public static function providerRowChangingMutation(): iterable
47 {
48 yield 'insert' => [new InsertMutation('parents')];
49 yield 'update' => [new UpdateMutation('parents', ['id'])];
50 yield 'delete' => [new DeleteMutation('parents', ['id'])];
51 yield 'replace' => [new ReplaceMutation('parents')];
52 yield 'upsert' => [new UpsertMutation('parents', ['id'])];
53 yield 'truncate' => [new TruncateMutation('parents')];
54 yield 'multi update' => [new MultiUpdateMutation([])];
55 yield 'multi delete' => [new MultiDeleteMutation([])];
56 yield 'multi truncate' => [new MultiTruncateMutation([])];
57 yield 'synchronize' => [new SynchronizeMutation('parents')];
58 }
59
60 public function testAMutationThatChangesNoRowsCarriesNothingToTheChildren(): void
61 {
62 $registry = CascadingShop::registry();
63 $before = CascadingShop::shadow();
64 $after = $before->snapshot();
65 $after->set('parents', []);
66 $mutation = new CreateTableMutation(
67 'extra',
68 new TableDefinition(['id'], [], ['id'], [], []),
69 $registry,
70 'CREATE TABLE extra (id INT)',
71 );
72
73 (new ReferentialIntegrityEnforcer($registry))->synchronize($before, $after, $mutation, [], 'SQL');
74
75 self::assertSame([['id' => 10, 'parent_id' => 1]], $after->get('children'));
76 }
77}
78