Typo3ServiceTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\Tests\Unit\Service\Typo3;
  3. use App\Service\Typo3\Typo3Service;
  4. use App\Service\Utils\DatesUtils;
  5. use PHPUnit\Framework\MockObject\MockBuilder;
  6. use PHPUnit\Framework\MockObject\MockObject;
  7. use PHPUnit\Framework\TestCase;
  8. use Symfony\Contracts\HttpClient\HttpClientInterface;
  9. use Symfony\Contracts\HttpClient\ResponseInterface;
  10. class TestableTypo3Service extends Typo3Service
  11. {
  12. public function sendCommand(string $route, array $parameters, array $headers = []): ResponseInterface
  13. {
  14. return parent::sendCommand($route, $parameters, $headers);
  15. }
  16. }
  17. class Typo3ServiceTest extends TestCase
  18. {
  19. private HttpClientInterface|MockObject $typo3Client;
  20. public function setUp(): void
  21. {
  22. $this->typo3Client = $this
  23. ->getMockBuilder(HttpClientInterface::class)
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. }
  27. private function getTypo3ServiceMockFor(string $methodName): TestableTypo3Service|MockObject
  28. {
  29. return $this->getMockBuilder(TestableTypo3Service::class)
  30. ->setConstructorArgs([$this->typo3Client])
  31. ->setMethodsExcept([$methodName])
  32. ->getMock();
  33. }
  34. /**
  35. * @see Typo3Service::sendCommand()
  36. */
  37. public function testSendCommand(): void
  38. {
  39. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  40. $this->typo3Client
  41. ->expects(self::once())
  42. ->method('request')
  43. ->with('GET', '/typo3/foo?param=bar')
  44. ->willReturn($response);
  45. $typo3Service = $this->getTypo3ServiceMockFor('sendCommand');
  46. $typo3Service->sendCommand('foo', ['param' => 'bar']);
  47. }
  48. /**
  49. * @see Typo3Service::clearSiteCache()
  50. */
  51. public function testClearSiteCache(): void
  52. {
  53. $typo3Service = $this->getTypo3ServiceMockFor('clearSiteCache');
  54. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  55. $typo3Service->expects(self::once())
  56. ->method('sendCommand')->
  57. with('/otadmin/site/clear-cache', ['organization-id' => 1])
  58. ->willReturn($response);
  59. $typo3Service->clearSiteCache(1);
  60. }
  61. /**
  62. * @see Typo3Service::createSite()
  63. */
  64. public function testCreateSite(): void
  65. {
  66. $typo3Service = $this->getTypo3ServiceMockFor('createSite');
  67. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  68. $typo3Service->expects(self::once())
  69. ->method('sendCommand')->
  70. with('/otadmin/site/create', ['organization-id' => 1])
  71. ->willReturn($response);
  72. $typo3Service->createSite(1);
  73. }
  74. /**
  75. * @see Typo3Service::updateSite()
  76. */
  77. public function testUpdateSite(): void
  78. {
  79. $typo3Service = $this->getTypo3ServiceMockFor('updateSite');
  80. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  81. $typo3Service->expects(self::once())
  82. ->method('sendCommand')->
  83. with('/otadmin/site/update', ['organization-id' => 1])
  84. ->willReturn($response);
  85. $typo3Service->updateSite(1);
  86. }
  87. /**
  88. * @see Typo3Service::deleteSite()
  89. */
  90. public function testDeleteSite(): void
  91. {
  92. $typo3Service = $this->getTypo3ServiceMockFor('deleteSite');
  93. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  94. $typo3Service->expects(self::once())
  95. ->method('sendCommand')->
  96. with('/otadmin/site/delete', ['organization-id' => 1])
  97. ->willReturn($response);
  98. $typo3Service->deleteSite(1);
  99. }
  100. /**
  101. * @see Typo3Service::deleteSite()
  102. */
  103. public function testHardDeleteSite(): void
  104. {
  105. DatesUtils::setFakeDatetime('2025-01-01 00:00:00');
  106. $typo3Service = $this->getTypo3ServiceMockFor('hardDeleteSite');
  107. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  108. $typo3Service->expects(self::once())
  109. ->method('sendCommand')
  110. ->with(
  111. '/otadmin/site/delete',
  112. ['organization-id' => 1, 'hard' => 1],
  113. ['Confirmation-Token' => 'DEL-1-20250101']
  114. )
  115. ->willReturn($response);
  116. $typo3Service->hardDeleteSite(1);
  117. }
  118. /**
  119. * @see Typo3Service::undeleteSite()
  120. */
  121. public function testUndeleteSite(): void
  122. {
  123. $typo3Service = $this->getTypo3ServiceMockFor('undeleteSite');
  124. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  125. $typo3Service->expects(self::once())
  126. ->method('sendCommand')->
  127. with('/otadmin/site/undelete', ['organization-id' => 1])
  128. ->willReturn($response);
  129. $typo3Service->undeleteSite(1);
  130. }
  131. /**
  132. * @see Typo3Service::setSiteDomain()
  133. */
  134. public function testSetSiteDomain(): void
  135. {
  136. $typo3Service = $this->getTypo3ServiceMockFor('setSiteDomain');
  137. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  138. $typo3Service->expects(self::once())
  139. ->method('sendCommand')->
  140. with('/otadmin/site/set-domain', ['organization-id' => 1, 'domain' => 'new-domain'])
  141. ->willReturn($response);
  142. $typo3Service->setSiteDomain(1, 'new-domain', false);
  143. }
  144. /**
  145. * @see Typo3Service::setSiteDomain()
  146. */
  147. public function testSetSiteDomainWithRedirection(): void
  148. {
  149. $typo3Service = $this->getTypo3ServiceMockFor('setSiteDomain');
  150. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  151. $typo3Service->expects(self::once())
  152. ->method('sendCommand')->
  153. with('/otadmin/site/set-domain', ['organization-id' => 1, 'domain' => 'new-domain', 'redirect' => 1])
  154. ->willReturn($response);
  155. $typo3Service->setSiteDomain(1, 'new-domain', true);
  156. }
  157. /**
  158. * @see Typo3Service::resetSitePerms()
  159. */
  160. public function testResetSitePerms(): void
  161. {
  162. $typo3Service = $this->getTypo3ServiceMockFor('resetSitePerms');
  163. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  164. $typo3Service->expects(self::once())
  165. ->method('sendCommand')->
  166. with('/otadmin/site/reset-perms', ['organization-id' => 1])
  167. ->willReturn($response);
  168. $typo3Service->resetSitePerms(1);
  169. }
  170. /**
  171. * @see Typo3Service::getSiteStatus()
  172. */
  173. public function testGetSiteStatus(): void
  174. {
  175. $typo3Service = $this->getTypo3ServiceMockFor('getSiteStatus');
  176. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  177. $typo3Service->expects(self::once())
  178. ->method('sendCommand')->
  179. with('/otadmin/site/status', ['organization-id' => 1])
  180. ->willReturn($response);
  181. $typo3Service->getSiteStatus(1);
  182. }
  183. /**
  184. * @see Typo3Service::addRedirection()
  185. */
  186. public function testAddRedirection(): void
  187. {
  188. $typo3Service = $this->getTypo3ServiceMockFor('addRedirection');
  189. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  190. $typo3Service->expects(self::once())
  191. ->method('sendCommand')->
  192. with('/otadmin/redirect/add', ['from-domain' => 'foo', 'to-domain' => 'bar'])
  193. ->willReturn($response);
  194. $typo3Service->addRedirection('foo', 'bar');
  195. }
  196. }