Skip to content

Commit 1b16d97

Browse files
authored
Merge pull request #440 from php-http/fix-deprecations
fix some deprecations
2 parents 143dba2 + cec50f6 commit 1b16d97

13 files changed

+170
-263
lines changed

Diff for: composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"php-http/discovery": "^1.14",
3232
"php-http/httplug": "^2.0",
3333
"php-http/logger-plugin": "^1.1",
34-
"php-http/message": "^1.9",
34+
"php-http/message": "^1.13",
3535
"php-http/message-factory": "^1.0.2",
3636
"php-http/stopwatch-plugin": "^1.2",
3737
"psr/http-message": "^1.0 || ^2.0",
@@ -73,7 +73,8 @@
7373
"config": {
7474
"sort-packages": true,
7575
"allow-plugins": {
76-
"symfony/flex": true
76+
"symfony/flex": true,
77+
"php-http/discovery": false
7778
}
7879
},
7980
"autoload": {

Diff for: src/Collector/Formatter.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Formatter implements MessageFormatter
3333
*/
3434
private $curlFormatter;
3535

36-
public function __construct(MessageFormatter $formatter, CurlCommandFormatter $curlFormatter)
36+
public function __construct(MessageFormatter $formatter, MessageFormatter $curlFormatter)
3737
{
3838
$this->formatter = $formatter;
3939
$this->curlFormatter = $curlFormatter;
@@ -47,7 +47,7 @@ public function __construct(MessageFormatter $formatter, CurlCommandFormatter $c
4747
public function formatException(\Throwable $exception)
4848
{
4949
if ($exception instanceof HttpException) {
50-
return $this->formatter->formatResponse($exception->getResponse());
50+
return $this->formatter->formatResponseForRequest($exception->getResponse(), $exception->getRequest());
5151
}
5252

5353
if ($exception instanceof TransferException || $exception instanceof NetworkExceptionInterface) {
@@ -74,9 +74,6 @@ public function formatResponseForRequest(ResponseInterface $response, RequestInt
7474
return $this->formatter->formatResponse($response);
7575
}
7676

77-
/**
78-
* {@inheritdoc}
79-
*/
8077
public function formatResponse(ResponseInterface $response)
8178
{
8279
return $this->formatter->formatResponse($response);

Diff for: src/Collector/ProfileClient.php

+9-21
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ public function __construct($client, Collector $collector, Formatter $formatter,
7070
$this->stopwatch = $stopwatch;
7171
}
7272

73-
/**
74-
* {@inheritdoc}
75-
*/
7673
public function sendAsyncRequest(RequestInterface $request)
7774
{
7875
$activateStack = true;
@@ -89,8 +86,8 @@ public function sendAsyncRequest(RequestInterface $request)
8986
$this->collectRequestInformations($request, $stack);
9087
$event = $this->stopwatch->start($this->getStopwatchEventName($request), self::STOPWATCH_CATEGORY);
9188

92-
$onFulfilled = function (ResponseInterface $response) use ($event, $stack) {
93-
$this->collectResponseInformations($response, $event, $stack);
89+
$onFulfilled = function (ResponseInterface $response) use ($request, $event, $stack) {
90+
$this->collectResponseInformations($request, $response, $event, $stack);
9491
$event->stop();
9592

9693
return $response;
@@ -134,13 +131,9 @@ protected function doSendRequest(RequestInterface $request)
134131

135132
try {
136133
$response = $this->client->sendRequest($request);
137-
$this->collectResponseInformations($response, $event, $stack);
134+
$this->collectResponseInformations($request, $response, $event, $stack);
138135

139136
return $response;
140-
} catch (\Exception $e) {
141-
$this->collectExceptionInformations($e, $event, $stack);
142-
143-
throw $e;
144137
} catch (\Throwable $e) {
145138
$this->collectExceptionInformations($e, $event, $stack);
146139

@@ -150,7 +143,7 @@ protected function doSendRequest(RequestInterface $request)
150143
}
151144
}
152145

153-
private function collectRequestInformations(RequestInterface $request, Stack $stack)
146+
private function collectRequestInformations(RequestInterface $request, Stack $stack): void
154147
{
155148
$uri = $request->getUri();
156149
$stack->setRequestTarget($request->getRequestTarget());
@@ -162,29 +155,24 @@ private function collectRequestInformations(RequestInterface $request, Stack $st
162155
$stack->setCurlCommand($this->formatter->formatAsCurlCommand($request));
163156
}
164157

165-
private function collectResponseInformations(ResponseInterface $response, StopwatchEvent $event, Stack $stack)
158+
private function collectResponseInformations(RequestInterface $request, ResponseInterface $response, StopwatchEvent $event, Stack $stack): void
166159
{
167160
$stack->setDuration($event->getDuration());
168161
$stack->setResponseCode($response->getStatusCode());
169-
$stack->setClientResponse($this->formatter->formatResponse($response));
162+
$stack->setClientResponse($this->formatter->formatResponseForRequest($response, $request));
170163
}
171164

172-
private function collectExceptionInformations(\Throwable $exception, StopwatchEvent $event, Stack $stack)
165+
private function collectExceptionInformations(\Throwable $exception, StopwatchEvent $event, Stack $stack): void
173166
{
174167
if ($exception instanceof HttpException) {
175-
$this->collectResponseInformations($exception->getResponse(), $event, $stack);
168+
$this->collectResponseInformations($exception->getRequest(), $exception->getResponse(), $event, $stack);
176169
}
177170

178171
$stack->setDuration($event->getDuration());
179172
$stack->setClientException($this->formatter->formatException($exception));
180173
}
181174

182-
/**
183-
* Generates the event name.
184-
*
185-
* @return string
186-
*/
187-
private function getStopwatchEventName(RequestInterface $request)
175+
private function getStopwatchEventName(RequestInterface $request): string
188176
{
189177
$name = sprintf('%s %s', $request->getMethod(), $request->getUri());
190178

Diff for: src/Collector/ProfilePlugin.php

+10-13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
4848
$profile = new Profile(get_class($this->plugin));
4949

5050
$stack = $this->collector->getActiveStack();
51+
if (null === $stack) {
52+
throw new \LogicException('No active stack');
53+
}
5154
$stack->addProfile($profile);
5255

5356
// wrap the next callback to profile the plugin request changes
@@ -83,39 +86,33 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
8386
});
8487
}
8588

86-
/**
87-
* @param Stack $stack
88-
*/
8989
private function onException(
9090
RequestInterface $request,
9191
Profile $profile,
9292
Exception $exception,
93-
Stack $stack = null
94-
) {
93+
Stack $stack
94+
): void {
9595
$profile->setFailed(true);
9696
$profile->setResponse($this->formatter->formatException($exception));
9797
$this->collectRequestInformation($request, $stack);
9898
}
9999

100-
private function onOutgoingRequest(RequestInterface $request, Profile $profile)
100+
private function onOutgoingRequest(RequestInterface $request, Profile $profile): void
101101
{
102102
$profile->setRequest($this->formatter->formatRequest($request));
103103
}
104104

105-
/**
106-
* @param Stack $stack
107-
*/
108-
private function onOutgoingResponse(ResponseInterface $response, Profile $profile, RequestInterface $request, Stack $stack = null)
105+
private function onOutgoingResponse(ResponseInterface $response, Profile $profile, RequestInterface $request, Stack $stack): void
109106
{
110-
$profile->setResponse($this->formatter->formatResponse($response));
107+
$profile->setResponse($this->formatter->formatResponseForRequest($response, $request));
111108
$this->collectRequestInformation($request, $stack);
112109
}
113110

114111
/**
115112
* Collect request information when not already done by the HTTP client. This happens when using the CachePlugin
116113
* and the cache is hit without re-validation.
117114
*/
118-
private function collectRequestInformation(RequestInterface $request, Stack $stack = null)
115+
private function collectRequestInformation(RequestInterface $request, Stack $stack): void
119116
{
120117
$uri = $request->getUri();
121118
if (empty($stack->getRequestTarget())) {
@@ -127,7 +124,7 @@ private function collectRequestInformation(RequestInterface $request, Stack $sta
127124
if (empty($stack->getRequestScheme())) {
128125
$stack->setRequestScheme($uri->getScheme());
129126
}
130-
if (empty($stack->getRequestPort())) {
127+
if (null === $stack->getRequestPort()) {
131128
$stack->setRequestPort($uri->getPort());
132129
}
133130
if (empty($stack->getRequestHost())) {

Diff for: src/Collector/StackPlugin.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
5353
$this->collector->addStack($stack);
5454
$this->collector->activateStack($stack);
5555

56-
$onFulfilled = function (ResponseInterface $response) use ($stack) {
57-
$stack->setResponse($this->formatter->formatResponse($response));
56+
$onFulfilled = function (ResponseInterface $response) use ($stack, $request) {
57+
$stack->setResponse($this->formatter->formatResponseForRequest($response, $request));
5858

5959
return $response;
6060
};

Diff for: tests/Unit/ClientFactory/CurlFactoryTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public function testCreateClient(): void
2222
}
2323

2424
$factory = new CurlFactory(
25-
$this->getMockBuilder(ResponseFactoryInterface::class)->getMock(),
26-
$this->getMockBuilder(StreamFactoryInterface::class)->getMock()
25+
$this->createMock(ResponseFactoryInterface::class),
26+
$this->createMock(StreamFactoryInterface::class)
2727
);
2828
$client = $factory->createClient();
2929

Diff for: tests/Unit/ClientFactory/SymfonyFactoryTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public function testCreateClient(): void
2222
}
2323

2424
$factory = new SymfonyFactory(
25-
$this->getMockBuilder(ResponseFactoryInterface::class)->getMock(),
26-
$this->getMockBuilder(StreamFactoryInterface::class)->getMock()
25+
$this->createMock(ResponseFactoryInterface::class),
26+
$this->createMock(StreamFactoryInterface::class)
2727
);
2828
$client = $factory->createClient();
2929

Diff for: tests/Unit/Collector/FormatterTest.php

+32-8
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111
use Http\HttplugBundle\Collector\Formatter;
1212
use Http\Message\Formatter as MessageFormatter;
1313
use Http\Message\Formatter\CurlCommandFormatter;
14+
use Http\Message\Formatter\SimpleFormatter;
1415
use PHPUnit\Framework\MockObject\MockObject;
1516
use PHPUnit\Framework\TestCase;
1617

1718
class FormatterTest extends TestCase
1819
{
1920
/**
20-
* @var MessageFormatter|MockObject
21+
* @var MessageFormatter&MockObject
2122
*/
2223
private $formatter;
2324

2425
/**
25-
* @var CurlCommandFormatter|MockObject
26+
* @var CurlCommandFormatter&MockObject
2627
*/
2728
private $curlFormatter;
2829

@@ -33,8 +34,8 @@ class FormatterTest extends TestCase
3334

3435
public function setUp(): void
3536
{
36-
$this->formatter = $this->getMockBuilder(MessageFormatter::class)->getMock();
37-
$this->curlFormatter = $this->getMockBuilder(CurlCommandFormatter::class)->getMock();
37+
$this->formatter = $this->createMock(MessageFormatter::class);
38+
$this->curlFormatter = $this->createMock(CurlCommandFormatter::class);
3839

3940
$this->subject = new Formatter($this->formatter, $this->curlFormatter);
4041
}
@@ -52,6 +53,26 @@ public function testFormatRequest(): void
5253
$this->subject->formatRequest($request);
5354
}
5455

56+
public function testFormatResponseForRequest(): void
57+
{
58+
$formatter = $this->createMock(SimpleFormatter::class);
59+
$subject = new Formatter($formatter, $this->curlFormatter);
60+
61+
$response = new Response();
62+
$request = new Request('GET', '/');
63+
64+
$formatter
65+
->expects($this->once())
66+
->method('formatResponseForRequest')
67+
->with($this->identicalTo($response), $this->identicalTo($request))
68+
;
69+
70+
$subject->formatResponseForRequest($response, $request);
71+
}
72+
73+
/**
74+
* @group legacy
75+
*/
5576
public function testFormatResponse(): void
5677
{
5778
$response = new Response();
@@ -67,18 +88,21 @@ public function testFormatResponse(): void
6788

6889
public function testFormatHttpException(): void
6990
{
91+
$formatter = $this->createMock(SimpleFormatter::class);
92+
$subject = new Formatter($formatter, $this->curlFormatter);
93+
7094
$request = new Request('GET', '/');
7195
$response = new Response();
7296
$exception = new HttpException('', $request, $response);
7397

74-
$this->formatter
98+
$formatter
7599
->expects($this->once())
76-
->method('formatResponse')
77-
->with($this->identicalTo($response))
100+
->method('formatResponseForRequest')
101+
->with($this->identicalTo($response), $this->identicalTo($request))
78102
->willReturn('FormattedException')
79103
;
80104

81-
$this->assertEquals('FormattedException', $this->subject->formatException($exception));
105+
$this->assertEquals('FormattedException', $subject->formatException($exception));
82106
}
83107

84108
public function testFormatTransferException(): void

Diff for: tests/Unit/Collector/PluginClientFactoryListenerTest.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Http\HttplugBundle\Collector\Formatter;
1010
use Http\HttplugBundle\Collector\PluginClientFactory;
1111
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
12+
use Http\Message\Formatter as MessageFormatter;
1213
use Nyholm\NSA;
1314
use PHPUnit\Framework\TestCase;
1415
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
@@ -20,9 +21,9 @@ final class PluginClientFactoryListenerTest extends TestCase
2021
{
2122
public function testRegisterPluginClientFactory(): void
2223
{
23-
$collector = $this->getMockBuilder(Collector::class)->getMock();
24-
$formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
25-
$stopwatch = $this->getMockBuilder(Stopwatch::class)->getMock();
24+
$collector = new Collector();
25+
$formatter = new Formatter($this->createMock(MessageFormatter::class), $this->createMock(MessageFormatter::class));
26+
$stopwatch = $this->createMock(Stopwatch::class);
2627

2728
$factory = new PluginClientFactory($collector, $formatter, $stopwatch);
2829

@@ -31,6 +32,6 @@ public function testRegisterPluginClientFactory(): void
3132
$class = (Kernel::MAJOR_VERSION >= 5) ? Event::class : LegacyEvent::class;
3233
$listener->onEvent(new $class());
3334

34-
$this->assertTrue(is_callable(NSA::getProperty(DefaultPluginClientFactory::class, 'factory')));
35+
$this->assertIsCallable(NSA::getProperty(DefaultPluginClientFactory::class, 'factory'));
3536
}
3637
}

Diff for: tests/Unit/Collector/ProfileClientFactoryTest.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Http\HttplugBundle\Collector\Formatter;
1111
use Http\HttplugBundle\Collector\ProfileClient;
1212
use Http\HttplugBundle\Collector\ProfileClientFactory;
13+
use Http\Message\Formatter as MessageFormatter;
14+
use PHPUnit\Framework\MockObject\MockObject;
1315
use PHPUnit\Framework\TestCase;
1416
use Symfony\Component\Stopwatch\Stopwatch;
1517

@@ -26,21 +28,21 @@ class ProfileClientFactoryTest extends TestCase
2628
private $formatter;
2729

2830
/**
29-
* @var Stopwatch
31+
* @var Stopwatch&MockObject
3032
*/
3133
private $stopwatch;
3234

3335
/**
34-
* @var HttpClient
36+
* @var HttpClient&MockObject
3537
*/
3638
private $client;
3739

3840
public function setUp(): void
3941
{
40-
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
41-
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
42-
$this->stopwatch = $this->getMockBuilder(Stopwatch::class)->getMock();
43-
$this->client = $this->getMockBuilder(HttpClient::class)->getMock();
42+
$this->collector = new Collector();
43+
$this->formatter = new Formatter($this->createMock(MessageFormatter::class), $this->createMock(MessageFormatter::class));
44+
$this->stopwatch = $this->createMock(Stopwatch::class);
45+
$this->client = $this->createMock(HttpClient::class);
4446
}
4547

4648
public function testCreateClientFromClientFactory(): void

0 commit comments

Comments
 (0)