Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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