packages/ztd-query-pdo-adapter/tests/Integration/PostgreSql/SelectQualifiedTableTest.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#[CoversNothing]
21#[Large]
22final class SelectQualifiedTableTest extends TestCase
23{
24 public function testSchemaQualifiedTableReadsShadowRows(): void
25 {
26 $endpoint = \Testcontainers\Testcontainers::run(PostgreSqlRelease::fromEnvironment())->getData(Endpoint::class);
27 /** @var PDO $rawPdo */
28 $rawPdo = new PDO(
29 $endpoint->dsn(),
30 $endpoint->username,
31 $endpoint->password,
32 [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
33 );
34
35 $schemaName = 'ztd_' . bin2hex(random_bytes(8));
36 $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
37 $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
38
39
40 try {
41 $rawPdo->exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)');
42 $ztdPdo = ZtdPdo::fromPdo($rawPdo);
43 $ztdPdo->exec("INSERT INTO users (id, name) VALUES (1, 'Alice')");
44
45 $statement = $ztdPdo->query(sprintf('SELECT name FROM "%s".users WHERE id = 1', $schemaName));
46
47 self::assertNotFalse($statement);
48 self::assertSame([['name' => 'Alice']], $statement->fetchAll());
49 } finally {
50 $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
51 }
52 }
53}
54