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\Endpoint;
8use Container\PostgreSqlRelease;
9use PDO;
10use PHPUnit\Framework\Attributes\CoversNothing;
11use PHPUnit\Framework\Attributes\Large;
12use PHPUnit\Framework\TestCase;
13use ZtdQuery\Adapter\Pdo\ZtdPdo;
14
15/**
16 * @requires extension pdo_pgsql
17 * @group integration
18 * @group postgres
19 *
20 * @phpstan-type Row array<string, mixed>
21 */
22#[CoversNothing]
23#[Large]
24final class DeleteBasicTest extends TestCase
25{
26 public function testDeleteSingleRow(): void
27 {
28 $endpoint = \Testcontainers\Testcontainers::run(PostgreSqlRelease::fromEnvironment())->getData(Endpoint::class);
29 /** @var PDO $rawPdo */
30 $rawPdo = new PDO(
31 $endpoint->dsn(),
32 $endpoint->username,
33 $endpoint->password,
34 [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
35 );
36
37 $schemaName = 'ztd_' . bin2hex(random_bytes(8));
38 $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
39 $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
40
41 $table = 'prefix_' . bin2hex(random_bytes(8));
42
43 try {
44 $rawPdo->exec("CREATE TABLE {$table} (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER NOT NULL)");
45 $rawPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
46
47 $ztdPdo = ZtdPdo::fromPdo($rawPdo);
48
49 $ztdPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
50
51 $rawPdo->exec("DELETE FROM {$table} WHERE id = 1");
52 $ztdPdo->exec("DELETE FROM {$table} WHERE id = 1");
53
54 $stmt = $rawPdo->query("SELECT * FROM {$table} ORDER BY id");
55 self::assertNotFalse($stmt);
56 /** @var list<Row> */
57 $rawRows = $stmt->fetchAll();
58
59 $stmt = $ztdPdo->query("SELECT * FROM {$table} ORDER BY id");
60 self::assertNotFalse($stmt);
61 /** @var list<Row> */
62 $ztdRows = $stmt->fetchAll();
63
64 self::assertSame($rawRows, $ztdRows);
65 } finally {
66 $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
67 }
68 }
69
70 public function testDeleteAllRowsWithCondition(): void
71 {
72 $endpoint = \Testcontainers\Testcontainers::run(PostgreSqlRelease::fromEnvironment())->getData(Endpoint::class);
73 /** @var PDO $rawPdo */
74 $rawPdo = new PDO(
75 $endpoint->dsn(),
76 $endpoint->username,
77 $endpoint->password,
78 [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
79 );
80
81 $schemaName = 'ztd_' . bin2hex(random_bytes(8));
82 $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
83 $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
84
85 $table = 'prefix_' . bin2hex(random_bytes(8));
86
87 try {
88 $rawPdo->exec("CREATE TABLE {$table} (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER NOT NULL)");
89 $rawPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
90
91 $ztdPdo = ZtdPdo::fromPdo($rawPdo);
92
93 $ztdPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
94
95 $rawPdo->exec("DELETE FROM {$table} WHERE 1=1");
96 $ztdPdo->exec("DELETE FROM {$table} WHERE 1=1");
97
98 $stmt = $rawPdo->query("SELECT * FROM {$table}");
99 self::assertNotFalse($stmt);
100 /** @var list<Row> */
101 $rawRows = $stmt->fetchAll();
102
103 $stmt = $ztdPdo->query("SELECT * FROM {$table}");
104 self::assertNotFalse($stmt);
105 /** @var list<Row> */
106 $ztdRows = $stmt->fetchAll();
107
108 self::assertSame($rawRows, $ztdRows);
109 } finally {
110 $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
111 }
112 }
113
114 public function testDeleteDoesNotModifyPhysicalDatabase(): void
115 {
116 $endpoint = \Testcontainers\Testcontainers::run(PostgreSqlRelease::fromEnvironment())->getData(Endpoint::class);
117 /** @var PDO $rawPdo */
118 $rawPdo = new PDO(
119 $endpoint->dsn(),
120 $endpoint->username,
121 $endpoint->password,
122 [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
123 );
124
125 $schemaName = 'ztd_' . bin2hex(random_bytes(8));
126 $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
127 $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
128
129 $table = 'prefix_' . bin2hex(random_bytes(8));
130
131 try {
132 $rawPdo->exec("CREATE TABLE {$table} (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER NOT NULL)");
133 $rawPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
134
135 $ztdPdo = ZtdPdo::fromPdo($rawPdo);
136
137 $ztdPdo->exec("INSERT INTO {$table} (id, name, age) VALUES (1, 'Alice', 30), (2, 'Bob', 25), (3, 'Charlie', 35)");
138
139 $ztdPdo->exec("DELETE FROM {$table} WHERE id = 1");
140
141 $stmt = $rawPdo->query("SELECT * FROM {$table}");
142 self::assertNotFalse($stmt);
143 /** @var list<Row> */
144 $rawRows = $stmt->fetchAll();
145
146 self::assertCount(3, $rawRows);
147 } finally {
148 $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
149 }
150 }
151}
152