TrialTest.php 6.9 KB

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