packages/ztd-query-pdo-adapter/tests/Integration/PostgreSql/DeleteBasicTest.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 DeleteBasicTest extends TestCase
24{
25 public function testDeleteSingleRow(): 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("DELETE FROM {$table} WHERE id = 1");
51 $ztdPdo->exec("DELETE FROM {$table} WHERE id = 1");
52
53 $stmt = $rawPdo->query("SELECT * FROM {$table} ORDER BY id");
54 self::assertNotFalse($stmt);
55 /** @var list<Row> */
56 $rawRows = $stmt->fetchAll();
57
58 $stmt = $ztdPdo->query("SELECT * FROM {$table} ORDER BY id");
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 testDeleteAllRowsWithCondition(): 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("DELETE FROM {$table} WHERE 1=1");
95 $ztdPdo->exec("DELETE FROM {$table} WHERE 1=1");
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 testDeleteDoesNotModifyPhysicalDatabase(): 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("DELETE FROM {$table} WHERE id = 1");
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