IsArrayTest.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Hamcrest\Arrays;
  3. use Hamcrest\AbstractMatcherTest;
  4. class IsArrayTest extends AbstractMatcherTest
  5. {
  6. protected function createMatcher()
  7. {
  8. return IsArray::anArray(array(equalTo('irrelevant')));
  9. }
  10. public function testMatchesAnArrayThatMatchesAllTheElementMatchers()
  11. {
  12. $this->assertMatches(
  13. anArray(array(equalTo('a'), equalTo('b'), equalTo('c'))),
  14. array('a', 'b', 'c'),
  15. 'should match array with matching elements'
  16. );
  17. }
  18. public function testDoesNotMatchAnArrayWhenElementsDoNotMatch()
  19. {
  20. $this->assertDoesNotMatch(
  21. anArray(array(equalTo('a'), equalTo('b'))),
  22. array('b', 'c'),
  23. 'should not match array with different elements'
  24. );
  25. }
  26. public function testDoesNotMatchAnArrayOfDifferentSize()
  27. {
  28. $this->assertDoesNotMatch(
  29. anArray(array(equalTo('a'), equalTo('b'))),
  30. array('a', 'b', 'c'),
  31. 'should not match larger array'
  32. );
  33. $this->assertDoesNotMatch(
  34. anArray(array(equalTo('a'), equalTo('b'))),
  35. array('a'),
  36. 'should not match smaller array'
  37. );
  38. }
  39. public function testDoesNotMatchNull()
  40. {
  41. $this->assertDoesNotMatch(
  42. anArray(array(equalTo('a'))),
  43. null,
  44. 'should not match null'
  45. );
  46. }
  47. public function testHasAReadableDescription()
  48. {
  49. $this->assertDescription(
  50. '["a", "b"]',
  51. anArray(array(equalTo('a'), equalTo('b')))
  52. );
  53. }
  54. public function testHasAReadableMismatchDescriptionWhenKeysDontMatch()
  55. {
  56. $this->assertMismatchDescription(
  57. 'array keys were [<1>, <2>]',
  58. anArray(array(equalTo('a'), equalTo('b'))),
  59. array(1 => 'a', 2 => 'b')
  60. );
  61. }
  62. public function testSupportsMatchesAssociativeArrays()
  63. {
  64. $this->assertMatches(
  65. anArray(array('x'=>equalTo('a'), 'y'=>equalTo('b'), 'z'=>equalTo('c'))),
  66. array('x'=>'a', 'y'=>'b', 'z'=>'c'),
  67. 'should match associative array with matching elements'
  68. );
  69. }
  70. public function testDoesNotMatchAnAssociativeArrayWhenKeysDoNotMatch()
  71. {
  72. $this->assertDoesNotMatch(
  73. anArray(array('x'=>equalTo('a'), 'y'=>equalTo('b'))),
  74. array('x'=>'b', 'z'=>'c'),
  75. 'should not match array with different keys'
  76. );
  77. }
  78. }