No Description

ExceptionComparatorTest.php 3.8KB

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