No Description

ArrayComparatorTest.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * @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. }
  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. public function testAcceptsSucceeds(): void
  113. {
  114. $this->assertTrue(
  115. $this->comparator->accepts([], [])
  116. );
  117. }
  118. /**
  119. * @dataProvider acceptsFailsProvider
  120. */
  121. public function testAcceptsFails($expected, $actual): void
  122. {
  123. $this->assertFalse(
  124. $this->comparator->accepts($expected, $actual)
  125. );
  126. }
  127. /**
  128. * @dataProvider assertEqualsSucceedsProvider
  129. */
  130. public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0, $canonicalize = false): void
  131. {
  132. $exception = null;
  133. try {
  134. $this->comparator->assertEquals($expected, $actual, $delta, $canonicalize);
  135. } catch (ComparisonFailure $exception) {
  136. }
  137. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  138. }
  139. /**
  140. * @dataProvider assertEqualsFailsProvider
  141. */
  142. public function testAssertEqualsFails($expected, $actual, $delta = 0.0, $canonicalize = false): void
  143. {
  144. $this->expectException(ComparisonFailure::class);
  145. $this->expectExceptionMessage('Failed asserting that two arrays are equal');
  146. $this->comparator->assertEquals($expected, $actual, $delta, $canonicalize);
  147. }
  148. }