ServiceInstantiationTest::testHttpClientNoDebug()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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'));
0 ignored issues
show
Bug introduced by
It seems like $client defined by $container->get('httplug.client.acme') on line 89 can also be of type null; however, Nyholm\NSA::getProperty() does only seem to accept object|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
93
94
        $plugins = NSA::getProperty($client, 'plugins');
0 ignored issues
show
Bug introduced by
It seems like $client defined by $container->get('httplug.client.acme') on line 89 can also be of type null; however, Nyholm\NSA::getProperty() does only seem to accept object|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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');
0 ignored issues
show
Bug introduced by
It seems like $client defined by $container->get('httplug.client.my_psr18') on line 112 can also be of type null; however, Nyholm\NSA::getProperty() does only seem to accept object|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$event is of type object, but the function expects a null|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
142
        }
143
144
        return static::$kernel;
145
    }
146
}
147