dataShutdownWithoutSkipCommandCollect()   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
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Tests\Unit;
6
7
use Nyholm\Psr7\ServerRequest;
8
use PHPUnit\Framework\TestCase;
9
use stdClass;
10
use Yiisoft\Yii\Console\Event\ApplicationStartup;
11
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
12
use Yiisoft\Yii\Debug\Debugger;
13
use Yiisoft\Yii\Debug\DebuggerIdGenerator;
14
use Yiisoft\Yii\Debug\Storage\MemoryStorage;
15
use Yiisoft\Yii\Debug\Storage\StorageInterface;
16
use Yiisoft\Yii\Http\Event\BeforeRequest;
17
18
final class DebuggerTest extends TestCase
19
{
20
    public function testStartup(): void
21
    {
22
        $idGenerator = new DebuggerIdGenerator();
23
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
24
        $collector->expects($this->once())->method('startup');
25
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
26
        $storage->expects($this->once())->method('addCollector');
27
28
        $debugger = new Debugger($idGenerator, $storage, [$collector]);
29
        $debugger->startup(new stdClass());
30
    }
31
32
    public function testStartupWithSkipCollect(): void
33
    {
34
        $idGenerator = new DebuggerIdGenerator();
35
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
36
        $collector->expects($this->once())->method('startup');
37
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
38
        $storage->expects($this->once())->method('addCollector');
39
40
        $debugger = new Debugger($idGenerator, $storage, [$collector], ['/test']);
41
        $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/debug')));
42
    }
43
44
    public function testGetId(): void
45
    {
46
        $idGenerator = new DebuggerIdGenerator();
47
        $debugger = new Debugger($idGenerator, new MemoryStorage($idGenerator), []);
48
49
        $this->assertEquals($idGenerator->getId(), $debugger->getId());
50
    }
51
52
    public function testWithIgnoredRequests(): void
53
    {
54
        $idGenerator = new DebuggerIdGenerator();
55
        $debugger1 = new Debugger($idGenerator, new MemoryStorage($idGenerator), []);
56
        $debugger2 = $debugger1->withIgnoredRequests(['/test']);
57
58
        $this->assertNotSame($debugger1, $debugger2);
59
    }
60
61
    public function testIgnoreByHeader(): void
62
    {
63
        $idGenerator = new DebuggerIdGenerator();
64
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
65
        $collector->expects($this->once())->method('shutdown');
66
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
67
        $storage->expects($this->never())->method('flush');
68
69
        $debugger = new Debugger($idGenerator, $storage, [$collector], []);
70
        $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test', ['X-Debug-Ignore' => 'true'])));
71
        $debugger->shutdown();
72
    }
73
74
    public function testWithIgnoredCommands(): void
75
    {
76
        $idGenerator = new DebuggerIdGenerator();
77
        $debugger1 = new Debugger($idGenerator, new MemoryStorage($idGenerator), []);
78
        $debugger2 = $debugger1->withIgnoredCommands(['command/test']);
79
80
        $this->assertNotSame($debugger1, $debugger2);
81
    }
82
83
    public function testIgnoreByEnv(): void
84
    {
85
        $idGenerator = new DebuggerIdGenerator();
86
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
87
        $collector->expects($this->once())->method('shutdown');
88
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
89
        $storage->expects($this->never())->method('flush');
90
91
        putenv('YII_DEBUG_IGNORE=true');
92
        $debugger = new Debugger($idGenerator, $storage, [$collector], []);
93
        $debugger->startup(new ApplicationStartup('command'));
94
        putenv('YII_DEBUG_IGNORE=false');
95
        $debugger->shutdown();
96
    }
97
98
    public function testShutdown(): void
99
    {
100
        $idGenerator = new DebuggerIdGenerator();
101
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
102
        $collector->expects($this->once())->method('shutdown');
103
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
104
        $storage->expects($this->once())->method('flush');
105
106
        $debugger = new Debugger($idGenerator, $storage, [$collector]);
107
        $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test')));
108
        $debugger->shutdown();
109
        $debugger->shutdown();
110
        $debugger->shutdown();
111
    }
112
113
    public function testShutdownWithSkipRequestCollect(): void
114
    {
115
        $idGenerator = new DebuggerIdGenerator();
116
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
117
        $collector->expects($this->once())->method('shutdown');
118
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
119
        $storage->expects($this->never())->method('flush');
120
121
        $debugger = new Debugger($idGenerator, $storage, [$collector], ['/test']);
122
        $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test')));
123
        $debugger->shutdown();
124
    }
125
126
    /**
127
     * @dataProvider dataShutdownWithSkipCommandCollect
128
     */
129
    public function testShutdownWithSkipCommandCollect(array $ignoredCommands, ?string $ignoredCommand): void
130
    {
131
        $idGenerator = new DebuggerIdGenerator();
132
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
133
        $collector->expects($this->never())->method('startup');
134
        $collector->expects($this->once())->method('shutdown');
135
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
136
        $storage->expects($this->never())->method('addCollector');
137
        $storage->expects($this->never())->method('flush');
138
139
        $debugger = new Debugger($idGenerator, $storage, [$collector], [], $ignoredCommands);
140
        $debugger->startup(new ApplicationStartup($ignoredCommand));
141
        $debugger->shutdown();
142
    }
143
144
    public static function dataShutdownWithSkipCommandCollect(): iterable
145
    {
146
        yield [
147
            ['app:ignored-command'],
148
            'app:ignored-command',
149
        ];
150
        yield [
151
            ['app:ignored-command1', 'app:ignored-command2'],
152
            'app:ignored-command2',
153
        ];
154
        yield [
155
            ['app:ignored-command'],
156
            null,
157
        ];
158
        yield [
159
            ['app:ignored-command'],
160
            '',
161
        ];
162
    }
163
164
    /**
165
     * @dataProvider dataShutdownWithoutSkipCommandCollect
166
     */
167
    public function testShutdownWithoutSkipCommandCollect(array $ignoredCommands, ?string $ignoredCommand): void
168
    {
169
        $idGenerator = new DebuggerIdGenerator();
170
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
171
        $collector->expects($this->once())->method('startup');
172
        $collector->expects($this->once())->method('shutdown');
173
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
174
        $storage->expects($this->once())->method('addCollector');
175
        $storage->expects($this->once())->method('flush');
176
177
        $debugger = new Debugger($idGenerator, $storage, [$collector], [], $ignoredCommands);
178
        $debugger->startup(new ApplicationStartup($ignoredCommand));
179
        $debugger->shutdown();
180
    }
181
182
    public static function dataShutdownWithoutSkipCommandCollect(): iterable
183
    {
184
        yield [
185
            [],
186
            'app:not-ignored-command',
187
        ];
188
        yield [
189
            ['app:ignored-command'],
190
            'app:not-ignored-command',
191
        ];
192
    }
193
194
    public function testStopSkipped(): void
195
    {
196
        $idGenerator = new DebuggerIdGenerator();
197
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
198
        $collector->expects($this->once())->method('shutdown');
199
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
200
        $storage->expects($this->never())->method('clear');
201
        $storage->expects($this->never())->method('flush');
202
203
        $debugger = new Debugger($idGenerator, $storage, [$collector]);
204
        $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test')));
205
        $debugger->stop();
206
        $debugger->stop();
207
    }
208
}
209