|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GacelaTest\Integration\Framework\Container; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Gacela\Framework\Bootstrap\GacelaConfig; |
|
9
|
|
|
use Gacela\Framework\Container\Container; |
|
10
|
|
|
use Gacela\Framework\Gacela; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
use ReflectionClass; |
|
13
|
|
|
|
|
14
|
|
|
final class ProtectedServicesTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
protected function tearDown(): void |
|
17
|
|
|
{ |
|
18
|
|
|
$reflection = new ReflectionClass(Gacela::class); |
|
19
|
|
|
$method = $reflection->getMethod('resetCache'); |
|
20
|
|
|
$method->invoke(null); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function test_protected_service_returns_closure_not_invoked(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$callable = static fn (): string => 'value'; |
|
26
|
|
|
|
|
27
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config) use ($callable): void { |
|
28
|
|
|
$config->addProtected('test-service', $callable); |
|
29
|
|
|
}); |
|
30
|
|
|
|
|
31
|
|
|
$container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance()); |
|
32
|
|
|
|
|
33
|
|
|
$result = $container->get('test-service'); |
|
34
|
|
|
|
|
35
|
|
|
self::assertSame($callable, $result, 'Protected service should return the closure itself'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function test_protected_service_cannot_be_extended(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->expectException(\Gacela\Container\Exception\ContainerException::class); |
|
41
|
|
|
$this->expectExceptionMessage("The instance 'test-service' is protected and cannot be extended"); |
|
42
|
|
|
|
|
43
|
|
|
$callable = static fn (): string => 'original'; |
|
44
|
|
|
|
|
45
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config) use ($callable): void { |
|
46
|
|
|
$config->addProtected('test-service', $callable); |
|
47
|
|
|
$config->extendService('test-service', static fn ($c, $previous): Closure => static fn (): string => 'extended'); |
|
|
|
|
|
|
48
|
|
|
}); |
|
49
|
|
|
|
|
50
|
|
|
Container::withConfig(\Gacela\Framework\Config\Config::getInstance()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function test_multiple_protected_services(): void |
|
54
|
|
|
{ |
|
55
|
|
|
$callableA = static fn (): string => 'A'; |
|
56
|
|
|
$callableB = static fn (): string => 'B'; |
|
57
|
|
|
|
|
58
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config) use ($callableA, $callableB): void { |
|
59
|
|
|
$config->addProtected('service-a', $callableA); |
|
60
|
|
|
$config->addProtected('service-b', $callableB); |
|
61
|
|
|
}); |
|
62
|
|
|
|
|
63
|
|
|
$container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance()); |
|
64
|
|
|
|
|
65
|
|
|
$resultA = $container->get('service-a'); |
|
66
|
|
|
$resultB = $container->get('service-b'); |
|
67
|
|
|
|
|
68
|
|
|
self::assertSame($callableA, $resultA); |
|
69
|
|
|
self::assertSame($callableB, $resultB); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.