<?php
declare(strict_types=1);

namespace Apexl\Vault\Tests;

use Apexl\Vault\Operators\Pdo;
use PDO as PdoConnection;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use stdClass;

/**
 * Regression coverage for executeRaw() reading the typed property
 * $statementError before it was ever assigned. Under PHP 8.4 an uninitialized
 * typed property access throws `Error: ... must not be accessed before
 * initialization` — execute() stopped populating $statementError once error
 * handling moved to PDO::ERRMODE_EXCEPTION (mysql::makeConnection()), leaving
 * the debug-log read in executeRaw() dangling. Uses a real sqlite::memory:
 * PDO connection (no MySQL/DB server needed) so this exercises the actual
 * execute() -> executeRaw() path end to end.
 */
final class PdoExecuteRawTest extends TestCase
{
    private function driver(): Pdo
    {
        return new class(new stdClass()) extends Pdo {
            protected function makeConnection(): PdoConnection
            {
                return new PdoConnection('sqlite::memory:');
            }
        };
    }

    #[Test]
    public function execute_raw_does_not_touch_uninitialized_statement_error(): void
    {
        $result = $this->driver()->executeRaw('SELECT 1');

        self::assertSame([true, null], $result);
    }

    #[Test]
    public function statement_error_is_populated_after_a_successful_execute(): void
    {
        $drv = $this->driver();
        $drv->executeRaw('SELECT 1');

        self::assertTrue(isset($drv->statementError));
        self::assertSame('00000', $drv->statementError[0]);
    }
}
