Completed
Push — master ( b587fb...d05392 )
by David
20:34
created

ProfilingTest::createClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\Discovery\UriFactoryDiscovery;
10
use Http\HttplugBundle\Collector\Collector;
11
use Http\HttplugBundle\Collector\Formatter;
12
use Http\HttplugBundle\Collector\ProfileClient;
13
use Http\HttplugBundle\Collector\ProfilePlugin;
14
use Http\HttplugBundle\Collector\StackPlugin;
15
use Http\Message\Formatter\CurlCommandFormatter;
16
use Http\Message\Formatter\FullHttpMessageFormatter;
17
use Http\Mock\Client;
18
use PHPUnit\Framework\TestCase;
19
use Psr\Http\Message\RequestInterface;
20
use Symfony\Component\Cache\Adapter\ArrayAdapter;
21
use Symfony\Component\Stopwatch\Stopwatch;
22
23
class ProfilingTest extends TestCase
24
{
25
    /**
26
     * @var Collector
27
     */
28
    private $collector;
29
30
    /**
31
     * @var Formatter
32
     */
33
    private $formatter;
34
35
    /**
36
     * @var Stopwatch
37
     */
38
    private $stopwatch;
39
40
    public function setUp()
41
    {
42
        $this->collector = new Collector([]);
0 ignored issues
show
Unused Code introduced by
The call to Collector::__construct() has too many arguments starting with array().

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.

Loading history...
43
        $this->formatter = new Formatter(new FullHttpMessageFormatter(), new CurlCommandFormatter());
44
        $this->stopwatch = new Stopwatch();
45
    }
46
47
    public function testProfilingWithCachePlugin()
48
    {
49
        $client = $this->createClient([
50
            new Plugin\CachePlugin(new ArrayAdapter(), StreamFactoryDiscovery::find(), [
51
                'respect_response_cache_directives' => [],
52
                'default_ttl' => 86400,
53
            ]),
54
        ]);
55
56
        $client->sendRequest(new Request('GET', 'https://example.com'));
57
        $client->sendRequest(new Request('GET', 'https://example.com'));
58
59
        $this->assertCount(2, $this->collector->getStacks());
60
        $stack = $this->collector->getStacks()[1];
61
        $this->assertEquals('GET', $stack->getRequestMethod());
62
        $this->assertEquals('https', $stack->getRequestScheme());
63
        $this->assertEquals('/', $stack->getRequestTarget());
64
        $this->assertEquals('example.com', $stack->getRequestHost());
65
    }
66
67
    /**
68
     * @expectedException \Exception
69
     */
70
    public function testProfilingWhenPluginThrowException()
71
    {
72
        $client = $this->createClient([
73
            new ExceptionThrowerPlugin(),
74
        ]);
75
76
        try {
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()
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));
0 ignored issues
show
Unused Code introduced by
The call to ProfilePlugin::__construct() has too many arguments starting with get_class($plugin).

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.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $next is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $first is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
        throw new \Exception();
130
    }
131
}
132