Skip to content

Commit

Permalink
fix some deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Nov 19, 2023
1 parent 143dba2 commit cec50f6
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 263 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"php-http/discovery": "^1.14",
"php-http/httplug": "^2.0",
"php-http/logger-plugin": "^1.1",
"php-http/message": "^1.9",
"php-http/message": "^1.13",
"php-http/message-factory": "^1.0.2",
"php-http/stopwatch-plugin": "^1.2",
"psr/http-message": "^1.0 || ^2.0",
Expand Down Expand Up @@ -73,7 +73,8 @@
"config": {
"sort-packages": true,
"allow-plugins": {
"symfony/flex": true
"symfony/flex": true,
"php-http/discovery": false
}
},
"autoload": {
Expand Down
7 changes: 2 additions & 5 deletions src/Collector/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Formatter implements MessageFormatter
*/
private $curlFormatter;

public function __construct(MessageFormatter $formatter, CurlCommandFormatter $curlFormatter)
public function __construct(MessageFormatter $formatter, MessageFormatter $curlFormatter)
{
$this->formatter = $formatter;
$this->curlFormatter = $curlFormatter;
Expand All @@ -47,7 +47,7 @@ public function __construct(MessageFormatter $formatter, CurlCommandFormatter $c
public function formatException(\Throwable $exception)
{
if ($exception instanceof HttpException) {
return $this->formatter->formatResponse($exception->getResponse());
return $this->formatter->formatResponseForRequest($exception->getResponse(), $exception->getRequest());
}

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

/**
* {@inheritdoc}
*/
public function formatResponse(ResponseInterface $response)
{
return $this->formatter->formatResponse($response);
Expand Down
30 changes: 9 additions & 21 deletions src/Collector/ProfileClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public function __construct($client, Collector $collector, Formatter $formatter,
$this->stopwatch = $stopwatch;
}

/**
* {@inheritdoc}
*/
public function sendAsyncRequest(RequestInterface $request)
{
$activateStack = true;
Expand All @@ -89,8 +86,8 @@ public function sendAsyncRequest(RequestInterface $request)
$this->collectRequestInformations($request, $stack);
$event = $this->stopwatch->start($this->getStopwatchEventName($request), self::STOPWATCH_CATEGORY);

$onFulfilled = function (ResponseInterface $response) use ($event, $stack) {
$this->collectResponseInformations($response, $event, $stack);
$onFulfilled = function (ResponseInterface $response) use ($request, $event, $stack) {
$this->collectResponseInformations($request, $response, $event, $stack);
$event->stop();

return $response;
Expand Down Expand Up @@ -134,13 +131,9 @@ protected function doSendRequest(RequestInterface $request)

try {
$response = $this->client->sendRequest($request);
$this->collectResponseInformations($response, $event, $stack);
$this->collectResponseInformations($request, $response, $event, $stack);

return $response;
} catch (\Exception $e) {
$this->collectExceptionInformations($e, $event, $stack);

throw $e;
} catch (\Throwable $e) {
$this->collectExceptionInformations($e, $event, $stack);

Expand All @@ -150,7 +143,7 @@ protected function doSendRequest(RequestInterface $request)
}
}

private function collectRequestInformations(RequestInterface $request, Stack $stack)
private function collectRequestInformations(RequestInterface $request, Stack $stack): void
{
$uri = $request->getUri();
$stack->setRequestTarget($request->getRequestTarget());
Expand All @@ -162,29 +155,24 @@ private function collectRequestInformations(RequestInterface $request, Stack $st
$stack->setCurlCommand($this->formatter->formatAsCurlCommand($request));
}

private function collectResponseInformations(ResponseInterface $response, StopwatchEvent $event, Stack $stack)
private function collectResponseInformations(RequestInterface $request, ResponseInterface $response, StopwatchEvent $event, Stack $stack): void
{
$stack->setDuration($event->getDuration());
$stack->setResponseCode($response->getStatusCode());
$stack->setClientResponse($this->formatter->formatResponse($response));
$stack->setClientResponse($this->formatter->formatResponseForRequest($response, $request));
}

private function collectExceptionInformations(\Throwable $exception, StopwatchEvent $event, Stack $stack)
private function collectExceptionInformations(\Throwable $exception, StopwatchEvent $event, Stack $stack): void
{
if ($exception instanceof HttpException) {
$this->collectResponseInformations($exception->getResponse(), $event, $stack);
$this->collectResponseInformations($exception->getRequest(), $exception->getResponse(), $event, $stack);
}

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

/**
* Generates the event name.
*
* @return string
*/
private function getStopwatchEventName(RequestInterface $request)
private function getStopwatchEventName(RequestInterface $request): string
{
$name = sprintf('%s %s', $request->getMethod(), $request->getUri());

Expand Down
23 changes: 10 additions & 13 deletions src/Collector/ProfilePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
$profile = new Profile(get_class($this->plugin));

$stack = $this->collector->getActiveStack();
if (null === $stack) {
throw new \LogicException('No active stack');
}
$stack->addProfile($profile);

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

/**
* @param Stack $stack
*/
private function onException(
RequestInterface $request,
Profile $profile,
Exception $exception,
Stack $stack = null
) {
Stack $stack
): void {
$profile->setFailed(true);
$profile->setResponse($this->formatter->formatException($exception));
$this->collectRequestInformation($request, $stack);
}

private function onOutgoingRequest(RequestInterface $request, Profile $profile)
private function onOutgoingRequest(RequestInterface $request, Profile $profile): void
{
$profile->setRequest($this->formatter->formatRequest($request));
}

/**
* @param Stack $stack
*/
private function onOutgoingResponse(ResponseInterface $response, Profile $profile, RequestInterface $request, Stack $stack = null)
private function onOutgoingResponse(ResponseInterface $response, Profile $profile, RequestInterface $request, Stack $stack): void
{
$profile->setResponse($this->formatter->formatResponse($response));
$profile->setResponse($this->formatter->formatResponseForRequest($response, $request));
$this->collectRequestInformation($request, $stack);
}

/**
* Collect request information when not already done by the HTTP client. This happens when using the CachePlugin
* and the cache is hit without re-validation.
*/
private function collectRequestInformation(RequestInterface $request, Stack $stack = null)
private function collectRequestInformation(RequestInterface $request, Stack $stack): void
{
$uri = $request->getUri();
if (empty($stack->getRequestTarget())) {
Expand All @@ -127,7 +124,7 @@ private function collectRequestInformation(RequestInterface $request, Stack $sta
if (empty($stack->getRequestScheme())) {
$stack->setRequestScheme($uri->getScheme());
}
if (empty($stack->getRequestPort())) {
if (null === $stack->getRequestPort()) {
$stack->setRequestPort($uri->getPort());
}
if (empty($stack->getRequestHost())) {
Expand Down
4 changes: 2 additions & 2 deletions src/Collector/StackPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
$this->collector->addStack($stack);
$this->collector->activateStack($stack);

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

return $response;
};
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/ClientFactory/CurlFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function testCreateClient(): void
}

$factory = new CurlFactory(
$this->getMockBuilder(ResponseFactoryInterface::class)->getMock(),
$this->getMockBuilder(StreamFactoryInterface::class)->getMock()
$this->createMock(ResponseFactoryInterface::class),
$this->createMock(StreamFactoryInterface::class)
);
$client = $factory->createClient();

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/ClientFactory/SymfonyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function testCreateClient(): void
}

$factory = new SymfonyFactory(
$this->getMockBuilder(ResponseFactoryInterface::class)->getMock(),
$this->getMockBuilder(StreamFactoryInterface::class)->getMock()
$this->createMock(ResponseFactoryInterface::class),
$this->createMock(StreamFactoryInterface::class)
);
$client = $factory->createClient();

Expand Down
40 changes: 32 additions & 8 deletions tests/Unit/Collector/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
use Http\HttplugBundle\Collector\Formatter;
use Http\Message\Formatter as MessageFormatter;
use Http\Message\Formatter\CurlCommandFormatter;
use Http\Message\Formatter\SimpleFormatter;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class FormatterTest extends TestCase
{
/**
* @var MessageFormatter|MockObject
* @var MessageFormatter&MockObject
*/
private $formatter;

/**
* @var CurlCommandFormatter|MockObject
* @var CurlCommandFormatter&MockObject
*/
private $curlFormatter;

Expand All @@ -33,8 +34,8 @@ class FormatterTest extends TestCase

public function setUp(): void
{
$this->formatter = $this->getMockBuilder(MessageFormatter::class)->getMock();
$this->curlFormatter = $this->getMockBuilder(CurlCommandFormatter::class)->getMock();
$this->formatter = $this->createMock(MessageFormatter::class);
$this->curlFormatter = $this->createMock(CurlCommandFormatter::class);

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

public function testFormatResponseForRequest(): void
{
$formatter = $this->createMock(SimpleFormatter::class);
$subject = new Formatter($formatter, $this->curlFormatter);

$response = new Response();
$request = new Request('GET', '/');

$formatter
->expects($this->once())
->method('formatResponseForRequest')
->with($this->identicalTo($response), $this->identicalTo($request))
;

$subject->formatResponseForRequest($response, $request);
}

/**
* @group legacy
*/
public function testFormatResponse(): void
{
$response = new Response();
Expand All @@ -67,18 +88,21 @@ public function testFormatResponse(): void

public function testFormatHttpException(): void
{
$formatter = $this->createMock(SimpleFormatter::class);
$subject = new Formatter($formatter, $this->curlFormatter);

$request = new Request('GET', '/');
$response = new Response();
$exception = new HttpException('', $request, $response);

$this->formatter
$formatter
->expects($this->once())
->method('formatResponse')
->with($this->identicalTo($response))
->method('formatResponseForRequest')
->with($this->identicalTo($response), $this->identicalTo($request))
->willReturn('FormattedException')
;

$this->assertEquals('FormattedException', $this->subject->formatException($exception));
$this->assertEquals('FormattedException', $subject->formatException($exception));
}

public function testFormatTransferException(): void
Expand Down
9 changes: 5 additions & 4 deletions tests/Unit/Collector/PluginClientFactoryListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Http\HttplugBundle\Collector\Formatter;
use Http\HttplugBundle\Collector\PluginClientFactory;
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
use Http\Message\Formatter as MessageFormatter;
use Nyholm\NSA;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
Expand All @@ -20,9 +21,9 @@ final class PluginClientFactoryListenerTest extends TestCase
{
public function testRegisterPluginClientFactory(): void
{
$collector = $this->getMockBuilder(Collector::class)->getMock();
$formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
$stopwatch = $this->getMockBuilder(Stopwatch::class)->getMock();
$collector = new Collector();
$formatter = new Formatter($this->createMock(MessageFormatter::class), $this->createMock(MessageFormatter::class));
$stopwatch = $this->createMock(Stopwatch::class);

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

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

$this->assertTrue(is_callable(NSA::getProperty(DefaultPluginClientFactory::class, 'factory')));
$this->assertIsCallable(NSA::getProperty(DefaultPluginClientFactory::class, 'factory'));
}
}
14 changes: 8 additions & 6 deletions tests/Unit/Collector/ProfileClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Http\HttplugBundle\Collector\Formatter;
use Http\HttplugBundle\Collector\ProfileClient;
use Http\HttplugBundle\Collector\ProfileClientFactory;
use Http\Message\Formatter as MessageFormatter;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Stopwatch\Stopwatch;

Expand All @@ -26,21 +28,21 @@ class ProfileClientFactoryTest extends TestCase
private $formatter;

/**
* @var Stopwatch
* @var Stopwatch&MockObject
*/
private $stopwatch;

/**
* @var HttpClient
* @var HttpClient&MockObject
*/
private $client;

public function setUp(): void
{
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
$this->stopwatch = $this->getMockBuilder(Stopwatch::class)->getMock();
$this->client = $this->getMockBuilder(HttpClient::class)->getMock();
$this->collector = new Collector();
$this->formatter = new Formatter($this->createMock(MessageFormatter::class), $this->createMock(MessageFormatter::class));
$this->stopwatch = $this->createMock(Stopwatch::class);
$this->client = $this->createMock(HttpClient::class);
}

public function testCreateClientFromClientFactory(): void
Expand Down
Loading

0 comments on commit cec50f6

Please sign in to comment.