Skip to content

Commit 2cbceeb

Browse files
committed
add failing profiling test when using cache plugin
1 parent 7447f5a commit 2cbceeb

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

Tests/Functional/ProfilingTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Tests\Functional;
4+
5+
use GuzzleHttp\Psr7\Request;
6+
use Http\Client\Common\Plugin;
7+
use Http\Client\Common\PluginClient;
8+
use Http\Discovery\StreamFactoryDiscovery;
9+
use Http\HttplugBundle\Collector\Collector;
10+
use Http\HttplugBundle\Collector\Formatter;
11+
use Http\HttplugBundle\Collector\ProfileClient;
12+
use Http\HttplugBundle\Collector\ProfilePlugin;
13+
use Http\HttplugBundle\Collector\StackPlugin;
14+
use Http\Message\Formatter\CurlCommandFormatter;
15+
use Http\Message\Formatter\FullHttpMessageFormatter;
16+
use Http\Mock\Client;
17+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
18+
use Symfony\Component\Stopwatch\Stopwatch;
19+
20+
class ProfilingTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @var Collector
24+
*/
25+
private $collector;
26+
27+
/**
28+
* @var Formatter
29+
*/
30+
private $formatter;
31+
32+
/**
33+
* @var Stopwatch
34+
*/
35+
private $stopwatch;
36+
37+
public function setUp()
38+
{
39+
$this->collector = new Collector([]);
40+
$this->formatter = new Formatter(new FullHttpMessageFormatter(), new CurlCommandFormatter());
41+
$this->stopwatch = new Stopwatch();
42+
}
43+
44+
public function testCachePluginProfiling()
45+
{
46+
$pool = new ArrayAdapter();
47+
48+
$client = $this->createClient([
49+
new Plugin\CachePlugin($pool, StreamFactoryDiscovery::find(), [
50+
'respect_response_cache_directives' => [],
51+
'default_ttl' => 86400,
52+
]),
53+
]);
54+
55+
$client->sendRequest(new Request('GET', 'https://example.com'));
56+
$client->sendRequest(new Request('GET', 'https://example.com'));
57+
58+
$this->assertCount(2, $this->collector->getStacks());
59+
$stack = $this->collector->getStacks()[1];
60+
$this->assertEquals('GET', $stack->getRequestMethod());
61+
$this->assertEquals('https', $stack->getRequestScheme());
62+
$this->assertEquals('/', $stack->getRequestTarget());
63+
$this->assertEquals('example.com', $stack->getRequestHost());
64+
}
65+
66+
private function createClient(array $plugins, $clientName = 'Acme', array $clientOptions = [])
67+
{
68+
$plugins = array_map(function (Plugin $plugin) {
69+
return new ProfilePlugin($plugin, $this->collector, $this->formatter, get_class($plugin));
70+
}, $plugins);
71+
72+
array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName));
73+
74+
$client = new Client();
75+
$client = new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch);
76+
$client = new PluginClient($client, $plugins, $clientOptions);
77+
78+
return $client;
79+
}
80+
}

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@
3737
"php-http/guzzle6-adapter": "^1.1.1",
3838
"php-http/react-adapter": "^0.2.1",
3939
"php-http/buzz-adapter": "^0.3",
40+
"php-http/mock-client": "^1.0",
4041
"symfony/phpunit-bridge": "^3.2",
4142
"symfony/twig-bundle": "^2.8 || ^3.0",
4243
"symfony/twig-bridge": "^2.8 || ^3.0",
4344
"symfony/web-profiler-bundle": "^2.8 || ^3.0",
4445
"symfony/finder": "^2.7 || ^3.0",
46+
"symfony/cache": "^3.1",
4547
"polishsymfonycommunity/symfony-mocker-container": "^1.0",
4648
"matthiasnoback/symfony-dependency-injection-test": "^1.0"
4749
},

0 commit comments

Comments
 (0)