exceptions_test.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. // $Id: exceptions_test.php 1618 2007-12-29 22:52:30Z lastcraft $
  3. require_once(dirname(__FILE__) . '/../autorun.php');
  4. require_once(dirname(__FILE__) . '/../exceptions.php');
  5. require_once(dirname(__FILE__) . '/../expectation.php');
  6. require_once(dirname(__FILE__) . '/../test_case.php');
  7. Mock::generate('SimpleTestCase');
  8. Mock::generate('SimpleExpectation');
  9. class MyTestException extends Exception {}
  10. class HigherTestException extends MyTestException {}
  11. class OtherTestException extends Exception {}
  12. class TestOfExceptionExpectation extends UnitTestCase {
  13. function testExceptionClassAsStringWillMatchExceptionsRootedOnThatClass() {
  14. $expectation = new ExceptionExpectation('MyTestException');
  15. $this->assertTrue($expectation->test(new MyTestException()));
  16. $this->assertTrue($expectation->test(new HigherTestException()));
  17. $this->assertFalse($expectation->test(new OtherTestException()));
  18. }
  19. function testMatchesClassAndMessageWhenExceptionExpected() {
  20. $expectation = new ExceptionExpectation(new MyTestException('Hello'));
  21. $this->assertTrue($expectation->test(new MyTestException('Hello')));
  22. $this->assertFalse($expectation->test(new HigherTestException('Hello')));
  23. $this->assertFalse($expectation->test(new OtherTestException('Hello')));
  24. $this->assertFalse($expectation->test(new MyTestException('Goodbye')));
  25. $this->assertFalse($expectation->test(new MyTestException()));
  26. }
  27. function testMessagelessExceptionMatchesOnlyOnClass() {
  28. $expectation = new ExceptionExpectation(new MyTestException());
  29. $this->assertTrue($expectation->test(new MyTestException()));
  30. $this->assertFalse($expectation->test(new HigherTestException()));
  31. }
  32. }
  33. class TestOfExceptionTrap extends UnitTestCase {
  34. function testNoExceptionsInQueueMeansNoTestMessages() {
  35. $test = new MockSimpleTestCase();
  36. $test->expectNever('assert');
  37. $queue = new SimpleExceptionTrap();
  38. $this->assertFalse($queue->isExpected($test, new Exception()));
  39. }
  40. function testMatchingExceptionGivesTrue() {
  41. $expectation = new MockSimpleExpectation();
  42. $expectation->setReturnValue('test', true);
  43. $test = new MockSimpleTestCase();
  44. $test->setReturnValue('assert', true);
  45. $queue = new SimpleExceptionTrap();
  46. $queue->expectException($expectation, 'message');
  47. $this->assertTrue($queue->isExpected($test, new Exception()));
  48. }
  49. function testMatchingExceptionTriggersAssertion() {
  50. $test = new MockSimpleTestCase();
  51. $test->expectOnce('assert', array(
  52. '*',
  53. new ExceptionExpectation(new Exception()),
  54. 'message'));
  55. $queue = new SimpleExceptionTrap();
  56. $queue->expectException(new ExceptionExpectation(new Exception()), 'message');
  57. $queue->isExpected($test, new Exception());
  58. }
  59. }
  60. class TestOfCatchingExceptions extends UnitTestCase {
  61. function testCanCatchAnyExpectedException() {
  62. $this->expectException();
  63. throw new Exception();
  64. }
  65. function testCanMatchExceptionByClass() {
  66. $this->expectException('MyTestException');
  67. throw new HigherTestException();
  68. }
  69. function testCanMatchExceptionExactly() {
  70. $this->expectException(new Exception('Ouch'));
  71. throw new Exception('Ouch');
  72. }
  73. function testLastListedExceptionIsTheOneThatCounts() {
  74. $this->expectException('OtherTestException');
  75. $this->expectException('MyTestException');
  76. throw new HigherTestException();
  77. }
  78. }
  79. class TestOfCallingTearDownAfterExceptions extends UnitTestCase {
  80. private $debri = 0;
  81. function tearDown() {
  82. $this->debri--;
  83. }
  84. function testLeaveSomeDebri() {
  85. $this->debri++;
  86. $this->expectException();
  87. throw new Exception(__FUNCTION__);
  88. }
  89. function testDebriWasRemovedOnce() {
  90. $this->assertEqual($this->debri, 0);
  91. }
  92. }
  93. class TestOfExceptionThrownInSetUpDoesNotRunTestBody extends UnitTestCase {
  94. function setUp() {
  95. $this->expectException();
  96. throw new Exception();
  97. }
  98. function testShouldNotBeRun() {
  99. $this->fail('This test body should not be run');
  100. }
  101. function testShouldNotBeRunEither() {
  102. $this->fail('This test body should not be run either');
  103. }
  104. }
  105. class TestOfExpectExceptionWithSetUp extends UnitTestCase {
  106. function setUp() {
  107. $this->expectException();
  108. }
  109. function testThisExceptionShouldBeCaught() {
  110. throw new Exception();
  111. }
  112. function testJustThrowingMyTestException() {
  113. throw new MyTestException();
  114. }
  115. }
  116. class TestOfThrowingExceptionsInTearDown extends UnitTestCase {
  117. function tearDown() {
  118. throw new Exception();
  119. }
  120. function testDoesntFatal() {
  121. $this->expectException();
  122. }
  123. }
  124. ?>