packages/ztd-query-pdo-adapter/tests/Integration/PostgreSql/DropTableTest.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 DropTableTest extends TestCase
25{
26    public function testDropTableRemovesFromRegistry(): 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)");
45            $rawPdo->exec("INSERT INTO {$table} (id, name) VALUES (1, 'Alice')");
46
47            $ztdPdo = ZtdPdo::fromPdo($rawPdo);
48
49            $ztdPdo->exec("INSERT INTO {$table} (id, name) VALUES (1, 'Alice')");
50
51            $stmt = $ztdPdo->query("SELECT * FROM {$table}");
52            self::assertNotFalse($stmt);
53            /** @var list<Row> */
54            $rows = $stmt->fetchAll();
55
56            self::assertCount(1, $rows);
57
58            $ztdPdo->exec("DROP TABLE {$table}");
59
60            $stmt = $rawPdo->query("SELECT * FROM {$table}");
61            self::assertNotFalse($stmt);
62            /** @var list<Row> */
63            $rawRows = $stmt->fetchAll();
64
65            self::assertCount(1, $rawRows);
66        } finally {
67            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
68        }
69    }
70
71    public function testDropTableIfExists(): void
72    {
73        $endpoint = \Testcontainers\Testcontainers::run(PostgreSqlRelease::fromEnvironment())->getData(Endpoint::class);
74        /** @var PDO $rawPdo */
75        $rawPdo = new PDO(
76            $endpoint->dsn(),
77            $endpoint->username,
78            $endpoint->password,
79            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
80        );
81
82        $schemaName = 'ztd_' . bin2hex(random_bytes(8));
83        $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
84        $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
85
86        $table = 'prefix_' . bin2hex(random_bytes(8));
87
88        try {
89            $ztdPdo = ZtdPdo::fromPdo($rawPdo, null);
90
91            $ztdPdo->exec("DROP TABLE IF EXISTS {$table}");
92            self::addToAssertionCount(1);
93        } finally {
94            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
95        }
96    }
97}
98