IsArrayContainingKeyTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Hamcrest\Arrays;
  3. use Hamcrest\AbstractMatcherTest;
  4. class IsArrayContainingKeyTest extends AbstractMatcherTest
  5. {
  6. protected function createMatcher()
  7. {
  8. return IsArrayContainingKey::hasKeyInArray('irrelevant');
  9. }
  10. public function testMatchesSingleElementArrayContainingKey()
  11. {
  12. $array = array('a'=>1);
  13. $this->assertMatches(hasKey('a'), $array, 'Matches single key');
  14. }
  15. public function testMatchesArrayContainingKey()
  16. {
  17. $array = array('a'=>1, 'b'=>2, 'c'=>3);
  18. $this->assertMatches(hasKey('a'), $array, 'Matches a');
  19. $this->assertMatches(hasKey('c'), $array, 'Matches c');
  20. }
  21. public function testMatchesArrayContainingKeyWithIntegerKeys()
  22. {
  23. $array = array(1=>'A', 2=>'B');
  24. assertThat($array, hasKey(1));
  25. }
  26. public function testMatchesArrayContainingKeyWithNumberKeys()
  27. {
  28. $array = array(1=>'A', 2=>'B');
  29. assertThat($array, hasKey(1));
  30. // very ugly version!
  31. assertThat($array, IsArrayContainingKey::hasKeyInArray(2));
  32. }
  33. public function testHasReadableDescription()
  34. {
  35. $this->assertDescription('array with key "a"', hasKey('a'));
  36. }
  37. public function testDoesNotMatchEmptyArray()
  38. {
  39. $this->assertMismatchDescription('array was []', hasKey('Foo'), array());
  40. }
  41. public function testDoesNotMatchArrayMissingKey()
  42. {
  43. $array = array('a'=>1, 'b'=>2, 'c'=>3);
  44. $this->assertMismatchDescription('array was ["a" => <1>, "b" => <2>, "c" => <3>]', hasKey('d'), $array);
  45. }
  46. }