Typo3ServiceTest.php 7.7 KB

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