3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Constraint that asserts that the Traversable it is applied to contains
15 * @since Class available since Release 3.0.0
17 class PHPUnit_Framework_Constraint_TraversableContains extends PHPUnit_Framework_Constraint
22 protected $checkForObjectIdentity;
27 protected $checkForNonObjectIdentity;
36 * @param bool $checkForObjectIdentity
37 * @param bool $checkForNonObjectIdentity
39 * @throws PHPUnit_Framework_Exception
41 public function __construct($value, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
43 parent::__construct();
45 if (!is_bool($checkForObjectIdentity)) {
46 throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'boolean');
49 if (!is_bool($checkForNonObjectIdentity)) {
50 throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'boolean');
53 $this->checkForObjectIdentity = $checkForObjectIdentity;
54 $this->checkForNonObjectIdentity = $checkForNonObjectIdentity;
55 $this->value = $value;
59 * Evaluates the constraint for parameter $other. Returns true if the
60 * constraint is met, false otherwise.
62 * @param mixed $other Value or object to evaluate.
66 protected function matches($other)
68 if ($other instanceof SplObjectStorage) {
69 return $other->contains($this->value);
72 if (is_object($this->value)) {
73 foreach ($other as $element) {
74 if ($this->checkForObjectIdentity && $element === $this->value) {
76 } elseif (!$this->checkForObjectIdentity && $element == $this->value) {
81 foreach ($other as $element) {
82 if ($this->checkForNonObjectIdentity && $element === $this->value) {
84 } elseif (!$this->checkForNonObjectIdentity && $element == $this->value) {
94 * Returns a string representation of the constraint.
98 public function toString()
100 if (is_string($this->value) && strpos($this->value, "\n") !== false) {
101 return 'contains "' . $this->value . '"';
103 return 'contains ' . $this->exporter->export($this->value);
108 * Returns the description of the failure
110 * The beginning of failure messages is "Failed asserting that" in most
111 * cases. This method should return the second part of that sentence.
113 * @param mixed $other Evaluated value or object.
117 protected function failureDescription($other)
121 is_array($other) ? 'an array' : 'a traversable',