ProfileClientTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Tests\Unit\Collector;
6
7
use GuzzleHttp\Psr7\Request;
8
use GuzzleHttp\Psr7\Response;
9
use GuzzleHttp\Psr7\Uri;
10
use Http\Client\HttpAsyncClient;
11
use Http\Client\HttpClient;
12
use Http\HttplugBundle\Collector\Collector;
13
use Http\HttplugBundle\Collector\Formatter;
14
use Http\HttplugBundle\Collector\ProfileClient;
15
use Http\HttplugBundle\Collector\Stack;
16
use Http\Promise\FulfilledPromise;
17
use Http\Promise\Promise;
18
use Http\Promise\RejectedPromise;
19
use PHPUnit\Framework\MockObject\MockObject;
20
use PHPUnit\Framework\TestCase;
21
use Psr\Http\Message\RequestInterface;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\UriInterface;
24
use Symfony\Component\Stopwatch\Stopwatch;
25
use Symfony\Component\Stopwatch\StopwatchEvent;
26
27
class ProfileClientTest extends TestCase
28
{
29
    /**
30
     * @var Collector
31
     */
32
    private $collector;
33
34
    /**
35
     * @var Stack
36
     */
37
    private $activeStack;
38
39
    /**
40
     * @var HttpClient|MockObject
41
     */
42
    private $client;
43
44
    /**
45
     * @var RequestInterface
46
     */
47
    private $request;
48
49
    /**
50
     * @var Formatter|MockObject
51
     */
52
    private $formatter;
53
54
    /**
55
     * @var Stopwatch
56
     */
57
    private $stopwatch;
58
59
    /**
60
     * @var StopwatchEvent
61
     */
62
    private $stopwatchEvent;
63
64
    /**
65
     * @var ProfileClient
66
     */
67
    private $subject;
68
69
    /**
70
     * @var ResponseInterface
71
     */
72
    private $response;
73
74
    /**
75
     * @var Promise
76
     */
77
    private $fulfilledPromise;
78
79
    /**
80
     * @var \Exception
81
     */
82
    private $exception;
83
84
    /**
85
     * @var RejectedPromise
86
     */
87
    private $rejectedPromise;
88
89
    /**
90
     * @var UriInterface
91
     */
92
    private $uri;
93
94
    public function setUp(): void
95
    {
96
        $this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\H...onstructor()->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Http\HttplugBundle\Collector\Collector> of property $collector.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
97
        $this->activeStack = new Stack('default', 'FormattedRequest');
98
        $this->client = $this->getMockBuilder(ClientInterface::class)->getMock();
99
        $this->uri = new Uri('https://example.com/target');
100
        $this->request = new Request('GET', $this->uri);
101
        $this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
102
        $this->stopwatch = $this->getMockBuilder(Stopwatch::class)->disableOriginalConstructor()->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\S...onstructor()->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component\Stopwatch\Stopwatch> of property $stopwatch.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
103
        $this->stopwatchEvent = $this->getMockBuilder(StopwatchEvent::class)->disableOriginalConstructor()->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\S...onstructor()->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...opwatch\StopwatchEvent> of property $stopwatchEvent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
104
        $this->subject = new ProfileClient($this->client, $this->collector, $this->formatter, $this->stopwatch);
0 ignored issues
show
Documentation introduced by
$this->client is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Http\Client\HttpC...Client\HttpAsyncClient>.

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...
Documentation introduced by
$this->collector is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Http\HttplugBundle\Collector\Collector>.

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...
Documentation introduced by
$this->formatter is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Http\HttplugBundle\Collector\Formatter>.

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...
Documentation introduced by
$this->stopwatch is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component\Stopwatch\Stopwatch>.

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...
105
        $this->response = new Response();
106
        $this->exception = new \Exception();
107
        $this->fulfilledPromise = new FulfilledPromise($this->response);
108
        $this->rejectedPromise = new RejectedPromise($this->exception);
109
110
        $this->collector->method('getActiveStack')->willReturn($this->activeStack);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
        $this->formatter
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
            ->method('formatResponse')
113
            ->with($this->response)
114
            ->willReturn('FormattedResponse')
115
        ;
116
117
        $this->stopwatch
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
            ->method('start')
119
            ->willReturn($this->stopwatchEvent)
120
        ;
121
122
        $this->stopwatchEvent
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
            ->method('getDuration')
124
            ->willReturn(42)
125
        ;
126
    }
127
128
    public function testSendRequest(): void
129
    {
130
        $this->client
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Http\Client\HttpClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
131
            ->expects($this->once())
132
            ->method('sendRequest')
133
            ->with($this->identicalTo($this->request))
134
            ->willReturn($this->response)
135
        ;
136
137
        $response = $this->subject->sendRequest($this->request);
138
139
        $this->assertEquals($this->response, $response);
140
        $this->assertEquals('GET', $this->activeStack->getRequestMethod());
141
        $this->assertEquals('/target', $this->activeStack->getRequestTarget());
142
        $this->assertEquals('example.com', $this->activeStack->getRequestHost());
143
        $this->assertEquals('https', $this->activeStack->getRequestScheme());
144
    }
145
146
    public function testSendRequestTypeError()
147
    {
148
        $this->client
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Http\Client\HttpClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
149
            ->expects($this->once())
150
            ->method('sendRequest')
151
            ->willReturnCallback(function () {
152
                throw new \Error('You set string to int prop');
0 ignored issues
show
Unused Code introduced by
The call to Error::__construct() has too many arguments starting with 'You set string to int prop'.

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...
153
            });
154
        $this->formatter
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Http\HttplugBundle\Collector\Formatter.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
155
            ->expects($this->once())
156
            ->method('formatException')
157
            ->with($this->isInstanceOf(\Error::class));
158
159
        $this->expectException(\Error::class);
160
        $this->expectExceptionMessage('You set string to int prop');
161
        $this->subject->sendRequest($this->request);
162
    }
163
164
    public function testSendAsyncRequest(): void
165
    {
166
        $this->client
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Http\Client\HttpClient.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
167
            ->expects($this->once())
168
            ->method('sendAsyncRequest')
169
            ->with($this->identicalTo($this->request))
170
            ->willReturn($this->fulfilledPromise)
171
        ;
172
173
        $this->collector
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\HttplugBundle\Collector\Collector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
174
            ->expects($this->once())
175
            ->method('deactivateStack')
176
        ;
177
178
        $promise = $this->subject->sendAsyncRequest($this->request);
179
180
        $this->assertEquals($this->fulfilledPromise, $promise);
181
        $this->assertEquals('GET', $this->activeStack->getRequestMethod());
182
        $this->assertEquals('/target', $this->activeStack->getRequestTarget());
183
        $this->assertEquals('example.com', $this->activeStack->getRequestHost());
184
        $this->assertEquals('https', $this->activeStack->getRequestScheme());
185
    }
186
187 View Code Duplication
    public function testOnFulfilled(): 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...
188
    {
189
        $this->collector
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\HttplugBundle\Collector\Collector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
190
            ->expects($this->once())
191
            ->method('activateStack')
192
            ->with($this->activeStack)
193
        ;
194
195
        $this->stopwatchEvent
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...opwatch\StopwatchEvent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
196
            ->expects($this->once())
197
            ->method('stop')
198
        ;
199
200
        $this->client
201
            ->method('sendAsyncRequest')
202
            ->willReturn($this->fulfilledPromise)
203
        ;
204
205
        $this->subject->sendAsyncRequest($this->request);
206
207
        $this->assertEquals(42, $this->activeStack->getDuration());
208
        $this->assertEquals(200, $this->activeStack->getResponseCode());
209
        $this->assertEquals('FormattedResponse', $this->activeStack->getClientResponse());
210
    }
211
212 View Code Duplication
    public function testOnRejected(): 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...
213
    {
214
        $this->collector
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\HttplugBundle\Collector\Collector>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
215
            ->expects($this->once())
216
            ->method('activateStack')
217
            ->with($this->activeStack)
218
        ;
219
220
        $this->stopwatchEvent
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...opwatch\StopwatchEvent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
221
            ->expects($this->once())
222
            ->method('stop')
223
        ;
224
225
        $this->client
226
            ->method('sendAsyncRequest')
227
            ->willReturn($this->rejectedPromise)
228
        ;
229
230
        $this->formatter
231
            ->method('formatException')
232
            ->with($this->exception)
233
            ->willReturn('FormattedException')
234
        ;
235
236
        $this->subject->sendAsyncRequest($this->request);
237
238
        $this->assertEquals(42, $this->activeStack->getDuration());
239
        $this->assertEquals('FormattedException', $this->activeStack->getClientException());
240
    }
241
}
242
243
interface ClientInterface extends HttpClient, HttpAsyncClient
244
{
245
}
246