GacelaClassResolverGeneralListenerTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
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\Cache\ClassNameInMemoryCacheCreatedEvent;
11
use Gacela\Framework\Event\ClassResolver\ClassNameFinder\ClassNameInvalidCandidateFoundEvent;
12
use Gacela\Framework\Event\ClassResolver\ClassNameFinder\ClassNameNotFoundEvent;
13
use Gacela\Framework\Event\ClassResolver\ClassNameFinder\ClassNameValidCandidateFoundEvent;
14
use Gacela\Framework\Event\ClassResolver\ResolvedClassCachedEvent;
15
use Gacela\Framework\Event\ClassResolver\ResolvedClassCreatedEvent;
16
use Gacela\Framework\Event\ClassResolver\ResolvedClassTriedFromParentEvent;
17
use Gacela\Framework\Event\ClassResolver\ResolvedCreatedDefaultClassEvent;
18
use Gacela\Framework\Event\GacelaEventInterface;
19
use Gacela\Framework\Gacela;
20
use PHPUnit\Framework\TestCase;
21
22
final class GacelaClassResolverGeneralListenerTest extends TestCase
23
{
24
    /** @var list<GacelaEventInterface> */
25
    private static array $inMemoryEvents = [];
26
27
    protected function setUp(): void
28
    {
29
        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...
30
31
        Gacela::bootstrap(__DIR__, function (GacelaConfig $config): void {
32
            $config->resetInMemoryCache();
33
34
            $config->registerGenericListener(function (GacelaEventInterface $event): void {
35
                $this->saveInMemoryEvent($event);
36
            });
37
        });
38
    }
39
40
    public function saveInMemoryEvent(GacelaEventInterface $event): void
41
    {
42
        self::$inMemoryEvents[] = $event;
43
    }
44
45
    public function test_resolved_class_created(): void
46
    {
47
        $facade = new Module\Facade();
48
        $facade->doString();
49
50
        self::assertEquals([
51
            new ClassNameInMemoryCacheCreatedEvent(),
52
            new ClassNameInvalidCandidateFoundEvent('\GacelaTest\Feature\Framework\ListeningEvents\ClassResolver\Module\ModuleFactory'),
53
            new ClassNameValidCandidateFoundEvent('\GacelaTest\Feature\Framework\ListeningEvents\ClassResolver\Module\Factory'),
54
            new ResolvedClassCreatedEvent(ClassInfo::from(Module\Facade::class, 'Factory')),
55
        ], self::$inMemoryEvents);
56
    }
57
58
    public function test_resolved_class_cached(): void
59
    {
60
        $facade = new Module\Facade();
61
        $facade->doString();
62
63
        $facade = new Module\Facade();
64
        $facade->doString();
65
66
        self::assertEquals([
67
            new ClassNameInMemoryCacheCreatedEvent(),
68
            new ClassNameInvalidCandidateFoundEvent('\GacelaTest\Feature\Framework\ListeningEvents\ClassResolver\Module\ModuleFactory'),
69
            new ClassNameValidCandidateFoundEvent('\GacelaTest\Feature\Framework\ListeningEvents\ClassResolver\Module\Factory'),
70
            new ResolvedClassCreatedEvent(ClassInfo::from(Module\Facade::class, 'Factory')),
71
        ], self::$inMemoryEvents);
72
    }
73
74
    public function test_resolved_parent_and_default_class(): void
75
    {
76
        $factory = new Module\Factory();
77
        $factory->getConfig();
78
79
        self::assertEquals([
80
            new ClassNameInMemoryCacheCreatedEvent(),
81
            new ClassNameInvalidCandidateFoundEvent('\GacelaTest\Feature\Framework\ListeningEvents\ClassResolver\Module\ModuleConfig'),
82
            new ClassNameInvalidCandidateFoundEvent('\GacelaTest\Feature\Framework\ListeningEvents\ClassResolver\Module\Config'),
83
            new ClassNameNotFoundEvent(ClassInfo::from(Module\Factory::class, 'Config'), ['Config']),
84
            new ResolvedClassTriedFromParentEvent(ClassInfo::from(Module\Factory::class, 'Config')),
85
            new ClassNameInvalidCandidateFoundEvent('\Gacela\Framework\FrameworkConfig'),
86
            new ClassNameInvalidCandidateFoundEvent('\Gacela\Framework\Config'),
87
            new ClassNameNotFoundEvent(ClassInfo::from(AbstractFactory::class, 'Config'), ['Config']),
88
            new ResolvedCreatedDefaultClassEvent(ClassInfo::from(AbstractFactory::class, 'Config')),
89
        ], self::$inMemoryEvents);
90
91
        // And again would simply load the cached event
92
        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...
93
        $factory = new Module\Factory();
94
        $factory->getConfig();
95
96
        self::assertEquals([
97
            new ResolvedClassCachedEvent(ClassInfo::from(Module\Factory::class, 'Config')),
98
        ], self::$inMemoryEvents);
99
    }
100
}
101