ArrayComparatorTest.php 3.6 KB

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