packages/ztd-query-mysqli-adapter/tests/Unit/Native/MysqliPropertyReaderTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Native;
6
7use Container\Endpoint;
8use Container\MySqlRelease;
9use mysqli;
10use PHPUnit\Framework\Attributes\CoversClass;
11use PHPUnit\Framework\Attributes\Large;
12use PHPUnit\Framework\TestCase;
13use Testcontainers\Testcontainers;
14use ZtdQuery\Adapter\Mysqli\Native\MysqliPropertyReader;
15
16#[CoversClass(MysqliPropertyReader::class)]
17#[Large]
18final class MysqliPropertyReaderTest extends TestCase
19{
20    public function testReadReturnsNativePropertiesAndNullForUnknownNames(): void
21    {
22        $container = Testcontainers::run(MySqlRelease::fromEnvironment());
23        $endpoint = $container->getData(Endpoint::class);
24        try {
25            $connection = new mysqli($endpoint->host, $endpoint->username, $endpoint->password, $endpoint->database, $endpoint->port);
26            $connection->set_charset('utf8mb4');
27            $reader = new MysqliPropertyReader();
28            self::assertSame($connection->affected_rows, $reader->read($connection, 'affected_rows'));
29            self::assertSame($connection->client_info, $reader->read($connection, 'client_info'));
30            self::assertSame($connection->client_version, $reader->read($connection, 'client_version'));
31            self::assertSame($connection->connect_errno, $reader->read($connection, 'connect_errno'));
32            self::assertSame($connection->connect_error, $reader->read($connection, 'connect_error'));
33            self::assertSame($connection->errno, $reader->read($connection, 'errno'));
34            self::assertSame($connection->error, $reader->read($connection, 'error'));
35            self::assertSame($connection->error_list, $reader->read($connection, 'error_list'));
36            self::assertSame($connection->field_count, $reader->read($connection, 'field_count'));
37            self::assertSame($connection->host_info, $reader->read($connection, 'host_info'));
38            self::assertSame($connection->info, $reader->read($connection, 'info'));
39            self::assertSame($connection->insert_id, $reader->read($connection, 'insert_id'));
40            self::assertSame($connection->server_info, $reader->read($connection, 'server_info'));
41            self::assertSame($connection->server_version, $reader->read($connection, 'server_version'));
42            self::assertSame($connection->sqlstate, $reader->read($connection, 'sqlstate'));
43            self::assertSame($connection->protocol_version, $reader->read($connection, 'protocol_version'));
44            self::assertSame($connection->thread_id, $reader->read($connection, 'thread_id'));
45            self::assertSame($connection->warning_count, $reader->read($connection, 'warning_count'));
46            self::assertNull($reader->read($connection, 'unsupported'));
47            $connection->close();
48        } finally {
49            $container->stop();
50        }
51    }
52
53    public function testReadPreservesFailedConnectionDetails(): void
54    {
55        $container = Testcontainers::run(MySqlRelease::fromEnvironment());
56        $endpoint = $container->getData(Endpoint::class);
57        try {
58            mysqli_report(MYSQLI_REPORT_OFF);
59            try {
60                $connection = new mysqli($endpoint->host, 'missing_' . bin2hex(random_bytes(8)), 'invalid', $endpoint->database, $endpoint->port);
61                $reader = new MysqliPropertyReader();
62                $error = $connection->connect_error;
63                self::assertIsString($error);
64                self::assertNotSame('', $error);
65                self::assertGreaterThan(0, $connection->connect_errno);
66                self::assertSame($connection->connect_errno, $reader->read($connection, 'connect_errno'));
67                self::assertSame($error, $reader->read($connection, 'connect_error'));
68            } finally {
69                mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
70            }
71        } finally {
72            $container->stop();
73        }
74    }
75
76    public function testReadPreservesInformationFromMultipleWrites(): void
77    {
78        $container = Testcontainers::run(MySqlRelease::fromEnvironment());
79        $endpoint = $container->getData(Endpoint::class);
80        try {
81            $connection = new mysqli($endpoint->host, $endpoint->username, $endpoint->password, $endpoint->database, $endpoint->port);
82            $connection->set_charset('utf8mb4');
83            $connection->query('CREATE TEMPORARY TABLE info_rows (id INT PRIMARY KEY)');
84            $connection->query('INSERT INTO info_rows VALUES (1), (2)');
85            $reader = new MysqliPropertyReader();
86            $info = $connection->info;
87            self::assertIsString($info);
88            self::assertNotSame('', $info);
89            self::assertSame($info, $reader->read($connection, 'info'));
90            self::assertSame(2, $reader->read($connection, 'affected_rows'));
91            $connection->close();
92        } finally {
93            $container->stop();
94        }
95    }
96
97}
98