packages/ztd-query-pdo-adapter/tests/Integration/PostgreSql/TruncateTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Integration\PostgreSql;
6
7use Container\PostgreSql16Container;
8use PDO;
9use PHPUnit\Framework\Attributes\CoversNothing;
10use PHPUnit\Framework\Attributes\Large;
11use PHPUnit\Framework\TestCase;
12use ZtdQuery\Adapter\Pdo\ZtdPdo;
13
14/**
15 * @requires extension pdo_pgsql
16 * @group integration
17 * @group postgres
18 *
19 * @phpstan-type Row array<string, mixed>
20 */
21#[CoversNothing]
22#[Large]
23final class TruncateTest extends TestCase
24{
25    public function testTruncateTable(): void
26    {
27        $containerInstance = \Testcontainers\Testcontainers::run(PostgreSql16Container::class);
28        /** @var PDO $rawPdo */
29        $rawPdo = new PDO(
30            sprintf('pgsql:host=%s;port=%d;dbname=test', str_replace('localhost', '127.0.0.1', $containerInstance->getHost()), $containerInstance->getMappedPort(5432)),
31            'test',
32            'test',
33            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
34        );
35
36        $schemaName = 'ztd_' . bin2hex(random_bytes(8));
37        $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
38        $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
39
40        $table = 'prefix_' . bin2hex(random_bytes(8));
41
42        try {
43            $rawPdo->exec("CREATE TABLE {$table} (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER NOT NULL)");
44            $rawPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
45
46            $ztdPdo = ZtdPdo::fromPdo($rawPdo);
47
48            $ztdPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
49
50            $rawPdo->exec("TRUNCATE TABLE {$table}");
51            $ztdPdo->exec("TRUNCATE TABLE {$table}");
52
53            $stmt = $rawPdo->query("SELECT * FROM {$table}");
54            self::assertNotFalse($stmt);
55            /** @var list<Row> */
56            $rawRows = $stmt->fetchAll();
57
58            $stmt = $ztdPdo->query("SELECT * FROM {$table}");
59            self::assertNotFalse($stmt);
60            /** @var list<Row> */
61            $ztdRows = $stmt->fetchAll();
62
63            self::assertSame($rawRows, $ztdRows);
64        } finally {
65            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
66        }
67    }
68
69    public function testTruncateWithoutTableKeyword(): void
70    {
71        $containerInstance = \Testcontainers\Testcontainers::run(PostgreSql16Container::class);
72        /** @var PDO $rawPdo */
73        $rawPdo = new PDO(
74            sprintf('pgsql:host=%s;port=%d;dbname=test', str_replace('localhost', '127.0.0.1', $containerInstance->getHost()), $containerInstance->getMappedPort(5432)),
75            'test',
76            'test',
77            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
78        );
79
80        $schemaName = 'ztd_' . bin2hex(random_bytes(8));
81        $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
82        $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
83
84        $table = 'prefix_' . bin2hex(random_bytes(8));
85
86        try {
87            $rawPdo->exec("CREATE TABLE {$table} (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER NOT NULL)");
88            $rawPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
89
90            $ztdPdo = ZtdPdo::fromPdo($rawPdo);
91
92            $ztdPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
93
94            $rawPdo->exec("TRUNCATE {$table}");
95            $ztdPdo->exec("TRUNCATE {$table}");
96
97            $stmt = $rawPdo->query("SELECT * FROM {$table}");
98            self::assertNotFalse($stmt);
99            /** @var list<Row> */
100            $rawRows = $stmt->fetchAll();
101
102            $stmt = $ztdPdo->query("SELECT * FROM {$table}");
103            self::assertNotFalse($stmt);
104            /** @var list<Row> */
105            $ztdRows = $stmt->fetchAll();
106
107            self::assertSame($rawRows, $ztdRows);
108        } finally {
109            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
110        }
111    }
112
113    public function testTruncateDoesNotModifyPhysicalDatabase(): void
114    {
115        $containerInstance = \Testcontainers\Testcontainers::run(PostgreSql16Container::class);
116        /** @var PDO $rawPdo */
117        $rawPdo = new PDO(
118            sprintf('pgsql:host=%s;port=%d;dbname=test', str_replace('localhost', '127.0.0.1', $containerInstance->getHost()), $containerInstance->getMappedPort(5432)),
119            'test',
120            'test',
121            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
122        );
123
124        $schemaName = 'ztd_' . bin2hex(random_bytes(8));
125        $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
126        $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
127
128        $table = 'prefix_' . bin2hex(random_bytes(8));
129
130        try {
131            $rawPdo->exec("CREATE TABLE {$table} (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER NOT NULL)");
132            $rawPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
133
134            $ztdPdo = ZtdPdo::fromPdo($rawPdo);
135
136            $ztdPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
137
138            $ztdPdo->exec("TRUNCATE TABLE {$table}");
139
140            $stmt = $rawPdo->query("SELECT * FROM {$table}");
141            self::assertNotFalse($stmt);
142            /** @var list<Row> */
143            $rawRows = $stmt->fetchAll();
144
145            self::assertCount(3, $rawRows);
146        } finally {
147            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
148        }
149    }
150
151    public function testTruncateMultipleTables(): void
152    {
153        $containerInstance = \Testcontainers\Testcontainers::run(PostgreSql16Container::class);
154        /** @var PDO $rawPdo */
155        $rawPdo = new PDO(
156            sprintf('pgsql:host=%s;port=%d;dbname=test', str_replace('localhost', '127.0.0.1', $containerInstance->getHost()), $containerInstance->getMappedPort(5432)),
157            'test',
158            'test',
159            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
160        );
161
162        $schemaName = 'ztd_' . bin2hex(random_bytes(8));
163        $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
164        $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
165
166        $alpha = 'prefix_' . bin2hex(random_bytes(8));
167        $beta = 'prefix_' . bin2hex(random_bytes(8));
168
169        try {
170            $rawPdo->exec("CREATE TABLE {$alpha} (id INTEGER PRIMARY KEY, name TEXT)");
171            $rawPdo->exec("CREATE TABLE {$beta} (id INTEGER PRIMARY KEY, name TEXT)");
172            $rawPdo->exec("INSERT INTO {$alpha} VALUES (1, 'alpha')");
173            $rawPdo->exec("INSERT INTO {$beta} VALUES (2, 'beta')");
174            $ztdPdo = ZtdPdo::fromPdo($rawPdo);
175            $ztdPdo->exec("INSERT INTO {$alpha} VALUES (1, 'alpha')");
176            $ztdPdo->exec("INSERT INTO {$beta} VALUES (2, 'beta')");
177
178            $rawPdo->exec("TRUNCATE TABLE {$alpha}, {$beta}");
179            $ztdPdo->exec("TRUNCATE TABLE {$alpha}, {$beta}");
180
181            $rawAlpha = $rawPdo->query("SELECT * FROM {$alpha}");
182            $rawBeta = $rawPdo->query("SELECT * FROM {$beta}");
183            $shadowAlpha = $ztdPdo->query("SELECT * FROM {$alpha}");
184            $shadowBeta = $ztdPdo->query("SELECT * FROM {$beta}");
185            self::assertNotFalse($rawAlpha);
186            self::assertNotFalse($rawBeta);
187            self::assertNotFalse($shadowAlpha);
188            self::assertNotFalse($shadowBeta);
189            self::assertSame($rawAlpha->fetchAll(), $shadowAlpha->fetchAll());
190            self::assertSame($rawBeta->fetchAll(), $shadowBeta->fetchAll());
191        } finally {
192            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
193        }
194    }
195}
196