ArrayComparatorTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php declare(strict_types=1);
  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. * @covers \SebastianBergmann\Comparator\ArrayComparator<extended>
  14. *
  15. * @uses \SebastianBergmann\Comparator\Comparator
  16. * @uses \SebastianBergmann\Comparator\Factory
  17. * @uses \SebastianBergmann\Comparator\ComparisonFailure
  18. */
  19. final class ArrayComparatorTest extends TestCase
  20. {
  21. /**
  22. * @var ArrayComparator
  23. */
  24. private $comparator;
  25. protected function setUp(): void
  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. ['true'],
  72. [true],
  73. ],
  74. ];
  75. }
  76. public function assertEqualsFailsProvider()
  77. {
  78. return [
  79. [
  80. [],
  81. [0 => 1],
  82. ],
  83. [
  84. [0 => 1],
  85. [],
  86. ],
  87. [
  88. [0 => null],
  89. [],
  90. ],
  91. [
  92. [0 => 1, 1 => 2],
  93. [0 => 1, 1 => 3],
  94. ],
  95. [
  96. ['a', 'b' => [1, 2]],
  97. ['a', 'b' => [2, 1]],
  98. ],
  99. [
  100. [2.3],
  101. [4.2],
  102. 0.5,
  103. ],
  104. [
  105. [[2.3]],
  106. [[4.2]],
  107. 0.5,
  108. ],
  109. [
  110. [new Struct(2.3)],
  111. [new Struct(4.2)],
  112. 0.5,
  113. ],
  114. [
  115. ['false'],
  116. [false],
  117. ],
  118. ];
  119. }
  120. public function testAcceptsSucceeds(): void
  121. {
  122. $this->assertTrue(
  123. $this->comparator->accepts([], [])
  124. );
  125. }
  126. /**
  127. * @dataProvider acceptsFailsProvider
  128. */
  129. public function testAcceptsFails($expected, $actual): void
  130. {
  131. $this->assertFalse(
  132. $this->comparator->accepts($expected, $actual)
  133. );
  134. }
  135. /**
  136. * @dataProvider assertEqualsSucceedsProvider
  137. */
  138. public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0, $canonicalize = false): void
  139. {
  140. $exception = null;
  141. try {
  142. $this->comparator->assertEquals($expected, $actual, $delta, $canonicalize);
  143. } catch (ComparisonFailure $exception) {
  144. }
  145. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  146. }
  147. /**
  148. * @dataProvider assertEqualsFailsProvider
  149. */
  150. public function testAssertEqualsFails($expected, $actual, $delta = 0.0, $canonicalize = false): void
  151. {
  152. $this->expectException(ComparisonFailure::class);
  153. $this->expectExceptionMessage('Failed asserting that two arrays are equal');
  154. $this->comparator->assertEquals($expected, $actual, $delta, $canonicalize);
  155. }
  156. }