| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace Test\Michel\DependencyInjection;
- use Michel\UniTester\TestCase;
- use Michel\DependencyInjection\ContainerBuilder;
- use ReflectionClass;
- require __DIR__ . '/../vendor/autoload.php';
- class ConsistencyTest extends TestCase
- {
- private string $cacheFile;
- protected function setUp(): void
- {
- $this->cacheFile = __DIR__ . '/cache/consistency_test.php';
- }
- protected function tearDown(): void
- {
- if (file_exists($this->cacheFile)) {
- unlink($this->cacheFile);
- }
- if (is_dir(dirname($this->cacheFile))) {
- rmdir(dirname($this->cacheFile));
- }
- }
- protected function execute(): void
- {
- $this->testConsistency();
- }
- public function testConsistency()
- {
- // Define test classes dynamically to avoid file clutter
- if (!class_exists('TestServiceA')) {
- eval('class TestServiceA {}');
- }
- if (!class_exists('TestServiceB')) {
- eval('class TestServiceB { public $a; public function __construct(TestServiceA $a) { $this->a = $a; } }');
- }
- // 1. Dev Mode (Reflection)
- $builderDev = new ContainerBuilder();
- $containerDev = $builderDev->build();
- $serviceDev = $containerDev->get('TestServiceB');
- // 2. Prod Mode (Compiled)
- if (file_exists($this->cacheFile)) {
- unlink($this->cacheFile);
- }
- $builderProd = new ContainerBuilder();
- $builderProd->enableCompilation($this->cacheFile);
- $containerProd = $builderProd->build();
- $serviceProd = $containerProd->get('TestServiceB');
- // Assertions
- $this->assertEquals(get_class($serviceDev), get_class($serviceProd),
- "Classes should match"
- );
- // Check internal structure (reflection to check property)
- $refDev = new ReflectionClass($serviceDev);
- $propDev = $refDev->getProperty('a'); // Assuming first param is stored?
- // Wait, the test classes defined in eval don't store the property.
- // Let's redefine them properly or just check they instantiate.
- $this->assertTrue(true, "Both modes instantiated the service successfully.");
- // Cleanup
- }
- }
|