Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ services:
- '@auto1.api.request.serializer'
- '@auto1.api.request.visitor.registry'
- '@auto1.api.uri_factory'
- '@auto1.api.message_factory'
- '@auto1.api.request_factory'
- '@auto1.api.stream_factory'
- '%auto1_service_api_client.strict_mode%'
calls:
- [setLogger, ['@logger']]
Expand Down Expand Up @@ -138,9 +139,13 @@ services:
factory: Http\Discovery\HttpAsyncClientDiscovery::find

auto1.api.uri_factory:
class: Http\Message\UriFactory
factory: Http\Discovery\UriFactoryDiscovery::find
class: Psr\Http\Message\UriFactoryInterface
factory: Http\Discovery\Psr17FactoryDiscovery::findUriFactory

auto1.api.message_factory:
class: Http\Message\MessageFactory
factory: Http\Discovery\MessageFactoryDiscovery::find
auto1.api.request_factory:
class: Psr\Http\Message\RequestFactoryInterface
factory: Http\Discovery\Psr17FactoryDiscovery::findRequestFactory

auto1.api.stream_factory:
class: Psr\Http\Message\StreamFactoryInterface
factory: Http\Discovery\Psr17FactoryDiscovery::findStreamFactory
44 changes: 28 additions & 16 deletions Service/Request/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
use Auto1\ServiceAPIComponentsBundle\Service\Endpoint\EndpointRegistryInterface;
use Auto1\ServiceAPIComponentsBundle\Service\Endpoint\EndpointInterface;
use Auto1\ServiceAPIComponentsBundle\Service\Logger\LoggerAwareTrait;
use Http\Message\MessageFactory;
use Http\Message\UriFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\RequestFactoryInterface as PsrRequestFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\StreamFactoryInterface as PsrStreamFactoryInterface;
use Psr\Http\Message\UriInterface;
use Psr\Http\Message\UriFactoryInterface as PsrUriFactoryInterface;
use Psr\Log\LoggerAwareInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;
Expand Down Expand Up @@ -50,14 +51,19 @@ class RequestFactory implements RequestFactoryInterface, LoggerAwareInterface
private $requestVisitorRegistry;

/**
* @var UriFactory
* @var PsrUriFactoryInterface
*/
private $uriFactory;

/**
* @var MessageFactory
* @var PsrRequestFactoryInterface
*/
private $messageFactory;
private $requestFactory;

/**
* @var PsrStreamFactoryInterface
*/
private $streamFactory;

/**
* @var bool
Expand All @@ -70,23 +76,26 @@ class RequestFactory implements RequestFactoryInterface, LoggerAwareInterface
* @param EndpointRegistryInterface $endpointRegistry
* @param SerializerInterface $serializer
* @param RequestVisitorRegistryInterface $requestVisitorRegistry
* @param UriFactory $uriFactory
* @param MessageFactory $messageFactory
* @param PsrUriFactoryInterface $uriFactory
* @param PsrRequestFactoryInterface $requestFactory
* @param PsrStreamFactoryInterface $streamFactory
* @param bool $strictModeEnabled
*/
public function __construct(
EndpointRegistryInterface $endpointRegistry,
SerializerInterface $serializer,
RequestVisitorRegistryInterface $requestVisitorRegistry,
UriFactory $uriFactory,
MessageFactory $messageFactory,
PsrUriFactoryInterface $uriFactory,
PsrRequestFactoryInterface $requestFactory,
PsrStreamFactoryInterface $streamFactory,
bool $strictModeEnabled = false
) {
$this->endpointRegistry = $endpointRegistry;
$this->serializer = $serializer;
$this->requestVisitorRegistry = $requestVisitorRegistry;
$this->uriFactory = $uriFactory;
$this->messageFactory = $messageFactory;
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
$this->strictModeEnabled = $strictModeEnabled;
}

Expand All @@ -101,12 +110,15 @@ public function create(ServiceRequestInterface $serviceRequest): RequestInterfac
$uri = $this->getRequestUri($serviceRequest);
$requestBody = $this->getRequestBody($serviceRequest, $endpoint);

$httpRequest = $this->messageFactory->createRequest(
$endpoint->getMethod(),
$uri,
[],
$requestBody
);
$httpRequest = $this->requestFactory->createRequest($endpoint->getMethod(), $uri);

