1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Http\HttplugBundle\Tests\Functional; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use Http\Client\Common\Plugin; |
9
|
|
|
use Http\Client\Common\PluginClient; |
10
|
|
|
use Http\Discovery\StreamFactoryDiscovery; |
11
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
12
|
|
|
use Http\HttplugBundle\Collector\Collector; |
13
|
|
|
use Http\HttplugBundle\Collector\Formatter; |
14
|
|
|
use Http\HttplugBundle\Collector\ProfileClient; |
15
|
|
|
use Http\HttplugBundle\Collector\ProfilePlugin; |
16
|
|
|
use Http\HttplugBundle\Collector\StackPlugin; |
17
|
|
|
use Http\Message\Formatter\CurlCommandFormatter; |
18
|
|
|
use Http\Message\Formatter\FullHttpMessageFormatter; |
19
|
|
|
use Http\Mock\Client; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
use Psr\Http\Message\RequestInterface; |
22
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
23
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
24
|
|
|
|
25
|
|
|
class ProfilingTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Collector |
29
|
|
|
*/ |
30
|
|
|
private $collector; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Formatter |
34
|
|
|
*/ |
35
|
|
|
private $formatter; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Stopwatch |
39
|
|
|
*/ |
40
|
|
|
private $stopwatch; |
41
|
|
|
|
42
|
|
|
public function setUp(): void |
43
|
|
|
{ |
44
|
|
|
$this->collector = new Collector([]); |
|
|
|
|
45
|
|
|
$this->formatter = new Formatter(new FullHttpMessageFormatter(), new CurlCommandFormatter()); |
46
|
|
|
$this->stopwatch = new Stopwatch(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testProfilingWithCachePlugin(): void |
50
|
|
|
{ |
51
|
|
|
$client = $this->createClient([ |
52
|
|
|
new Plugin\CachePlugin(new ArrayAdapter(), StreamFactoryDiscovery::find(), [ |
53
|
|
|
'respect_response_cache_directives' => [], |
54
|
|
|
'default_ttl' => 86400, |
55
|
|
|
]), |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
$client->sendRequest(new Request('GET', 'https://example.com')); |
59
|
|
|
$client->sendRequest(new Request('GET', 'https://example.com')); |
60
|
|
|
|
61
|
|
|
$this->assertCount(2, $this->collector->getStacks()); |
|
|
|
|
62
|
|
|
$stack = $this->collector->getStacks()[1]; |
63
|
|
|
$this->assertEquals('GET', $stack->getRequestMethod()); |
64
|
|
|
$this->assertEquals('https', $stack->getRequestScheme()); |
65
|
|
|
$this->assertEquals('/', $stack->getRequestTarget()); |
66
|
|
|
$this->assertEquals('example.com', $stack->getRequestHost()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testProfilingWhenPluginThrowException(): void |
70
|
|
|
{ |
71
|
|
|
$client = $this->createClient([ |
72
|
|
|
new ExceptionThrowerPlugin(), |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$this->expectException(\Exception::class); |
77
|
|
|
$client->sendRequest(new Request('GET', 'https://example.com')); |
78
|
|
|
} finally { |
79
|
|
|
$this->assertCount(1, $this->collector->getStacks()); |
|
|
|
|
80
|
|
|
$stack = $this->collector->getStacks()[0]; |
81
|
|
|
$this->assertEquals('GET', $stack->getRequestMethod()); |
82
|
|
|
$this->assertEquals('https', $stack->getRequestScheme()); |
83
|
|
|
$this->assertEquals('/', $stack->getRequestTarget()); |
84
|
|
|
$this->assertEquals('example.com', $stack->getRequestHost()); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testProfiling(): void |
89
|
|
|
{ |
90
|
|
|
$client = $this->createClient([ |
91
|
|
|
new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri('https://example.com')), |
92
|
|
|
new Plugin\RedirectPlugin(), |
93
|
|
|
new Plugin\RetryPlugin(), |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
$client->sendRequest(new Request('GET', '/')); |
97
|
|
|
|
98
|
|
|
$this->assertCount(1, $this->collector->getStacks()); |
|
|
|
|
99
|
|
|
$stack = $this->collector->getStacks()[0]; |
100
|
|
|
$this->assertCount(3, $stack->getProfiles()); |
|
|
|
|
101
|
|
|
$this->assertEquals('GET', $stack->getRequestMethod()); |
102
|
|
|
$this->assertEquals('https', $stack->getRequestScheme()); |
103
|
|
|
$this->assertEquals('/', $stack->getRequestTarget()); |
104
|
|
|
$this->assertEquals('example.com', $stack->getRequestHost()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function createClient(array $plugins, $clientName = 'Acme', array $clientOptions = []) |
108
|
|
|
{ |
109
|
|
|
$plugins = array_map(function (Plugin $plugin) { |
110
|
|
|
return new ProfilePlugin($plugin, $this->collector, $this->formatter, get_class($plugin)); |
|
|
|
|
111
|
|
|
}, $plugins); |
112
|
|
|
|
113
|
|
|
array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName)); |
114
|
|
|
|
115
|
|
|
$client = new Client(); |
116
|
|
|
$client = new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch); |
117
|
|
|
$client = new PluginClient($client, $plugins, $clientOptions); |
118
|
|
|
|
119
|
|
|
return $client; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
class ExceptionThrowerPlugin implements Plugin |
124
|
|
|
{ |
125
|
|
|
use Plugin\VersionBridgePlugin; |
126
|
|
|
|
127
|
|
|
protected function doHandleRequest(RequestInterface $request, callable $next, callable $first): void |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
throw new \Exception(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.