packages/ztd-query-mysqli-adapter/fuzz/Container/MysqliConnector.php

1<?php
2
3declare(strict_types=1);
4
5namespace Fuzz\Container;
6
7use Container\MySql80Container;
8use Container\MySql84Container;
9use RuntimeException;
10use Testcontainers\Testcontainers;
11
12/**
13 * Resolves a disposable MySQL service for a versioned fuzz campaign.
14 */
15final class MysqliConnector
16{
17    /**
18     * Start a pinned container and return its mapped endpoint.
19     *
20     * @return array{string, int}
21     * @throws RuntimeException If the requested server version or port is invalid.
22     */
23    public static function endpoint(): array
24    {
25        $version = getenv('MYSQL_VERSION');
26        $container = match ($version === false ? '8.0.44' : $version) {
27            '8.0.44' => MySql80Container::class,
28            '8.4.7' => MySql84Container::class,
29            default => throw new RuntimeException('Unsupported MYSQL_VERSION.'),
30        };
31        $instance = Testcontainers::run($container);
32        return [str_replace('localhost', '127.0.0.1', $instance->getHost()), $instance->getMappedPort(3306) ?? throw new RuntimeException('MySQL port was not mapped.')];
33    }
34}
35