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