[Maintenance] Fix HTTP Client deprecations

This commit is contained in:
Kevin Kaniaburka 2023-04-20 13:35:04 +02:00
parent 238472d99d
commit 69ed3e7b47
No known key found for this signature in database
GPG key ID: 8DB4C54474F3FD72
8 changed files with 89 additions and 42 deletions

View file

@ -53,7 +53,6 @@
to `Sylius\Bundle\CoreBundle\Validator\Constraints\HasEnabledEntityValidator`
as the second argument is deprecated.
<<<<<<< HEAD
1. Class `\Sylius\Bundle\ShopBundle\Calculator\OrderItemsSubtotalCalculator` has been deprecated. Order items subtotal calculation
is now available on the Order model `\Sylius\Component\Core\Model\Order::getItemsSubtotal`.
@ -68,11 +67,25 @@
To add more data per variant create a service implementing the `Sylius\Component\Core\Provider\ProductVariantMap\ProductVariantMapProviderInterface` and tag it with `sylius.product_variant_data_map_provider`.
1. Using Guzzle has been deprecated in favor of Symfony HTTP Client. Therefore, passing `GuzzleHttp\ClientInterface`
=======
1. Using Guzzle has been deprecated in favor of Symfony HTTP Client. Therefore, passing Guzzle 6 `GuzzleHttp\ClientInterface`
or any other HTTP Client that does not implement `Psr\Http\Client\ClientInterface`
>>>>>>> 577914fa71 ([Admin] Improve deprecations in NotificationController)
to `Sylius\Bundle\AdminBundle\Controller\NotificationController` as a second argument is deprecated.
1. Using Guzzle 6 has been deprecated in favor of Symfony HTTP Client.
Therefore, the constructor of `Sylius\Bundle\AdminBundle\Controller\NotificationController` has been changed:
1. The `sylius.http_message_factory` service has been deprecated. Use `sylius.http_request_factory` instead.
```diff
public function __construct(
- private ClientInterface $client,
- private MessageFactory $messageFactory,
+ private ClientInterface|DeprecatedClientInterface $client,
+ private RequestFactoryInterface|MessageFactory $requestFactory,
private string $hubUri,
private string $environment,
+ private ?StreamFactoryInterface $streamFactory = null,
) {
...
}
```
1. The `sylius.http_message_factory` service has been deprecated. Use `Psr\Http\Message\RequestFactoryInterface` instead.
1. The `sylius.http_client` has become an alias to `psr18.http_client` service.
1. The `sylius.payum.http_client` has become a service ID of newly created `Sylius\Bundle\PayumBundle\HttpClient\HttpClient`.

View file

