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->getCurrentData());
  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->setApiRequestService($this->apiRequestService);
  51. $operation->execute();
  52. $this->assertEquals(BaseRestOperation::STATUS_DONE, $operation->getStatus());
  53. $this->assertEquals("", $operation->getErrorMessage());
  54. }
  55. /**
  56. * Test execution with an invalid request (api returns an error 404 for example)
  57. */
  58. public function testExecuteInvalid()
  59. {
  60. $operation = new TestableBaseRestOperation(
  61. 'Update entity 1', 'PUT', 'entity/2'
  62. );
  63. $responseError = $this->getMockBuilder(ResponseInterface::class)->getMock();
  64. $responseError->method('getStatusCode')->willReturn(404);
  65. $responseError->method('getContent')->willReturn('Not found');
  66. $this->apiRequestService
  67. ->expects($this->once())
  68. ->method('request')
  69. ->with('PUT', 'entity/2')
  70. ->willReturn($responseError);
  71. $operation->setApiRequestService($this->apiRequestService);
  72. $operation->execute();
  73. $this->assertEquals(BaseRestOperation::STATUS_ERROR, $operation->getStatus());
  74. $this->assertEquals('Error 404 : Not found', $operation->getErrorMessage());
  75. }
  76. /**
  77. * Test execution if the request throw an HTTP exception
  78. */
  79. public function testExecutionError() {
  80. $operation = new TestableBaseRestOperation(
  81. 'Update entity 1', 'PUT', 'entity/3'
  82. );
  83. $responseException = $this->getMockBuilder(ResponseInterface::class)->getMock();
  84. $responseException->method('getStatusCode')->willReturn(500);
  85. $responseException->method('getContent')->willReturn('The server said the request is bad');
  86. $responseException->method('getInfo')->willReturnMap([
  87. ['http_code', 500],
  88. ['url', 'entity/3'],
  89. ['response_headers', ['content-type: json']]
  90. ]);
  91. $this->apiRequestService
  92. ->expects($this->once())
  93. ->method('request')
  94. ->with('PUT', 'entity/3')
  95. ->willThrowException(new ClientException($responseException));
  96. $operation->setApiRequestService($this->apiRequestService);
  97. $operation->execute();
  98. $this->assertEquals(BaseRestOperation::STATUS_ERROR, $operation->getStatus());
  99. $this->assertMatchesRegularExpression(
  100. '/.*ClientException: HTTP 500 returned for "entity\/3".*/',
  101. $operation->getErrorMessage()
  102. );
  103. }
  104. }