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 array it is evaluated for has a specified subset.
14 * Uses array_replace_recursive() to check if a key value subset is part of the
17 * @since Class available since Release 4.4.0
19 class PHPUnit_Framework_Constraint_ArraySubset extends PHPUnit_Framework_Constraint
22 * @var array|ArrayAccess
32 * @param array|ArrayAccess $subset
33 * @param bool $strict Check for object identity
35 public function __construct($subset, $strict = false)
37 parent::__construct();
38 $this->strict = $strict;
39 $this->subset = $subset;
43 * Evaluates the constraint for parameter $other. Returns true if the
44 * constraint is met, false otherwise.
46 * @param array|ArrayAccess $other Array or ArrayAccess object to evaluate.
50 protected function matches($other)
52 //type cast $other & $this->subset as an array to allow
53 //support in standard array functions.
54 if($other instanceof ArrayAccess) {
55 $other = (array) $other;
58 if($this->subset instanceof ArrayAccess) {
59 $this->subset = (array) $this->subset;
62 $patched = array_replace_recursive($other, $this->subset);
65 return $other === $patched;
67 return $other == $patched;
72 * Returns a string representation of the constraint.
76 public function toString()
78 return 'has the subset ' . $this->exporter->export($this->subset);
82 * Returns the description of the failure
84 * The beginning of failure messages is "Failed asserting that" in most
85 * cases. This method should return the second part of that sentence.
87 * @param mixed $other Evaluated value or object.
91 protected function failureDescription($other)
93 return 'an array ' . $this->toString();