packages/ztd-query-pdo-adapter/tests/Integration/PostgreSql/PreparedExecutionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Integration\PostgreSql;
6
7use Container\Endpoint;
8use Container\PostgreSqlRelease;
9use PDO;
10use PDOStatement;
11use PHPUnit\Framework\Attributes\CoversNothing;
12use PHPUnit\Framework\Attributes\Large;
13use PHPUnit\Framework\TestCase;
14use ZtdQuery\Adapter\Pdo\ZtdPdo;
15
16#[CoversNothing]
17#[Large]
18final class PreparedExecutionTest extends TestCase
19{
20    public function testNativePositionsRemainBoundAcrossExpressionsAndMutations(): void
21    {
22        $endpoint = \Testcontainers\Testcontainers::run(PostgreSqlRelease::fromEnvironment())->getData(Endpoint::class);
23        /** @var PDO $rawPdo */
24        $rawPdo = new PDO(
25            $endpoint->dsn(),
26            $endpoint->username,
27            $endpoint->password,
28            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
29        );
30
31        $schemaName = 'ztd_' . bin2hex(random_bytes(8));
32        $rawPdo->exec(sprintf('CREATE SCHEMA "%s"', $schemaName));
33        $rawPdo->exec(sprintf('SET search_path TO "%s"', $schemaName));
34
35        $products = 'products_' . bin2hex(random_bytes(8));
36        $categories = 'categories_' . bin2hex(random_bytes(8));
37
38        try {
39            $rawPdo->exec("CREATE TABLE {$categories} (category_id INTEGER PRIMARY KEY, name TEXT)");
40            $rawPdo->exec("CREATE TABLE {$products} (id INTEGER PRIMARY KEY, category_id INTEGER, name TEXT, price INTEGER, status TEXT)");
41            $ztdPdo = ZtdPdo::fromPdo($rawPdo);
42
43            $insertCategory = $ztdPdo->prepare("INSERT INTO {$categories} VALUES (\$1, \$2)");
44            self::assertInstanceOf(PDOStatement::class, $insertCategory);
45            self::assertTrue($insertCategory->execute([1, 'tools']));
46
47            $insertProduct = $ztdPdo->prepare("INSERT INTO {$products} VALUES (\$1, \$2, \$3, \$4, \$5)");
48            self::assertInstanceOf(PDOStatement::class, $insertProduct);
49            self::assertTrue($insertProduct->execute([1, 1, 'Hammer', 20, 'active']));
50            self::assertTrue($insertProduct->execute([2, 1, 'Saw', 40, 'active']));
51
52            $aggregate = $ztdPdo->prepare("SELECT category_id, SUM(price) FILTER (WHERE status = \$1) AS total FROM {$products} GROUP BY category_id HAVING SUM(price) > \$2");
53            self::assertInstanceOf(PDOStatement::class, $aggregate);
54            self::assertTrue($aggregate->execute(['active', 50]));
55            self::assertSame(60, $aggregate->fetchColumn(1));
56
57            $joined = $ztdPdo->prepare("SELECT p.name FROM {$products} p JOIN {$categories} c USING (category_id) WHERE p.price BETWEEN \$1 AND \$2 ORDER BY p.id");
58            self::assertInstanceOf(PDOStatement::class, $joined);
59            self::assertTrue($joined->execute([10, 30]));
60            self::assertSame(['Hammer'], $joined->fetchAll(PDO::FETCH_COLUMN));
61
62            $update = $ztdPdo->prepare("UPDATE {$products} SET status = CASE WHEN price >= \$1 THEN \$2 ELSE status END WHERE id IN (\$3, \$4)");
63            self::assertInstanceOf(PDOStatement::class, $update);
64            self::assertTrue($update->execute([30, 'premium', 1, 2]));
65
66            $current = $ztdPdo->prepare("SELECT status FROM {$products} WHERE id = \$1");
67            self::assertInstanceOf(PDOStatement::class, $current);
68            self::assertTrue($current->execute([2]));
69            self::assertSame('premium', $current->fetchColumn());
70        } finally {
71            $rawPdo->exec(sprintf('DROP SCHEMA IF EXISTS "%s" CASCADE', $schemaName));
72        }
73    }
74}
75