Passed
Push — main ( 60c49f...544d0e )
by Chema
06:20 queued 04:56
created

test_multiple_protected_services()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 10
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 17
rs 9.9332
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');
0 ignored issues
show
Unused Code introduced by
The parameter $previous is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
            $config->extendService('test-service', static fn ($c, /** @scrutinizer ignore-unused */ $previous): Closure => static fn (): string => 'extended');

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
            $config->extendService('test-service', static fn (/** @scrutinizer ignore-unused */ $c, $previous): Closure => static fn (): string => 'extended');

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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