MockClock.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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;
  11. /**
  12. * A clock that always returns the same date, suitable for testing time-sensitive logic.
  13. *
  14. * Consider using ClockSensitiveTrait in your test cases instead of using this class directly.
  15. *
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. final class MockClock implements ClockInterface
  19. {
  20. private DatePoint $now;
  21. /**
  22. * @throws \DateMalformedStringException When $now is invalid
  23. * @throws \DateInvalidTimeZoneException When $timezone is invalid
  24. */
  25. public function __construct(\DateTimeImmutable|string $now = 'now', \DateTimeZone|string|null $timezone = null)
  26. {
  27. if (\is_string($timezone)) {
  28. $timezone = new \DateTimeZone($timezone);
  29. }
  30. if (\is_string($now)) {
  31. $now = new DatePoint($now, $timezone ?? new \DateTimeZone('UTC'));
  32. } elseif (!$now instanceof DatePoint) {
  33. $now = DatePoint::createFromInterface($now);
  34. }
  35. $this->now = null !== $timezone ? $now->setTimezone($timezone) : $now;
  36. }
  37. public function now(): DatePoint
  38. {
  39. return clone $this->now;
  40. }
  41. public function sleep(float|int $seconds): void
  42. {
  43. if (0 >= $seconds) {
  44. return;
  45. }
  46. $now = (float) $this->now->format('Uu') + $seconds * 1e6;
  47. $now = substr_replace(\sprintf('@%07.0F', $now), '.', -6, 0);
  48. $timezone = $this->now->getTimezone();
  49. $this->now = DatePoint::createFromInterface(new \DateTimeImmutable($now, $timezone))->setTimezone($timezone);
  50. }
  51. /**
  52. * @throws \DateMalformedStringException When $modifier is invalid
  53. */
  54. public function modify(string $modifier): void
  55. {
  56. $this->now = $this->now->modify($modifier);
  57. }
  58. /**
  59. * @throws \DateInvalidTimeZoneException When the timezone name is invalid
  60. */
  61. public function withTimeZone(\DateTimeZone|string $timezone): static
  62. {
  63. if (\is_string($timezone)) {
  64. $timezone = new \DateTimeZone($timezone);
  65. }
  66. $clone = clone $this;
  67. $clone->now = $clone->now->setTimezone($timezone);
  68. return $clone;
  69. }
  70. }