<?php

namespace MintyPHP\Tests\Support;

use MintyPHP\App\AppContainer;
use PHPUnit\Framework\TestCase;

final class AppContainerIsolationTraitTest extends TestCase
{
    public function testPushAndRestoreRoundTripKeepsOriginalGlobalContainer(): void
    {
        $original = $GLOBALS['minty_app_container'];
        self::assertInstanceOf(AppContainer::class, $original);

        $harness = new class () {
            use AppContainerIsolationTrait;

            public function push(AppContainer $container): void
            {
                $this->pushAppContainer($container);
            }

            public function restore(): void
            {
                $this->restoreAppContainer();
            }
        };

        $replacement = new AppContainer();
        $harness->push($replacement);

        self::assertSame($replacement, $GLOBALS['minty_app_container']);

        $harness->restore();

        self::assertSame($original, $GLOBALS['minty_app_container']);
    }

    public function testMultiplePushesRestoreToInitialContainer(): void
    {
        $original = $GLOBALS['minty_app_container'];
        self::assertInstanceOf(AppContainer::class, $original);

        $harness = new class () {
            use AppContainerIsolationTrait;

            public function push(AppContainer $container): void
            {
                $this->pushAppContainer($container);
            }

            public function restore(): void
            {
                $this->restoreAppContainer();
            }
        };

        $first = new AppContainer();
        $second = new AppContainer();

        $harness->push($first);
        $harness->push($second);

        self::assertSame($second, $GLOBALS['minty_app_container']);

        $harness->restore();

        self::assertSame($original, $GLOBALS['minty_app_container']);
    }
}
