ArrayComparatorTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /*
  3. * This file is part of the Comparator package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Comparator;
  11. /**
  12. * @coversDefaultClass SebastianBergmann\Comparator\ArrayComparator
  13. *
  14. */
  15. class ArrayComparatorTest extends \PHPUnit_Framework_TestCase
  16. {
  17. private $comparator;
  18. protected function setUp()
  19. {
  20. $this->comparator = new ArrayComparator;
  21. $this->comparator->setFactory(new Factory);
  22. }
  23. public function acceptsFailsProvider()
  24. {
  25. return array(
  26. array(array(), null),
  27. array(null, array()),
  28. array(null, null)
  29. );
  30. }
  31. public function assertEqualsSucceedsProvider()
  32. {
  33. return array(
  34. array(
  35. array('a' => 1, 'b' => 2),
  36. array('b' => 2, 'a' => 1)
  37. ),
  38. array(
  39. array(1),
  40. array('1')
  41. ),
  42. array(
  43. array(3, 2, 1),
  44. array(2, 3, 1),
  45. 0,
  46. true
  47. ),
  48. array(
  49. array(2.3),
  50. array(2.5),
  51. 0.5
  52. ),
  53. array(
  54. array(array(2.3)),
  55. array(array(2.5)),
  56. 0.5
  57. ),
  58. array(
  59. array(new Struct(2.3)),
  60. array(new Struct(2.5)),
  61. 0.5
  62. ),
  63. );
  64. }
  65. public function assertEqualsFailsProvider()
  66. {
  67. return array(
  68. array(
  69. array(),
  70. array(0 => 1)
  71. ),
  72. array(
  73. array(0 => 1),
  74. array()
  75. ),
  76. array(
  77. array(0 => null),
  78. array()
  79. ),
  80. array(
  81. array(0 => 1, 1 => 2),
  82. array(0 => 1, 1 => 3)
  83. ),
  84. array(
  85. array('a', 'b' => array(1, 2)),
  86. array('a', 'b' => array(2, 1))
  87. ),
  88. array(
  89. array(2.3),
  90. array(4.2),
  91. 0.5
  92. ),
  93. array(
  94. array(array(2.3)),
  95. array(array(4.2)),
  96. 0.5
  97. ),
  98. array(
  99. array(new Struct(2.3)),
  100. array(new Struct(4.2)),
  101. 0.5
  102. )
  103. );
  104. }
  105. /**
  106. * @covers ::accepts
  107. */
  108. public function testAcceptsSucceeds()
  109. {
  110. $this->assertTrue(
  111. $this->comparator->accepts(array(), array())
  112. );
  113. }
  114. /**
  115. * @covers ::accepts
  116. * @dataProvider acceptsFailsProvider
  117. */
  118. public function testAcceptsFails($expected, $actual)
  119. {
  120. $this->assertFalse(
  121. $this->comparator->accepts($expected, $actual)
  122. );
  123. }
  124. /**
  125. * @covers ::assertEquals
  126. * @dataProvider assertEqualsSucceedsProvider
  127. */
  128. public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0, $canonicalize = false)
  129. {
  130. $exception = null;
  131. try {
  132. $this->comparator->assertEquals($expected, $actual, $delta, $canonicalize);
  133. }
  134. catch (ComparisonFailure $exception) {
  135. }
  136. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  137. }
  138. /**
  139. * @covers ::assertEquals
  140. * @dataProvider assertEqualsFailsProvider
  141. */
  142. public function testAssertEqualsFails($expected, $actual,$delta = 0.0, $canonicalize = false)
  143. {
  144. $this->setExpectedException(
  145. 'SebastianBergmann\\Comparator\\ComparisonFailure',
  146. 'Failed asserting that two arrays are equal'
  147. );
  148. $this->comparator->assertEquals($expected, $actual, $delta, $canonicalize);
  149. }
  150. }