Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update phpunit and use direct call instead of __call #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
6 changes: 2 additions & 4 deletions Service/Request/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private function visitRequest(RequestInterface $request, string $requestFormat):
*
* @return UriInterface
*/
private function getRequestUri(ServiceRequestInterface $serviceRequest)
private function getRequestUri(ServiceRequestInterface $serviceRequest): UriInterface
{
$endpoint = $this->endpointRegistry->getEndpoint($serviceRequest);
$baseUrl = $endpoint->getBaseUrl();
Expand Down Expand Up @@ -148,9 +148,7 @@ private function getRequestUri(ServiceRequestInterface $serviceRequest)
throw new MalformedRequestException($message, $errorCode);
}

$uri = $this->uriFactory->createUri($baseUrl.$path);

return $uri;
return $this->uriFactory->createUri($baseUrl.$path);
}

/**
Expand Down
14 changes: 8 additions & 6 deletions Tests/Service/APIAsyncClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ protected function setUp()
public function testSendAsync()
{
$uriProphecy = $this->prophesize(UriInterface::class);
$uriProphecy->__call('getPath', [])->willReturn('somePath');
$uriProphecy->getPath()->willReturn('somePath');

$requestProphecy = $this->prophesize(RequestInterface::class);
$requestProphecy->__call('getUri', [])->willReturn($uriProphecy->reveal());
$requestProphecy->__call('getHeaders', [])->willReturn(['someHeader']);
$requestProphecy->getUri()->willReturn($uriProphecy->reveal());
$requestProphecy->getHeaders()->willReturn(['someHeader']);

$request = $requestProphecy->reveal();
$response = $this->prophesize(ResponseInterface::class)->reveal();
$object = new \stdClass();
Expand All @@ -61,19 +63,19 @@ public function testSendAsync()
$serviceRequest = $this->prophesize(ServiceRequestInterface::class)->reveal();

$this->requestFactoryProphecy
->__call('create', [$serviceRequest])
->create($serviceRequest)
->willReturn($request)
->shouldBeCalled()
;

$promise = new FulfilledPromise($response);
$this->clientProphecy
->__call('sendAsyncRequest', [$request])
->sendAsyncRequest($request)
->willReturn($promise)
->shouldBeCalled()
;
$this->responseTransformerProphecy
->__call('transform', [$response, $serviceRequest])
->transform($response, $serviceRequest)
->willReturn($object)
->shouldBeCalled()
;
Expand Down
14 changes: 8 additions & 6 deletions Tests/Service/APIClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ protected function setUp()
public function testSend()
{
$uriProphecy = $this->prophesize(UriInterface::class);
$uriProphecy->__call('getPath', [])->willReturn('somePath');
$uriProphecy->getPath()->willReturn('somePath');

$requestProphecy = $this->prophesize(RequestInterface::class);
$requestProphecy->__call('getUri', [])->willReturn($uriProphecy->reveal());
$requestProphecy->__call('getHeaders', [])->willReturn(['someHeader']);
$requestProphecy->getUri()->willReturn($uriProphecy->reveal());
$requestProphecy->getHeaders()->willReturn(['someHeader']);

$request = $requestProphecy->reveal();
$response = $this->prophesize(ResponseInterface::class)->reveal();
$object = new \stdClass();
Expand All @@ -60,19 +62,19 @@ public function testSend()
$serviceRequest = $this->prophesize(ServiceRequestInterface::class)->reveal();

$this->requestFactoryProphecy
->__call('create', [$serviceRequest])
->create($serviceRequest)
->willReturn($request)
->shouldBeCalled()
;

$this->clientProphecy
->__call('sendRequest', [$request])
->sendRequest($request)
->willReturn($response)
->shouldBeCalled()
;

$this->responseTransformerProphecy
->__call('transform', [$response, $serviceRequest])
->transform($response, $serviceRequest)
->willReturn($object)
->shouldBeCalled()
;
Expand Down
76 changes: 39 additions & 37 deletions Tests/Service/Request/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Http\Message\MessageFactory;
use Http\Message\UriFactory;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
Expand Down Expand Up @@ -84,20 +83,21 @@ public function testBuildFlow()
$routeString = 'routeString';
$requestMethod = 'GET';
$requestBody = '{requestBody:requestBody}';

$endpointProphecy = $this->prophesize(EndpointInterface::class);
$endpointProphecy->__call('getBaseUrl', [])
$endpointProphecy->getBaseUrl()
->willReturn($baseUrl)
->shouldBeCalled()
;
$endpointProphecy->__call('getPath', [])
$endpointProphecy->getPath()
->willReturn($routeString)
->shouldBeCalled()
;
$endpointProphecy->__call('getMethod', [])
$endpointProphecy->getMethod()
->willReturn($requestMethod)
->shouldBeCalled()
;
$endpointProphecy->__call('getRequestFormat', [])
$endpointProphecy->getRequestFormat()
->willReturn(EndpointInterface::FORMAT_JSON)
->shouldBeCalled()
;
Expand All @@ -107,41 +107,42 @@ public function testBuildFlow()
$request = $this->prophesize(RequestInterface::class)->reveal();

$this->endpointRegistryProphecy
->__call('getEndpoint', [Argument::type(ServiceRequestInterface::class)])
->getEndpoint($this->serviceRequestProphecy->reveal())
->willReturn($endpoint)
->shouldBeCalled()
;

$this->serializerProphecy
->__call('serialize', [Argument::type(ServiceRequestInterface::class), EndpointInterface::FORMAT_JSON])
->serialize($this->serviceRequestProphecy->reveal(), EndpointInterface::FORMAT_JSON)
->willReturn($requestBody)
->shouldBeCalled()
;

$this->uriFactoryProphecy
->__call('createUri', [$baseUrl.$routeString])
->createUri($baseUrl . $routeString)
->willReturn($uri)
->shouldBeCalled()
;

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

$this->requestVisitorRegistryProphecy->getRegisteredRequestVisitors(EndpointInterface::FORMAT_JSON)
$this->requestVisitorRegistryProphecy
->getRegisteredRequestVisitors(EndpointInterface::FORMAT_JSON)
->willReturn([$this->requestDecoratorProphecy->reveal(), $this->requestDecoratorProphecy->reveal()])
->shouldBeCalled()
;

$this->requestDecoratorProphecy
->__call('visit', [Argument::type(RequestInterface::class)])
->visit($request)
->willReturn($request)
->shouldBeCalledTimes(2)
;
Expand Down Expand Up @@ -174,19 +175,19 @@ public function testBuildFlowWithQueryParams()
$expectedUri = 'baseUrl/routeString?first-param=value+with+whitespaces&second-param=ignored value';

$endpointProphecy = $this->prophesize(EndpointInterface::class);
$endpointProphecy->__call('getBaseUrl', [])
$endpointProphecy->getBaseUrl()
->willReturn($baseUrl)
->shouldBeCalled()
;
$endpointProphecy->__call('getPath', [])
$endpointProphecy->getPath()
->willReturn($routeString)
->shouldBeCalled()
;
$endpointProphecy->__call('getMethod', [])
$endpointProphecy->getMethod()
->willReturn($requestMethod)
->shouldBeCalled()
;
$endpointProphecy->__call('getRequestFormat', [])
$endpointProphecy->getRequestFormat()
->willReturn(EndpointInterface::FORMAT_JSON)
->shouldBeCalled()
;
Expand All @@ -195,56 +196,55 @@ public function testBuildFlowWithQueryParams()
$uri = $this->prophesize(UriInterface::class)->reveal();
$request = $this->prophesize(RequestInterface::class)->reveal();

// Mock non existing method of ServiceRequest `getParam`
$serviceRequest = $this->getMockBuilder(ServiceRequestInterface::class)
->setMethods(['getFirstParam'])
->getMock();

$serviceRequest
->method('getFirstParam')
->willReturn($originParamValue);

$this->endpointRegistryProphecy
->__call('getEndpoint', [Argument::type(ServiceRequestInterface::class)])
->getEndpoint($serviceRequest)
->willReturn($endpoint)
->shouldBeCalled()
;

$this->serializerProphecy
->__call('serialize', [Argument::type(ServiceRequestInterface::class), EndpointInterface::FORMAT_JSON])
->serialize($serviceRequest, EndpointInterface::FORMAT_JSON)
->willReturn($requestBody)
->shouldBeCalled()
;

$this->uriFactoryProphecy
->__call('createUri', [$expectedUri])
->createUri($expectedUri)
->willReturn($uri)
->shouldBeCalled()
;

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

$this->requestVisitorRegistryProphecy->getRegisteredRequestVisitors(EndpointInterface::FORMAT_JSON)
$this->requestVisitorRegistryProphecy
->getRegisteredRequestVisitors(EndpointInterface::FORMAT_JSON)
->willReturn([])
->shouldBeCalled()
;

$this->requestDecoratorProphecy
->__call('visit', [Argument::type(RequestInterface::class)])
->visit($request)
->shouldNotBeCalled()
;

// Mock non existing method of ServiceRequest `getParam`
$serviceRequest = $this->getMockBuilder(ServiceRequestInterface::class)
->setMethods(['getFirstParam'])
->getMock()
;

$serviceRequest->expects($this->once())
->method('getFirstParam')
->willReturn($originParamValue)
;

$requestBuilder = new RequestFactory(
$this->endpointRegistryProphecy->reveal(),
$this->serializerProphecy->reveal(),
Expand All @@ -264,10 +264,11 @@ public function testBuildFlowWithQueryParams()
*/
public function testBuildFlowValidationFailsOnUnmappedRequestArguments()
{
$this->setExpectedException(InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);

$baseUrl = 'baseUrl';
$routeString = 'routeString\{invalidArgument}';

$endpointProphecy = $this->prophesize(EndpointInterface::class);
$endpointProphecy->getBaseUrl()
->willReturn($baseUrl)
Expand All @@ -278,6 +279,7 @@ public function testBuildFlowValidationFailsOnUnmappedRequestArguments()
->shouldBeCalled()
;
$endpoint = $endpointProphecy->reveal();
$serviceRequest = $this->serviceRequestProphecy->reveal();

$this->endpointRegistryProphecy
->getEndpoint($this->serviceRequestProphecy->reveal())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,25 @@ public function testDecorate()
'additionalHeader1',
'additionalHeader2',
];
$this->headerBagProphecy->__call('has', [Argument::type('string')])
->shouldBeCalledTimes(\count($headerNamesArray))
$timesShouldBeCalled = \count($headerNamesArray);

$this->headerBagProphecy
->__call('has', [Argument::type('string')])
->shouldBeCalledTimes($timesShouldBeCalled)
->willReturn($this->requestProphecy)
;
$this->headerBagProphecy->__call('get', [Argument::type('string')])
->shouldBeCalledTimes(\count($headerNamesArray))
$this->headerBagProphecy
->__call('get', [Argument::type('string')])
->shouldBeCalledTimes($timesShouldBeCalled)
->willReturn('someHeaderValue')
;
/** @var HeaderBag $headerBag */
$headerBag = $this->headerBagProphecy->reveal();
$this->previousRequest->headers = $headerBag;

$this->requestProphecy->__call('withHeader', [Argument::type('string'), Argument::type('string')])
->shouldBeCalledTimes(\count($headerNamesArray))
$this->requestProphecy
->__call('withHeader', [Argument::type('string'), Argument::type('string')])
->shouldBeCalledTimes($timesShouldBeCalled)
->willReturn($this->requestProphecy)
;
/** @var Request $request */
Expand All @@ -69,6 +74,7 @@ public function testDecorate()
$this->previousRequest,
$headerNamesArray
);

$headerPropagationRequestVisitor->visit($request);
}
}
Loading