|
|
@@ -3,44 +3,169 @@
|
|
|
namespace App\Tests\Service;
|
|
|
|
|
|
use App\Service\Rest\ApiRequestService;
|
|
|
+use AssertionError;
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
+use Symfony\Component\HttpClient\Exception\TransportException;
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
|
|
+use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
|
|
class ApiRequestServiceTest extends TestCase
|
|
|
{
|
|
|
- private ApiRequestService $apiRequestService;
|
|
|
+ private HttpClientInterface $client;
|
|
|
|
|
|
public function setUp(): void {
|
|
|
- $client = $this->getMockBuilder(HttpClientInterface::class)
|
|
|
- ->disableOriginalConstructor()
|
|
|
+ $this->client = $this->getMockBuilder(HttpClientInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testGetJsonContent() {
|
|
|
+ $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ ->setConstructorArgs([$this->client])
|
|
|
+ ->onlyMethods(['getContent'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $apiRequestService->expects(self::once())
|
|
|
+ ->method('getContent')
|
|
|
+ ->with('path/to/data', [], [])
|
|
|
+ ->willReturn('{"foo": "bar"}');
|
|
|
+
|
|
|
+ $data = $apiRequestService->getJsonContent('path/to/data');
|
|
|
+
|
|
|
+ $this->assertEquals(['foo' => 'bar'], $data);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testGetContent() {
|
|
|
+ $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ ->setConstructorArgs([$this->client])
|
|
|
+ ->onlyMethods(['get'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $response->expects(self::once())->method('getContent')->willReturn('{foo: bar}');
|
|
|
+
|
|
|
+ $apiRequestService->expects(self::once())
|
|
|
+ ->method('get')
|
|
|
+ ->with('path/to/data', [], [])
|
|
|
+ ->willReturn($response);
|
|
|
+
|
|
|
+ $content = $apiRequestService->getContent('path/to/data');
|
|
|
+
|
|
|
+ $this->assertEquals('{foo: bar}', $content);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testGetContentWithError() {
|
|
|
+ $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ ->setConstructorArgs([$this->client])
|
|
|
+ ->onlyMethods(['get'])
|
|
|
->getMock();
|
|
|
|
|
|
- $response = $this->getMockBuilder(ResponseInterface::class)
|
|
|
- ->disableOriginalConstructor()
|
|
|
+ $apiRequestService->expects(self::once())
|
|
|
+ ->method('get')
|
|
|
+ ->willThrowException(new TransportException());
|
|
|
+
|
|
|
+ try {
|
|
|
+ $apiRequestService->getContent('path/to/data');
|
|
|
+ throw new AssertionError('An HttpException should have been thrown, but has not');
|
|
|
+ } catch (HttpException $e) {
|
|
|
+ $this->assertEquals(500, $e->getStatusCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testGet() {
|
|
|
+ $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ ->setConstructorArgs([$this->client])
|
|
|
+ ->onlyMethods(['request'])
|
|
|
->getMock();
|
|
|
- $response->method('getContent')->willReturn('{"a": 1}');
|
|
|
|
|
|
- $client
|
|
|
- ->expects($this->once())
|
|
|
+ $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+
|
|
|
+ $apiRequestService->expects(self::once())
|
|
|
->method('request')
|
|
|
- ->with("GET", "my_url.org")
|
|
|
+ ->with('GET', 'path/to/data', [], [])
|
|
|
->willReturn($response);
|
|
|
|
|
|
- $this->apiRequestService = new ApiRequestService($client);
|
|
|
+ $actualResponse = $apiRequestService->get('path/to/data');
|
|
|
+
|
|
|
+ $this->assertEquals($response, $actualResponse);
|
|
|
}
|
|
|
|
|
|
- public function testGetJsonContent() {
|
|
|
- $this->assertEquals(
|
|
|
- ['a' => 1],
|
|
|
- $this->apiRequestService->getJsonContent('my_url.org')
|
|
|
- );
|
|
|
+ public function testPost() {
|
|
|
+ $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ ->setConstructorArgs([$this->client])
|
|
|
+ ->onlyMethods(['request'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+
|
|
|
+ $apiRequestService->expects(self::once())
|
|
|
+ ->method('request')
|
|
|
+ ->with('POST', 'path/to/data', [], [])
|
|
|
+ ->willReturn($response);
|
|
|
+
|
|
|
+ $actualResponse = $apiRequestService->post('path/to/data');
|
|
|
+
|
|
|
+ $this->assertEquals($response, $actualResponse);
|
|
|
}
|
|
|
|
|
|
- public function testGetContent() {
|
|
|
- $this->assertEquals(
|
|
|
- '{"a": 1}',
|
|
|
- $this->apiRequestService->getContent('my_url.org')
|
|
|
- );
|
|
|
+ public function testPut() {
|
|
|
+ $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ ->setConstructorArgs([$this->client])
|
|
|
+ ->onlyMethods(['request'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+
|
|
|
+ $apiRequestService->expects(self::once())
|
|
|
+ ->method('request')
|
|
|
+ ->with('PUT', 'path/to/data', [], [])
|
|
|
+ ->willReturn($response);
|
|
|
+
|
|
|
+ $actualResponse = $apiRequestService->put('path/to/data');
|
|
|
+
|
|
|
+ $this->assertEquals($response, $actualResponse);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testDelete() {
|
|
|
+ $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ ->setConstructorArgs([$this->client])
|
|
|
+ ->onlyMethods(['request'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+
|
|
|
+ $apiRequestService->expects(self::once())
|
|
|
+ ->method('request')
|
|
|
+ ->with('DELETE', 'path/to/data', [], [])
|
|
|
+ ->willReturn($response);
|
|
|
+
|
|
|
+ $actualResponse = $apiRequestService->delete('path/to/data');
|
|
|
+
|
|
|
+ $this->assertEquals($response, $actualResponse);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testRequest() {
|
|
|
+ $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+
|
|
|
+ $this->client->expects(self::once())->method('request')->with('GET', 'path/to/data?param=1', [])->willReturn($response);
|
|
|
+
|
|
|
+ $apiRequestService = new ApiRequestService($this->client);
|
|
|
+
|
|
|
+ $actualResponse = $apiRequestService->request('GET', 'path/to/data', ['param' => 1]);
|
|
|
+
|
|
|
+ $this->assertEquals($response, $actualResponse);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testRequestWithError() {
|
|
|
+
|
|
|
+ $this->client->expects(self::once())->method('request')->willThrowException(new TransportException('error', 500));
|
|
|
+
|
|
|
+ $apiRequestService = new ApiRequestService($this->client);
|
|
|
+
|
|
|
+ try {
|
|
|
+ $apiRequestService->request('GET', 'path/to/data');
|
|
|
+ throw new AssertionError('An HttpException should have been thrown, but has not');
|
|
|
+ } catch (HttpException $e) {
|
|
|
+ $this->assertEquals(500, $e->getStatusCode());
|
|
|
+ }
|
|
|
}
|
|
|
}
|