NumericComparatorTest.php 2.8 KB

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