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

ProfileClientTest::testOnRejected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Http\HttplugBundle\Tests\Unit\Collector;
4
5
use GuzzleHttp\Psr7\Request;
6
use GuzzleHttp\Psr7\Response;
7
use GuzzleHttp\Psr7\Uri;
8
use Http\Client\HttpAsyncClient;
9
use Http\Client\HttpClient;
10
use Http\HttplugBundle\Collector\Collector;
11
use Http\HttplugBundle\Collector\Formatter;
12
use Http\HttplugBundle\Collector\ProfileClient;
13
use Http\HttplugBundle\Collector\Stack;
14
use Http\Promise\FulfilledPromise;
15
use Http\Promise\Promise;
16
use Http\Promise\RejectedPromise;
17
use PHPUnit\Framework\TestCase;
18
use Psr\Http\Message\RequestInterface;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\UriInterface;
21
use Symfony\Component\Stopwatch\Stopwatch;
22
use Symfony\Component\Stopwatch\StopwatchEvent;
23
24
class ProfileClientTest extends TestCase
25
{
26
    /**
27
     * @var Collector
28
     */
29
    private $collector;
30
31
    /**
32
     * @var Stack
33
     */
34
    private $activeStack;
35
36
    /**
37
     * @var HttpClient
38
     */
39
    private $client;
40
41
    /**
42
     * @var RequestInterface
43
     */
44
    private $request;
45
46
    /**
47
     * @var Formatter
48
     */
49
    private $formatter;
50
51
    /**
52
     * @var Stopwatch
53
     */
54
    private $stopwatch;
55
56
    /**
57
     * @var StopwatchEvent
58
     */
59
    private $stopwatchEvent;
60
61
    /**
62
     * @var ProfileClient
63
     */
64
    private $subject;
65
66
    /**
67
     * @var ResponseInterface
68
     */
69
    private $response;
70
71
    /**
72
     * @var Promise
73
     */
74
    private $fulfilledPromise;
75
76
    /**
77
     * @var \Exception
78
     */
79
    private $exception;
80
81
    /**
82
     * @var RejectedPromise
83
     */
84
    private $rejectedPromise;
85
86
    /**
87
     * @var UriInterface
88
     */
89
    private $uri;
90
91
    public function setUp()
92
    {
93
        $this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
94
        $this->activeStack = new Stack('default', 'FormattedRequest');
95
        $this->client = $this->getMockBuilder(ClientInterface::class)->getMock();
96
        $this->uri = new Uri('https://example.com/target');
97
        $this->request = new Request('GET', $this->uri);
98
        $this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
99
        $this->stopwatch = $this->getMockBuilder(Stopwatch::class)->disableOriginalConstructor()->getMock();
100
        $this->stopwatchEvent = $this->getMockBuilder(StopwatchEvent::class)->disableOriginalConstructor()->getMock();
101
        $this->subject = new ProfileClient($this->client, $this->collector, $this->formatter, $this->stopwatch);
102
        $this->response = new Response();
103
        $this->exception = new \Exception();
104
        $this->fulfilledPromise = new FulfilledPromise($this->response);
105
        $this->rejectedPromise = new RejectedPromise($this->exception);
106
107
        $this->collector->method('getActiveStack')->willReturn($this->activeStack);
108
        $this->formatter
109
            ->method('formatResponse')
110
            ->with($this->response)
111
            ->willReturn('FormattedResponse')
112
        ;
113
        $this->formatter
114
            ->method('formatException')
115
            ->with($this->exception)
116
            ->willReturn('FormattedException')
117
        ;
118
119
        $this->stopwatch
120
            ->method('start')
121
            ->willReturn($this->stopwatchEvent)
122
        ;
123
124
        $this->stopwatchEvent
125
            ->method('getDuration')
126
            ->willReturn(42)
127
        ;
128
    }
129
130
    public function testSendRequest()
131
    {
132
        $this->client
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\Client\HttpClient>.

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...
133
            ->expects($this->once())
134
            ->method('sendRequest')
135
            ->with($this->identicalTo($this->request))
136
            ->willReturn($this->response)
137
        ;
138
139
        $response = $this->subject->sendRequest($this->request);
140
141
        $this->assertEquals($this->response, $response);
142
        $this->assertEquals('GET', $this->activeStack->getRequestMethod());
143
        $this->assertEquals('/target', $this->activeStack->getRequestTarget());
144
        $this->assertEquals('example.com', $this->activeStack->getRequestHost());
145
        $this->assertEquals('https', $this->activeStack->getRequestScheme());
146
    }
147
148
    public function testSendAsyncRequest()
149
    {
150
        $this->client
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Http\Client\HttpClient>.

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...
151
            ->expects($this->once())
152
            ->method('sendAsyncRequest')
153
            ->with($this->identicalTo($this->request))
154
            ->willReturn($this->fulfilledPromise)
155
        ;
156
157
        $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...
158
            ->expects($this->once())
159
            ->method('deactivateStack')
160
        ;
161
162
        $promise = $this->subject->sendAsyncRequest($this->request);
163
164
        $this->assertEquals($this->fulfilledPromise, $promise);
165
        $this->assertEquals('GET', $this->activeStack->getRequestMethod());
166
        $this->assertEquals('/target', $this->activeStack->getRequestTarget());
167
        $this->assertEquals('example.com', $this->activeStack->getRequestHost());
168
        $this->assertEquals('https', $this->activeStack->getRequestScheme());
169
    }
170
171
    public function testOnFulfilled()
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('activateStack')
176
            ->with($this->activeStack)
177
        ;
178
179
        $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...
180
            ->expects($this->once())
181
            ->method('stop')
182
        ;
183
184
        $this->client
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Http\Client\HttpClient>.

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...
185
            ->method('sendAsyncRequest')
186
            ->willReturn($this->fulfilledPromise)
187
        ;
188
189
        $this->subject->sendAsyncRequest($this->request);
190
191
        $this->assertEquals(42, $this->activeStack->getDuration());
192
        $this->assertEquals(200, $this->activeStack->getResponseCode());
193
        $this->assertEquals('FormattedResponse', $this->activeStack->getClientResponse());
194
    }
195
196
    public function testOnRejected()
197
    {
198
        $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...
199
            ->expects($this->once())
200
            ->method('activateStack')
201
            ->with($this->activeStack)
202
        ;
203
204
        $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...
205
            ->expects($this->once())
206
            ->method('stop')
207
        ;
208
209
        $this->client
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Http\Client\HttpClient>.

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...
210
            ->method('sendAsyncRequest')
211
            ->willReturn($this->rejectedPromise)
212
        ;
213
214
        $this->subject->sendAsyncRequest($this->request);
215
216
        $this->assertEquals(42, $this->activeStack->getDuration());
217
        $this->assertEquals('FormattedException', $this->activeStack->getClientException());
218
    }
219
}
220
221
interface ClientInterface extends HttpClient, HttpAsyncClient
222
{
223
}
224