No Description

ScalarComparatorTest.php 4.9KB

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