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