ScalarComparatorTest.php 5.0 KB

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