packages/ztd-query-core/tests/Unit/Shadow/Mutation/Table/DropTableMutationTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Shadow\Mutation\Table;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use ZtdQuery\Exception\SchemaNotFoundException;
11use ZtdQuery\Schema\TableDefinition;
12use ZtdQuery\Schema\TableDefinitionRegistry;
13use ZtdQuery\Shadow\Mutation\Table\DropTableMutation;
14use ZtdQuery\Shadow\ShadowStore;
15
16#[UsesClass(SchemaNotFoundException::class)]
17#[UsesClass(TableDefinition::class)]
18#[UsesClass(TableDefinitionRegistry::class)]
19#[UsesClass(ShadowStore::class)]
20#[CoversClass(DropTableMutation::class)]
21#[UsesClass(\ZtdQuery\Schema\RowSet::class)]
22final class DropTableMutationTest extends TestCase
23{
24    public function testApplyUnregistersTableFromSchema(): void
25    {
26        $registry = new TableDefinitionRegistry();
27        $definition = new TableDefinition(
28            ['id'],
29            ['id' => 'INT'],
30            ['id'],
31            ['id'],
32            [],
33        );
34        $registry->register('users', $definition);
35        $store = new ShadowStore();
36        $store->set('users', [['id' => 1]]);
37
38        $mutation = new DropTableMutation('users', $registry, 'fixture statement');
39        $mutation->apply($store, []);
40
41        self::assertNull($registry->get('users'));
42        self::assertTrue($registry->isRemoved('users'));
43        self::assertSame(['users' => $definition], $registry->getAllRemoved());
44    }
45
46    public function testApplyClearsDataFromShadowStore(): void
47    {
48        $registry = new TableDefinitionRegistry();
49        $definition = new TableDefinition(
50            ['id'],
51            ['id' => 'INT'],
52            ['id'],
53            ['id'],
54            [],
55        );
56        $registry->register('users', $definition);
57        $store = new ShadowStore();
58        $store->set('users', [['id' => 1], ['id' => 2]]);
59
60        $mutation = new DropTableMutation('users', $registry, 'fixture statement');
61        $mutation->apply($store, []);
62
63        self::assertSame([], $store->get('users'));
64    }
65
66    public function testTableNameReturnsTableName(): void
67    {
68        $registry = new TableDefinitionRegistry();
69        $mutation = new DropTableMutation('users', $registry, 'fixture statement');
70
71        self::assertSame('users', $mutation->tableName());
72    }
73
74    public function testApplyThrowsExceptionWhenTableDoesNotExist(): void
75    {
76        $registry = new TableDefinitionRegistry();
77        $store = new ShadowStore();
78
79        $mutation = new DropTableMutation('users', $registry, 'fixture statement');
80
81        try {
82            $mutation->apply($store, []);
83            self::fail('Expected an unknown virtual table to fail.');
84        } catch (SchemaNotFoundException $exception) {
85            self::assertSame("Table 'users' does not exist.", $exception->getMessage());
86            self::assertSame('fixture statement', $exception->getSql());
87        }
88    }
89
90    public function testApplyWithIfExistsSkipsWhenTableDoesNotExist(): void
91    {
92        $registry = new TableDefinitionRegistry();
93        $store = new ShadowStore();
94
95        $mutation = new DropTableMutation('users', $registry, 'fixture statement', true);
96
97        $mutation->apply($store, []);
98
99        self::assertNull($registry->get('users'));
100    }
101
102    public function testApplyWithIfExistsDropsExistingTable(): void
103    {
104        $registry = new TableDefinitionRegistry();
105        $definition = new TableDefinition(
106            ['id'],
107            ['id' => 'INT'],
108            ['id'],
109            ['id'],
110            [],
111        );
112        $registry->register('users', $definition);
113        $store = new ShadowStore();
114        $store->set('users', [['id' => 1]]);
115
116        $mutation = new DropTableMutation('users', $registry, 'fixture statement', true);
117        $mutation->apply($store, []);
118
119        self::assertNull($registry->get('users'));
120        self::assertSame([], $store->get('users'));
121    }
122}
123