1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Tests\Unit\Collector; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Yii\Debug\Collector\CollectorInterface; |
8
|
|
|
use Yiisoft\Yii\Debug\Collector\Stream\HttpStreamCollector; |
9
|
|
|
use Yiisoft\Yii\Debug\Tests\Shared\AbstractCollectorTestCase; |
10
|
|
|
|
11
|
|
|
final class HttpStreamCollectorTest extends AbstractCollectorTestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param HttpStreamCollector $collector |
15
|
|
|
*/ |
16
|
|
|
protected function collectTestData(CollectorInterface $collector): void |
17
|
|
|
{ |
18
|
|
|
$collector->collect( |
|
|
|
|
19
|
|
|
operation: 'read', |
20
|
|
|
path: __FILE__, |
21
|
|
|
args: ['arg1' => 'v1', 'arg2' => 'v2'], |
22
|
|
|
); |
23
|
|
|
$collector->collect( |
24
|
|
|
operation: 'read', |
25
|
|
|
path: __FILE__, |
26
|
|
|
args: ['arg3' => 'v3', 'arg4' => 'v4'], |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @dataProvider dataSkipCollectOnMatchIgnoreReferences |
32
|
|
|
*/ |
33
|
|
|
public function testSkipCollectOnMatchIgnoreReferences( |
34
|
|
|
string $url, |
35
|
|
|
callable $before, |
36
|
|
|
array $ignoredPathPatterns, |
37
|
|
|
array $ignoredClasses, |
38
|
|
|
array $ignoredUrls, |
39
|
|
|
callable $operation, |
40
|
|
|
callable $after, |
41
|
|
|
array|callable $assertResult, |
42
|
|
|
): void { |
43
|
|
|
$before($url); |
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
$collector = new HttpStreamCollector( |
47
|
|
|
ignoredPathPatterns: $ignoredPathPatterns, |
48
|
|
|
ignoredClasses: $ignoredClasses, |
49
|
|
|
ignoredUrls: $ignoredUrls, |
50
|
|
|
); |
51
|
|
|
$collector->startup(); |
52
|
|
|
|
53
|
|
|
$operation($url); |
54
|
|
|
|
55
|
|
|
$collected = $collector->getCollected(); |
56
|
|
|
$collector->shutdown(); |
57
|
|
|
} finally { |
58
|
|
|
$after($url); |
59
|
|
|
} |
60
|
|
|
if (is_array($assertResult)) { |
|
|
|
|
61
|
|
|
$this->assertSame($assertResult, $collected); |
62
|
|
|
} else { |
63
|
|
|
$assertResult($url, $collected); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function dataSkipCollectOnMatchIgnoreReferences(): iterable |
68
|
|
|
{ |
69
|
|
|
$httpStreamBefore = function (string $url) { |
|
|
|
|
70
|
|
|
}; |
71
|
|
|
$httpStreamOperation = function (string $url) { |
72
|
|
|
$stream = fopen($url, 'r'); |
73
|
|
|
fread($stream, 4); |
74
|
|
|
ftell($stream); |
75
|
|
|
feof($stream); |
76
|
|
|
fstat($stream); |
77
|
|
|
fclose($stream); |
78
|
|
|
}; |
79
|
|
|
$httpStreamAfter = $httpStreamBefore; |
80
|
|
|
|
81
|
|
|
yield 'file stream matched' => [ |
82
|
|
|
$url = 'http://example.com', |
83
|
|
|
$httpStreamBefore, |
84
|
|
|
[], |
85
|
|
|
[], |
86
|
|
|
[], |
87
|
|
|
$httpStreamOperation, |
88
|
|
|
$httpStreamAfter, |
89
|
|
|
function (string $url, array $collected) { |
90
|
|
|
$this->assertArrayHasKey('read', $collected); |
91
|
|
|
$this->assertIsArray($collected['read']); |
92
|
|
|
$this->assertCount(1, $collected['read']); |
93
|
|
|
|
94
|
|
|
$readItem = $collected['read'][0]; |
95
|
|
|
$this->assertSame($url, $readItem['uri']); |
96
|
|
|
$this->assertArrayHasKey('args', $readItem); |
97
|
|
|
|
98
|
|
|
$readItemArgs = $readItem['args']; |
99
|
|
|
$this->assertCount(3, $readItemArgs); |
100
|
|
|
|
101
|
|
|
$this->assertSame('GET', $readItemArgs['method']); |
102
|
|
|
$this->assertIsArray($readItemArgs['response_headers']); |
103
|
|
|
$this->assertNotEmpty($readItemArgs['response_headers']); |
104
|
|
|
$this->assertIsArray($readItemArgs['request_headers']); |
105
|
|
|
$this->assertEmpty($readItemArgs['request_headers']); |
106
|
|
|
}, |
107
|
|
|
]; |
108
|
|
|
yield 'file stream ignored by path' => [ |
109
|
|
|
$url, |
110
|
|
|
$httpStreamBefore, |
111
|
|
|
['/' . basename(__FILE__, '.php') . '/'], |
112
|
|
|
[], |
113
|
|
|
[], |
114
|
|
|
$httpStreamOperation, |
115
|
|
|
$httpStreamAfter, |
116
|
|
|
[], |
117
|
|
|
]; |
118
|
|
|
yield 'file stream ignored by class' => [ |
119
|
|
|
$url, |
120
|
|
|
$httpStreamBefore, |
121
|
|
|
[], |
122
|
|
|
[self::class], |
123
|
|
|
[], |
124
|
|
|
$httpStreamOperation, |
125
|
|
|
$httpStreamAfter, |
126
|
|
|
[], |
127
|
|
|
]; |
128
|
|
|
yield 'file stream ignored by url' => [ |
129
|
|
|
$url, |
130
|
|
|
$httpStreamBefore, |
131
|
|
|
[], |
132
|
|
|
[], |
133
|
|
|
['/example/'], |
134
|
|
|
$httpStreamOperation, |
135
|
|
|
$httpStreamAfter, |
136
|
|
|
[], |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function getCollector(): CollectorInterface |
141
|
|
|
{ |
142
|
|
|
return new HttpStreamCollector(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
protected function checkCollectedData(array $data): void |
146
|
|
|
{ |
147
|
|
|
parent::checkCollectedData($data); |
148
|
|
|
$collected = $data; |
149
|
|
|
$this->assertCount(1, $collected); |
150
|
|
|
|
151
|
|
|
$this->assertCount(2, $collected['read']); |
152
|
|
|
$this->assertEquals([ |
153
|
|
|
['uri' => __FILE__, 'args' => ['arg1' => 'v1', 'arg2' => 'v2']], |
154
|
|
|
['uri' => __FILE__, 'args' => ['arg3' => 'v3', 'arg4' => 'v4']], |
155
|
|
|
], $collected['read']); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function checkSummaryData(array $data): void |
159
|
|
|
{ |
160
|
|
|
parent::checkSummaryData($data); |
161
|
|
|
$this->assertArrayHasKey('http_stream', $data); |
162
|
|
|
$this->assertEquals(['read' => 2], $data['http_stream']); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|