Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen 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 PHPUnit.
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 PHPUnit\Framework\Constraint;
11
 
12
use function sprintf;
13
use function trim;
14
use PHPUnit\Framework\ExpectationFailedException;
15
use SebastianBergmann\Comparator\ComparisonFailure;
16
use SebastianBergmann\Comparator\Factory as ComparatorFactory;
17
 
18
/**
19
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
20
 */
21
final class IsEqualWithDelta extends Constraint
22
{
23
    /**
24
     * @var mixed
25
     */
26
    private $value;
27
 
28
    /**
29
     * @var float
30
     */
31
    private $delta;
32
 
33
    public function __construct($value, float $delta)
34
    {
35
        $this->value = $value;
36
        $this->delta = $delta;
37
    }
38
 
39
    /**
40
     * Evaluates the constraint for parameter $other.
41
     *
42
     * If $returnResult is set to false (the default), an exception is thrown
43
     * in case of a failure. null is returned otherwise.
44
     *
45
     * If $returnResult is true, the result of the evaluation is returned as
46
     * a boolean value instead: true in case of success, false in case of a
47
     * failure.
48
     *
49
     * @throws ExpectationFailedException
50
     */
51
    public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
52
    {
53
        // If $this->value and $other are identical, they are also equal.
54
        // This is the most common path and will allow us to skip
55
        // initialization of all the comparators.
56
        if ($this->value === $other) {
57
            return true;
58
        }
59
 
60
        $comparatorFactory = ComparatorFactory::getInstance();
61
 
62
        try {
63
            $comparator = $comparatorFactory->getComparatorFor(
64
                $this->value,
65
                $other
66
            );
67
 
68
            $comparator->assertEquals(
69
                $this->value,
70
                $other,
71
                $this->delta
72
            );
73
        } catch (ComparisonFailure $f) {
74
            if ($returnResult) {
75
                return false;
76
            }
77
 
78
            throw new ExpectationFailedException(
79
                trim($description . "\n" . $f->getMessage()),
80
                $f
81
            );
82
        }
83
 
84
        return true;
85
    }
86
 
87
    /**
88
     * Returns a string representation of the constraint.
89
     *
90
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
91
     */
92
    public function toString(): string
93
    {
94
        return sprintf(
95
            'is equal to %s with delta <%F>>',
96
            $this->exporter()->export($this->value),
97
            $this->delta
98
        );
99
    }
100
}