IsArrayTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. }