packages/ztd-query-pdo-adapter/tests/Integration/PostgreSqlDriverDetectionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Integration;
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;
14use ZtdQuery\Config\ZtdConfig;
15use ZtdQuery\Platform\Postgres\PgSqlPlatform;
16
17/**
18 * @requires extension pdo_pgsql
19 * @group integration
20 * @group postgres
21 */
22#[CoversNothing]
23#[Large]
24final class PostgreSqlDriverDetectionTest extends TestCase
25{
26    public function testAutoDetectionCreatesPgSqlSession(): 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
42        try {
43            $ztdPdo = ZtdPdo::fromPdo($rawPdo);
44
45            self::assertTrue($ztdPdo->isZtdEnabled());
46        } finally {
47            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
48        }
49    }
50
51    public function testExplicitPlatformInjection(): void
52    {
53        $endpoint = \Testcontainers\Testcontainers::run(PostgreSqlRelease::fromEnvironment())->getData(Endpoint::class);
54        /** @var PDO $rawPdo */
55        $rawPdo = new PDO(
56            $endpoint->dsn(),
57            $endpoint->username,
58            $endpoint->password,
59            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
60        );
61
62        $schemaName = 'ztd_' . bin2hex(random_bytes(8));
63        $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
64        $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
65
66
67        try {
68            $platform = new PgSqlPlatform();
69            $ztdPdo = ZtdPdo::fromPdo($rawPdo, null, $platform);
70
71            self::assertTrue($ztdPdo->isZtdEnabled());
72        } finally {
73            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
74        }
75    }
76
77    public function testCustomConfigPassedToSession(): void
78    {
79        $endpoint = \Testcontainers\Testcontainers::run(PostgreSqlRelease::fromEnvironment())->getData(Endpoint::class);
80        /** @var PDO $rawPdo */
81        $rawPdo = new PDO(
82            $endpoint->dsn(),
83            $endpoint->username,
84            $endpoint->password,
85            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
86        );
87
88        $schemaName = 'ztd_' . bin2hex(random_bytes(8));
89        $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
90        $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
91
92
93        try {
94            $config = ZtdConfig::default();
95            $ztdPdo = ZtdPdo::fromPdo($rawPdo, $config);
96
97            self::assertTrue($ztdPdo->isZtdEnabled());
98        } finally {
99            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
100        }
101    }
102}
103