ScalarComparatorTest.php 4.5 KB

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