Passed
Pull Request — master (#156)
by Wilmer
02:29
created

ContainerInterfaceProxyTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 94
dl 0
loc 174
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 17 1
A testGetAndHasWithCallableServices() 0 25 1
A testImmutability() 0 9 1
A testGetAndHasWithWrongId() 0 15 1
A testGetWithArrayConfigWithStringKeys() 0 29 1
A testGetAndHasWithNotService() 0 9 1
A getContainer() 0 11 1
A tearDown() 0 4 1
A testGetWithoutConfig() 0 20 1
A testGetAndHas() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Tests\Collector;
6
7
use PHPUnit\Framework\TestCase;
8
use Psr\Container\ContainerExceptionInterface;
9
use Psr\EventDispatcher\EventDispatcherInterface;
10
use Psr\EventDispatcher\ListenerProviderInterface;
11
use Psr\Log\LoggerInterface;
12
use Psr\Log\NullLogger;
13
use stdClass;
14
use Yiisoft\Di\Container;
15
use Yiisoft\Di\ContainerConfig;
16
use Yiisoft\EventDispatcher\Dispatcher\Dispatcher;
17
use Yiisoft\EventDispatcher\Provider\Provider;
18
use Yiisoft\Files\FileHelper;
19
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
20
use Yiisoft\Yii\Debug\Collector\EventCollector;
21
use Yiisoft\Yii\Debug\Collector\LogCollector;
22
use Yiisoft\Yii\Debug\Collector\ServiceCollector;
23
use Yiisoft\Yii\Debug\Collector\ContainerInterfaceProxy;
24
use Yiisoft\Yii\Debug\Collector\ContainerProxyConfig;
25
use Yiisoft\Yii\Debug\Collector\EventDispatcherInterfaceProxy;
26
use Yiisoft\Yii\Debug\Collector\LoggerInterfaceProxy;
27
28
class ContainerInterfaceProxyTest extends TestCase
29
{
30
    private string $path = 'tests/container-proxy';
31
32
    protected function tearDown(): void
33
    {
34
        parent::tearDown();
35
        FileHelper::removeDirectory($this->path);
36
    }
37
38
    public function testImmutability(): void
39
    {
40
        $containerProxy = new ContainerInterfaceProxy(new Container(ContainerConfig::create()), new ContainerProxyConfig());
41
42
        $this->assertNotSame(
43
            $containerProxy,
44
            $containerProxy->withDecoratedServices(
45
                [
46
                    LoggerInterface::class => [LoggerInterfaceProxy::class, LogCollector::class],
47
                ]
48
            )
49
        );
50
    }
51
52
    public function testGetAndHas(): void
53
    {
54
        $containerProxy = new ContainerInterfaceProxy($this->getContainer(), $this->getConfig());
55
56
        $this->assertTrue($containerProxy->isActive());
57
        $this->assertTrue($containerProxy->has(LoggerInterface::class));
58
        $this->assertInstanceOf(LoggerInterfaceProxy::class, $containerProxy->get(LoggerInterface::class));
59
    }
60
61
    public function testGetAndHasWithCallableServices(): void
62
    {
63
        $dispatcherMock = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
64
        $config = new ContainerProxyConfig(
65
            true,
66
            [
67
                LoggerInterface::class => fn (Container $container) => $container->get(LoggerInterfaceProxy::class),
68
                EventDispatcherInterface::class => [
69
                    EventDispatcherInterfaceProxy::class,
70
                    EventCollector::class,
71
                ],
72
            ],
73
            $dispatcherMock,
74
            new ServiceCollector(),
75
            $this->path,
76
            1
77
        );
78
        $containerProxy = new ContainerInterfaceProxy($this->getContainer(), $config);
79
80
        $this->assertTrue($containerProxy->isActive());
81
        $this->assertTrue($containerProxy->has(LoggerInterface::class));
82
83
        $containerProxy->get(LogCollector::class)->startup();
84
        $containerProxy->get(LoggerInterface::class)->log('test', 'test message');
85
        $this->assertNotEmpty($containerProxy->get(LogCollector::class)->getCollected());
86
    }
87
88
    public function testGetWithArrayConfigWithStringKeys(): void
89
    {
90
        $dispatcherMock = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
91
        $serviceCollector = new ServiceCollector();
92
        $serviceCollector->startup(); // activate collector
93
94
        $config = new ContainerProxyConfig(
95
            true,
96
            [
97
                LoggerInterface::class => ['logger' => LoggerInterfaceProxy::class],
98
                EventDispatcherInterface::class => [
99
                    EventDispatcherInterfaceProxy::class,
100
                    EventCollector::class,
101
                ],
102
            ],
103
            $dispatcherMock,
104
            $serviceCollector,
105
            $this->path,
106
            1
107
        );
108
        $container = $this->getContainer();
109
        $containerProxy = new ContainerInterfaceProxy($container, $config);
110
111
        $this->assertTrue($containerProxy->isActive());
112
        $this->assertInstanceOf(LoggerInterface::class, $containerProxy->get(LoggerInterface::class));
113
114
        $containerProxy->get(LoggerInterface::class)->log('test', 'test message');
115
        $this->assertInstanceOf(LoggerInterface::class, $containerProxy->get(LoggerInterface::class));
116
        $this->assertNotEmpty($config->getCollector()->getCollected());
117
    }
118
119
    public function testGetWithoutConfig(): void
120
    {
121
        $dispatcherMock = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
122
        $config = new ContainerProxyConfig(
123
            true,
124
            [
125
                LoggerInterface::class => [LoggerInterfaceProxy::class, LogCollector::class],
126
                EventDispatcherInterface::class,
127
            ],
128
            $dispatcherMock,
129
            new ServiceCollector(),
130
            $this->path,
131
            1
132
        );
133
        $containerProxy = new ContainerInterfaceProxy($this->getContainer(), $config);
134
135
        $this->assertInstanceOf(EventDispatcherInterface::class, $containerProxy->get(EventDispatcherInterface::class));
136
        $this->assertInstanceOf(
137
            stdClass::class,
138
            $containerProxy->get(EventDispatcherInterface::class)->dispatch(new stdClass())
139
        );
140
    }
141
142
    public function testGetAndHasWithWrongId(): void
143
    {
144
        $this->expectException(ContainerExceptionInterface::class);
145
        $this->expectExceptionMessage(
146
            sprintf(
147
                'No definition or class found or resolvable for "%s" while building "%s".',
148
                CollectorInterface::class,
149
                CollectorInterface::class
150
            )
151
        );
152
153
        $containerProxy = new ContainerInterfaceProxy($this->getContainer(), $this->getConfig());
154
155
        $containerProxy->has(CollectorInterface::class);
156
        $containerProxy->get(CollectorInterface::class);
157
    }
158
159
    public function testGetAndHasWithNotService(): void
160
    {
161
        $containerProxy = new ContainerInterfaceProxy($this->getContainer(), $this->getConfig());
162
163
        $this->assertTrue($containerProxy->has(ListenerProviderInterface::class));
164
        $this->assertNotNull($containerProxy->get(ListenerProviderInterface::class));
165
        $this->assertInstanceOf(
166
            ListenerProviderInterface::class,
167
            $containerProxy->get(ListenerProviderInterface::class)
168
        );
169
    }
170
171
    private function getConfig(): ContainerProxyConfig
172
    {
173
        $dispatcherMock = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
174
175
        return new ContainerProxyConfig(
176
            true,
177
            [
178
                LoggerInterface::class => [LoggerInterfaceProxy::class, LogCollector::class],
179
                EventDispatcherInterface::class => [
180
                    EventDispatcherInterfaceProxy::class,
181
                    EventCollector::class,
182
                ],
183
            ],
184
            $dispatcherMock,
185
            new ServiceCollector(),
186
            $this->path,
187
            1
188
        );
189
    }
190
191
    private function getContainer(): Container
192
    {
193
        $config = ContainerConfig::create()
194
            ->withDefinitions([
195
                EventDispatcherInterface::class => Dispatcher::class,
196
                ListenerProviderInterface::class => Provider::class,
197
                LoggerInterface::class => NullLogger::class,
198
                LogCollector::class => LogCollector::class,
199
                EventCollector::class => EventCollector::class,
200
            ]);
201
        return new Container($config);
202
    }
203
}
204