| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- namespace App\Tests\Unit\Service\Rest;
- use App\Service\Rest\ApiRequestService;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\HttpClient\Exception\TransportException;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Symfony\Contracts\HttpClient\HttpClientInterface;
- use Symfony\Contracts\HttpClient\ResponseInterface;
- class TestableApiRequestService extends ApiRequestService
- {
- public function addBodyOption(array $options, array|string $body): array
- {
- return parent::addBodyOption($options, $body);
- }
- }
- class ApiRequestServiceTest extends TestCase
- {
- private HttpClientInterface $client;
- public function setUp(): void
- {
- $this->client = $this->getMockBuilder(HttpClientInterface::class)->disableOriginalConstructor()->getMock();
- }
- /**
- * @see TestableApiRequestService::getJsonContent()
- */
- public function testGetJsonContent(): void
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['getJsonContent'])
- ->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);
- }
- /**
- * @see TestableApiRequestService::getJsonContent()
- */
- public function testGetContent(): void
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['getContent'])
- ->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);
- }
- /**
- * @see TestableApiRequestService::getContent()
- */
- public function testGetContentWithError(): void
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['getContent'])
- ->getMock();
- $apiRequestService->expects(self::once())
- ->method('get')
- ->willThrowException(new TransportException());
- $this->expectException(HttpException::class);
- $apiRequestService->getContent('path/to/data');
- }
- /**
- * @see TestableApiRequestService::get()
- */
- public function testGet(): void
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['get'])
- ->getMock();
- $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
- $apiRequestService->expects(self::once())
- ->method('request')
- ->with('GET', 'path/to/data', [], [])
- ->willReturn($response);
- $actualResponse = $apiRequestService->get('path/to/data');
- $this->assertEquals($response, $actualResponse);
- }
- public function testAddBodyOptionBodyIsString()
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['addBodyOption'])
- ->getMock();
- $options = ['some_option' => 'some_value'];
- $body = 'foo';
- $result = $apiRequestService->addBodyOption($options, $body);
- $this->assertEquals(
- ['some_option' => 'some_value', 'body' => 'foo'],
- $result
- );
- }
- public function testAddBodyOptionBodyIsArray()
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['addBodyOption'])
- ->getMock();
- $options = ['some_option' => 'some_value'];
- $body = ['foo' => 'bar'];
- $result = $apiRequestService->addBodyOption($options, $body);
- $this->assertEquals(
- ['some_option' => 'some_value', 'json' => ['foo' => 'bar']],
- $result
- );
- }
- public function testAddBodyOptionKeyExists()
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['addBodyOption'])
- ->getMock();
- $options = ['some_option' => 'some_value', 'body' => 'exists'];
- $body = 'foo';
- $result = $apiRequestService->addBodyOption($options, $body);
- $this->assertEquals(
- ['some_option' => 'some_value', 'body' => 'foo'],
- $result
- );
- }
- /**
- * @see TestableApiRequestService::post()
- */
- public function testPost(): void
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['post'])
- ->getMock();
- $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
- $apiRequestService
- ->expects(self::once())
- ->method('request')
- ->with('POST', 'path/to/data', [], ['option' => 2, 'json' => ['foo' => 1]])
- ->willReturn($response);
- $apiRequestService
- ->expects(self::once())
- ->method('addBodyOption')
- ->with(['option' => 2, 'json' => 3], ['foo' => 1])
- ->willReturn(['option' => 2, 'json' => ['foo' => 1]]);
- $actualResponse = $apiRequestService->post('path/to/data', ['foo' => 1], [], ['option' => 2, 'json' => 3]);
- $this->assertEquals($response, $actualResponse);
- }
- /**
- * @see TestableApiRequestService::put()
- */
- public function testPut(): void
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['put'])
- ->getMock();
- $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
- $apiRequestService->expects(self::once())
- ->method('request')
- ->with('PUT', 'path/to/data', [], ['option' => 2, 'body' => 'foo'])
- ->willReturn($response);
- $apiRequestService
- ->expects(self::once())
- ->method('addBodyOption')
- ->with([], 'foo')
- ->willReturn(['option' => 2, 'body' => 'foo']);
- $actualResponse = $apiRequestService->put('path/to/data', 'foo');
- $this->assertEquals($response, $actualResponse);
- }
- /**
- * @see TestableApiRequestService::delete()
- */
- public function testDelete(): void
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['delete'])
- ->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);
- }
- /**
- * @see TestableApiRequestService::request()
- */
- public function testRequest(): void
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['request'])
- ->getMock();
- $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
- $this->client->expects(self::once())->method('request')->with('GET', 'path/to/data?param=1', [])->willReturn($response);
- $actualResponse = $apiRequestService->request('GET', 'path/to/data', ['param' => 1]);
- $this->assertEquals($response, $actualResponse);
- }
- /**
- * @see TestableApiRequestService::request()
- */
- public function testRequestWithError(): void
- {
- $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['request'])
- ->getMock();
- $this->client->expects(self::once())->method('request')->willThrowException(new TransportException('error', 500));
- $this->expectException(HttpException::class);
- $apiRequestService->request('GET', 'path/to/data');
- }
- }
|