for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* This file is part of expect package.
*
* (c) Noritaka Horio <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace expect\matcher;
use expect\FailedMessage;
use InvalidArgumentException;
* Verify key exists.
* <code>
* $matcher = new ToHaveKey('foo');
* $matcher->match([ 'foo' => 1 ]); //return true
* $matcher->match([ 'bar' => 1 ]); //return false
* @author Noritaka Horio <[email protected]>
* @copyright Noritaka Horio <[email protected]>
final class ToHaveKey implements ReportableMatcher
{
* @var array
private $actual;
* @var string|int
private $expected;
* @param string|int $expected
public function __construct($expected)
$this->expected = $expected;
}
* {@inheritdoc}
public function match($actual)
if (is_array($actual) === false) {
throw new InvalidArgumentException("The actual value is not a array");
$this->actual = $actual;
return array_key_exists($this->expected, $this->actual);
public function reportFailed(FailedMessage $message)
$message->appendText('Expected array to have the key ')
->appendValue($this->expected);
public function reportNegativeFailed(FailedMessage $message)
$message->appendText('Expected array not to have the key ')