packages/ztd-query-core/tests/Unit/Shadow/Mutation/MutationRowIdentityTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Shadow\Mutation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9use ZtdQuery\Shadow\Mutation\MutationRowIdentity;
10
11#[CoversClass(MutationRowIdentity::class)]
12final class MutationRowIdentityTest extends TestCase
13{
14 public function testColumnBuildsReservedMetadataColumnName(): void
15 {
16 self::assertSame('__ztd_original_id', (new MutationRowIdentity())->column('id'));
17 }
18
19 public function testSeparatesOriginalCompositeKeyFromTheUpdatedRow(): void
20 {
21 self::assertSame(
22 [
23 'row' => ['tenant_id' => 2, 'id' => 20, 'value' => 'changed'],
24 'identity' => ['tenant_id' => 1, 'id' => 10],
25 ],
26 (new MutationRowIdentity())->extract([
27 'tenant_id' => 2,
28 'id' => 20,
29 'value' => 'changed',
30 '__ztd_original_tenant_id' => 1,
31 '__ztd_original_id' => 10,
32 ], ['tenant_id', 'id']),
33 );
34 }
35
36 public function testFallsBackToCurrentKeyForLegacyProjections(): void
37 {
38 self::assertSame(
39 ['row' => ['id' => 1], 'identity' => ['id' => 1]],
40 (new MutationRowIdentity())->extract(['id' => 1], ['id']),
41 );
42 }
43
44 public function testStripRemovesEveryInternalIdentityColumn(): void
45 {
46 self::assertSame(
47 ['id' => 2, 'name' => 'updated'],
48 (new MutationRowIdentity())->strip([
49 'id' => 2,
50 'name' => 'updated',
51 '__ztd_original_id' => 1,
52 '__ztd_original_tenant_id' => 10,
53 ]),
54 );
55 }
56 public function testStripAllTakesTheCarriedNamesOffEveryRow(): void
57 {
58 $rows = [
59 ['id' => 1, '__ztd_original_id' => 0],
60 ['id' => 2, '__ztd_original_id' => 1],
61 ];
62
63 self::assertSame(
64 [['id' => 1], ['id' => 2]],
65 (new MutationRowIdentity())->stripAll($rows),
66 );
67 }
68
69 public function testStripAllAnswersNothingForNoRows(): void
70 {
71 self::assertSame([], (new MutationRowIdentity())->stripAll([]));
72 }
73
74 public function testExtractSplitsTheRowFromTheKeyItUsedToHave(): void
75 {
76 self::assertSame(
77 ['row' => ['id' => 2], 'identity' => ['id' => 1]],
78 (new MutationRowIdentity())->extract(['id' => 2, '__ztd_original_id' => 1], ['id']),
79 );
80 }
81
82 public function testExtractReadsAnUnchangedKeyAsItsOwnOldValue(): void
83 {
84 self::assertSame(
85 ['row' => ['id' => 1], 'identity' => ['id' => 1]],
86 (new MutationRowIdentity())->extract(['id' => 1], ['id']),
87 );
88 }
89
90 public function testExtractCarriesNoKeyWhereTheRowHasNoneOfIt(): void
91 {
92 self::assertSame(
93 ['row' => ['name' => 'a'], 'identity' => []],
94 (new MutationRowIdentity())->extract(['name' => 'a'], ['id']),
95 );
96 }
97
98}
99