ClockSensitiveTrait.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Clock\Test;
  11. use PHPUnit\Framework\Attributes\After;
  12. use PHPUnit\Framework\Attributes\Before;
  13. use PHPUnit\Framework\Attributes\BeforeClass;
  14. use Symfony\Component\Clock\Clock;
  15. use Symfony\Component\Clock\ClockInterface;
  16. use Symfony\Component\Clock\MockClock;
  17. use function Symfony\Component\Clock\now;
  18. /**
  19. * Helps with mocking the time in your test cases.
  20. *
  21. * This trait provides one self::mockTime() method that freezes the time.
  22. * It restores the global clock after each test case.
  23. * self::mockTime() accepts either a string (eg '+1 days' or '2022-12-22'),
  24. * a DateTimeImmutable, or a boolean (to freeze/restore the global clock).
  25. *
  26. * @author Nicolas Grekas <p@tchwork.com>
  27. */
  28. trait ClockSensitiveTrait
  29. {
  30. public static function mockTime(string|\DateTimeImmutable|bool $when = true): ClockInterface
  31. {
  32. Clock::set(match (true) {
  33. false === $when => self::saveClockBeforeTest(false),
  34. true === $when => new MockClock(),
  35. $when instanceof \DateTimeImmutable => new MockClock($when),
  36. default => new MockClock(now($when)),
  37. });
  38. return Clock::get();
  39. }
  40. /**
  41. * @beforeClass
  42. *
  43. * @before
  44. *
  45. * @internal
  46. */
  47. #[Before]
  48. #[BeforeClass]
  49. public static function saveClockBeforeTest(bool $save = true): ClockInterface
  50. {
  51. static $originalClock;
  52. if ($save && $originalClock) {
  53. self::restoreClockAfterTest();
  54. }
  55. return $save ? $originalClock = Clock::get() : $originalClock;
  56. }
  57. /**
  58. * @after
  59. *
  60. * @internal
  61. */
  62. #[After]
  63. protected static function restoreClockAfterTest(): void
  64. {
  65. Clock::set(self::saveClockBeforeTest(false));
  66. }
  67. }