PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/cases/core/ErrorHandlerTest.php

http://github.com/UnionOfRAD/lithium
PHP | 182 lines | 139 code | 36 blank | 7 comment | 2 complexity | af8d0649f4aba90860ef82c728bf3692 MD5 | raw file
  1. <?php
  2. /**
  3. * li₃: the most RAD framework for PHP (http://li3.me)
  4. *
  5. * Copyright 2010, Union of RAD. All rights reserved. This source
  6. * code is distributed under the terms of the BSD 3-Clause License.
  7. * The full license text can be found in the LICENSE.txt file.
  8. */
  9. namespace lithium\tests\cases\core;
  10. use Closure;
  11. use Exception;
  12. use UnexpectedValueException;
  13. use lithium\core\ErrorHandler;
  14. use lithium\aop\Filters;
  15. use lithium\tests\mocks\core\MockStaticObjectDeprecated;
  16. use lithium\tests\mocks\core\MockErrorHandler;
  17. class ErrorHandlerTest extends \lithium\test\Unit {
  18. public $errors = [];
  19. public function setUp() {
  20. if (!ErrorHandler::isRunning()) {
  21. ErrorHandler::run();
  22. }
  23. ErrorHandler::reset();
  24. $this->errors = [];
  25. }
  26. public function tearDown() {
  27. if (ErrorHandler::isRunning()) {
  28. ErrorHandler::stop();
  29. }
  30. }
  31. public function testExceptionCatching() {
  32. $self = $this;
  33. ErrorHandler::config([[
  34. 'type' => 'Exception',
  35. 'handler' => function($info) use ($self) {
  36. $self->errors[] = $info;
  37. }
  38. ]]);
  39. ErrorHandler::handle(new Exception('Test!'));
  40. $this->assertCount(1, $this->errors);
  41. $result = end($this->errors);
  42. $expected = 'Test!';
  43. $this->assertEqual($expected, $result['message']);
  44. $backup = error_reporting();
  45. error_reporting($backup | E_WARNING);
  46. $this->assertException('/Test/', function() {
  47. trigger_error('Test warning!', E_USER_WARNING);
  48. });
  49. $this->assertCount(1, $this->errors);
  50. error_reporting($backup);
  51. }
  52. public function testExceptionSubclassCatching() {
  53. $self = $this;
  54. ErrorHandler::config([[
  55. 'type' => 'Exception',
  56. 'handler' => function($info) use ($self) {
  57. $self->errors[] = $info;
  58. }
  59. ]]);
  60. ErrorHandler::handle(new UnexpectedValueException('Test subclass'));
  61. $this->assertCount(1, $this->errors);
  62. $result = end($this->errors);
  63. $expected = 'Test subclass';
  64. $this->assertEqual($expected, $result['message']);
  65. }
  66. public function testErrorCatching() {
  67. $this->skipIf(true, 'Refactoring original error-handling iteration.');
  68. $self = $this;
  69. ErrorHandler::config([[
  70. 'code' => E_WARNING | E_USER_WARNING,
  71. 'handler' => function($info) use ($self) {
  72. $self->errors[] = $info;
  73. }
  74. ]]);
  75. file_get_contents(false);
  76. $this->assertCount(1, $this->errors);
  77. $result = end($this->errors);
  78. $this->assertPattern('/Filename cannot be empty/', $result['message']);
  79. trigger_error('Test warning', E_USER_WARNING);
  80. $this->assertCount(2, $this->errors);
  81. $result = end($this->errors);
  82. $this->assertEqual('Test warning', $result['message']);
  83. trigger_error('Test notice', E_USER_NOTICE);
  84. $this->assertCount(2, $this->errors);
  85. }
  86. public function testApply() {
  87. $class = 'lithium\tests\mocks\core\MockStaticObjectDeprecated';
  88. ErrorHandler::apply("{$class}::throwException", [], function($details) {
  89. return $details['exception']->getMessage();
  90. });
  91. $this->assertEqual('foo', MockStaticObjectDeprecated::throwException());
  92. Filters::clear('lithium\tests\mocks\core\MockStaticObjectDeprecated');
  93. }
  94. public function testTrace() {
  95. $current = debug_backtrace();
  96. $results = ErrorHandler::trace($current);
  97. $this->assertEqual(count($current), count($results));
  98. $this->assertEqual($results[0], 'lithium\tests\cases\core\ErrorHandlerTest::testTrace');
  99. }
  100. public function testRun() {
  101. ErrorHandler::stop();
  102. $this->assertEqual(ErrorHandler::isRunning(), false);
  103. ErrorHandler::run();
  104. $this->assertEqual(ErrorHandler::isRunning(), true);
  105. $result = ErrorHandler::run();
  106. $this->assertEqual(ErrorHandler::isRunning(), true);
  107. $this->assertNull($result);
  108. ErrorHandler::stop();
  109. $this->assertEqual(ErrorHandler::isRunning(), false);
  110. }
  111. public function testReset() {
  112. $checks = MockErrorHandler::checks();
  113. $defaultChecks = 4;
  114. $this->assertEqual($defaultChecks, count($checks));
  115. $this->assertInstanceOf('Closure', $checks['type']);
  116. $checks = MockErrorHandler::checks(['foo' => 'bar']);
  117. $this->assertCount(1, $checks);
  118. $this->assertFalse(isset($checks['type']));
  119. MockErrorHandler::reset();
  120. $checks = MockErrorHandler::checks();
  121. $this->assertEqual($defaultChecks, count($checks));
  122. $this->assertInstanceOf('Closure', $checks['type']);
  123. }
  124. public function testErrorTrapping() {
  125. ErrorHandler::stop();
  126. $self = $this;
  127. ErrorHandler::config([[
  128. 'handler' => function($info) use ($self) {
  129. $self->errors[] = $info;
  130. }]
  131. ]);
  132. ErrorHandler::run(['trapErrors' => true]);
  133. $this->assertCount(0, $this->errors);
  134. list($foo, $bar) = ['baz'];
  135. $this->assertCount(1, $this->errors);
  136. }
  137. public function testRenderedOutput() {
  138. $class = 'lithium\tests\mocks\core\MockStaticObjectDeprecated';
  139. ob_start();
  140. echo 'Some Output';
  141. ErrorHandler::apply("{$class}::throwException", [], function($details) {});
  142. MockStaticObjectDeprecated::throwException();
  143. $this->assertEmpty(ob_get_length());
  144. }
  145. }
  146. ?>