Geen omschrijving

ExceptionComparatorTest.php 3.6KB

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