if (null !== $requestBody) {
$body = $requestBody instanceof StreamInterface
? $requestBody
: $this->streamFactory->createStream((string) $requestBody);

$httpRequest = $httpRequest->withBody($body);
}

return $this->visitRequest($httpRequest, $endpoint->getRequestFormat());
}
Expand Down
109 changes: 74 additions & 35 deletions Tests/Service/Request/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
use Auto1\ServiceAPIComponentsBundle\Exception\Request\InvalidArgumentException;
use Auto1\ServiceAPIComponentsBundle\Service\Endpoint\EndpointInterface;
use Auto1\ServiceAPIComponentsBundle\Service\Endpoint\EndpointRegistryInterface;
use Http\Message\MessageFactory;
use Http\Message\UriFactory;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\RequestFactoryInterface as PsrRequestFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\StreamFactoryInterface as PsrStreamFactoryInterface;
use Psr\Http\Message\UriInterface;
use Psr\Http\Message\UriFactoryInterface as PsrUriFactoryInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Auto1\ServiceAPIClientBundle\Service\Request\RequestVisitorRegistry;
use Auto1\ServiceAPIClientBundle\Service\Request\RequestVisitorRegistryInterface;
Expand Down Expand Up @@ -58,14 +60,19 @@ class RequestFactoryTest extends TestCase
private $requestDecoratorProphecy;

/**
* @var UriFactory|ObjectProphecy
* @var PsrUriFactoryInterface|ObjectProphecy
*/
private $uriFactoryProphecy;

/**
* @var MessageFactory|ObjectProphecy
* @var PsrRequestFactoryInterface|ObjectProphecy
*/
private $messageFactoryProphecy;
private $requestFactoryProphecy;

/**
* @var PsrStreamFactoryInterface|ObjectProphecy
*/
private $streamFactoryProphecy;

