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 sprintf;
13
use function strpos;
14
use Throwable;
15
 
16
/**
17
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
18
 */
19
final class ExceptionMessage extends Constraint
20
{
21
    /**
22
     * @var string
23
     */
24
    private $expectedMessage;
25
 
26
    public function __construct(string $expected)
27
    {
28
        $this->expectedMessage = $expected;
29
    }
30
 
31
    public function toString(): string
32
    {
33
        if ($this->expectedMessage === '') {
34
            return 'exception message is empty';
35
        }
36
 
37
        return 'exception message contains ';
38
    }
39
 
40
    /**
41
     * Evaluates the constraint for parameter $other. Returns true if the
42
     * constraint is met, false otherwise.
43
     *
44
     * @param Throwable $other
45
     */
46
    protected function matches($other): bool
47
    {
48
        if ($this->expectedMessage === '') {
49
            return $other->getMessage() === '';
50
        }
51
 
52
        return strpos((string) $other->getMessage(), $this->expectedMessage) !== false;
53
    }
54
 
55
    /**
56
     * Returns the description of the failure.
57
     *
58
     * The beginning of failure messages is "Failed asserting that" in most
59
     * cases. This method should return the second part of that sentence.
60
     *
61
     * @param mixed $other evaluated value or object
62
     */
63
    protected function failureDescription($other): string
64
    {
65
        if ($this->expectedMessage === '') {
66
            return sprintf(
67
                "exception message is empty but is '%s'",
68
                $other->getMessage()
69
            );
70
        }
71
 
72
        return sprintf(
73
            "exception message '%s' contains '%s'",
74
            $other->getMessage(),
75
            $this->expectedMessage
76
        );
77
    }
78
}