<?php

namespace MintyPHP\Tests\App;

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

final class AppContainerTest extends TestCase
{
    public function testOverwritingBindingIsAllowedBeforeProtection(): void
    {
        $container = new AppContainer();
        $container->set('service.test', static fn (): string => 'first');
        $container->set('service.test', static fn (): string => 'second');

        self::assertSame('second', $container->get('service.test'));
    }

    public function testOverwritingExistingBindingIsBlockedAfterProtection(): void
    {
        $container = new AppContainer();
        $container->set('service.test', static fn (): string => 'first');
        $container->protectExistingBindings();

        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Refusing to overwrite existing service binding: service.test');
        $container->set('service.test', static fn (): string => 'second');
    }

    public function testAddingNewBindingStillWorksAfterProtection(): void
    {
        $container = new AppContainer();
        $container->set('service.first', static fn (): string => 'first');
        $container->protectExistingBindings();
        $container->set('service.second', static fn (): string => 'second');

        self::assertSame('second', $container->get('service.second'));
    }
}
