53 lines
1.6 KiB
PHP
53 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MeshVault\Tests\Unit\Console;
|
|
|
|
use MeshVault\Console\ConsoleServices;
|
|
use MeshVault\Console\LazyConsoleServices;
|
|
use PHPUnit\Framework\TestCase;
|
|
use RuntimeException;
|
|
|
|
final class ConsoleServicesWiringTest extends TestCase
|
|
{
|
|
public function testLazyServicesDoNotBuildTheEngineUntilUsed(): void
|
|
{
|
|
$built = 0;
|
|
$services = new LazyConsoleServices(function () use (&$built): ConsoleServices {
|
|
$built++;
|
|
return new FakeConsoleServices(importResult: 9);
|
|
});
|
|
|
|
self::assertSame(0, $built, 'Construction alone must not touch configuration.');
|
|
self::assertSame(9, $services->import('/srv/in', ['stl']));
|
|
self::assertSame(1, $built);
|
|
}
|
|
|
|
public function testLazyServicesBuildTheEngineOnlyOnce(): void
|
|
{
|
|
$built = 0;
|
|
$delegate = new FakeConsoleServices(jobs: [1, 2]);
|
|
$services = new LazyConsoleServices(function () use (&$built, $delegate): ConsoleServices {
|
|
$built++;
|
|
return $delegate;
|
|
});
|
|
|
|
$services->runWorkerOnce(static fn (): bool => false);
|
|
$services->runWorkerOnce(static fn (): bool => false);
|
|
|
|
self::assertSame(1, $built);
|
|
self::assertSame(2, $delegate->runs);
|
|
}
|
|
|
|
public function testLazyServicesPropagateConstructionFailures(): void
|
|
{
|
|
$services = new LazyConsoleServices(static function (): ConsoleServices {
|
|
throw new RuntimeException('bad configuration');
|
|
});
|
|
|
|
$this->expectException(RuntimeException::class);
|
|
$services->runWorkerOnce(static fn (): bool => false);
|
|
}
|
|
}
|