Skip to content

Commit 710ae3a

Browse files
authored
Merge pull request #447 from php-http/1-to-2
1 to 2
2 parents 8d796dd + 0f11377 commit 710ae3a

15 files changed

+198
-279
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ jobs:
4343
- dependencies: "php-http/guzzle7-adapter"
4444
php-version: "8.2"
4545
symfony-deprecations-helper: "weak"
46+
- dependencies: "php-http/guzzle7-adapter"
47+
php-version: "8.3"
48+
symfony-deprecations-helper: "weak"
4649

4750
# Test with httplug 2.x clients
4851
- dependencies: "php-http/guzzle7-adapter php-http/curl-client:^2.0.0 php-http/vcr-plugin:^1.0@dev php-http/socket-client:^2.0"
@@ -68,6 +71,10 @@ jobs:
6871
symfony-require: "6.3.*"
6972
php-version: "8.2"
7073
symfony-deprecations-helper: "weak"
74+
- dependencies: "php-http/guzzle7-adapter symfony/http-client:^6.0"
75+
symfony-require: "6.4.*"
76+
php-version: "8.1"
77+
symfony-deprecations-helper: "weak"
7178

7279
steps:
7380
- name: "Checkout"
@@ -86,8 +93,10 @@ jobs:
8693
run: "composer config minimum-stability stable"
8794
if: "${{ matrix.stability == 'stable' }}"
8895

89-
- name: "Install dependencies"
90-
run: "composer require --no-progress --no-scripts --no-plugins symfony/flex ${{ matrix.dependencies }}"
96+
- name: "Add dependencies and enable flex"
97+
run: |
98+
composer require --no-update symfony/flex ${{ matrix.dependencies }}
99+
composer config --no-plugins allow-plugins.symfony/flex true
91100
92101
- name: "Install dependencies with Composer"
93102
uses: "ramsey/composer-install@v2"

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
1919

2020
# Version 1
2121

22+
# 1.32.0 - 2023-12-06
23+
24+
- Added support for Symfony 7
25+
2226
# 1.31.0 - 2023-11-06
2327

2428
- Added configuration for the `header` authentication plugin (#437).

composer.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
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/stopwatch-plugin": "^1.2",
3636
"psr/http-factory-implementation": "^1.0",
3737
"psr/http-message": "^1.0 || ^2.0",
38-
"symfony/config": "^5.4 || ^6.0",
39-
"symfony/dependency-injection": "^5.4 || ^6.0",
40-
"symfony/event-dispatcher": "^5.4 || ^6.0",
41-
"symfony/http-kernel": "^5.4 || ^6.0",
42-
"symfony/options-resolver": "^5.4 || ^6.0"
38+
"symfony/config": "^5.4 || ^6.0 || ^7.0",
39+
"symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0",
40+
"symfony/event-dispatcher": "^5.4 || ^6.0 || ^7.0",
41+
"symfony/http-kernel": "^5.4 || ^6.0 || ^7.0",
42+
"symfony/options-resolver": "^5.4 || ^6.0 || ^7.0"
4343
},
4444
"conflict": {
4545
"php-http/guzzle6-adapter": "<1.1",
@@ -51,21 +51,21 @@
5151
},
5252
"require-dev": {
5353
"guzzlehttp/psr7": "^1.7 || ^2.0",
54-
"matthiasnoback/symfony-dependency-injection-test": "^4.0",
54+
"matthiasnoback/symfony-dependency-injection-test": "^4.0 || ^5.0",
5555
"nyholm/nsa": "^1.1",
5656
"nyholm/psr7": "^1.2.1",
5757
"php-http/cache-plugin": "^1.7",
5858
"php-http/mock-client": "^1.2",
5959
"php-http/promise": "^1.0",
60-
"symfony/browser-kit": "^5.4 || ^6.0",
61-
"symfony/cache": "^5.4 || ^6.0",
62-
"symfony/dom-crawler": "^5.4 || ^6.0",
63-
"symfony/framework-bundle": "^5.4 || ^6.0",
64-
"symfony/http-foundation": "^5.4 || ^6.0",
65-
"symfony/phpunit-bridge": "^5.4 || ^6.0",
66-
"symfony/stopwatch": "^5.4 || ^6.0",
67-
"symfony/twig-bundle": "^5.4 || ^6.0",
68-
"symfony/web-profiler-bundle": "^5.4 || ^6.0",
60+
"symfony/browser-kit": "^5.4 || ^6.0 || ^7.0",
61+
"symfony/cache": "^5.4 || ^6.0 || ^7.0",
62+
"symfony/dom-crawler": "^5.4 || ^6.0 || ^7.0",
63+
"symfony/framework-bundle": "^5.4 || ^6.0 || ^7.0",
64+
"symfony/http-foundation": "^5.4 || ^6.0 || ^7.0",
65+
"symfony/phpunit-bridge": "^7.0.3",
66+
"symfony/stopwatch": "^5.4 || ^6.0 || ^7.0",
67+
"symfony/twig-bundle": "^5.4 || ^6.0 || ^7.0",
68+
"symfony/web-profiler-bundle": "^5.4 || ^6.0 || ^7.0",
6969
"twig/twig": "^1.41 || ^2.10 || ^3.0"
7070
},
7171
"suggest": {

src/Collector/Formatter.php

Lines changed: 2 additions & 5 deletions
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);

