TrialTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Service\Organization;
  4. use App\Service\Organization\Trial;
  5. use App\Service\Utils\DatesUtils;
  6. use PHPUnit\Framework\TestCase;
  7. /**
  8. * Unit tests for Trial class
  9. * @see Trial
  10. */
  11. class TrialTest extends TestCase
  12. {
  13. private DatesUtils $datesUtils;
  14. private Trial $trial;
  15. public function setUp(): void
  16. {
  17. $this->datesUtils = new DatesUtils();
  18. $this->trial = new Trial($this->datesUtils);
  19. }
  20. public function tearDown(): void
  21. {
  22. DatesUtils::clearFakeDatetime();
  23. }
  24. /**
  25. * @see Trial::getTrialCountdown()
  26. */
  27. public function testGetTrialCountdownWithNullStartDate(): void
  28. {
  29. $result = $this->trial->getTrialCountdown(null);
  30. $this->assertEquals(0, $result);
  31. }
  32. /**
  33. * @see Trial::getTrialCountdown()
  34. */
  35. public function testGetTrialCountdownWithRecentStartDate(): void
  36. {
  37. // Set up a trial start date
  38. $trialStartDate = new \DateTime('2023-01-01');
  39. // Set the current date to be 10 days after the trial start date
  40. DatesUtils::setFakeDatetime('2023-01-11');
  41. $result = $this->trial->getTrialCountdown($trialStartDate);
  42. // Should return 30 - 10 = 20 days remaining
  43. $this->assertEquals(20, $result);
  44. }
  45. /**
  46. * @see Trial::getTrialCountdown()
  47. */
  48. public function testGetTrialCountdownWithExactly30DaysAgo(): void
  49. {
  50. // Set up a trial start date
  51. $trialStartDate = new \DateTime('2023-01-01');
  52. // Set the current date to be 30 days after the trial start date
  53. DatesUtils::setFakeDatetime('2023-01-31');
  54. $result = $this->trial->getTrialCountdown($trialStartDate);
  55. // Should return 30 - 30 = 0 days remaining
  56. $this->assertEquals(0, $result);
  57. }
  58. /**
  59. * @see Trial::getTrialCountdown()
  60. */
  61. public function testGetTrialCountdownWithOldStartDate(): void
  62. {
  63. // Set up a trial start date
  64. $trialStartDate = new \DateTime('2023-01-01');
  65. // Set the current date to be 40 days after the trial start date
  66. DatesUtils::setFakeDatetime('2023-02-10');
  67. $result = $this->trial->getTrialCountdown($trialStartDate);
  68. // Should return 0 days remaining since the trial has expired
  69. $this->assertEquals(0, $result);
  70. }
  71. }