BaseRestOperationTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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->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. $operation->execute($this->apiRequestService);
  71. $this->assertEquals(BaseRestOperation::STATUS_ERROR, $operation->getStatus());
  72. $this->assertEquals('Error 404 : Not found', $operation->getErrorMessage());
  73. }
  74. /**
  75. * Test execution if the request throw an HTTP exception
  76. */
  77. public function testExecutionError() {
  78. $operation = new TestableBaseRestOperation(
  79. 'Update entity 1', 'PUT', 'entity/3'
  80. );
  81. $responseException = $this->getMockBuilder(ResponseInterface::class)->getMock();
  82. $responseException->method('getStatusCode')->willReturn(500);
  83. $responseException->method('getContent')->willReturn('The server said the request is bad');
  84. $responseException->method('getInfo')->willReturnMap([
  85. ['http_code', 500],
  86. ['url', 'entity/3'],
  87. ['response_headers', ['content-type: json']]
  88. ]);
  89. $this->apiRequestService
  90. ->expects($this->once())
  91. ->method('request')
  92. ->with('PUT', 'entity/3')
  93. ->willThrowException(new ClientException($responseException));
  94. $operation->execute($this->apiRequestService);
  95. $this->assertEquals(BaseRestOperation::STATUS_ERROR, $operation->getStatus());
  96. $this->assertMatchesRegularExpression(
  97. '/.*ClientException: HTTP 500 returned for "entity\/3".*/',
  98. $operation->getErrorMessage()
  99. );
  100. }
  101. }