<?php
// tests/_support/bootstrap.php

if (! getenv('CI_ENVIRONMENT')) {
    putenv('CI_ENVIRONMENT=testing');
    $_ENV['CI_ENVIRONMENT'] = 'testing';
    $_SERVER['CI_ENVIRONMENT'] = 'testing';
}

$writablePath = realpath(__DIR__ . '/../..') . DIRECTORY_SEPARATOR . 'writable';
$runtimeDirectories = [
    $writablePath . DIRECTORY_SEPARATOR . 'cache',
    $writablePath . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'testing',
    $writablePath . DIRECTORY_SEPARATOR . 'logs',
    $writablePath . DIRECTORY_SEPARATOR . 'session',
    $writablePath . DIRECTORY_SEPARATOR . 'uploads',
    $writablePath . DIRECTORY_SEPARATOR . 'debugbar',
];

foreach ($runtimeDirectories as $directory) {
    if (! is_dir($directory) && ! mkdir($directory, 0775, true) && ! is_dir($directory)) {
        fwrite(STDERR, "Unable to create writable test directory: {$directory}" . PHP_EOL);
        exit(1);
    }

    if (! is_writable($directory)) {
        fwrite(STDERR, "Writable test directory is not writable: {$directory}" . PHP_EOL);
        exit(1);
    }
}

// Load Composer autoloader
require __DIR__ . '/../../vendor/autoload.php';

// Load CodeIgniter testing bootstrap from Composer package
require __DIR__ . '/../../vendor/codeigniter4/framework/system/Test/bootstrap.php';
