菜谱项目

ScalarComparatorTest.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\ScalarComparator
  13. *
  14. */
  15. class ScalarComparatorTest extends \PHPUnit_Framework_TestCase
  16. {
  17. private $comparator;
  18. protected function setUp()
  19. {
  20. $this->comparator = new ScalarComparator;
  21. }
  22. public function acceptsSucceedsProvider()
  23. {
  24. return array(
  25. array("string", "string"),
  26. array(new ClassWithToString, "string"),
  27. array("string", new ClassWithToString),
  28. array("string", null),
  29. array(false, "string"),
  30. array(false, true),
  31. array(null, false),
  32. array(null, null),
  33. array("10", 10),
  34. array("", false),
  35. array("1", true),
  36. array(1, true),
  37. array(0, false),
  38. array(0.1, "0.1")
  39. );
  40. }
  41. public function acceptsFailsProvider()
  42. {
  43. return array(
  44. array(array(), array()),
  45. array("string", array()),
  46. array(new ClassWithToString, new ClassWithToString),
  47. array(false, new ClassWithToString),
  48. array(tmpfile(), tmpfile())
  49. );
  50. }
  51. public function assertEqualsSucceedsProvider()
  52. {
  53. return array(
  54. array("string", "string"),
  55. array(new ClassWithToString, new ClassWithToString),
  56. array("string representation", new ClassWithToString),
  57. array(new ClassWithToString, "string representation"),
  58. array("string", "STRING", true),
  59. array("STRING", "string", true),
  60. array("String Representation", new ClassWithToString, true),
  61. array(new ClassWithToString, "String Representation", true),
  62. array("10", 10),
  63. array("", false),
  64. array("1", true),
  65. array(1, true),
  66. array(0, false),
  67. array(0.1, "0.1"),
  68. array(false, null),
  69. array(false, false),
  70. array(true, true),
  71. array(null, null)
  72. );
  73. }
  74. public function assertEqualsFailsProvider()
  75. {
  76. $stringException = 'Failed asserting that two strings are equal.';
  77. $otherException = 'matches expected';
  78. return array(
  79. array("string", "other string", $stringException),
  80. array("string", "STRING", $stringException),
  81. array("STRING", "string", $stringException),
  82. array("string", "other string", $stringException),
  83. // https://github.com/sebastianbergmann/phpunit/issues/1023
  84. array('9E6666666','9E7777777', $stringException),
  85. array(new ClassWithToString, "does not match", $otherException),
  86. array("does not match", new ClassWithToString, $otherException),
  87. array(0, 'Foobar', $otherException),
  88. array('Foobar', 0, $otherException),
  89. array("10", 25, $otherException),
  90. array("1", false, $otherException),
  91. array("", true, $otherException),
  92. array(false, true, $otherException),
  93. array(true, false, $otherException),
  94. array(null, true, $otherException),
  95. array(0, true, $otherException)
  96. );
  97. }
  98. /**
  99. * @covers ::accepts
  100. * @dataProvider acceptsSucceedsProvider
  101. */
  102. public function testAcceptsSucceeds($expected, $actual)
  103. {
  104. $this->assertTrue(
  105. $this->comparator->accepts($expected, $actual)
  106. );
  107. }
  108. /**
  109. * @covers ::accepts
  110. * @dataProvider acceptsFailsProvider
  111. */
  112. public function testAcceptsFails($expected, $actual)
  113. {
  114. $this->assertFalse(
  115. $this->comparator->accepts($expected, $actual)
  116. );
  117. }
  118. /**
  119. * @covers ::assertEquals
  120. * @dataProvider assertEqualsSucceedsProvider
  121. */
  122. public function testAssertEqualsSucceeds($expected, $actual, $ignoreCase = false)
  123. {
  124. $exception = null;
  125. try {
  126. $this->comparator->assertEquals($expected, $actual, 0.0, false, $ignoreCase);
  127. }
  128. catch (ComparisonFailure $exception) {
  129. }
  130. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  131. }
  132. /**
  133. * @covers ::assertEquals
  134. * @dataProvider assertEqualsFailsProvider
  135. */
  136. public function testAssertEqualsFails($expected, $actual, $message)
  137. {
  138. $this->setExpectedException(
  139. 'SebastianBergmann\\Comparator\\ComparisonFailure', $message
  140. );
  141. $this->comparator->assertEquals($expected, $actual);
  142. }
  143. }