#!/usr/bin/env php8.1
<?php

$command = 'docker ps -q -f name=api-resources-test-mysql';
$containerId = shell_exec($command);

if (!$containerId) {
    echo 'Start mysql docker container' . PHP_EOL;
    exec('docker-compose up -d');

    $trials = 1;
    while (true) {
        $result = shell_exec('phinx migrate');

        if (!preg_match('/(PDOExceptionInvalidArgument|Exception)/', $result)) {
            break;
        }

        echo 'Versuch ' . $trials . ' to start mysql' . PHP_EOL;

        if ($trials > 10) {
            echo 'Can\'t start mysql server: ' . $result . PHP_EOL;
            exit;
        }

        $trials++;
        sleep(2);
    }
}

include '_phpunit';
