-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathProfilingTest.php
More file actions
119 lines (98 loc) · 4.25 KB
/
ProfilingTest.php
File metadata and controls
119 lines (98 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
namespace Http\HttplugBundle\Tests\Functional;
use GuzzleHttp\Psr7\Request;
use Http\Client\Common\Plugin;
use Http\Client\Common\PluginClient;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\HttplugBundle\Collector\Collector;
use Http\HttplugBundle\Collector\Formatter;
use Http\HttplugBundle\Collector\ProfileClient;
use Http\HttplugBundle\Collector\ProfilePlugin;
use Http\HttplugBundle\Collector\StackPlugin;
use Http\Message\Formatter\CurlCommandFormatter;
use Http\Message\Formatter\FullHttpMessageFormatter;
use Http\Mock\Client;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Stopwatch\Stopwatch;
final class ProfilingTest extends TestCase
{
private Collector $collector;
private Formatter $formatter;
private Stopwatch $stopwatch;
public function setUp(): void
{
$this->collector = new Collector();
$this->formatter = new Formatter(new FullHttpMessageFormatter(), new CurlCommandFormatter());
$this->stopwatch = new Stopwatch();
}
public function testProfilingWithCachePlugin(): void
{
$client = $this->createClient([
new Plugin\CachePlugin(new ArrayAdapter(), Psr17FactoryDiscovery::findStreamFactory(), [
'respect_response_cache_directives' => [],
'default_ttl' => 86400,
]),
]);
$client->sendRequest(new Request('GET', 'https://example.com'));
$client->sendRequest(new Request('GET', 'https://example.com'));
$this->assertCount(2, $this->collector->getStacks());
$stack = $this->collector->getStacks()[1];
$this->assertEquals('GET', $stack->getRequestMethod());
$this->assertEquals('https', $stack->getRequestScheme());
$this->assertEquals('/', $stack->getRequestTarget());
$this->assertEquals('example.com', $stack->getRequestHost());
}
public function testProfilingWhenPluginThrowException(): void
{
$client = $this->createClient([
new ExceptionThrowerPlugin(),
]);
try {
$this->expectException(\Exception::class);
$client->sendRequest(new Request('GET', 'https://example.com'));
} finally {
$this->assertCount(1, $this->collector->getStacks());
$stack = $this->collector->getStacks()[0];
$this->assertEquals('GET', $stack->getRequestMethod());
$this->assertEquals('https', $stack->getRequestScheme());
$this->assertEquals('/', $stack->getRequestTarget());
$this->assertEquals('example.com', $stack->getRequestHost());
}
}
public function testProfiling(): void
{
$client = $this->createClient([
new Plugin\AddHostPlugin(Psr17FactoryDiscovery::findUriFactory()->createUri('https://example.com')),
new Plugin\RedirectPlugin(),
new Plugin\RetryPlugin(),
]);
$client->sendRequest(new Request('GET', '/'));
$this->assertCount(1, $this->collector->getStacks());
$stack = $this->collector->getStacks()[0];
$this->assertCount(3, $stack->getProfiles());
$this->assertEquals('GET', $stack->getRequestMethod());
$this->assertEquals('https', $stack->getRequestScheme());
$this->assertEquals('/', $stack->getRequestTarget());
$this->assertEquals('example.com', $stack->getRequestHost());
}
private function createClient(array $plugins, string $clientName = 'Acme', array $clientOptions = [])
{
$plugins = array_map(fn (Plugin $plugin) => new ProfilePlugin($plugin, $this->collector, $this->formatter, $plugin::class), $plugins);
array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName));
$client = new Client();
$client = new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch);
$client = new PluginClient($client, $plugins, $clientOptions);
return $client;
}
}
class ExceptionThrowerPlugin implements Plugin
{
use Plugin\VersionBridgePlugin;
protected function doHandleRequest(RequestInterface $request, callable $next, callable $first): void
{
throw new \Exception();
}
}