Нет описания

NumericComparatorTest.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\NumericComparator<extended>
  14. *
  15. * @uses \SebastianBergmann\Comparator\Comparator
  16. * @uses \SebastianBergmann\Comparator\Factory
  17. * @uses \SebastianBergmann\Comparator\ComparisonFailure
  18. */
  19. final class NumericComparatorTest extends TestCase
  20. {
  21. /**
  22. * @var NumericComparator
  23. */
  24. private $comparator;
  25. protected function setUp(): void
  26. {
  27. $this->comparator = new NumericComparator;
  28. }
  29. public function acceptsSucceedsProvider()
  30. {
  31. return [
  32. [5, 10],
  33. [8, '0'],
  34. ['10', 0],
  35. [0x74c3b00c, 42],
  36. [0755, 0777]
  37. ];
  38. }
  39. public function acceptsFailsProvider()
  40. {
  41. return [
  42. ['5', '10'],
  43. [8, 5.0],
  44. [5.0, 8],
  45. [10, null],
  46. [false, 12]
  47. ];
  48. }
  49. public function assertEqualsSucceedsProvider()
  50. {
  51. return [
  52. [1337, 1337],
  53. ['1337', 1337],
  54. [0x539, 1337],
  55. [02471, 1337],
  56. [1337, 1338, 1],
  57. ['1337', 1340, 5],
  58. ];
  59. }
  60. public function assertEqualsFailsProvider()
  61. {
  62. return [
  63. [1337, 1338],
  64. ['1338', 1337],
  65. [0x539, 1338],
  66. [1337, 1339, 1],
  67. ['1337', 1340, 2],
  68. ];
  69. }
  70. /**
  71. * @dataProvider acceptsSucceedsProvider
  72. */
  73. public function testAcceptsSucceeds($expected, $actual): void
  74. {
  75. $this->assertTrue(
  76. $this->comparator->accepts($expected, $actual)
  77. );
  78. }
  79. /**
  80. * @dataProvider acceptsFailsProvider
  81. */
  82. public function testAcceptsFails($expected, $actual): void
  83. {
  84. $this->assertFalse(
  85. $this->comparator->accepts($expected, $actual)
  86. );
  87. }
  88. /**
  89. * @dataProvider assertEqualsSucceedsProvider
  90. */
  91. public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0): void
  92. {
  93. $exception = null;
  94. try {
  95. $this->comparator->assertEquals($expected, $actual, $delta);
  96. } catch (ComparisonFailure $exception) {
  97. }
  98. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  99. }
  100. /**
  101. * @dataProvider assertEqualsFailsProvider
  102. */
  103. public function testAssertEqualsFails($expected, $actual, $delta = 0.0): void
  104. {
  105. $this->expectException(ComparisonFailure::class);
  106. $this->expectExceptionMessage('matches expected');
  107. $this->comparator->assertEquals($expected, $actual, $delta);
  108. }
  109. }