TrialTest.php 2.3 KB

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