<?php

declare(strict_types=1);

namespace RainbowDrive\Tests\Unit;

use PHPUnit\Framework\TestCase;
use RainbowDrive\Support\Psr4Autoloader;

final class Psr4AutoloaderTest extends TestCase
{
    public function testLoadsClassFromRegisteredNamespace(): void
    {
        $tmpDir = sys_get_temp_dir() . '/rd_psr4_' . bin2hex(random_bytes(8));
        mkdir($tmpDir, 0777, true);
        mkdir($tmpDir . '/Foo', 0777, true);

        $classFile = $tmpDir . '/Foo/Bar.php';
        file_put_contents(
            $classFile,
            "<?php\n\ndeclare(strict_types=1);\n\nnamespace Tmp\\\\Foo;\n\nfinal class Bar\n{\n    public static function ping(): string\n    {\n        return 'pong';\n    }\n}\n"
        );

        $loader = new Psr4Autoloader();
        $loader->addNamespace('Tmp\\', $tmpDir);
        $loader->register();

        self::assertTrue(class_exists('Tmp\\Foo\\Bar'));
        self::assertSame('pong', \Tmp\Foo\Bar::ping());
    }
}

