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