|
| 1 | +<?php |
| 2 | +namespace Tests\Functional\Infra\Endpoint\JsonRpcHttpEndpoint; |
| 3 | + |
| 4 | +use PHPUnit\Framework\TestCase; |
| 5 | +use Prophecy\Argument; |
| 6 | +use Prophecy\Prophecy\ObjectProphecy; |
| 7 | +use Symfony\Component\HttpFoundation\Request; |
| 8 | +use Symfony\Component\HttpFoundation\Response; |
| 9 | +use Yoanm\SymfonyJsonRpcHttpServer\Infra\Endpoint\JsonRpcHttpEndpoint; |
| 10 | +use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint as SDKJsonRpcEndpoint; |
| 11 | + |
| 12 | +/** |
| 13 | + * @covers \Yoanm\SymfonyJsonRpcHttpServer\Infra\Endpoint\JsonRpcHttpEndpoint |
| 14 | + */ |
| 15 | +class JsonRpcHttpEndpointTest extends TestCase |
| 16 | +{ |
| 17 | + /** @var JsonRpcHttpEndpoint */ |
| 18 | + private $endpoint; |
| 19 | + |
| 20 | + /** @var SdkJsonRpcEndpoint|ObjectProphecy */ |
| 21 | + private $sdkEndpoint; |
| 22 | + |
| 23 | + protected function setUp() |
| 24 | + { |
| 25 | + $this->sdkEndpoint = $this->prophesize(SdkJsonRpcEndpoint::class); |
| 26 | + |
| 27 | + $this->endpoint = new JsonRpcHttpEndpoint( |
| 28 | + $this->sdkEndpoint->reveal() |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + public function testShouldHandleRequestContentAndReturnA200ResponseContainingSDKEndpointReturnedValue() |
| 33 | + { |
| 34 | + $requestContent = 'request-content'; |
| 35 | + $expextedResponseContent = 'expected-response-content'; |
| 36 | + |
| 37 | + /** @var Request|ObjectProphecy $request */ |
| 38 | + $request = $this->prophesize(Request::class); |
| 39 | + |
| 40 | + $request->getMethod() |
| 41 | + ->willReturn(Request::METHOD_POST) |
| 42 | + ->shouldBeCalled(); |
| 43 | + |
| 44 | + $request->getContent() |
| 45 | + ->willReturn($requestContent) |
| 46 | + ->shouldBeCalled(); |
| 47 | + |
| 48 | + $this->sdkEndpoint->index($requestContent) |
| 49 | + ->willReturn($expextedResponseContent) |
| 50 | + ->shouldBeCalled(); |
| 51 | + |
| 52 | + $response = $this->endpoint->index($request->reveal()); |
| 53 | + |
| 54 | + $this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
| 55 | + $this->assertSame($expextedResponseContent, $response->getContent()); |
| 56 | + } |
| 57 | + |
| 58 | + public function testShouldCheckIfRequestUsePostMethodAndReturnErrorResponseIfNot() |
| 59 | + { |
| 60 | + /** @var Request|ObjectProphecy $request */ |
| 61 | + $request = $this->prophesize(Request::class); |
| 62 | + |
| 63 | + $request->getMethod() |
| 64 | + ->willReturn(Request::METHOD_GET) |
| 65 | + ->shouldBeCalled(); |
| 66 | + |
| 67 | + $response = $this->endpoint->index($request->reveal()); |
| 68 | + |
| 69 | + $this->assertSame(Response::HTTP_METHOD_NOT_ALLOWED, $response->getStatusCode()); |
| 70 | + $this->assertSame('A JSON-RPC HTTP call must use POST', $response->getContent()); |
| 71 | + } |
| 72 | +} |
0 commit comments