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 given key.
14 * Uses array_key_exists() to check if the key is found in the input array, if
15 * not found the evaluation fails.
17 * The array key is passed in the constructor.
19 * @since Class available since Release 3.0.0
21 class PHPUnit_Framework_Constraint_ArrayHasKey extends PHPUnit_Framework_Constraint
29 * @param int|string $key
31 public function __construct($key)
33 parent::__construct();
38 * Evaluates the constraint for parameter $other. Returns true if the
39 * constraint is met, false otherwise.
41 * @param mixed $other Value or object to evaluate.
45 protected function matches($other)
47 if (is_array($other)) {
48 return array_key_exists($this->key, $other);
51 if ($other instanceof ArrayAccess) {
52 return $other->offsetExists($this->key);
59 * Returns a string representation of the constraint.
63 public function toString()
65 return 'has the key ' . $this->exporter->export($this->key);
69 * Returns the description of the failure
71 * The beginning of failure messages is "Failed asserting that" in most
72 * cases. This method should return the second part of that sentence.
74 * @param mixed $other Evaluated value or object.
78 protected function failureDescription($other)
80 return 'an array ' . $this->toString();