BaseRestOperationTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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(): void
  19. {
  20. $operation = new TestableBaseRestOperation(
  21. 'a label',
  22. 'GET',
  23. '/a/path',
  24. ['data' => 1],
  25. ['param' => 2],
  26. ['option' => 3]
  27. );
  28. $this->assertEquals('a label', $operation->getLabel());
  29. $this->assertEquals('GET', $operation->getMethod());
  30. $this->assertEquals('/a/path', $operation->getPath());
  31. $this->assertEquals(['data' => 1], $operation->getInitialData());
  32. $this->assertEquals(['param' => 2], $operation->getParameters());
  33. $this->assertEquals(['option' => 3], $operation->getOptions());
  34. $this->assertEquals('GET /a/path', (string)$operation);
  35. }
  36. /**
  37. * Test execution with a valid request
  38. */
  39. public function testExecuteValid(): void
  40. {
  41. $operation = $this->getMockBuilder(TestableBaseRestOperation::class)
  42. ->setConstructorArgs(['Update entity 1', 'PUT', 'entity/1', [], [], ['json' => '{"a":1}']])
  43. ->setMethodsExcept(['execute', 'getStatus', 'getErrorMessage'])
  44. ->getMock();
  45. $responseOk = $this->getMockBuilder(ResponseInterface::class)->getMock();
  46. $responseOk->method('getStatusCode')->willReturn(200);
  47. $this->apiRequestService
  48. ->expects($this->once())
  49. ->method('request')
  50. ->with('PUT', 'entity/1', [], ['json' => '{"a":1}'])
  51. ->willReturn($responseOk);
  52. $operation->execute($this->apiRequestService);
  53. $this->assertEquals(BaseRestOperation::STATUS_DONE, $operation->getStatus());
  54. $this->assertEquals("", $operation->getErrorMessage());
  55. }
  56. /**
  57. * Test execution with an invalid request (api returns an error 404 for example)
  58. */
  59. public function testExecuteInvalid(): void
  60. {
  61. $operation = $this->getMockBuilder(TestableBaseRestOperation::class)
  62. ->setConstructorArgs(['Update entity 1', 'PUT', 'entity/2'])
  63. ->setMethodsExcept(['execute', 'getStatus', 'getErrorMessage'])
  64. ->getMock();
  65. $responseError = $this->getMockBuilder(ResponseInterface::class)->getMock();
  66. $responseError->method('getStatusCode')->willReturn(404);
  67. $responseError->method('getContent')->willReturn('Not found');
  68. $this->apiRequestService
  69. ->expects($this->once())
  70. ->method('request')
  71. ->with('PUT', 'entity/2')
  72. ->willReturn($responseError);
  73. try {
  74. $operation->execute($this->apiRequestService);
  75. } catch (RuntimeException) {
  76. }
  77. $this->assertEquals(BaseRestOperation::STATUS_ERROR, $operation->getStatus());
  78. $this->assertMatchesRegularExpression('/.*Not found.*/', $operation->getErrorMessage());
  79. }
  80. /**
  81. * Test execution if the request throw an HTTP exception
  82. */
  83. public function testExecutionError(): void
  84. {
  85. $operation = $this->getMockBuilder(TestableBaseRestOperation::class)
  86. ->setConstructorArgs(['Update entity 1', 'PUT', 'entity/3'])
  87. ->setMethodsExcept(['execute', 'getStatus', 'getErrorMessage'])
  88. ->getMock();
  89. $responseException = $this->getMockBuilder(ResponseInterface::class)->getMock();
  90. $responseException->method('getStatusCode')->willReturn(500);
  91. $responseException->method('getContent')->willReturn('The server said the request is bad');
  92. $responseException->method('getInfo')->willReturnMap([
  93. ['http_code', 500],
  94. ['url', 'entity/3'],
  95. ['response_headers', ['content-type: json']]
  96. ]);
  97. $this->apiRequestService
  98. ->expects($this->once())
  99. ->method('request')
  100. ->with('PUT', 'entity/3')
  101. ->willThrowException(new ClientException($responseException));
  102. try {
  103. $operation->execute($this->apiRequestService);
  104. } catch (RuntimeException) {
  105. }
  106. $this->assertEquals(BaseRestOperation::STATUS_ERROR, $operation->getStatus());
  107. $this->assertMatchesRegularExpression(
  108. '/.*ClientException: HTTP 500 returned for "entity\/3".*/',
  109. $operation->getErrorMessage()
  110. );
  111. }
  112. }