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
|
|
|
$_ENV['YII_DEBUG_IGNORE'] = 'true'; |
92
|
|
|
$debugger = new Debugger($idGenerator, $storage, [$collector], []); |
93
|
|
|
$debugger->startup(new ApplicationStartup('')); |
94
|
|
|
$debugger->shutdown(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testShutdown(): void |
98
|
|
|
{ |
99
|
|
|
$idGenerator = new DebuggerIdGenerator(); |
100
|
|
|
$collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); |
101
|
|
|
$collector->expects($this->once())->method('shutdown'); |
102
|
|
|
$storage = $this->getMockBuilder(StorageInterface::class)->getMock(); |
103
|
|
|
$storage->expects($this->once())->method('flush'); |
104
|
|
|
|
105
|
|
|
$debugger = new Debugger($idGenerator, $storage, [$collector]); |
106
|
|
|
$debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test'))); |
107
|
|
|
$debugger->shutdown(); |
108
|
|
|
$debugger->shutdown(); |
109
|
|
|
$debugger->shutdown(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testShutdownWithSkipRequestCollect(): void |
113
|
|
|
{ |
114
|
|
|
$idGenerator = new DebuggerIdGenerator(); |
115
|
|
|
$collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); |
116
|
|
|
$collector->expects($this->once())->method('shutdown'); |
117
|
|
|
$storage = $this->getMockBuilder(StorageInterface::class)->getMock(); |
118
|
|
|
$storage->expects($this->never())->method('flush'); |
119
|
|
|
|
120
|
|
|
$debugger = new Debugger($idGenerator, $storage, [$collector], ['/test']); |
121
|
|
|
$debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test'))); |
122
|
|
|
$debugger->shutdown(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @dataProvider dataShutdownWithSkipCommandCollect |
127
|
|
|
*/ |
128
|
|
|
public function testShutdownWithSkipCommandCollect(array $ignoredCommands, ?string $ignoredCommand): void |
129
|
|
|
{ |
130
|
|
|
$idGenerator = new DebuggerIdGenerator(); |
131
|
|
|
$collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); |
132
|
|
|
$collector->expects($this->never())->method('startup'); |
133
|
|
|
$collector->expects($this->once())->method('shutdown'); |
134
|
|
|
$storage = $this->getMockBuilder(StorageInterface::class)->getMock(); |
135
|
|
|
$storage->expects($this->never())->method('addCollector'); |
136
|
|
|
$storage->expects($this->never())->method('flush'); |
137
|
|
|
|
138
|
|
|
$debugger = new Debugger($idGenerator, $storage, [$collector], [], $ignoredCommands); |
139
|
|
|
$debugger->startup(new ApplicationStartup($ignoredCommand)); |
140
|
|
|
$debugger->shutdown(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public static function dataShutdownWithSkipCommandCollect(): iterable |
144
|
|
|
{ |
145
|
|
|
yield [ |
146
|
|
|
['app:ignored-command'], |
147
|
|
|
'app:ignored-command', |
148
|
|
|
]; |
149
|
|
|
yield [ |
150
|
|
|
['app:ignored-command1', 'app:ignored-command2'], |
151
|
|
|
'app:ignored-command2', |
152
|
|
|
]; |
153
|
|
|
yield [ |
154
|
|
|
['app:ignored-command'], |
155
|
|
|
null, |
156
|
|
|
]; |
157
|
|
|
yield [ |
158
|
|
|
['app:ignored-command'], |
159
|
|
|
'', |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @dataProvider dataShutdownWithoutSkipCommandCollect |
165
|
|
|
*/ |
166
|
|
|
public function testShutdownWithoutSkipCommandCollect(array $ignoredCommands, ?string $ignoredCommand): void |
167
|
|
|
{ |
168
|
|
|
$idGenerator = new DebuggerIdGenerator(); |
169
|
|
|
$collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); |
170
|
|
|
$collector->expects($this->once())->method('startup'); |
171
|
|
|
$collector->expects($this->once())->method('shutdown'); |
172
|
|
|
$storage = $this->getMockBuilder(StorageInterface::class)->getMock(); |
173
|
|
|
$storage->expects($this->once())->method('addCollector'); |
174
|
|
|
$storage->expects($this->once())->method('flush'); |
175
|
|
|
|
176
|
|
|
$debugger = new Debugger($idGenerator, $storage, [$collector], [], $ignoredCommands); |
177
|
|
|
$debugger->startup(new ApplicationStartup($ignoredCommand)); |
178
|
|
|
$debugger->shutdown(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public static function dataShutdownWithoutSkipCommandCollect(): iterable |
182
|
|
|
{ |
183
|
|
|
yield [ |
184
|
|
|
[], |
185
|
|
|
'app:not-ignored-command', |
186
|
|
|
]; |
187
|
|
|
yield [ |
188
|
|
|
['app:ignored-command'], |
189
|
|
|
'app:not-ignored-command', |
190
|
|
|
]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testStopSkipped(): void |
194
|
|
|
{ |
195
|
|
|
$idGenerator = new DebuggerIdGenerator(); |
196
|
|
|
$collector = $this->getMockBuilder(CollectorInterface::class)->getMock(); |
197
|
|
|
$collector->expects($this->once())->method('shutdown'); |
198
|
|
|
$storage = $this->getMockBuilder(StorageInterface::class)->getMock(); |
199
|
|
|
$storage->expects($this->once())->method('clear'); |
200
|
|
|
$storage->expects($this->never())->method('flush'); |
201
|
|
|
|
202
|
|
|
$debugger = new Debugger($idGenerator, $storage, [$collector]); |
203
|
|
|
$debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test'))); |
204
|
|
|
$debugger->stop(); |
205
|
|
|
$debugger->stop(); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|