1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Http\HttplugBundle\Tests\Functional; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\Request as GuzzleRequest; |
8
|
|
|
use Http\Adapter\Guzzle7\Client; |
9
|
|
|
use Http\Client\Common\Plugin\RedirectPlugin; |
10
|
|
|
use Http\Client\Common\PluginClient; |
11
|
|
|
use Http\Client\HttpClient; |
12
|
|
|
use Http\HttplugBundle\Collector\Collector; |
13
|
|
|
use Http\HttplugBundle\Collector\ProfileClient; |
14
|
|
|
use Http\HttplugBundle\Collector\ProfilePlugin; |
15
|
|
|
use Http\HttplugBundle\Collector\StackPlugin; |
16
|
|
|
use Nyholm\NSA; |
17
|
|
|
use Psr\Http\Client\ClientInterface; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
20
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request as SymfonyRequest; |
22
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
23
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
24
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
25
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
26
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
27
|
|
|
use Symfony\Component\HttpKernel\Profiler\Profiler; |
28
|
|
|
|
29
|
|
|
class ServiceInstantiationTest extends WebTestCase |
30
|
|
|
{ |
31
|
|
View Code Duplication |
public function testHttpClient(): void |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
if (!class_exists(Client::class)) { |
34
|
|
|
$this->markTestSkipped('Guzzle7 adapter is not installed'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
static::bootKernel(); |
38
|
|
|
$container = static::$kernel->getContainer(); |
39
|
|
|
$this->assertTrue($container->has('httplug.client')); |
40
|
|
|
$client = $container->get('httplug.client'); |
41
|
|
|
$this->assertInstanceOf(HttpClient::class, $client); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function testHttpClientNoDebug(): void |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
if (!class_exists(Client::class)) { |
47
|
|
|
$this->markTestSkipped('Guzzle7 adapter is not installed'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
static::bootKernel(['debug' => false]); |
51
|
|
|
$container = static::$kernel->getContainer(); |
52
|
|
|
$this->assertTrue($container->has('httplug.client')); |
53
|
|
|
$client = $container->get('httplug.client'); |
54
|
|
|
$this->assertInstanceOf(HttpClient::class, $client); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @group legacy |
59
|
|
|
*/ |
60
|
|
|
public function testDebugToolbar(): void |
61
|
|
|
{ |
62
|
|
|
static::bootKernel(['debug' => true]); |
63
|
|
|
$container = static::$kernel->getContainer(); |
64
|
|
|
$this->assertTrue($container->has('profiler')); |
65
|
|
|
$profiler = $container->get('profiler'); |
66
|
|
|
$this->assertInstanceOf(Profiler::class, $profiler); |
67
|
|
|
$this->assertTrue($profiler->has('httplug')); |
68
|
|
|
$collector = $profiler->get('httplug'); |
69
|
|
|
$this->assertInstanceOf(Collector::class, $collector); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testProfilingShouldNotChangeServiceReference(): void |
73
|
|
|
{ |
74
|
|
|
static::bootKernel(['debug' => true]); |
75
|
|
|
$container = static::$kernel->getContainer(); |
76
|
|
|
|
77
|
|
|
$this->assertInstanceof(RedirectPlugin::class, $container->get('app.http.plugin.custom')); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testProfilingDecoration(): void |
81
|
|
|
{ |
82
|
|
|
if (!class_exists(Client::class)) { |
83
|
|
|
$this->markTestSkipped('Guzzle7 adapter is not installed'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
static::bootKernel(['debug' => true]); |
87
|
|
|
$container = static::$kernel->getContainer(); |
88
|
|
|
|
89
|
|
|
$client = $container->get('httplug.client.acme'); |
90
|
|
|
|
91
|
|
|
$this->assertInstanceOf(PluginClient::class, $client); |
92
|
|
|
$this->assertInstanceOf(ProfileClient::class, NSA::getProperty($client, 'client')); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$plugins = NSA::getProperty($client, 'plugins'); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$this->assertInstanceOf(StackPlugin::class, $plugins[0]); |
97
|
|
|
$this->assertInstanceOf(ProfilePlugin::class, $plugins[1]); |
98
|
|
|
$this->assertInstanceOf(ProfilePlugin::class, $plugins[2]); |
99
|
|
|
$this->assertInstanceOf(ProfilePlugin::class, $plugins[3]); |
100
|
|
|
$this->assertInstanceOf(ProfilePlugin::class, $plugins[4]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testProfilingPsr18Decoration(): void |
104
|
|
|
{ |
105
|
|
|
if (!interface_exists(ClientInterface::class)) { |
106
|
|
|
$this->markTestSkipped('PSR-18 is not installed'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
static::bootKernel(['debug' => true, 'environment' => 'psr18']); |
110
|
|
|
$container = static::$kernel->getContainer(); |
111
|
|
|
|
112
|
|
|
$client = $container->get('httplug.client.my_psr18'); |
113
|
|
|
$this->assertInstanceOf(PluginClient::class, $client); |
114
|
|
|
$profileClient = NSA::getProperty($client, 'client'); |
|
|
|
|
115
|
|
|
$this->assertInstanceOf(ProfileClient::class, $profileClient); |
116
|
|
|
|
117
|
|
|
$flexibleClient = NSA::getProperty($profileClient, 'client'); |
118
|
|
|
$psr18Client = NSA::getProperty($flexibleClient, 'httpClient'); |
119
|
|
|
$this->assertInstanceOf(ClientInterface::class, $psr18Client); |
120
|
|
|
|
121
|
|
|
$response = $client->sendRequest(new GuzzleRequest('GET', 'https://example.com')); |
122
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
protected static function bootKernel(array $options = []) |
129
|
|
|
{ |
130
|
|
|
parent::bootKernel($options); |
131
|
|
|
|
132
|
|
|
/** @var EventDispatcherInterface $dispatcher */ |
133
|
|
|
$dispatcher = static::$kernel->getContainer()->get('event_dispatcher'); |
134
|
|
|
|
135
|
|
|
$class = (Kernel::MAJOR_VERSION >= 5) ? RequestEvent::class : GetResponseEvent::class; |
136
|
|
|
$event = new $class(static::$kernel, SymfonyRequest::create('/'), HttpKernelInterface::MASTER_REQUEST); |
137
|
|
|
|
138
|
|
|
if (version_compare(Kernel::VERSION, '4.3.0', '>=')) { |
139
|
|
|
$dispatcher->dispatch($event, KernelEvents::REQUEST); |
140
|
|
|
} else { |
141
|
|
|
$dispatcher->dispatch(KernelEvents::REQUEST, $event); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return static::$kernel; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.