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); $phoneNumber = $this->createMock(PhoneNumber::class); $phoneNumber->method('__toString')->willReturn('+33123456789'); $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($phoneNumber); $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); } }