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
|
|
|
|