PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/1.1.0/test/errors_test.php

https://bitbucket.org/bpanulla/simpletest
PHP | 229 lines | 180 code | 36 blank | 13 comment | 0 complexity | cfb17661312c034889e795dcd0187ec6 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. require_once(dirname(__FILE__) . '/../autorun.php');
  3. require_once(dirname(__FILE__) . '/../errors.php');
  4. require_once(dirname(__FILE__) . '/../expectation.php');
  5. require_once(dirname(__FILE__) . '/../test_case.php');
  6. Mock::generate('SimpleTestCase');
  7. Mock::generate('SimpleExpectation');
  8. SimpleTest::ignore('MockSimpleTestCase');
  9. class TestOfErrorQueue extends UnitTestCase {
  10. function setUp() {
  11. $context = SimpleTest::getContext();
  12. $queue = $context->get('SimpleErrorQueue');
  13. $queue->clear();
  14. }
  15. function tearDown() {
  16. $context = SimpleTest::getContext();
  17. $queue = $context->get('SimpleErrorQueue');
  18. $queue->clear();
  19. }
  20. function testExpectationMatchCancelsIncomingError() {
  21. $test = new MockSimpleTestCase();
  22. $test->expectOnce('assert', array(
  23. new IdenticalExpectation(new AnythingExpectation()),
  24. 'B',
  25. 'a message'));
  26. $test->setReturnValue('assert', true);
  27. $test->expectNever('error');
  28. $queue = new SimpleErrorQueue();
  29. $queue->setTestCase($test);
  30. $queue->expectError(new AnythingExpectation(), 'a message');
  31. $queue->add(1024, 'B', 'b.php', 100);
  32. }
  33. }
  34. class TestOfErrorTrap extends UnitTestCase {
  35. private $old;
  36. function setUp() {
  37. $this->old = error_reporting(E_ALL);
  38. set_error_handler('SimpleTestErrorHandler');
  39. }
  40. function tearDown() {
  41. restore_error_handler();
  42. error_reporting($this->old);
  43. }
  44. function testQueueStartsEmpty() {
  45. $context = SimpleTest::getContext();
  46. $queue = $context->get('SimpleErrorQueue');
  47. $this->assertFalse($queue->extract());
  48. }
  49. function testErrorsAreSwallowedByMatchingExpectation() {
  50. $this->expectError('Ouch!');
  51. trigger_error('Ouch!');
  52. }
  53. function testErrorsAreSwallowedInOrder() {
  54. $this->expectError('a');
  55. $this->expectError('b');
  56. trigger_error('a');
  57. trigger_error('b');
  58. }
  59. function testAnyErrorCanBeSwallowed() {
  60. $this->expectError();
  61. trigger_error('Ouch!');
  62. }
  63. function testErrorCanBeSwallowedByPatternMatching() {
  64. $this->expectError(new PatternExpectation('/ouch/i'));
  65. trigger_error('Ouch!');
  66. }
  67. function testErrorWithPercentsPassesWithNoSprintfError() {
  68. $this->expectError("%");
  69. trigger_error('%');
  70. }
  71. }
  72. class TestOfErrors extends UnitTestCase {
  73. private $old;
  74. function setUp() {
  75. $this->old = error_reporting(E_ALL);
  76. }
  77. function tearDown() {
  78. error_reporting($this->old);
  79. }
  80. function testDefaultWhenAllReported() {
  81. error_reporting(E_ALL);
  82. $this->expectError('Ouch!');
  83. trigger_error('Ouch!');
  84. }
  85. function testNoticeWhenReported() {
  86. error_reporting(E_ALL);
  87. $this->expectError('Ouch!');
  88. trigger_error('Ouch!', E_USER_NOTICE);
  89. }
  90. function testWarningWhenReported() {
  91. error_reporting(E_ALL);
  92. $this->expectError('Ouch!');
  93. trigger_error('Ouch!', E_USER_WARNING);
  94. }
  95. function testErrorWhenReported() {
  96. error_reporting(E_ALL);
  97. $this->expectError('Ouch!');
  98. trigger_error('Ouch!', E_USER_ERROR);
  99. }
  100. function testNoNoticeWhenNotReported() {
  101. error_reporting(0);
  102. trigger_error('Ouch!', E_USER_NOTICE);
  103. }
  104. function testNoWarningWhenNotReported() {
  105. error_reporting(0);
  106. trigger_error('Ouch!', E_USER_WARNING);
  107. }
  108. function testNoticeSuppressedWhenReported() {
  109. error_reporting(E_ALL);
  110. @trigger_error('Ouch!', E_USER_NOTICE);
  111. }
  112. function testWarningSuppressedWhenReported() {
  113. error_reporting(E_ALL);
  114. @trigger_error('Ouch!', E_USER_WARNING);
  115. }
  116. function testErrorWithPercentsReportedWithNoSprintfError() {
  117. $this->expectError('%');
  118. trigger_error('%');
  119. }
  120. }
  121. class TestOfPHP52RecoverableErrors extends UnitTestCase {
  122. function skip() {
  123. $this->skipIf(
  124. version_compare(phpversion(), '5.2', '<'),
  125. 'E_RECOVERABLE_ERROR not tested for PHP below 5.2');
  126. }
  127. function testError() {
  128. eval('
  129. class RecoverableErrorTestingStub {
  130. function ouch(RecoverableErrorTestingStub $obj) {
  131. }
  132. }
  133. ');
  134. $stub = new RecoverableErrorTestingStub();
  135. $this->expectError(new PatternExpectation('/must be an instance of RecoverableErrorTestingStub/i'));
  136. $stub->ouch(new stdClass());
  137. }
  138. }
  139. class TestOfErrorsExcludingPHP52AndAbove extends UnitTestCase {
  140. function skip() {
  141. $this->skipIf(
  142. version_compare(phpversion(), '5.2', '>='),
  143. 'E_USER_ERROR not tested for PHP 5.2 and above');
  144. }
  145. function testNoErrorWhenNotReported() {
  146. error_reporting(0);
  147. trigger_error('Ouch!', E_USER_ERROR);
  148. }
  149. function testErrorSuppressedWhenReported() {
  150. error_reporting(E_ALL);
  151. @trigger_error('Ouch!', E_USER_ERROR);
  152. }
  153. }
  154. SimpleTest::ignore('TestOfNotEnoughErrors');
  155. /**
  156. * This test is ignored as it is used by {@link TestRunnerForLeftOverAndNotEnoughErrors}
  157. * to verify that it fails as expected.
  158. *
  159. * @ignore
  160. */
  161. class TestOfNotEnoughErrors extends UnitTestCase {
  162. function testExpectTwoErrorsThrowOne() {
  163. $this->expectError('Error 1');
  164. trigger_error('Error 1');
  165. $this->expectError('Error 2');
  166. }
  167. }
  168. SimpleTest::ignore('TestOfLeftOverErrors');
  169. /**
  170. * This test is ignored as it is used by {@link TestRunnerForLeftOverAndNotEnoughErrors}
  171. * to verify that it fails as expected.
  172. *
  173. * @ignore
  174. */
  175. class TestOfLeftOverErrors extends UnitTestCase {
  176. function testExpectOneErrorGetTwo() {
  177. $this->expectError('Error 1');
  178. trigger_error('Error 1');
  179. trigger_error('Error 2');
  180. }
  181. }
  182. class TestRunnerForLeftOverAndNotEnoughErrors extends UnitTestCase {
  183. function testRunLeftOverErrorsTestCase() {
  184. $test = new TestOfLeftOverErrors();
  185. $this->assertFalse($test->run(new SimpleReporter()));
  186. }
  187. function testRunNotEnoughErrors() {
  188. $test = new TestOfNotEnoughErrors();
  189. $this->assertFalse($test->run(new SimpleReporter()));
  190. }
  191. }
  192. // TODO: Add stacked error handler test
  193. ?>