BaseRestOperationTest.php 5.6 KB

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