src/Collector/ProfileClient.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ public function __construct($client, Collector $collector, Formatter $formatter,
6969
$this->stopwatch = $stopwatch;
7070
}
7171

72-
/**
73-
* {@inheritdoc}
74-
*/
7572
public function sendAsyncRequest(RequestInterface $request)
7673
{
7774
$activateStack = true;
@@ -88,8 +85,8 @@ public function sendAsyncRequest(RequestInterface $request)
8885
$this->collectRequestInformations($request, $stack);
8986
$event = $this->stopwatch->start($this->getStopwatchEventName($request), self::STOPWATCH_CATEGORY);
9087

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

9592
return $response;
@@ -133,13 +130,9 @@ protected function doSendRequest(RequestInterface $request)
133130

134131
try {
135132
$response = $this->client->sendRequest($request);
136-
$this->collectResponseInformations($response, $event, $stack);
133+
$this->collectResponseInformations($request, $response, $event, $stack);
137134

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

@@ -149,7 +142,7 @@ protected function doSendRequest(RequestInterface $request)
149142
}
150143
}
151144

152-
private function collectRequestInformations(RequestInterface $request, Stack $stack)
145+
private function collectRequestInformations(RequestInterface $request, Stack $stack): void
153146
{
154147
$uri = $request->getUri();
155148
$stack->setRequestTarget($request->getRequestTarget());
@@ -161,29 +154,24 @@ private function collectRequestInformations(RequestInterface $request, Stack $st
161154
$stack->setCurlCommand($this->formatter->formatAsCurlCommand($request));
162155
}
163156

164-
private function collectResponseInformations(ResponseInterface $response, StopwatchEvent $event, Stack $stack)
157+
private function collectResponseInformations(RequestInterface $request, ResponseInterface $response, StopwatchEvent $event, Stack $stack): void
165158
{
166159
$stack->setDuration($event->getDuration());
167160
$stack->setResponseCode($response->getStatusCode());
168-
$stack->setClientResponse($this->formatter->formatResponse($response));
161+
$stack->setClientResponse($this->formatter->formatResponseForRequest($response, $request));
169162
}
170163

171-
private function collectExceptionInformations(\Throwable $exception, StopwatchEvent $event, Stack $stack)
164+
private function collectExceptionInformations(\Throwable $exception, StopwatchEvent $event, Stack $stack): void
172165
{
173166
if ($exception instanceof HttpException) {
174-
$this->collectResponseInformations($exception->getResponse(), $event, $stack);
167+
$this->collectResponseInformations($exception->getRequest(), $exception->getResponse(), $event, $stack);
175168
}
176169

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

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

src/Collector/ProfilePlugin.php

Lines changed: 10 additions & 13 deletions
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())) {

src/Collector/StackPlugin.php

Lines changed: 2 additions & 2 deletions
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
};

tests/Unit/ClientFactory/CurlFactoryTest.php

Lines changed: 2 additions & 2 deletions
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

tests/Unit/ClientFactory/SymfonyFactoryTest.php

Lines changed: 2 additions & 2 deletions
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

tests/Unit/Collector/FormatterTest.php

Lines changed: 32 additions & 8 deletions
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

0 commit comments

Comments
 (0)