Passed
Push — main ( 60c49f...544d0e )
by Chema
06:20 queued 04:56
created

GacelaPhpFileMergeTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Integration\Framework\Bootstrap\Setup;
6
7
use Gacela\Framework\Bootstrap\GacelaConfig;
8
use Gacela\Framework\Container\Container;
9
use Gacela\Framework\Gacela;
10
use PHPUnit\Framework\TestCase;
11
use ReflectionClass;
12
use stdClass;
13
14
final class GacelaPhpFileMergeTest extends TestCase
15
{
16
    private string $tempDir;
17
18
    protected function setUp(): void
19
    {
20
        $this->tempDir = sys_get_temp_dir() . '/gacela_merge_test_' . uniqid();
21
        mkdir($this->tempDir, 0777, true);
22
    }
23
24
    protected function tearDown(): void
25
    {
26
        if (is_dir($this->tempDir)) {
27
            $this->removeDirectory($this->tempDir);
28
        }
29
30
        $reflection = new ReflectionClass(Gacela::class);
31
        $method = $reflection->getMethod('resetCache');
32
        $method->invoke(null);
33
    }
34
35
    public function test_factories_from_gacela_php_file_survive_merge(): void
36
    {
37
        $gacelaPhpContent = <<<'PHP'
38
<?php
39
use Gacela\Framework\Bootstrap\GacelaConfig;
40
41
return static function (GacelaConfig $config): void {
42
    $config->addFactory('test-factory', static fn (): \stdClass => new \stdClass());
43
};
44
PHP;
45
46
        file_put_contents($this->tempDir . '/gacela.php', $gacelaPhpContent);
47
48
        Gacela::bootstrap($this->tempDir);
49
50
        $container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance());
51
52
        $instance1 = $container->get('test-factory');
53
        $instance2 = $container->get('test-factory');
54
55
        self::assertInstanceOf(stdClass::class, $instance1);
56
        self::assertInstanceOf(stdClass::class, $instance2);
57
        self::assertNotSame($instance1, $instance2, 'Factory should return new instances');
58
    }
59
60
    public function test_protected_services_from_gacela_php_file_survive_merge(): void
61
    {
62
        $callable = static fn (): string => 'protected-value';
63
64
        Gacela::bootstrap($this->tempDir, static function (GacelaConfig $config) use ($callable): void {
65
            $config->addProtected('test-protected', $callable);
66
        });
67
68
        $container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance());
69
70
        $result = $container->get('test-protected');
71
72
        self::assertSame($callable, $result, 'Protected service should return the closure itself');
73
    }
74
75
    public function test_aliases_from_gacela_php_file_survive_merge(): void
76
    {
77
        Gacela::bootstrap($this->tempDir, static function (GacelaConfig $config): void {
78
            $config->addFactory('original-service', static fn (): stdClass => new stdClass());
79
            $config->addAlias('my-alias', 'original-service');
80
        });
81
82
        $container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance());
83
84
        $instance = $container->get('my-alias');
85
86
        self::assertInstanceOf(stdClass::class, $instance, 'Alias should resolve to factory service');
87
    }
88
89
    public function test_all_container_features_from_gacela_php_file_survive_merge(): void
90
    {
91
        $protectedCallable = static fn (): string => 'protected';
92
93
        Gacela::bootstrap($this->tempDir, static function (GacelaConfig $config) use ($protectedCallable): void {
94
            $config->addFactory('my-factory', static fn (): stdClass => new stdClass());
95
            $config->addProtected('my-protected', $protectedCallable);
96
            $config->addAlias('my-alias', 'my-factory');
97
        });
98
99
        $container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance());
100
101
        // Test factory
102
        $factory1 = $container->get('my-factory');
103
        $factory2 = $container->get('my-factory');
104
        self::assertNotSame($factory1, $factory2);
105
106
        // Test protected
107
        $protected = $container->get('my-protected');
108
        self::assertSame($protectedCallable, $protected);
109
110
        // Test alias
111
        $aliased = $container->get('my-alias');
112
        self::assertInstanceOf(stdClass::class, $aliased);
113
    }
114
115
    private function removeDirectory(string $dir): void
116
    {
117
        if (!is_dir($dir)) {
118
            return;
119
        }
120
121
        $files = array_diff(scandir($dir), ['.', '..']);
122
        foreach ($files as $file) {
123
            $path = $dir . '/' . $file;
124
            is_dir($path) ? $this->removeDirectory($path) : unlink($path);
125
        }
126
127
        rmdir($dir);
128
    }
129
}
130