|
|
@@ -9,6 +9,15 @@ 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;
|
|
|
@@ -19,11 +28,11 @@ class ApiRequestServiceTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @see ApiRequestService::getJsonContent()
|
|
|
+ * @see TestableApiRequestService::getJsonContent()
|
|
|
*/
|
|
|
public function testGetJsonContent(): void
|
|
|
{
|
|
|
- $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
|
|
|
->setConstructorArgs([$this->client])
|
|
|
->setMethodsExcept(['getJsonContent'])
|
|
|
->getMock();
|
|
|
@@ -39,11 +48,11 @@ class ApiRequestServiceTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @see ApiRequestService::getJsonContent()
|
|
|
+ * @see TestableApiRequestService::getJsonContent()
|
|
|
*/
|
|
|
public function testGetContent(): void
|
|
|
{
|
|
|
- $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
|
|
|
->setConstructorArgs([$this->client])
|
|
|
->setMethodsExcept(['getContent'])
|
|
|
->getMock();
|
|
|
@@ -62,11 +71,11 @@ class ApiRequestServiceTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @see ApiRequestService::getContent()
|
|
|
+ * @see TestableApiRequestService::getContent()
|
|
|
*/
|
|
|
public function testGetContentWithError(): void
|
|
|
{
|
|
|
- $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
|
|
|
->setConstructorArgs([$this->client])
|
|
|
->setMethodsExcept(['getContent'])
|
|
|
->getMock();
|
|
|
@@ -80,11 +89,11 @@ class ApiRequestServiceTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @see ApiRequestService::get()
|
|
|
+ * @see TestableApiRequestService::get()
|
|
|
*/
|
|
|
public function testGet(): void
|
|
|
{
|
|
|
- $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
|
|
|
->setConstructorArgs([$this->client])
|
|
|
->setMethodsExcept(['get'])
|
|
|
->getMock();
|
|
|
@@ -102,33 +111,40 @@ class ApiRequestServiceTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @see ApiRequestService::post()
|
|
|
+ * @see TestableApiRequestService::post()
|
|
|
*/
|
|
|
public function testPost(): void
|
|
|
{
|
|
|
- $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
|
|
|
->setConstructorArgs([$this->client])
|
|
|
->setMethodsExcept(['post'])
|
|
|
->getMock();
|
|
|
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
- $apiRequestService->expects(self::once())
|
|
|
+ $apiRequestService
|
|
|
+ ->expects(self::once())
|
|
|
->method('request')
|
|
|
- ->with('POST', 'path/to/data', [], [])
|
|
|
+ ->with('POST', 'path/to/data', [], ['option' => 2, 'json' => ['foo' => 1]])
|
|
|
->willReturn($response);
|
|
|
|
|
|
- $actualResponse = $apiRequestService->post('path/to/data');
|
|
|
+ $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 ApiRequestService::put()
|
|
|
+ * @see TestableApiRequestService::put()
|
|
|
*/
|
|
|
public function testPut(): void
|
|
|
{
|
|
|
- $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
|
|
|
->setConstructorArgs([$this->client])
|
|
|
->setMethodsExcept(['put'])
|
|
|
->getMock();
|
|
|
@@ -137,20 +153,26 @@ class ApiRequestServiceTest extends TestCase
|
|
|
|
|
|
$apiRequestService->expects(self::once())
|
|
|
->method('request')
|
|
|
- ->with('PUT', 'path/to/data', [], [])
|
|
|
+ ->with('PUT', 'path/to/data', [], ['option' => 2, 'body' => 'foo'])
|
|
|
->willReturn($response);
|
|
|
|
|
|
- $actualResponse = $apiRequestService->put('path/to/data');
|
|
|
+ $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 ApiRequestService::delete()
|
|
|
+ * @see TestableApiRequestService::delete()
|
|
|
*/
|
|
|
public function testDelete(): void
|
|
|
{
|
|
|
- $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
|
|
|
->setConstructorArgs([$this->client])
|
|
|
->setMethodsExcept(['delete'])
|
|
|
->getMock();
|
|
|
@@ -168,11 +190,11 @@ class ApiRequestServiceTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @see ApiRequestService::request()
|
|
|
+ * @see TestableApiRequestService::request()
|
|
|
*/
|
|
|
public function testRequest(): void
|
|
|
{
|
|
|
- $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
|
|
|
->setConstructorArgs([$this->client])
|
|
|
->setMethodsExcept(['request'])
|
|
|
->getMock();
|
|
|
@@ -186,11 +208,11 @@ class ApiRequestServiceTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @see ApiRequestService::request()
|
|
|
+ * @see TestableApiRequestService::request()
|
|
|
*/
|
|
|
public function testRequestWithError(): void
|
|
|
{
|
|
|
- $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
|
|
|
+ $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
|
|
|
->setConstructorArgs([$this->client])
|
|
|
->setMethodsExcept(['request'])
|
|
|
->getMock();
|