@ -30,6 +30,8 @@
"League\\Flysystem\\FilesystemOperator",
"Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface",
"PHPUnit\\Framework\\ExpectationFailedException",
"Psr\\Http\\Message\\RequestFactoryInterface",
"Psr\\Http\\Message\\StreamFactoryInterface",
"Stripe\\Stripe"
],
"php-core-extensions" : [

View file

@ -15,9 +15,12 @@ namespace Sylius\Bundle\AdminBundle\Controller;
use GuzzleHttp\ClientInterface as DeprecatedClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use Http\Message\RequestFactory;
use Http\Message\MessageFactory;
use Nyholm\Psr7\Stream;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Sylius\Bundle\CoreBundle\SyliusCoreBundle;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
@ -26,33 +29,43 @@ final class NotificationController
{
public function __construct(
private ClientInterface|DeprecatedClientInterface $client,
private RequestFactory $requestFactory,
private RequestFactoryInterface|MessageFactory $requestFactory,
private string $hubUri,
private string $environment,
private ?StreamFactoryInterface $streamFactory = null,
) {
if (!$client instanceof ClientInterface) {
trigger_deprecation('sylius/admin-bundle', '1.13', 'Using a service that does not implement "%s" is deprecated and will be prohibited in 2.0.', ClientInterface::class);
trigger_deprecation('sylius/admin-bundle', '1.13', 'Using a service that does not implement "%s" as a 1st argument of "%s" constructor is deprecated and will be prohibited in 2.0.', ClientInterface::class, self::class);
}
if (!$requestFactory instanceof RequestFactoryInterface) {
trigger_deprecation('sylius/admin-bundle', '1.13', 'Using a service that does not implement "%s" as a 2nd argument of "%s" constructor is deprecated and will be prohibited in 2.0.', RequestFactoryInterface::class, self::class);
}
if (null === $streamFactory) {
trigger_deprecation('sylius/admin-bundle', '1.13', 'Not passing a service that implements "%s" as a 5th argument of "%s" constructor is deprecated and will be prohibited in 2.0.', StreamFactoryInterface::class, self::class);
}
}
public function getVersionAction(Request $request): JsonResponse
{
$content = [
$content = json_encode([
'version' => SyliusCoreBundle::VERSION,
'hostname' => $request->getHost(),
'locale' => $request->getLocale(),
'user_agent' => $request->headers->get('User-Agent'),
'environment' => $this->environment,
];
]);
$headers = ['Content-Type' => 'application/json'];
$hubRequest = $this->requestFactory->createRequest(
Request::METHOD_GET,
$this->hubUri,
$headers,
json_encode($content),
);
$hubRequest = $this->requestFactory
->createRequest(Request::METHOD_GET, $this->hubUri)
->withHeader('Content-Type', 'application/json')
->withBody(
null === $this->streamFactory
? Stream::create($content)
: $this->streamFactory->createStream($content)
)
;
try {
if ($this->client instanceof DeprecatedClientInterface) {

View file

@ -82,10 +82,9 @@
<tag name="twig.extension" />
</service>
<service id="sylius.http_client" alias="Http\Client\HttpClient" public="false" />
<service id="sylius.http_client" alias="psr18.http_client" public="false" />
<service id="sylius.http_message_factory" class="Http\Message\MessageFactory\GuzzleMessageFactory" public="false">
<deprecated package="sylius/admin-bundle" version="1.13">The "%service_id%" service is deprecated since 1.13 and will be removed in 2.0. Use "sylius.http_request_factory" instead.</deprecated>
<deprecated package="sylius/admin-bundle" version="1.13">The "%service_id%" service is deprecated since 1.13 and will be removed in 2.0. Use "Psr\Http\Message\RequestFactoryInterface" instead.</deprecated>
</service>
<service id="sylius.http_request_factory" alias="Http\Client\HttpClient" public="false" />
</services>
</container>

View file

@ -90,9 +90,10 @@
<service id="sylius.controller.admin.notification" class="Sylius\Bundle\AdminBundle\Controller\NotificationController" public="true">
<argument type="service" id="sylius.http_client" />
<argument type="service" id="sylius.http_request_factory" />
<argument type="service" id="Psr\Http\Message\RequestFactoryInterface" />
<argument type="string">%sylius.admin.notification.uri%</argument>
<argument type="string">%kernel.environment%</argument>
<argument type="service" id="Psr\Http\Message\StreamFactoryInterface" />
</service>
<service id="Sylius\Bundle\AdminBundle\Controller\RemoveCatalogPromotionAction" public="true">

View file

@ -16,14 +16,15 @@ namespace Sylius\Bundle\AdminBundle\Tests\Controller;
use GuzzleHttp\Exception\ConnectException;
use Http\Client\Exception\NetworkException;
use Http\Message\MessageFactory;
use Http\Message\RequestFactory;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Prophecy\Prophecy\ProphecyInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Sylius\Bundle\AdminBundle\Controller\NotificationController;
use Symfony\Component\HttpFoundation\JsonResponse;
@ -41,6 +42,8 @@ final class NotificationControllerTest extends TestCase
private ObjectProphecy $messageFactory;
private ObjectProphecy $streamFactory;
private NotificationController $controller;
private NotificationController $legacyController;
@ -50,9 +53,14 @@ final class NotificationControllerTest extends TestCase
/** @test */
public function it_returns_an_empty_json_response_upon_client_exception(): void
{
$this->requestFactory->createRequest(Argument::cetera())
->willReturn($this->prophesize(RequestInterface::class)->reveal())
;
$requestInterface = $this->prophesize(RequestInterface::class);
$streamInterface = $this->prophesize(StreamInterface::class);
$this->streamFactory->createStream(Argument::cetera())->willReturn($streamInterface);
$this->requestFactory->createRequest(Argument::cetera())->willReturn($requestInterface);
$requestInterface->withHeader('Content-Type', 'application/json')->willReturn($requestInterface);
$requestInterface->withBody($streamInterface)->willReturn($requestInterface);
$this->client->sendRequest(Argument::cetera())->willThrow(NetworkException::class);
@ -69,9 +77,11 @@ final class NotificationControllerTest extends TestCase
*/
public function it_returns_an_empty_json_response_upon_client_exception_deprecated(): void
{
$this->messageFactory->createRequest(Argument::any(), Argument::cetera())
->willReturn($this->prophesize(RequestInterface::class)->reveal())
;
$requestInterface = $this->prophesize(RequestInterface::class);
$this->messageFactory->createRequest(Argument::cetera())->willReturn($requestInterface);
$requestInterface->withHeader('Content-Type', 'application/json')->willReturn($requestInterface);
$requestInterface->withBody(Argument::any())->willReturn($requestInterface);
$this->legacyClient->send(Argument::cetera())->willThrow(ConnectException::class);
@ -86,9 +96,14 @@ final class NotificationControllerTest extends TestCase
{
$content = json_encode(['version' => '9001']);
$this->requestFactory->createRequest(Argument::cetera())
->willReturn($this->prophesize(RequestInterface::class)->reveal())
;
$requestInterface = $this->prophesize(RequestInterface::class);
$streamInterface = $this->prophesize(StreamInterface::class);
$this->streamFactory->createStream(Argument::cetera())->willReturn($streamInterface);
$this->requestFactory->createRequest(Argument::cetera())->willReturn($requestInterface);
$requestInterface->withHeader('Content-Type', 'application/json')->willReturn($requestInterface);
$requestInterface->withBody($streamInterface)->willReturn($requestInterface);
/** @var ProphecyInterface|StreamInterface $stream */
$stream = $this->prophesize(StreamInterface::class);
@ -115,9 +130,11 @@ final class NotificationControllerTest extends TestCase
{
$content = json_encode(['version' => '9001']);
$this->messageFactory->createRequest(Argument::any(), Argument::cetera())
->willReturn($this->prophesize(RequestInterface::class)->reveal())
;
$requestInterface = $this->prophesize(RequestInterface::class);
$this->messageFactory->createRequest(Argument::cetera())->willReturn($requestInterface);
$requestInterface->withHeader('Content-Type', 'application/json')->willReturn($requestInterface);
$requestInterface->withBody(Argument::any())->willReturn($requestInterface);
/** @var ProphecyInterface|StreamInterface $stream */
$stream = $this->prophesize(StreamInterface::class);
@ -139,14 +156,16 @@ final class NotificationControllerTest extends TestCase
{
$this->client = $this->prophesize(\Psr\Http\Client\ClientInterface::class);
$this->legacyClient = $this->prophesize(\GuzzleHttp\ClientInterface::class);
$this->requestFactory = $this->prophesize(RequestFactory::class);
$this->requestFactory = $this->prophesize(RequestFactoryInterface::class);
$this->messageFactory = $this->prophesize(MessageFactory::class);
$this->streamFactory = $this->prophesize(StreamFactoryInterface::class);
$this->controller = new NotificationController(
$this->client->reveal(),
$this->requestFactory->reveal(),
self::$hubUri,
'environment',
$this->streamFactory->reveal(),
);
$this->legacyController = new NotificationController(

View file

@ -13,13 +13,13 @@ declare(strict_types=1);
namespace Sylius\Bundle\PayumBundle\HttpClient;
use Http\Client\HttpClient as BaseHttpClientInterface;
use Payum\Core\HttpClientInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
final class HttpClient implements HttpClientInterface
{
public function __construct(private BaseHttpClientInterface $client)
public function __construct(private ClientInterface $client)
{
}

View file

@ -29,7 +29,7 @@
<defaults public="true" />
<service id="sylius.payum.http_client" class="Sylius\Bundle\PayumBundle\HttpClient\HttpClient">
<argument type="service" id="Http\Client\HttpClient" />
<argument type="service" id="Psr\Http\Client\ClientInterface" />
</service>
<service id="sylius.form_registry.payum_gateway_config" class="Sylius\Bundle\ResourceBundle\Form\Registry\FormTypeRegistry" />
</services>