MockClock.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 (\PHP_VERSION_ID >= 80300 && \is_string($timezone)) {
  28. $timezone = new \DateTimeZone($timezone);
  29. } elseif (\is_string($timezone)) {
  30. try {
  31. $timezone = new \DateTimeZone($timezone);
  32. } catch (\Exception $e) {
  33. throw new \DateInvalidTimeZoneException($e->getMessage(), $e->getCode(), $e);
  34. }
  35. }
  36. if (\is_string($now)) {
  37. $now = new DatePoint($now, $timezone ?? new \DateTimeZone('UTC'));
  38. } elseif (!$now instanceof DatePoint) {
  39. $now = DatePoint::createFromInterface($now);
  40. }
  41. $this->now = null !== $timezone ? $now->setTimezone($timezone) : $now;
  42. }
  43. public function now(): DatePoint
  44. {
  45. return clone $this->now;
  46. }
  47. public function sleep(float|int $seconds): void
  48. {
  49. if (0 >= $seconds) {
  50. return;
  51. }
  52. $now = (float) $this->now->format('Uu') + $seconds * 1e6;
  53. $now = substr_replace(\sprintf('@%07.0F', $now), '.', -6, 0);
  54. $timezone = $this->now->getTimezone();
  55. $this->now = DatePoint::createFromInterface(new \DateTimeImmutable($now, $timezone))->setTimezone($timezone);
  56. }
  57. /**
  58. * @throws \DateMalformedStringException When $modifier is invalid
  59. */
  60. public function modify(string $modifier): void
  61. {
  62. if (\PHP_VERSION_ID < 80300) {
  63. $this->now = @$this->now->modify($modifier) ?: throw new \DateMalformedStringException(error_get_last()['message'] ?? \sprintf('Invalid modifier: "%s". Could not modify MockClock.', $modifier));
  64. return;
  65. }
  66. $this->now = $this->now->modify($modifier);
  67. }
  68. /**
  69. * @throws \DateInvalidTimeZoneException When the timezone name is invalid
  70. */
  71. public function withTimeZone(\DateTimeZone|string $timezone): static
  72. {
  73. if (\PHP_VERSION_ID >= 80300 && \is_string($timezone)) {
  74. $timezone = new \DateTimeZone($timezone);
  75. } elseif (\is_string($timezone)) {
  76. try {
  77. $timezone = new \DateTimeZone($timezone);
  78. } catch (\Exception $e) {
  79. throw new \DateInvalidTimeZoneException($e->getMessage(), $e->getCode(), $e);
  80. }
  81. }
  82. $clone = clone $this;
  83. $clone->now = $clone->now->setTimezone($timezone);
  84. return $clone;
  85. }
  86. }