GacelaClassResolverSpecificListenerTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 38
c 0
b 0
f 0
dl 0
loc 73
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A saveInMemoryEvent() 0 3 1
A setUp() 0 18 1
A test_resolved_class_created() 0 8 1
A test_resolved_class_cached() 0 11 1
A test_resolved_parent_and_default_class() 0 18 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Feature\Framework\ListeningEvents\ClassResolver;
6
7
use Gacela\Framework\AbstractFactory;
8
use Gacela\Framework\Bootstrap\GacelaConfig;
9
use Gacela\Framework\ClassResolver\ClassInfo;
10
use Gacela\Framework\Event\ClassResolver\ResolvedClassCachedEvent;
11
use Gacela\Framework\Event\ClassResolver\ResolvedClassCreatedEvent;
12
use Gacela\Framework\Event\ClassResolver\ResolvedClassTriedFromParentEvent;
13
use Gacela\Framework\Event\ClassResolver\ResolvedCreatedDefaultClassEvent;
14
use Gacela\Framework\Event\GacelaEventInterface;
15
use Gacela\Framework\Gacela;
16
use PHPUnit\Framework\TestCase;
17
18
final class GacelaClassResolverSpecificListenerTest extends TestCase
19
{
20
    /** @var list<GacelaEventInterface> */
21
    private static array $inMemoryEvents = [];
22
23
    protected function setUp(): void
24
    {
25
        self::$inMemoryEvents = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type GacelaTest\Feature\Frame...ents\ClassResolver\list of property $inMemoryEvents.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26
27
        Gacela::bootstrap(__DIR__, function (GacelaConfig $config): void {
28
            $config->resetInMemoryCache();
29
30
            $config->registerSpecificListener(ResolvedClassCachedEvent::class, function (GacelaEventInterface $event): void {
31
                $this->saveInMemoryEvent($event);
32
            });
33
            $config->registerSpecificListener(ResolvedClassCreatedEvent::class, function (GacelaEventInterface $event): void {
34
                $this->saveInMemoryEvent($event);
35
            });
36
            $config->registerSpecificListener(ResolvedClassTriedFromParentEvent::class, function (GacelaEventInterface $event): void {
37
                $this->saveInMemoryEvent($event);
38
            });
39
            $config->registerSpecificListener(ResolvedCreatedDefaultClassEvent::class, function (GacelaEventInterface $event): void {
40
                $this->saveInMemoryEvent($event);
41
            });
42
        });
43
    }
44
45
    public function saveInMemoryEvent(GacelaEventInterface $event): void
46
    {
47
        self::$inMemoryEvents[] = $event;
48
    }
49
50
    public function test_resolved_class_created(): void
51
    {
52
        $facade = new Module\Facade();
53
        $facade->doString();
54
55
        self::assertEquals([
56
            new ResolvedClassCreatedEvent(ClassInfo::from(Module\Facade::class, 'Factory')),
57
        ], self::$inMemoryEvents);
58
    }
59
60
    public function test_resolved_class_cached(): void
61
    {
62
        $facade = new Module\Facade();
63
        $facade->doString();
64
65
        $facade = new Module\Facade();
66
        $facade->doString();
67
68
        self::assertEquals([
69
            new ResolvedClassCreatedEvent(ClassInfo::from(Module\Facade::class, 'Factory')),
70
        ], self::$inMemoryEvents);
71
    }
72
73
    public function test_resolved_parent_and_default_class(): void
74
    {
75
        $factory = new Module\Factory();
76
        $factory->getConfig();
77
78
        self::assertEquals([
79
            new ResolvedClassTriedFromParentEvent(ClassInfo::from(Module\Factory::class, 'Config')),
80
            new ResolvedCreatedDefaultClassEvent(ClassInfo::from(AbstractFactory::class, 'Config')),
81
        ], self::$inMemoryEvents);
82
83
        // And again would simply load the cached event
84
        self::$inMemoryEvents = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type GacelaTest\Feature\Frame...ents\ClassResolver\list of property $inMemoryEvents.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
85
        $factory = new Module\Factory();
86
        $factory->getConfig();
87
88
        self::assertEquals([
89
            new ResolvedClassCachedEvent(ClassInfo::from(Module\Factory::class, 'Config')),
90
        ], self::$inMemoryEvents);
91
    }
92
}
93