/**
* {@inheritdoc}
Expand All @@ -77,8 +84,9 @@ protected function setUp(): void
$this->serializerProphecy = $this->prophesize(SerializerInterface::class);
$this->requestVisitorRegistryProphecy = $this->prophesize(RequestVisitorRegistryInterface::class);
$this->requestDecoratorProphecy = $this->prophesize(RequestVisitorInterface::class);
$this->uriFactoryProphecy = $this->prophesize(UriFactory::class);
$this->messageFactoryProphecy = $this->prophesize(MessageFactory::class);
$this->uriFactoryProphecy = $this->prophesize(PsrUriFactoryInterface::class);
$this->requestFactoryProphecy = $this->prophesize(PsrRequestFactoryInterface::class);
$this->streamFactoryProphecy = $this->prophesize(PsrStreamFactoryInterface::class);
}

/**
Expand Down Expand Up @@ -110,7 +118,9 @@ public function testBuildFlow()
$endpoint = $endpointProphecy->reveal();

$uri = $this->prophesize(UriInterface::class)->reveal();
$request = $this->prophesize(RequestInterface::class)->reveal();
$requestProphecy = $this->prophesize(RequestInterface::class);
$request = $requestProphecy->reveal();
$stream = $this->prophesize(StreamInterface::class)->reveal();

$this->endpointRegistryProphecy
->getEndpoint($this->serviceRequestProphecy->reveal())
Expand All @@ -130,13 +140,20 @@ public function testBuildFlow()
->shouldBeCalled()
;

$this->messageFactoryProphecy
->createRequest(
$requestMethod,
$uri,
[],
$requestBody,
)
$this->requestFactoryProphecy
->createRequest($requestMethod, $uri)
->willReturn($request)
->shouldBeCalled()
;

$this->streamFactoryProphecy
->createStream($requestBody)
->willReturn($stream)
->shouldBeCalled()
;

$requestProphecy
->withBody($stream)
->willReturn($request)
->shouldBeCalled()
;
Expand All @@ -158,7 +175,8 @@ public function testBuildFlow()
$this->serializerProphecy->reveal(),
$this->requestVisitorRegistryProphecy->reveal(),
$this->uriFactoryProphecy->reveal(),
$this->messageFactoryProphecy->reveal(),
$this->requestFactoryProphecy->reveal(),
$this->streamFactoryProphecy->reveal(),
false
);

Expand Down Expand Up @@ -201,7 +219,9 @@ public function testBuildFlowWithQueryParams()
$endpoint = $endpointProphecy->reveal();

$uri = $this->prophesize(UriInterface::class)->reveal();
$request = $this->prophesize(RequestInterface::class)->reveal();
$requestProphecy = $this->prophesize(RequestInterface::class);
$request = $requestProphecy->reveal();
$stream = $this->prophesize(StreamInterface::class)->reveal();

// Mock non existing method of ServiceRequest `getParam`
$serviceRequest = $this->getMockBuilder(ServiceRequestInterface::class)
Expand Down Expand Up @@ -230,13 +250,20 @@ public function testBuildFlowWithQueryParams()
->shouldBeCalled()
;

$this->messageFactoryProphecy
->createRequest(
$requestMethod,
$uri,
[],
$requestBody,
)
$this->requestFactoryProphecy
->createRequest($requestMethod, $uri)
->willReturn($request)
->shouldBeCalled()
;

$this->streamFactoryProphecy
->createStream($requestBody)
->willReturn($stream)
->shouldBeCalled()
;

$requestProphecy
->withBody($stream)
->willReturn($request)
->shouldBeCalled()
;
Expand All @@ -257,7 +284,8 @@ public function testBuildFlowWithQueryParams()
$this->serializerProphecy->reveal(),
$this->requestVisitorRegistryProphecy->reveal(),
$this->uriFactoryProphecy->reveal(),
$this->messageFactoryProphecy->reveal(),
$this->requestFactoryProphecy->reveal(),
$this->streamFactoryProphecy->reveal(),
false
);

Expand Down Expand Up @@ -299,7 +327,8 @@ public function testBuildFlowValidationFailsOnUnmappedRequestArguments()
$this->serializerProphecy->reveal(),
$this->requestVisitorRegistryProphecy->reveal(),
$this->uriFactoryProphecy->reveal(),
$this->messageFactoryProphecy->reveal(),
$this->requestFactoryProphecy->reveal(),
$this->streamFactoryProphecy->reveal(),
false
);

Expand Down Expand Up @@ -348,7 +377,9 @@ public function testBuildFlowWithUnderscoreAndDashParams(): void
$endpoint = $endpointProphecy->reveal();

$uri = $this->prophesize(UriInterface::class)->reveal();
$request = $this->prophesize(RequestInterface::class)->reveal();
$requestProphecy = $this->prophesize(RequestInterface::class);
$request = $requestProphecy->reveal();
$stream = $this->prophesize(StreamInterface::class)->reveal();

// Mock non existing method of ServiceRequest `getParam`
$serviceRequest = $this->getMockBuilder(ServiceRequestInterface::class)
Expand Down Expand Up @@ -389,13 +420,20 @@ public function testBuildFlowWithUnderscoreAndDashParams(): void
->shouldBeCalled()
;

$this->messageFactoryProphecy
->createRequest(
$requestMethod,
$uri,
[],
$requestBody,
)
$this->requestFactoryProphecy
->createRequest($requestMethod, $uri)
->willReturn($request)
->shouldBeCalled()
;

$this->streamFactoryProphecy
->createStream($requestBody)
->willReturn($stream)
->shouldBeCalled()
;

$requestProphecy
->withBody($stream)
->willReturn($request)
->shouldBeCalled()
;
Expand All @@ -416,7 +454,8 @@ public function testBuildFlowWithUnderscoreAndDashParams(): void
$this->serializerProphecy->reveal(),
$this->requestVisitorRegistryProphecy->reveal(),
$this->uriFactoryProphecy->reveal(),
$this->messageFactoryProphecy->reveal(),
$this->requestFactoryProphecy->reveal(),
$this->streamFactoryProphecy->reveal(),
false
);

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"php-http/discovery": "^1.3",
"php-http/httplug": "^2.2",
"psr/http-message": "^1.0",
"psr/log": "^1.1|^2.0|^3.0",
"php-http/message-factory": "^1.1"
"psr/http-factory": "^1.0",
"psr/log": "^1.1|^2.0|^3.0"
},
"require-dev": {
"symfony/yaml": "~5.0|~6.0|~7.0",
Expand Down