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\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 DropTableTest extends TestCase
24{
25    public function testDropTableRemovesFromRegistry(): 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)");
44            $rawPdo->exec("INSERT INTO {$table} (id, name) VALUES (1, 'Alice')");
45
46            $ztdPdo = ZtdPdo::fromPdo($rawPdo);
47
48            $ztdPdo->exec("INSERT INTO {$table} (id, name) VALUES (1, 'Alice')");
49
50            $stmt = $ztdPdo->query("SELECT * FROM {$table}");
51            self::assertNotFalse($stmt);
52            /** @var list<Row> */
53            $rows = $stmt->fetchAll();
54
55            self::assertCount(1, $rows);
56
57            $ztdPdo->exec("DROP TABLE {$table}");
58
59            $stmt = $rawPdo->query("SELECT * FROM {$table}");
60            self::assertNotFalse($stmt);
61            /** @var list<Row> */
62            $rawRows = $stmt->fetchAll();
63
64            self::assertCount(1, $rawRows);
65        } finally {
66            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
67        }
68    }
69
70    public function testDropTableIfExists(): void
71    {
72        $containerInstance = \Testcontainers\Testcontainers::run(PostgreSql16Container::class);
73        /** @var PDO $rawPdo */
74        $rawPdo = new PDO(
75            sprintf('pgsql:host=%s;port=%d;dbname=test', str_replace('localhost', '127.0.0.1', $containerInstance->getHost()), $containerInstance->getMappedPort(5432)),
76            'test',
77            'test',
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            $ztdPdo = ZtdPdo::fromPdo($rawPdo, null);
89
90            $ztdPdo->exec("DROP TABLE IF EXISTS {$table}");
91            self::addToAssertionCount(1);
92        } finally {
93            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
94        }
95    }
96}
97