Geen omschrijving

ExceptionComparatorTest.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 Exception;
  12. use PHPUnit\Framework\TestCase;
  13. use RuntimeException;
  14. /**
  15. * @covers \SebastianBergmann\Comparator\ExceptionComparator<extended>
  16. *
  17. * @uses \SebastianBergmann\Comparator\Comparator
  18. * @uses \SebastianBergmann\Comparator\Factory
  19. * @uses \SebastianBergmann\Comparator\ComparisonFailure
  20. */
  21. final class ExceptionComparatorTest extends TestCase
  22. {
  23. /**
  24. * @var ExceptionComparator
  25. */
  26. private $comparator;
  27. protected function setUp(): void
  28. {
  29. $this->comparator = new ExceptionComparator;
  30. $this->comparator->setFactory(new Factory);
  31. }
  32. public function acceptsSucceedsProvider()
  33. {
  34. return [
  35. [new Exception, new Exception],
  36. [new RuntimeException, new RuntimeException],
  37. [new Exception, new RuntimeException]
  38. ];
  39. }
  40. public function acceptsFailsProvider()
  41. {
  42. return [
  43. [new Exception, null],
  44. [null, new Exception],
  45. [null, null]
  46. ];
  47. }
  48. public function assertEqualsSucceedsProvider()
  49. {
  50. $exception1 = new Exception;
  51. $exception2 = new Exception;
  52. $exception3 = new RuntimeException('Error', 100);
  53. $exception4 = new RuntimeException('Error', 100);
  54. return [
  55. [$exception1, $exception1],
  56. [$exception1, $exception2],
  57. [$exception3, $exception3],
  58. [$exception3, $exception4]
  59. ];
  60. }
  61. public function assertEqualsFailsProvider()
  62. {
  63. $typeMessage = 'not instance of expected class';
  64. $equalMessage = 'Failed asserting that two objects are equal.';
  65. $exception1 = new Exception('Error', 100);
  66. $exception2 = new Exception('Error', 101);
  67. $exception3 = new Exception('Errors', 101);
  68. $exception4 = new RuntimeException('Error', 100);
  69. $exception5 = new RuntimeException('Error', 101);
  70. return [
  71. [$exception1, $exception2, $equalMessage],
  72. [$exception1, $exception3, $equalMessage],
  73. [$exception1, $exception4, $typeMessage],
  74. [$exception2, $exception3, $equalMessage],
  75. [$exception4, $exception5, $equalMessage]
  76. ];
  77. }
  78. /**
  79. * @dataProvider acceptsSucceedsProvider
  80. */
  81. public function testAcceptsSucceeds($expected, $actual): void
  82. {
  83. $this->assertTrue(
  84. $this->comparator->accepts($expected, $actual)
  85. );
  86. }
  87. /**
  88. * @dataProvider acceptsFailsProvider
  89. */
  90. public function testAcceptsFails($expected, $actual): void
  91. {
  92. $this->assertFalse(
  93. $this->comparator->accepts($expected, $actual)
  94. );
  95. }
  96. /**
  97. * @dataProvider assertEqualsSucceedsProvider
  98. */
  99. public function testAssertEqualsSucceeds($expected, $actual): void
  100. {
  101. $exception = null;
  102. try {
  103. $this->comparator->assertEquals($expected, $actual);
  104. } catch (ComparisonFailure $exception) {
  105. }
  106. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  107. }
  108. /**
  109. * @dataProvider assertEqualsFailsProvider
  110. */
  111. public function testAssertEqualsFails($expected, $actual, $message): void
  112. {
  113. $this->expectException(ComparisonFailure::class);
  114. $this->expectExceptionMessage($message);
  115. $this->comparator->assertEquals($expected, $actual);
  116. }
  117. }