Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
GetContainerTrait
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 getDiContainer
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10namespace Tests\AppDemo\Acceptance\Trait;
11
12use DI\Container;
13use DI\ContainerBuilder;
14use Phexium\Plugin\EventBus\Port\EventBusInterface;
15
16trait GetContainerTrait
17{
18    protected static ?Container $container = null;
19
20    protected static string $repositoryImplementation;
21
22    protected static function getDiContainer(string $repositoryImplementation): Container
23    {
24        if (!self::$container instanceof Container) {
25            // Repository implementation type : InMemory|Mysqli
26            self::$repositoryImplementation = $repositoryImplementation;
27            $_ENV['database.type'] = self::$repositoryImplementation;
28
29            // DI Container Builder
30            $containerBuilder = new ContainerBuilder();
31            $containerBuilder->useAutowiring(true);
32            // Add DI container definitions
33            $containerBuilder->addDefinitions(ROOT_DIR.'/config/demo/container.php');
34            $containerBuilder->addDefinitions(ROOT_DIR.'/config/demo/container_test.php');
35
36            // Build the container
37            self::$container = $containerBuilder->build();
38
39            // Register event subscriptions (same as in bootstrap.php)
40            $eventBus = self::$container->get(EventBusInterface::class);
41            $eventSubscriptions = require ROOT_DIR.'/config/demo/events.php';
42
43            foreach ($eventSubscriptions as $eventName => $listeners) {
44                foreach ($listeners as $listenerName) {
45                    $listener = self::$container->get($listenerName);
46                    $eventBus->subscribe($eventName, $listener);
47                }
48            }
49        }
50
51        return self::$container;
52    }
53}