TrialTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Service\Organization;
  4. use App\ApiResources\Shop\NewStructureArtistPremiumTrialRequest;
  5. use App\Entity\Organization\Organization;
  6. use App\Entity\Organization\Settings;
  7. use App\Enum\Organization\SettingsProductEnum;
  8. use App\Service\Dolibarr\DolibarrApiService;
  9. use App\Service\Dolibarr\DolibarrUtils;
  10. use App\Service\Shop\Trial;
  11. use App\Service\Utils\DatesUtils;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use PHPUnit\Framework\TestCase;
  14. /**
  15. * Unit tests for Trial class.
  16. *
  17. * @see Trial
  18. */
  19. class TrialTest extends TestCase
  20. {
  21. private DatesUtils $datesUtils;
  22. private Trial $trial;
  23. private EntityManagerInterface $entityManager;
  24. private DolibarrApiService $dolibarrApiService;
  25. private DolibarrUtils $dolibarrUtils;
  26. public function setUp(): void
  27. {
  28. $this->datesUtils = new DatesUtils();
  29. $this->entityManager = $this->createMock(EntityManagerInterface::class);
  30. $this->dolibarrApiService = $this->createMock(DolibarrApiService::class);
  31. $this->dolibarrUtils = $this->createMock(DolibarrUtils::class);
  32. $this->trial = new Trial(
  33. $this->datesUtils,
  34. $this->entityManager,
  35. $this->dolibarrApiService,
  36. $this->dolibarrUtils
  37. );
  38. }
  39. public function tearDown(): void
  40. {
  41. DatesUtils::clearFakeDatetime();
  42. }
  43. /**
  44. * @see Trial::getTrialCountdown()
  45. */
  46. public function testGetTrialCountdownWithNullStartDate(): void
  47. {
  48. $result = $this->trial->getTrialCountdown(null);
  49. $this->assertEquals(0, $result);
  50. }
  51. /**
  52. * @see Trial::getTrialCountdown()
  53. */
  54. public function testGetTrialCountdownWithRecentStartDate(): void
  55. {
  56. // Set up a trial start date
  57. $trialStartDate = new \DateTime('2023-01-01');
  58. // Set the current date to be 10 days after the trial start date
  59. DatesUtils::setFakeDatetime('2023-01-11');
  60. $result = $this->trial->getTrialCountdown($trialStartDate);
  61. // Should return 30 - 10 = 20 days remaining
  62. $this->assertEquals(20, $result);
  63. }
  64. /**
  65. * @see Trial::getTrialCountdown()
  66. */
  67. public function testGetTrialCountdownWithExactly30DaysAgo(): void
  68. {
  69. // Set up a trial start date
  70. $trialStartDate = new \DateTime('2023-01-01');
  71. // Set the current date to be 30 days after the trial start date
  72. DatesUtils::setFakeDatetime('2023-01-31');
  73. $result = $this->trial->getTrialCountdown($trialStartDate);
  74. // Should return 30 - 30 = 0 days remaining
  75. $this->assertEquals(0, $result);
  76. }
  77. /**
  78. * @see Trial::getTrialCountdown()
  79. */
  80. public function testGetTrialCountdownWithOldStartDate(): void
  81. {
  82. // Set up a trial start date
  83. $trialStartDate = new \DateTime('2023-01-01');
  84. // Set the current date to be 40 days after the trial start date
  85. DatesUtils::setFakeDatetime('2023-02-10');
  86. $result = $this->trial->getTrialCountdown($trialStartDate);
  87. // Should return 0 days remaining since the trial has expired
  88. $this->assertEquals(0, $result);
  89. }
  90. /**
  91. * Test startArtistPremiumTrial method.
  92. *
  93. * @see Trial::startArtistPremiumTrialForNewStructure()
  94. */
  95. public function testStartArtistPremiumTrial(): void
  96. {
  97. DatesUtils::setFakeDatetime('2025-01-01 12:00:00');
  98. $organization = $this->createMock(Organization::class);
  99. $settings = $this->createMock(Settings::class);
  100. $settings->method('getProduct')->willReturn(SettingsProductEnum::FREEMIUM);
  101. $organization->method('getSettings')->willReturn($settings);
  102. $organization->method('getId')->willReturn(123);
  103. $request = $this->createMock(NewStructureArtistPremiumTrialRequest::class);
  104. $request->method('getRepresentativeFirstName')->willReturn('John');
  105. $request->method('getRepresentativeLastName')->willReturn('Doe');
  106. $request->method('getRepresentativeFunction')->willReturn('Manager');
  107. $request->method('getRepresentativeEmail')->willReturn('test@example.com');
  108. $request->method('getRepresentativePhone')->willReturn('+33123456789');
  109. $settings
  110. ->expects(self::once())
  111. ->method('setProductBeforeTrial')
  112. ->with(SettingsProductEnum::FREEMIUM);
  113. $settings
  114. ->expects(self::once())
  115. ->method('setTrialActive')
  116. ->with(true);
  117. $settings
  118. ->expects(self::once())
  119. ->method('setLastTrialStartDate')
  120. ->with(self::callback(function ($dateTime) {
  121. return $dateTime instanceof \DateTime && $dateTime->format('Y-m-d H:i:s') === '2025-01-01 12:00:00';
  122. }));
  123. $settings->expects(self::once())
  124. ->method('setProduct')
  125. ->with(SettingsProductEnum::ARTIST_PREMIUM);
  126. $this->entityManager->expects(self::once())
  127. ->method('persist')
  128. ->with($settings);
  129. $this->entityManager->expects(self::once())
  130. ->method('flush');
  131. $this->dolibarrApiService->expects(self::once())
  132. ->method('getSocietyId')
  133. ->with(123)
  134. ->willReturn(456);
  135. $this->dolibarrUtils->expects(self::once())
  136. ->method('getProductId')
  137. ->with(SettingsProductEnum::ARTIST_PREMIUM, true)
  138. ->willReturn(789);
  139. $this->dolibarrApiService->expects(self::once())
  140. ->method('createContract')
  141. ->with(456, 789, true, 1)
  142. ->willReturn(101112);
  143. $this->dolibarrApiService->expects(self::once())
  144. ->method('createContractLine')
  145. ->with(101112, 789, 1);
  146. $this->dolibarrUtils->expects(self::once())
  147. ->method('updateSocietyCommercialsWithApi')
  148. ->with(456);
  149. $this->dolibarrUtils->expects(self::once())
  150. ->method('getDolibarrProductName')
  151. ->with(SettingsProductEnum::ARTIST_PREMIUM, true)
  152. ->willReturn('Artist Premium (essai)');
  153. $this->dolibarrApiService->expects(self::once())
  154. ->method('updateSocietyProduct')
  155. ->with(456, 'Artist Premium (essai)');
  156. $this->dolibarrUtils->expects(self::once())
  157. ->method('addActionComm')
  158. ->with(456, "Ouverture de la période d'essai (nouvelle structure)", self::callback(function ($message) {
  159. return strpos($message, 'John') !== false
  160. && strpos($message, 'Doe') !== false
  161. && strpos($message, 'Manager') !== false
  162. && strpos($message, 'test@example.com') !== false
  163. && strpos($message, '+33123456789') !== false;
  164. }));
  165. $this->trial->startArtistPremiumTrialForNewStructure($organization, $request);
  166. }
  167. }