1903b1bd38a5c975207df5a554258a611331c530
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / TraversableContains.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 Traversable it is applied to contains
13  * a given value.
14  *
15  * @since Class available since Release 3.0.0
16  */
17 class PHPUnit_Framework_Constraint_TraversableContains extends PHPUnit_Framework_Constraint
18 {
19     /**
20      * @var bool
21      */
22     protected $checkForObjectIdentity;
23
24     /**
25      * @var bool
26      */
27     protected $checkForNonObjectIdentity;
28
29     /**
30      * @var mixed
31      */
32     protected $value;
33
34     /**
35      * @param mixed $value
36      * @param bool  $checkForObjectIdentity
37      * @param bool  $checkForNonObjectIdentity
38      *
39      * @throws PHPUnit_Framework_Exception
40      */
41     public function __construct($value, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
42     {
43         parent::__construct();
44
45         if (!is_bool($checkForObjectIdentity)) {
46             throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'boolean');
47         }
48
49         if (!is_bool($checkForNonObjectIdentity)) {
50             throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'boolean');
51         }
52
53         $this->checkForObjectIdentity    = $checkForObjectIdentity;
54         $this->checkForNonObjectIdentity = $checkForNonObjectIdentity;
55         $this->value                     = $value;
56     }
57
58     /**
59      * Evaluates the constraint for parameter $other. Returns true if the
60      * constraint is met, false otherwise.
61      *
62      * @param mixed $other Value or object to evaluate.
63      *
64      * @return bool
65      */
66     protected function matches($other)
67     {
68         if ($other instanceof SplObjectStorage) {
69             return $other->contains($this->value);
70         }
71
72         if (is_object($this->value)) {
73             foreach ($other as $element) {
74                 if ($this->checkForObjectIdentity && $element === $this->value) {
75                     return true;
76                 } elseif (!$this->checkForObjectIdentity && $element == $this->value) {
77                     return true;
78                 }
79             }
80         } else {
81             foreach ($other as $element) {
82                 if ($this->checkForNonObjectIdentity && $element === $this->value) {
83                     return true;
84                 } elseif (!$this->checkForNonObjectIdentity && $element == $this->value) {
85                     return true;
86                 }
87             }
88         }
89
90         return false;
91     }
92
93     /**
94      * Returns a string representation of the constraint.
95      *
96      * @return string
97      */
98     public function toString()
99     {
100         if (is_string($this->value) && strpos($this->value, "\n") !== false) {
101             return 'contains "' . $this->value . '"';
102         } else {
103             return 'contains ' . $this->exporter->export($this->value);
104         }
105     }
106
107     /**
108      * Returns the description of the failure
109      *
110      * The beginning of failure messages is "Failed asserting that" in most
111      * cases. This method should return the second part of that sentence.
112      *
113      * @param mixed $other Evaluated value or object.
114      *
115      * @return string
116      */
117     protected function failureDescription($other)
118     {
119         return sprintf(
120             '%s %s',
121             is_array($other) ? 'an array' : 'a traversable',
122             $this->toString()
123         );
124     }
125 }