|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Tests\Unit\Collector; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Yiisoft\Yii\Debug\Collector\CollectorInterface; |
|
10
|
|
|
use Yiisoft\Yii\Debug\Collector\Stream\HttpStreamCollector; |
|
11
|
|
|
use Yiisoft\Yii\Debug\Tests\Shared\AbstractCollectorTestCase; |
|
12
|
|
|
|
|
13
|
|
|
final class HttpStreamCollectorTest extends AbstractCollectorTestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param HttpStreamCollector $collector |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function collectTestData(CollectorInterface $collector): void |
|
19
|
|
|
{ |
|
20
|
|
|
$collector->collect( |
|
|
|
|
|
|
21
|
|
|
operation: 'read', |
|
22
|
|
|
path: __FILE__, |
|
23
|
|
|
args: ['arg1' => 'v1', 'arg2' => 'v2'], |
|
24
|
|
|
); |
|
25
|
|
|
$collector->collect( |
|
26
|
|
|
operation: 'read', |
|
27
|
|
|
path: __FILE__, |
|
28
|
|
|
args: ['arg3' => 'v3', 'arg4' => 'v4'], |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
#[DataProvider('dataSkipCollectOnMatchIgnoreReferences')] |
|
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($this, $url, $collected); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public static function dataSkipCollectOnMatchIgnoreReferences(): iterable |
|
68
|
|
|
{ |
|
69
|
|
|
$httpStreamBefore = function (string $url) { |
|
|
|
|
|
|
70
|
|
|
}; |
|
71
|
|
|
$httpStreamOperation = static 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 (TestCase $testCase, string $url, array $collected) { |
|
90
|
|
|
$testCase->assertArrayHasKey('read', $collected); |
|
91
|
|
|
$testCase->assertIsArray($collected['read']); |
|
92
|
|
|
$testCase->assertCount(1, $collected['read']); |
|
93
|
|
|
|
|
94
|
|
|
$readItem = $collected['read'][0]; |
|
95
|
|
|
$testCase->assertSame($url, $readItem['uri']); |
|
96
|
|
|
$testCase->assertArrayHasKey('args', $readItem); |
|
97
|
|
|
|
|
98
|
|
|
$readItemArgs = $readItem['args']; |
|
99
|
|
|
$testCase->assertCount(3, $readItemArgs); |
|
100
|
|
|
|
|
101
|
|
|
$testCase->assertSame('GET', $readItemArgs['method']); |
|
102
|
|
|
$testCase->assertIsArray($readItemArgs['response_headers']); |
|
103
|
|
|
$testCase->assertNotEmpty($readItemArgs['response_headers']); |
|
104
|
|
|
$testCase->assertIsArray($readItemArgs['request_headers']); |
|
105
|
|
|
$testCase->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
|
|
|
|