BaseRestOperationTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. use App\Service\Rest\ApiRequestService;
  3. use App\Service\Rest\Operation\BaseRestOperation;
  4. use PHPUnit\Framework\TestCase;
  5. use Symfony\Component\HttpClient\Exception\ClientException;
  6. use Symfony\Contracts\HttpClient\ResponseInterface;
  7. class TestableBaseRestOperation extends BaseRestOperation {
  8. public function getChangeLog(): array { return []; }
  9. }
  10. class BaseRestOperationTest extends TestCase
  11. {
  12. private ApiRequestService $apiRequestService;
  13. public function setUp(): void {
  14. $this->apiRequestService = $this->getMockBuilder(ApiRequestService::class)
  15. ->disableOriginalConstructor()
  16. ->getMock();
  17. }
  18. public function testGetters() {
  19. $operation = new TestableBaseRestOperation(
  20. 'a label',
  21. 'GET',
  22. '/a/path',
  23. ['data' => 1],
  24. ['param' => 2],
  25. ['option' => 3]
  26. );
  27. $this->assertEquals('a label', $operation->getLabel());
  28. $this->assertEquals('GET', $operation->getMethod());
  29. $this->assertEquals('/a/path', $operation->getPath());
  30. $this->assertEquals(['data' => 1], $operation->getInitialData());
  31. $this->assertEquals(['param' => 2], $operation->getParameters());
  32. $this->assertEquals(['option' => 3], $operation->getOptions());
  33. $this->assertEquals('GET /a/path', (string)$operation);
  34. }
  35. /**
  36. * Test execution with a valid request
  37. */
  38. public function testExecuteValid()
  39. {
  40. $operation = new TestableBaseRestOperation(
  41. 'Update entity 1', 'PUT', 'entity/1', [], [], ['json' => '{"a":1}']
  42. );
  43. $responseOk = $this->getMockBuilder(ResponseInterface::class)->getMock();
  44. $responseOk->method('getStatusCode')->willReturn(200);
  45. $this->apiRequestService
  46. ->expects($this->once())
  47. ->method('request')
  48. ->with('PUT', 'entity/1', [], ['json' => '{"a":1}'])
  49. ->willReturn($responseOk);
  50. $operation->execute($this->apiRequestService);
  51. $this->assertEquals(BaseRestOperation::STATUS_DONE, $operation->getStatus());
  52. $this->assertEquals("", $operation->getErrorMessage());
  53. }
  54. /**
  55. * Test execution with an invalid request (api returns an error 404 for example)
  56. */
  57. public function testExecuteInvalid()
  58. {
  59. $operation = new TestableBaseRestOperation(
  60. 'Update entity 1', 'PUT', 'entity/2'
  61. );
  62. $responseError = $this->getMockBuilder(ResponseInterface::class)->getMock();
  63. $responseError->method('getStatusCode')->willReturn(404);
  64. $responseError->method('getContent')->willReturn('Not found');
  65. $this->apiRequestService
  66. ->expects($this->once())
  67. ->method('request')
  68. ->with('PUT', 'entity/2')
  69. ->willReturn($responseError);
  70. try {
  71. $operation->execute($this->apiRequestService);
  72. } catch (RuntimeException) {
  73. }
  74. $this->assertEquals(BaseRestOperation::STATUS_ERROR, $operation->getStatus());
  75. $this->assertMatchesRegularExpression('/.*Not found.*/', $operation->getErrorMessage());
  76. }
  77. /**
  78. * Test execution if the request throw an HTTP exception
  79. */
  80. public function testExecutionError() {
  81. $operation = new TestableBaseRestOperation(
  82. 'Update entity 1', 'PUT', 'entity/3'
  83. );
  84. $responseException = $this->getMockBuilder(ResponseInterface::class)->getMock();
  85. $responseException->method('getStatusCode')->willReturn(500);
  86. $responseException->method('getContent')->willReturn('The server said the request is bad');
  87. $responseException->method('getInfo')->willReturnMap([
  88. ['http_code', 500],
  89. ['url', 'entity/3'],
  90. ['response_headers', ['content-type: json']]
  91. ]);
  92. $this->apiRequestService
  93. ->expects($this->once())
  94. ->method('request')
  95. ->with('PUT', 'entity/3')
  96. ->willThrowException(new ClientException($responseException));
  97. try {
  98. $operation->execute($this->apiRequestService);
  99. } catch (RuntimeException) {
  100. }
  101. $this->assertEquals(BaseRestOperation::STATUS_ERROR, $operation->getStatus());
  102. $this->assertMatchesRegularExpression(
  103. '/.*ClientException: HTTP 500 returned for "entity\/3".*/',
  104. $operation->getErrorMessage()
  105. );
  106. }
  107. }