forked from fa/breadcrumb-the-shire
71 lines
1.9 KiB
PHP
71 lines
1.9 KiB
PHP
|
|
<?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'] ?? null;
|
||
|
|
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'] ?? null);
|
||
|
|
|
||
|
|
$harness->restore();
|
||
|
|
|
||
|
|
self::assertSame($original, $GLOBALS['minty_app_container'] ?? null);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testMultiplePushesRestoreToInitialContainer(): void
|
||
|
|
{
|
||
|
|
$original = $GLOBALS['minty_app_container'] ?? null;
|
||
|
|
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'] ?? null);
|
||
|
|
|
||
|
|
$harness->restore();
|
||
|
|
|
||
|
|
self::assertSame($original, $GLOBALS['minty_app_container'] ?? null);
|
||
|
|
}
|
||
|
|
}
|