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

/sample/SimpleTest/errors_test.php

https://github.com/tmpvar/Blerby-Test-Runner
PHP | 300 lines | 245 code | 42 blank | 13 comment | 0 complexity | 80494edea77b221dd6244d7c6c991882 MD5 | raw file
  1. <?php
  2. require_once('simpletest/autorun.php');
  3. require_once('simpletest/errors.php');
  4. require_once('simpletest/expectation.php');
  5. require_once('simpletest/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 testOrder() {
  21. $context = &SimpleTest::getContext();
  22. $queue = &$context->get('SimpleErrorQueue');
  23. $queue->add(1024, 'Ouch', 'here.php', 100);
  24. $queue->add(512, 'Yuk', 'there.php', 101);
  25. $this->assertEqual(
  26. $queue->extract(),
  27. array(1024, 'Ouch', 'here.php', 100));
  28. $this->assertEqual(
  29. $queue->extract(),
  30. array(512, 'Yuk', 'there.php', 101));
  31. $this->assertFalse($queue->extract());
  32. }
  33. function testAssertNoErrorsGivesTrueWhenNoErrors() {
  34. $test = &new MockSimpleTestCase();
  35. $test->expectOnce('assert', array(
  36. new IdenticalExpectation(new TrueExpectation()),
  37. true,
  38. 'Should be no errors'));
  39. $test->setReturnValue('assert', true);
  40. $queue = &new SimpleErrorQueue();
  41. $queue->setTestCase($test);
  42. $this->assertTrue($queue->assertNoErrors('%s'));
  43. }
  44. function testAssertNoErrorsIssuesFailWhenErrors() {
  45. $test = &new MockSimpleTestCase();
  46. $test->expectOnce('assert', array(
  47. new IdenticalExpectation(new TrueExpectation()),
  48. false,
  49. 'Should be no errors'));
  50. $test->setReturnValue('assert', false);
  51. $queue = &new SimpleErrorQueue();
  52. $queue->setTestCase($test);
  53. $queue->add(1024, 'Ouch', 'here.php', 100);
  54. $this->assertFalse($queue->assertNoErrors('%s'));
  55. }
  56. function testAssertErrorFailsWhenNoError() {
  57. $test = &new MockSimpleTestCase();
  58. $test->expectOnce('fail', array('Expected error not found'));
  59. $test->setReturnValue('assert', false);
  60. $queue = &new SimpleErrorQueue();
  61. $queue->setTestCase($test);
  62. $this->assertFalse($queue->assertError(false, '%s'));
  63. }
  64. function testAssertErrorFailsWhenErrorDoesntMatchTheExpectation() {
  65. $test = &new MockSimpleTestCase();
  66. $test->expectOnce('assert', array(
  67. new IdenticalExpectation(new FailedExpectation()),
  68. 'B',
  69. 'Expected PHP error [B] severity [E_USER_NOTICE] in [b.php] line [100]'));
  70. $test->setReturnValue('assert', false);
  71. $queue = &new SimpleErrorQueue();
  72. $queue->setTestCase($test);
  73. $queue->add(1024, 'B', 'b.php', 100);
  74. $this->assertFalse($queue->assertError(new FailedExpectation(), '%s'));
  75. }
  76. function testExpectationMatchCancelsIncomingError() {
  77. $test = &new MockSimpleTestCase();
  78. $test->expectOnce('assert', array(
  79. new IdenticalExpectation(new AnythingExpectation()),
  80. 'B',
  81. 'a message'));
  82. $test->setReturnValue('assert', true);
  83. $test->expectNever('error');
  84. $queue = &new SimpleErrorQueue();
  85. $queue->setTestCase($test);
  86. $queue->expectError(new AnythingExpectation(), 'a message');
  87. $queue->add(1024, 'B', 'b.php', 100);
  88. }
  89. }
  90. class TestOfErrorTrap extends UnitTestCase {
  91. var $_old;
  92. function setUp() {
  93. $this->_old = error_reporting(E_ALL);
  94. set_error_handler('SimpleTestErrorHandler');
  95. }
  96. function tearDown() {
  97. restore_error_handler();
  98. error_reporting($this->_old);
  99. }
  100. function testQueueStartsEmpty() {
  101. $context = &SimpleTest::getContext();
  102. $queue = &$context->get('SimpleErrorQueue');
  103. $this->assertFalse($queue->extract());
  104. }
  105. function testTrappedErrorPlacedInQueue() {
  106. trigger_error('Ouch!');
  107. $context = &SimpleTest::getContext();
  108. $queue = &$context->get('SimpleErrorQueue');
  109. list($severity, $message, $file, $line) = $queue->extract();
  110. $this->assertEqual($message, 'Ouch!');
  111. $this->assertEqual($file, __FILE__);
  112. $this->assertFalse($queue->extract());
  113. }
  114. function testErrorsAreSwallowedByMatchingExpectation() {
  115. $this->expectError('Ouch!');
  116. trigger_error('Ouch!');
  117. }
  118. function testErrorsAreSwallowedInOrder() {
  119. $this->expectError('a');
  120. $this->expectError('b');
  121. trigger_error('a');
  122. trigger_error('b');
  123. }
  124. function testAnyErrorCanBeSwallowed() {
  125. $this->expectError();
  126. trigger_error('Ouch!');
  127. }
  128. function testErrorCanBeSwallowedByPatternMatching() {
  129. $this->expectError(new PatternExpectation('/ouch/i'));
  130. trigger_error('Ouch!');
  131. }
  132. function testErrorWithPercentsPassesWithNoSprintfError() {
  133. $this->expectError("%");
  134. trigger_error('%');
  135. }
  136. }
  137. class TestOfErrors extends UnitTestCase {
  138. var $_old;
  139. function setUp() {
  140. $this->_old = error_reporting(E_ALL);
  141. }
  142. function tearDown() {
  143. error_reporting($this->_old);
  144. }
  145. function testDefaultWhenAllReported() {
  146. error_reporting(E_ALL);
  147. trigger_error('Ouch!');
  148. $this->assertError('Ouch!');
  149. }
  150. function testNoticeWhenReported() {
  151. error_reporting(E_ALL);
  152. trigger_error('Ouch!', E_USER_NOTICE);
  153. $this->assertError('Ouch!');
  154. }
  155. function testWarningWhenReported() {
  156. error_reporting(E_ALL);
  157. trigger_error('Ouch!', E_USER_WARNING);
  158. $this->assertError('Ouch!');
  159. }
  160. function testErrorWhenReported() {
  161. error_reporting(E_ALL);
  162. trigger_error('Ouch!', E_USER_ERROR);
  163. $this->assertError('Ouch!');
  164. }
  165. function testNoNoticeWhenNotReported() {
  166. error_reporting(0);
  167. trigger_error('Ouch!', E_USER_NOTICE);
  168. }
  169. function testNoWarningWhenNotReported() {
  170. error_reporting(0);
  171. trigger_error('Ouch!', E_USER_WARNING);
  172. }
  173. function testNoticeSuppressedWhenReported() {
  174. error_reporting(E_ALL);
  175. @trigger_error('Ouch!', E_USER_NOTICE);
  176. }
  177. function testWarningSuppressedWhenReported() {
  178. error_reporting(E_ALL);
  179. @trigger_error('Ouch!', E_USER_WARNING);
  180. }
  181. function testErrorWithPercentsReportedWithNoSprintfError() {
  182. trigger_error('%');
  183. $this->assertError('%');
  184. }
  185. }
  186. class TestOfPHP52RecoverableErrors extends UnitTestCase {
  187. function skip() {
  188. $this->skipIf(
  189. version_compare(phpversion(), '5.2', '<'),
  190. 'E_RECOVERABLE_ERROR not tested for PHP below 5.2');
  191. }
  192. function testError() {
  193. eval('
  194. class RecoverableErrorTestingStub {
  195. function ouch(RecoverableErrorTestingStub $obj) {
  196. }
  197. }
  198. ');
  199. $stub = new RecoverableErrorTestingStub();
  200. $this->expectError(new PatternExpectation('/must be an instance of RecoverableErrorTestingStub/i'));
  201. $stub->ouch(new stdClass());
  202. }
  203. }
  204. class TestOfErrorsExcludingPHP52AndAbove extends UnitTestCase {
  205. function skip() {
  206. $this->skipIf(
  207. version_compare(phpversion(), '5.2', '>='),
  208. 'E_USER_ERROR not tested for PHP 5.2 and above');
  209. }
  210. function testNoErrorWhenNotReported() {
  211. error_reporting(0);
  212. trigger_error('Ouch!', E_USER_ERROR);
  213. }
  214. function testErrorSuppressedWhenReported() {
  215. error_reporting(E_ALL);
  216. @trigger_error('Ouch!', E_USER_ERROR);
  217. }
  218. }
  219. SimpleTest::ignore('TestOfNotEnoughErrors');
  220. /**
  221. * This test is ignored as it is used by {@link TestRunnerForLeftOverAndNotEnoughErrors}
  222. * to verify that it fails as expected.
  223. *
  224. * @ignore
  225. */
  226. class TestOfNotEnoughErrors extends UnitTestCase {
  227. function testExpectTwoErrorsThrowOne() {
  228. $this->expectError('Error 1');
  229. trigger_error('Error 1');
  230. $this->expectError('Error 2');
  231. }
  232. }
  233. SimpleTest::ignore('TestOfLeftOverErrors');
  234. /**
  235. * This test is ignored as it is used by {@link TestRunnerForLeftOverAndNotEnoughErrors}
  236. * to verify that it fails as expected.
  237. *
  238. * @ignore
  239. */
  240. class TestOfLeftOverErrors extends UnitTestCase {
  241. function testExpectOneErrorGetTwo() {
  242. $this->expectError('Error 1');
  243. trigger_error('Error 1');
  244. trigger_error('Error 2');
  245. }
  246. }
  247. class TestRunnerForLeftOverAndNotEnoughErrors extends UnitTestCase {
  248. function testRunLeftOverErrorsTestCase() {
  249. $test = new TestOfLeftOverErrors();
  250. $this->assertFalse($test->run(new SimpleReporter()));
  251. }
  252. function testRunNotEnoughErrors() {
  253. $test = new TestOfNotEnoughErrors();
  254. $this->assertFalse($test->run(new SimpleReporter()));
  255. }
  256. }
  257. // TODO: Add stacked error handler test
  258. ?>