test_allowed_config_anon_global()
last analyzed

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Unit\Framework\ClassResolver\GlobalInstance;
6
7
use Gacela\Framework\AbstractConfig;
8
use Gacela\Framework\AbstractFactory;
9
use Gacela\Framework\AbstractProvider;
10
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal;
11
use Gacela\Framework\Container\Container;
12
use PHPUnit\Framework\Attributes\DataProvider;
13
use PHPUnit\Framework\TestCase;
14
15
final class AnonymousGlobalTest extends TestCase
16
{
17
    /**
18
     * The anonymous class is not extending from Abstract[Factory,Config,AbstractProvider]
19
     * For this reason, the context of this anon-global will be the one of this (test)class
20
     * therefore it's not allowed.
21
     */
22
    public function test_error_when_non_allowed_anon_global_type(): void
23
    {
24
        $this->expectExceptionMessage("Type 'AnonymousGlobalTest' not allowed");
25
26
        AnonymousGlobal::addGlobal($this, new class() {});
27
    }
28
29
    public function test_allowed_factory_anon_global(): void
30
    {
31
        $this->expectNotToPerformAssertions();
32
33
        AnonymousGlobal::addGlobal($this, new class() extends AbstractFactory {});
34
    }
35
36
    public function test_allowed_config_anon_global(): void
37
    {
38
        $this->expectNotToPerformAssertions();
39
40
        AnonymousGlobal::addGlobal($this, new class() extends AbstractConfig {});
41
    }
42
43
    public function test_allowed_dependency_provider_anon_global(): void
44
    {
45
        $this->expectNotToPerformAssertions();
46
47
        AnonymousGlobal::addGlobal($this, new class() extends AbstractProvider {
48
            public function provideModuleDependencies(Container $container): void
49
            {
50
            }
51
        });
52
    }
53
54
    #[DataProvider('providerOverrideExistingResolvedClass')]
55
    public function test_override_existing_resolved_class(string $className): void
56
    {
57
        $resolvedClass = new class() {};
58
        AnonymousGlobal::overrideExistingResolvedClass($className, $resolvedClass);
59
60
        self::assertSame($resolvedClass, AnonymousGlobal::getByClassName($className));
61
    }
62
63
    public static function providerOverrideExistingResolvedClass(): iterable
64
    {
65
        yield 'using the module prefix' => [
66
            'App\Module\ModuleClassNameFacade',
67
        ];
68
69
        yield 'not using the module prefix in the class' => [
70
            'App\Module\ClassNameFacade',
71
        ];
72
73
        yield 'starting with \ and using the module prefix' => [
74
            '\App\Module\ModuleClassNameFacade',
75
        ];
76
77
        yield 'starting with \ and not using the module prefix in the class' => [
78
            '\App\Module\ClassNameFacade',
79
        ];
80
    }
81
}
82