Без опису

ScalarComparatorTest.php 4.8KB

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