<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

/**
 * Contract: Direct writes to $GLOBALS['minty_app_container'] are only allowed
 * inside the dedicated AppContainerIsolationTrait.
 */
final class AppContainerIsolationContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

    public function testDirectAppContainerGlobalWritesAreRestrictedToIsolationTrait(): void
    {
        $violations = array_merge(
            $this->findPatternMatchesInPhpFiles('tests', '/\\$GLOBALS\\[\'minty_app_container\'\\]\\s*=/'),
            $this->findPatternMatchesInPhpFiles('tests', '/unset\\(\\$GLOBALS\\[\'minty_app_container\'\\]\\)/')
        );

        $allowed = [
            'tests/Support/AppContainerIsolationTrait.php',
        ];

        $violations = array_values(array_filter(
            array_unique($violations),
            static fn (string $path): bool => !in_array($path, $allowed, true)
        ));
        sort($violations);

        self::assertSame([], $violations, "Direct global app-container writes found outside isolation trait:\n" . implode("\n", $violations));
    }
}
