Nessuna descrizione

DateTimeComparator.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 \Exception
  40. * @throws ComparisonFailure
  41. */
  42. public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])
  43. {
  44. /** @var \DateTimeInterface $expected */
  45. /** @var \DateTimeInterface $actual */
  46. $absDelta = \abs($delta);
  47. $delta = new \DateInterval(\sprintf('PT%dS', $absDelta));
  48. $delta->f = $absDelta - \floor($absDelta);
  49. $actualClone = (clone $actual)
  50. ->setTimezone(new \DateTimeZone('UTC'));
  51. $expectedLower = (clone $expected)
  52. ->setTimezone(new \DateTimeZone('UTC'))
  53. ->sub($delta);
  54. $expectedUpper = (clone $expected)
  55. ->setTimezone(new \DateTimeZone('UTC'))
  56. ->add($delta);
  57. if ($actualClone < $expectedLower || $actualClone > $expectedUpper) {
  58. throw new ComparisonFailure(
  59. $expected,
  60. $actual,
  61. $this->dateTimeToString($expected),
  62. $this->dateTimeToString($actual),
  63. false,
  64. 'Failed asserting that two DateTime objects are equal.'
  65. );
  66. }
  67. }
  68. /**
  69. * Returns an ISO 8601 formatted string representation of a datetime or
  70. * 'Invalid DateTimeInterface object' if the provided DateTimeInterface was not properly
  71. * initialized.
  72. */
  73. private function dateTimeToString(\DateTimeInterface $datetime): string
  74. {
  75. $string = $datetime->format('Y-m-d\TH:i:s.uO');
  76. return $string ?: 'Invalid DateTimeInterface object';
  77. }
  78. }