Uses array_key_exists() to check if the key is found in the input array, if not found the evaluation fails. The array key is passed in the constructor.
Inheritance: extends PHPUnit_Framework_Constraint
Example #1
0
 public function testConstraintArrayHasKey2()
 {
     $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey(0);
     try {
         $constraint->fail(array(), 'custom message');
     } catch (PHPUnit_Framework_ExpectationFailedException $e) {
         $this->assertEquals("custom message\nFailed asserting that \nArray\n(\n)\n has key <integer:0>.", $e->getDescription());
         return;
     }
     $this->fail();
 }
Example #2
0
 public function testFailureArrayHasKey2()
 {
     $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey(0);
     try {
         $constraint->fail(array(), 'custom message');
     } catch (PHPUnit_Framework_ExpectationFailedException $e) {
         $this->assertEquals("custom message\nFailed asserting that an array has the key <integer:0>.\n", PHPUnit_Framework_TestFailure::exceptionToString($e));
         return;
     }
     $this->fail();
 }
Example #3
0
 public function testConstraintArrayHasKey()
 {
     $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey(0);
     $this->assertFalse($constraint->evaluate(array()));
     $this->assertEquals('has key <integer:0>', $constraint->toString());
     try {
         $constraint->fail(array(), '');
     } catch (PHPUnit_Framework_ExpectationFailedException $e) {
         $this->assertEquals("Failed asserting that \nArray\n(\n)\n has key <integer:0>.", $e->getDescription());
         return;
     }
     $this->fail();
 }
 protected function assertGuzzleRequestFormFieldValueEquals($field, $expected)
 {
     $fields = $this->assertGuzzleRequest()->getPostFields()->toArray();
     if (empty($fields)) {
         throw new \PHPUnit_Framework_ExpectationFailedException("No form data in request");
     }
     $constraint = new \PHPUnit_Framework_Constraint_ArrayHasKey($field);
     $constraint->evaluate($fields, "Form data has no field " . $field);
     $constraint = new \PHPUnit_Framework_Constraint_IsEqual($expected);
     $actual = $fields[$field];
     $constraint->evaluate($actual, sprintf("Value for form field \"%s\" of \"%s\" is not equal to \"%s\"", $field, $expected, $actual));
 }