packages/ztd-query-core/tests/Unit/Shadow/Mutation/Row/DeleteMutationTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Shadow\Mutation\Row;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use ZtdQuery\Shadow\Mutation\Row\DeleteMutation;
11use ZtdQuery\Shadow\ShadowStore;
12
13#[UsesClass(ShadowStore::class)]
14#[CoversClass(DeleteMutation::class)]
15#[UsesClass(\ZtdQuery\Shadow\Row\RowMatch::class)]
16#[UsesClass(\ZtdQuery\Schema\RowSet::class)]
17final class DeleteMutationTest extends TestCase
18{
19 public function testApplyRemovesRowsByPrimaryKey(): void
20 {
21 $store = new ShadowStore();
22 $store->set('users', [
23 ['id' => 1, 'name' => 'Alice'],
24 ['id' => 2, 'name' => 'Bob'],
25 ]);
26
27 $mutation = new DeleteMutation('users', ['id']);
28 $mutation->apply($store, [['id' => 1]]);
29
30 self::assertSame([['id' => 2, 'name' => 'Bob']], $store->get('users'));
31 }
32
33 public function testTableNameReturnsTableName(): void
34 {
35 $mutation = new DeleteMutation('users', ['id']);
36
37 self::assertSame('users', $mutation->tableName());
38 }
39
40 public function testApplyRemovesMultipleRows(): void
41 {
42 $store = new ShadowStore();
43 $store->set('users', [
44 ['id' => 1, 'name' => 'Alice'],
45 ['id' => 2, 'name' => 'Bob'],
46 ['id' => 3, 'name' => 'Carol'],
47 ]);
48
49 $mutation = new DeleteMutation('users', ['id']);
50 $mutation->apply($store, [['id' => 1], ['id' => 3]]);
51
52 self::assertSame([['id' => 2, 'name' => 'Bob']], $store->get('users'));
53 }
54
55 public function testApplyWithCompositePrimaryKey(): void
56 {
57 $store = new ShadowStore();
58 $store->set('order_items', [
59 ['order_id' => 1, 'product_id' => 100, 'quantity' => 1],
60 ['order_id' => 1, 'product_id' => 200, 'quantity' => 2],
61 ['order_id' => 2, 'product_id' => 100, 'quantity' => 3],
62 ]);
63
64 $mutation = new DeleteMutation('order_items', ['order_id', 'product_id']);
65 $mutation->apply($store, [['order_id' => 1, 'product_id' => 100]]);
66
67 self::assertCount(2, $store->get('order_items'));
68 self::assertSame(200, $store->get('order_items')[0]['product_id']);
69 }
70
71 public function testApplyWithEmptyRowsDoesNothing(): void
72 {
73 $store = new ShadowStore();
74 $store->set('users', [
75 ['id' => 1, 'name' => 'Alice'],
76 ]);
77
78 $mutation = new DeleteMutation('users', ['id']);
79 $mutation->apply($store, []);
80
81 self::assertCount(1, $store->get('users'));
82 }
83
84 public function testApplyWithNonExistentRowDoesNothing(): void
85 {
86 $store = new ShadowStore();
87 $store->set('users', [
88 ['id' => 1, 'name' => 'Alice'],
89 ]);
90
91 $mutation = new DeleteMutation('users', ['id']);
92 $mutation->apply($store, [['id' => 999]]);
93
94 self::assertCount(1, $store->get('users'));
95 }
96}
97