packages/ztd-query-pdo-adapter/tests/Integration/MySql/UpdateEmptyStringTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Integration\MySql;
6
7use Container\MySql80Container;
8use Container\MySql84Container;
9use PDO;
10use PHPUnit\Framework\Attributes\CoversNothing;
11use PHPUnit\Framework\Attributes\Large;
12use PHPUnit\Framework\TestCase;
13use ZtdQuery\Adapter\Pdo\ZtdPdo;
14
15#[CoversNothing]
16#[Large]
17final class UpdateEmptyStringTest extends TestCase
18{
19    public function testUpdateReplacesExistingTextWithEmptyString(): void
20    {
21        $containerInstance = \Testcontainers\Testcontainers::run(getenv('MYSQL_VERSION') === '8.4.7' ? MySql84Container::class : MySql80Container::class);
22        /** @var PDO $rawPdo */
23        $rawPdo = new PDO(
24            sprintf('mysql:host=%s;port=%d;dbname=test;charset=utf8mb4', str_replace('localhost', '127.0.0.1', $containerInstance->getHost()), $containerInstance->getMappedPort(3306)),
25            'root',
26            'root',
27            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
28        );
29
30        $databaseName = 'ztd_' . bin2hex(random_bytes(8));
31        $rawPdo->exec(sprintf('CREATE DATABASE `%s` CHARACTER SET utf8mb4', $databaseName));
32        $rawPdo->exec(sprintf('USE `%s`', $databaseName));
33
34        $table = 'prefix_' . bin2hex(random_bytes(8));
35
36        try {
37            $rawPdo->exec("CREATE TABLE `{$table}` (id INT PRIMARY KEY, name VARCHAR(100), notes TEXT)");
38            $ztdPdo = ZtdPdo::fromPdo($rawPdo);
39            $ztdPdo->exec("INSERT INTO `{$table}` VALUES (1, 'Alice', 'some notes')");
40
41            self::assertSame(1, $ztdPdo->exec("UPDATE `{$table}` SET notes = '' WHERE name = 'Alice'"));
42
43            $statement = $ztdPdo->query("SELECT notes FROM `{$table}` WHERE id = 1");
44            self::assertNotFalse($statement);
45            self::assertSame('', $statement->fetchColumn());
46
47            $physical = $rawPdo->query("SELECT notes FROM `{$table}`");
48            self::assertNotFalse($physical);
49            self::assertSame([], $physical->fetchAll(PDO::FETCH_COLUMN));
50        } finally {
51            $rawPdo->exec(sprintf('DROP DATABASE IF EXISTS `%s`', $databaseName));
52        }
53    }
54}
55