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 IsEqualIgnoringCase extends Constraint
24
{
25
    /**
26
     * @var mixed
27
     */
28
    private $value;
29
 
30
    public function __construct($value)
31
    {
32
        $this->value = $value;
33
    }
34
 
35
    /**
36
     * Evaluates the constraint for parameter $other.
37
     *
38
     * If $returnResult is set to false (the default), an exception is thrown
39
     * in case of a failure. null is returned otherwise.
40
     *
41
     * If $returnResult is true, the result of the evaluation is returned as
42
     * a boolean value instead: true in case of success, false in case of a
43
     * failure.
44
     *
45
     * @throws ExpectationFailedException
46
     */
47
    public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
48
    {
49
        // If $this->value and $other are identical, they are also equal.
50
        // This is the most common path and will allow us to skip
51
        // initialization of all the comparators.
52
        if ($this->value === $other) {
53
            return true;
54
        }
55
 
56
        $comparatorFactory = ComparatorFactory::getInstance();
57
 
58
        try {
59
            $comparator = $comparatorFactory->getComparatorFor(
60
                $this->value,
61
                $other
62
            );
63
 
64
            $comparator->assertEquals(
65
                $this->value,
66
                $other,
67
                0.0,
68
                false,
69
                true
70
            );
71
        } catch (ComparisonFailure $f) {
72
            if ($returnResult) {
73
                return false;
74
            }
75
 
76
            throw new ExpectationFailedException(
77
                trim($description . "\n" . $f->getMessage()),
78
                $f
79
            );
80
        }
81
 
82
        return true;
83
    }
84
 
85
    /**
86
     * Returns a string representation of the constraint.
87
     *
88
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
89
     */
90
    public function toString(): string
91
    {
92
        if (is_string($this->value)) {
93
            if (strpos($this->value, "\n") !== false) {
94
                return 'is equal to <text>';
95
            }
96
 
97
            return sprintf(
98
                "is equal to '%s'",
99
                $this->value
100
            );
101
        }
102
 
103
        return sprintf(
104
            'is equal to %s',
105
            $this->exporter()->export($this->value)
106
        );
107
    }
108
}