1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\Plugin\RedirectPlugin; |
6
|
|
|
use Http\Client\Common\PluginClient; |
7
|
|
|
use Http\Client\HttpClient; |
8
|
|
|
use Http\HttplugBundle\Collector\Collector; |
9
|
|
|
use Http\HttplugBundle\Collector\ProfileClient; |
10
|
|
|
use Http\HttplugBundle\Collector\ProfilePlugin; |
11
|
|
|
use Http\HttplugBundle\Collector\StackPlugin; |
12
|
|
|
use Nyholm\NSA; |
13
|
|
|
use Psr\Http\Client\ClientInterface; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
19
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
20
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
21
|
|
|
use Symfony\Component\HttpKernel\Profiler\Profiler; |
22
|
|
|
|
23
|
|
|
class ServiceInstantiationTest extends WebTestCase |
24
|
|
|
{ |
25
|
|
View Code Duplication |
public function testHttpClient() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
static::bootKernel(); |
28
|
|
|
$container = static::$kernel->getContainer(); |
29
|
|
|
$this->assertTrue($container->has('httplug.client')); |
30
|
|
|
$client = $container->get('httplug.client'); |
31
|
|
|
$this->assertInstanceOf(HttpClient::class, $client); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function testHttpClientNoDebug() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
static::bootKernel(['debug' => false]); |
37
|
|
|
$container = static::$kernel->getContainer(); |
38
|
|
|
$this->assertTrue($container->has('httplug.client')); |
39
|
|
|
$client = $container->get('httplug.client'); |
40
|
|
|
$this->assertInstanceOf(HttpClient::class, $client); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testDebugToolbar() |
44
|
|
|
{ |
45
|
|
|
static::bootKernel(['debug' => true]); |
46
|
|
|
$container = static::$kernel->getContainer(); |
47
|
|
|
$this->assertTrue($container->has('profiler')); |
48
|
|
|
$profiler = $container->get('profiler'); |
49
|
|
|
$this->assertInstanceOf(Profiler::class, $profiler); |
50
|
|
|
$this->assertTrue($profiler->has('httplug')); |
51
|
|
|
$collector = $profiler->get('httplug'); |
52
|
|
|
$this->assertInstanceOf(Collector::class, $collector); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testProfilingShouldNotChangeServiceReference() |
56
|
|
|
{ |
57
|
|
|
static::bootKernel(['debug' => true]); |
58
|
|
|
$container = static::$kernel->getContainer(); |
59
|
|
|
|
60
|
|
|
$this->assertInstanceof(RedirectPlugin::class, $container->get('app.http.plugin.custom')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testProfilingDecoration() |
64
|
|
|
{ |
65
|
|
|
static::bootKernel(['debug' => true]); |
66
|
|
|
$container = static::$kernel->getContainer(); |
67
|
|
|
|
68
|
|
|
$client = $container->get('httplug.client.acme'); |
69
|
|
|
|
70
|
|
|
$this->assertInstanceOf(PluginClient::class, $client); |
71
|
|
|
$this->assertInstanceOf(ProfileClient::class, NSA::getProperty($client, 'client')); |
72
|
|
|
|
73
|
|
|
$plugins = NSA::getProperty($client, 'plugins'); |
74
|
|
|
|
75
|
|
|
$this->assertInstanceOf(StackPlugin::class, $plugins[0]); |
76
|
|
|
$this->assertInstanceOf(ProfilePlugin::class, $plugins[1]); |
77
|
|
|
$this->assertInstanceOf(ProfilePlugin::class, $plugins[2]); |
78
|
|
|
$this->assertInstanceOf(ProfilePlugin::class, $plugins[3]); |
79
|
|
|
$this->assertInstanceOf(ProfilePlugin::class, $plugins[4]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testProfilingPsr18Decoration() |
83
|
|
|
{ |
84
|
|
|
if (!interface_exists(ClientInterface::class)) { |
85
|
|
|
$this->markTestSkipped('PSR-18 is not installed'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
static::bootKernel(['debug' => true, 'environment' => 'psr18']); |
89
|
|
|
$container = static::$kernel->getContainer(); |
90
|
|
|
|
91
|
|
|
$client = $container->get('httplug.client.my_psr18'); |
92
|
|
|
$this->assertInstanceOf(PluginClient::class, $client); |
93
|
|
|
$profileClient = NSA::getProperty($client, 'client'); |
94
|
|
|
$this->assertInstanceOf(ProfileClient::class, $profileClient); |
95
|
|
|
|
96
|
|
|
$flexibleClient = NSA::getProperty($profileClient, 'client'); |
97
|
|
|
$psr18Client = NSA::getProperty($flexibleClient, 'httpClient'); |
98
|
|
|
$this->assertInstanceOf(ClientInterface::class, $psr18Client); |
99
|
|
|
|
100
|
|
|
$response = $client->sendRequest(new \GuzzleHttp\Psr7\Request('GET', 'https://example.com')); |
101
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
protected static function bootKernel(array $options = []) |
108
|
|
|
{ |
109
|
|
|
parent::bootKernel($options); |
110
|
|
|
|
111
|
|
|
/** @var EventDispatcherInterface $dispatcher */ |
112
|
|
|
$dispatcher = static::$kernel->getContainer()->get('event_dispatcher'); |
113
|
|
|
|
114
|
|
|
$event = new GetResponseEvent(static::$kernel, new Request(), HttpKernelInterface::MASTER_REQUEST); |
115
|
|
|
|
116
|
|
|
$dispatcher->dispatch(KernelEvents::REQUEST, $event); |
117
|
|
|
|
118
|
|
|
return static::$kernel; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.