BaseRestOperationTest.php 5.6 KB

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