DateTimeComparator.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * This file is part of sebastian/comparator.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  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 SebastianBergmann\Comparator;
  11. /**
  12. * Compares DateTimeInterface instances for equality.
  13. */
  14. class DateTimeComparator extends ObjectComparator
  15. {
  16. /**
  17. * Returns whether the comparator can compare two values.
  18. *
  19. * @param mixed $expected The first value to compare
  20. * @param mixed $actual The second value to compare
  21. *
  22. * @return bool
  23. */
  24. public function accepts($expected, $actual)
  25. {
  26. return ($expected instanceof \DateTime || $expected instanceof \DateTimeInterface) &&
  27. ($actual instanceof \DateTime || $actual instanceof \DateTimeInterface);
  28. }
  29. /**
  30. * Asserts that two values are equal.
  31. *
  32. * @param mixed $expected First value to compare
  33. * @param mixed $actual Second value to compare
  34. * @param float $delta Allowed numerical distance between two values to consider them equal
  35. * @param bool $canonicalize Arrays are sorted before comparison when set to true
  36. * @param bool $ignoreCase Case is ignored when set to true
  37. * @param array $processed List of already processed elements (used to prevent infinite recursion)
  38. *
  39. * @throws ComparisonFailure
  40. */
  41. public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])
  42. {
  43. /** @var \DateTimeInterface $expected */
  44. /** @var \DateTimeInterface $actual */
  45. $delta = new \DateInterval(\sprintf('PT%dS', \abs($delta)));
  46. $actualClone = (clone $actual)
  47. ->setTimezone(new \DateTimeZone('UTC'));
  48. $expectedLower = (clone $expected)
  49. ->setTimezone(new \DateTimeZone('UTC'))
  50. ->sub($delta);
  51. $expectedUpper = (clone $expected)
  52. ->setTimezone(new \DateTimeZone('UTC'))
  53. ->add($delta);
  54. if ($actualClone < $expectedLower || $actualClone > $expectedUpper) {
  55. throw new ComparisonFailure(
  56. $expected,
  57. $actual,
  58. $this->dateTimeToString($expected),
  59. $this->dateTimeToString($actual),
  60. false,
  61. 'Failed asserting that two DateTime objects are equal.'
  62. );
  63. }
  64. }
  65. /**
  66. * Returns an ISO 8601 formatted string representation of a datetime or
  67. * 'Invalid DateTimeInterface object' if the provided DateTimeInterface was not properly
  68. * initialized.
  69. */
  70. private function dateTimeToString(\DateTimeInterface $datetime): string
  71. {
  72. $string = $datetime->format('Y-m-d\TH:i:s.uO');
  73. return $string ?: 'Invalid DateTimeInterface object';
  74. }
  75. }