Passed
Push — master ( bace86...bb4cad )
by Dmitriy
05:30 queued 02:57
created

DebuggerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 91
dl 0
loc 161
rs 10
c 0
b 0
f 0
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testShutdownWithSkipRequestCollect() 0 11 1
A testGetId() 0 6 1
A testShutdownWithSkipCommandCollect() 0 13 1
A testWithIgnoredRequests() 0 7 1
A dataShutdownWithSkipCommandCollect() 0 17 1
A testStartupWithSkipCollect() 0 10 1
A testStopSkipped() 0 13 1
A testWithIgnoredCommands() 0 7 1
A testShutdown() 0 13 1
A testShutdownWithoutSkipCommandCollect() 0 13 1
A testStartup() 0 10 1
A dataShutdownWithoutSkipCommandCollect() 0 9 1
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 testWithIgnoredCommands(): void
62
    {
63
        $idGenerator = new DebuggerIdGenerator();
64
        $debugger1 = new Debugger($idGenerator, new MemoryStorage($idGenerator), []);
65
        $debugger2 = $debugger1->withIgnoredCommands(['command/test']);
66
67
        $this->assertNotSame($debugger1, $debugger2);
68
    }
69
70
    public function testShutdown(): void
71
    {
72
        $idGenerator = new DebuggerIdGenerator();
73
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
74
        $collector->expects($this->once())->method('shutdown');
75
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
76
        $storage->expects($this->once())->method('flush');
77
78
        $debugger = new Debugger($idGenerator, $storage, [$collector]);
79
        $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test')));
80
        $debugger->shutdown();
81
        $debugger->shutdown();
82
        $debugger->shutdown();
83
    }
84
85
    public function testShutdownWithSkipRequestCollect(): void
86
    {
87
        $idGenerator = new DebuggerIdGenerator();
88
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
89
        $collector->expects($this->once())->method('shutdown');
90
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
91
        $storage->expects($this->never())->method('flush');
92
93
        $debugger = new Debugger($idGenerator, $storage, [$collector], ['/test']);
94
        $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test')));
95
        $debugger->shutdown();
96
    }
97
98
    /**
99
     * @dataProvider dataShutdownWithSkipCommandCollect
100
     */
101
    public function testShutdownWithSkipCommandCollect(array $ignoredCommands, ?string $ignoredCommand): void
102
    {
103
        $idGenerator = new DebuggerIdGenerator();
104
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
105
        $collector->expects($this->never())->method('startup');
106
        $collector->expects($this->once())->method('shutdown');
107
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
108
        $storage->expects($this->never())->method('addCollector');
109
        $storage->expects($this->never())->method('flush');
110
111
        $debugger = new Debugger($idGenerator, $storage, [$collector], [], $ignoredCommands);
112
        $debugger->startup(new ApplicationStartup($ignoredCommand));
113
        $debugger->shutdown();
114
    }
115
116
    public static function dataShutdownWithSkipCommandCollect(): iterable
117
    {
118
        yield [
119
            ['app:ignored-command'],
120
            'app:ignored-command',
121
        ];
122
        yield [
123
            ['app:ignored-command1', 'app:ignored-command2'],
124
            'app:ignored-command2',
125
        ];
126
        yield [
127
            ['app:ignored-command'],
128
            null,
129
        ];
130
        yield [
131
            ['app:ignored-command'],
132
            '',
133
        ];
134
    }
135
136
    /**
137
     * @dataProvider dataShutdownWithoutSkipCommandCollect
138
     */
139
    public function testShutdownWithoutSkipCommandCollect(array $ignoredCommands, ?string $ignoredCommand): void
140
    {
141
        $idGenerator = new DebuggerIdGenerator();
142
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
143
        $collector->expects($this->once())->method('startup');
144
        $collector->expects($this->once())->method('shutdown');
145
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
146
        $storage->expects($this->once())->method('addCollector');
147
        $storage->expects($this->once())->method('flush');
148
149
        $debugger = new Debugger($idGenerator, $storage, [$collector], [], $ignoredCommands);
150
        $debugger->startup(new ApplicationStartup($ignoredCommand));
151
        $debugger->shutdown();
152
    }
153
154
    public static function dataShutdownWithoutSkipCommandCollect(): iterable
155
    {
156
        yield [
157
            [],
158
            'app:not-ignored-command',
159
        ];
160
        yield [
161
            ['app:ignored-command'],
162
            'app:not-ignored-command',
163
        ];
164
    }
165
166
    public function testStopSkipped(): void
167
    {
168
        $idGenerator = new DebuggerIdGenerator();
169
        $collector = $this->getMockBuilder(CollectorInterface::class)->getMock();
170
        $collector->expects($this->once())->method('shutdown');
171
        $storage = $this->getMockBuilder(StorageInterface::class)->getMock();
172
        $storage->expects($this->once())->method('clear');
173
        $storage->expects($this->never())->method('flush');
174
175
        $debugger = new Debugger($idGenerator, $storage, [$collector]);
176
        $debugger->startup(new BeforeRequest(new ServerRequest('GET', '/test')));
177
        $debugger->stop();
178
        $debugger->stop();
179
    }
180
}
181