No Description

ArrayComparatorTest.php 3.9KB

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