<?php

use classes\pdf_store;

it('falls back to local test storage when MinIO config values are empty', function (): void {
    global $MINIO;

    $previousRunApiTests = getenv('RUN_API_TESTS');
    $previousMinio = $MINIO ?? null;
    putenv('RUN_API_TESTS=1');
    $MINIO = [
        'endpoint' => null,
        'access_key' => null,
        'secret_key' => null,
    ];

    try {
        $file = 'minio-local-test-' . bin2hex(random_bytes(4)) . '.pdf';
        $store = new pdf_store();

        expect($store->createObject($file, 'local-pdf-content'))->toBeTrue();

        $path = $store->download($file);

        expect(is_file($path))->toBeTrue()
            ->and(file_get_contents($path))->toBe('local-pdf-content');
    } finally {
        if (isset($path) && is_file($path)) {
            unlink($path);
        }
        if ($previousRunApiTests === false) {
            putenv('RUN_API_TESTS');
        } else {
            putenv('RUN_API_TESTS=' . $previousRunApiTests);
        }
        $MINIO = $previousMinio;
    }
});
