You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.4 KiB

3 years ago
  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. }