Version 1
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / IsInstanceOf.php
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
11 /**
12  * Constraint that asserts that the object it is evaluated for is an instance
13  * of a given class.
14  *
15  * The expected class name is passed in the constructor.
16  *
17  * @since Class available since Release 3.0.0
18  */
19 class PHPUnit_Framework_Constraint_IsInstanceOf extends PHPUnit_Framework_Constraint
20 {
21     /**
22      * @var string
23      */
24     protected $className;
25
26     /**
27      * @param string $className
28      */
29     public function __construct($className)
30     {
31         parent::__construct();
32         $this->className = $className;
33     }
34
35     /**
36      * Evaluates the constraint for parameter $other. Returns true if the
37      * constraint is met, false otherwise.
38      *
39      * @param mixed $other Value or object to evaluate.
40      *
41      * @return bool
42      */
43     protected function matches($other)
44     {
45         return ($other instanceof $this->className);
46     }
47
48     /**
49      * Returns the description of the failure
50      *
51      * The beginning of failure messages is "Failed asserting that" in most
52      * cases. This method should return the second part of that sentence.
53      *
54      * @param mixed $other Evaluated value or object.
55      *
56      * @return string
57      */
58     protected function failureDescription($other)
59     {
60         return sprintf(
61             '%s is an instance of %s "%s"',
62             $this->exporter->shortenedExport($other),
63             $this->getType(),
64             $this->className
65         );
66     }
67
68     /**
69      * Returns a string representation of the constraint.
70      *
71      * @return string
72      */
73     public function toString()
74     {
75         return sprintf(
76             'is instance of %s "%s"',
77             $this->getType(),
78             $this->className
79         );
80     }
81
82     private function getType()
83     {
84         try {
85             $reflection = new ReflectionClass($this->className);
86             if ($reflection->isInterface()) {
87                 return 'interface';
88             }
89         } catch (ReflectionException $e) {
90         }
91
92         return 'class';
93     }
94 }