|
| 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 | +} |
0 commit comments