ExceptionComparatorTest.php 3.6 KB

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