Passed
Push — master ( 949343...ba585c )
by Jesús
03:36 queued 12s
created

AnonymousGlobalTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 70
rs 10
wmc 2

13 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$4 ➔ providerOverrideExistingResolvedClass() 0 16 1
A hp$1 ➔ test_allowed_factory_anon_global() 0 6 1
test_allowed_factory_anon_global() 0 6 ?
A hp$0 ➔ test_error_when_non_allowed_anon_global_type() 0 5 1
test_override_existing_resolved_class() 0 7 ?
A hp$3 ➔ test_allowed_dependency_provider_anon_global() 0 9 1
providerOverrideExistingResolvedClass() 0 16 ?
test_error_when_non_allowed_anon_global_type() 0 5 ?
A hp$2 ➔ test_allowed_config_anon_global() 0 6 1
test_allowed_config_anon_global() 0 6 ?
test_allowed_dependency_provider_anon_global() 0 9 ?
A hp$4 ➔ test_override_existing_resolved_class() 0 7 1
A hp$3 ➔ provideModuleDependencies() 0 2 1
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\AbstractDependencyProvider;
9
use Gacela\Framework\AbstractFactory;
10
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal;
11
use Gacela\Framework\Container\Container;
12
use PHPUnit\Framework\TestCase;
13
14
final class AnonymousGlobalTest extends TestCase
15
{
16
    /**
17
     * The anonymous class is not extending from Abstract[Factory,Config,DependencyProvider]
18
     * For this reason, the context of this anon-global will be the one of this (test)class
19
     * therefore it's not allowed.
20
     */
21
    public function test_error_when_non_allowed_anon_global_type(): void
22
    {
23
        $this->expectErrorMessage("Type 'AnonymousGlobalTest' not allowed");
24
25
        AnonymousGlobal::addGlobal($this, new class () {
26
        });
27
    }
28
29
    public function test_allowed_factory_anon_global(): void
30
    {
31
        AnonymousGlobal::addGlobal($this, new class () extends AbstractFactory {
32
        });
33
34
        self::assertTrue(true); # Assert non error is thrown
35
    }
36
37
    public function test_allowed_config_anon_global(): void
38
    {
39
        AnonymousGlobal::addGlobal($this, new class () extends AbstractConfig {
40
        });
41
42
        self::assertTrue(true); # Assert non error is thrown
43
    }
44
45
    public function test_allowed_dependency_provider_anon_global(): void
46
    {
47
        AnonymousGlobal::addGlobal($this, new class () extends AbstractDependencyProvider {
48
            public function provideModuleDependencies(Container $container): void
49
            {
50
            }
51
        });
52
53
        self::assertTrue(true); # Assert non error is thrown
54
    }
55
56
    /**
57
     * @dataProvider providerOverrideExistingResolvedClass
58
     */
59
    public function test_override_existing_resolved_class(string $className): void
60
    {
61
        $resolvedClass = new class () {
62
        };
63
        AnonymousGlobal::overrideExistingResolvedClass($className, $resolvedClass);
64
65
        self::assertSame($resolvedClass, AnonymousGlobal::getByClassName($className));
66
    }
67
68
    public function providerOverrideExistingResolvedClass(): iterable
69
    {
70
        yield 'using the module prefix' => [
71
            'App\Module\ModuleClassNameFacade',
72
        ];
73
74
        yield 'not using the module prefix in the class' => [
75
            'App\Module\ClassNameFacade',
76
        ];
77
78
        yield 'starting with \ and using the module prefix' => [
79
            '\App\Module\ModuleClassNameFacade',
80
        ];
81
82
        yield 'starting with \ and not using the module prefix in the class' => [
83
            '\App\Module\ClassNameFacade',
84
        ];
85
    }
86
}
87