| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- declare(strict_types=1);
- namespace App\Tests\Unit\Service\Organization;
- use App\ApiResources\Shop\NewStructureArtistPremiumTrialRequest;
- use App\Entity\Organization\Organization;
- use App\Entity\Organization\Settings;
- use App\Enum\Organization\SettingsProductEnum;
- use App\Service\Dolibarr\DolibarrApiService;
- use App\Service\Dolibarr\DolibarrUtils;
- use App\Service\Shop\Trial;
- use App\Service\Utils\DatesUtils;
- use Doctrine\ORM\EntityManagerInterface;
- use PHPUnit\Framework\TestCase;
- /**
- * Unit tests for Trial class.
- *
- * @see Trial
- */
- class TrialTest extends TestCase
- {
- private DatesUtils $datesUtils;
- private Trial $trial;
- private EntityManagerInterface $entityManager;
- private DolibarrApiService $dolibarrApiService;
- private DolibarrUtils $dolibarrUtils;
- public function setUp(): void
- {
- $this->datesUtils = new DatesUtils();
- $this->entityManager = $this->createMock(EntityManagerInterface::class);
- $this->dolibarrApiService = $this->createMock(DolibarrApiService::class);
- $this->dolibarrUtils = $this->createMock(DolibarrUtils::class);
- $this->trial = new Trial(
- $this->datesUtils,
- $this->entityManager,
- $this->dolibarrApiService,
- $this->dolibarrUtils
- );
- }
- public function tearDown(): void
- {
- DatesUtils::clearFakeDatetime();
- }
- /**
- * @see Trial::getTrialCountdown()
- */
- public function testGetTrialCountdownWithNullStartDate(): void
- {
- $result = $this->trial->getTrialCountdown(null);
- $this->assertEquals(0, $result);
- }
- /**
- * @see Trial::getTrialCountdown()
- */
- public function testGetTrialCountdownWithRecentStartDate(): void
- {
- // Set up a trial start date
- $trialStartDate = new \DateTime('2023-01-01');
- // Set the current date to be 10 days after the trial start date
- DatesUtils::setFakeDatetime('2023-01-11');
- $result = $this->trial->getTrialCountdown($trialStartDate);
- // Should return 30 - 10 = 20 days remaining
- $this->assertEquals(20, $result);
- }
- /**
- * @see Trial::getTrialCountdown()
- */
- public function testGetTrialCountdownWithExactly30DaysAgo(): void
- {
- // Set up a trial start date
- $trialStartDate = new \DateTime('2023-01-01');
- // Set the current date to be 30 days after the trial start date
- DatesUtils::setFakeDatetime('2023-01-31');
- $result = $this->trial->getTrialCountdown($trialStartDate);
- // Should return 30 - 30 = 0 days remaining
- $this->assertEquals(0, $result);
- }
- /**
- * @see Trial::getTrialCountdown()
- */
- public function testGetTrialCountdownWithOldStartDate(): void
- {
- // Set up a trial start date
- $trialStartDate = new \DateTime('2023-01-01');
- // Set the current date to be 40 days after the trial start date
- DatesUtils::setFakeDatetime('2023-02-10');
- $result = $this->trial->getTrialCountdown($trialStartDate);
- // Should return 0 days remaining since the trial has expired
- $this->assertEquals(0, $result);
- }
- /**
- * Test startArtistPremiumTrial method.
- *
- * @see Trial::startArtistPremiumTrialForNewStructure()
- */
- public function testStartArtistPremiumTrial(): void
- {
- DatesUtils::setFakeDatetime('2025-01-01 12:00:00');
- $organization = $this->createMock(Organization::class);
- $settings = $this->createMock(Settings::class);
- $settings->method('getProduct')->willReturn(SettingsProductEnum::FREEMIUM);
- $organization->method('getSettings')->willReturn($settings);
- $organization->method('getId')->willReturn(123);
- $request = $this->createMock(NewStructureArtistPremiumTrialRequest::class);
- $request->method('getRepresentativeFirstName')->willReturn('John');
- $request->method('getRepresentativeLastName')->willReturn('Doe');
- $request->method('getRepresentativeFunction')->willReturn('Manager');
- $request->method('getRepresentativeEmail')->willReturn('test@example.com');
- $request->method('getRepresentativePhone')->willReturn('+33123456789');
- $settings
- ->expects(self::once())
- ->method('setProductBeforeTrial')
- ->with(SettingsProductEnum::FREEMIUM);
- $settings
- ->expects(self::once())
- ->method('setTrialActive')
- ->with(true);
- $settings
- ->expects(self::once())
- ->method('setLastTrialStartDate')
- ->with(self::callback(function ($dateTime) {
- return $dateTime instanceof \DateTime && $dateTime->format('Y-m-d H:i:s') === '2025-01-01 12:00:00';
- }));
- $settings->expects(self::once())
- ->method('setProduct')
- ->with(SettingsProductEnum::ARTIST_PREMIUM);
- $this->entityManager->expects(self::once())
- ->method('persist')
- ->with($settings);
- $this->entityManager->expects(self::once())
- ->method('flush');
- $this->dolibarrApiService->expects(self::once())
- ->method('getSocietyId')
- ->with(123)
- ->willReturn(456);
- $this->dolibarrUtils->expects(self::once())
- ->method('getProductId')
- ->with(SettingsProductEnum::ARTIST_PREMIUM, true)
- ->willReturn(789);
- $this->dolibarrApiService->expects(self::once())
- ->method('createContract')
- ->with(456, 789, true, 1)
- ->willReturn(101112);
- $this->dolibarrApiService->expects(self::once())
- ->method('createContractLine')
- ->with(101112, 789, 1);
- $this->dolibarrUtils->expects(self::once())
- ->method('updateSocietyCommercialsWithApi')
- ->with(456);
- $this->dolibarrUtils->expects(self::once())
- ->method('getDolibarrProductName')
- ->with(SettingsProductEnum::ARTIST_PREMIUM, true)
- ->willReturn('Artist Premium (essai)');
- $this->dolibarrApiService->expects(self::once())
- ->method('updateSocietyProduct')
- ->with(456, 'Artist Premium (essai)');
- $this->dolibarrUtils->expects(self::once())
- ->method('addActionComm')
- ->with(456, "Ouverture de la période d'essai (nouvelle structure)", self::callback(function ($message) {
- return strpos($message, 'John') !== false
- && strpos($message, 'Doe') !== false
- && strpos($message, 'Manager') !== false
- && strpos($message, 'test@example.com') !== false
- && strpos($message, '+33123456789') !== false;
- }));
- $this->trial->startArtistPremiumTrialForNewStructure($organization, $request);
- }
- }
|