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