Changeset 1088
- Timestamp:
- 11/29/07 18:25:56 (7 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/util/errorhandler/stubLogErrorHandler.php (modified) (8 diffs)
- trunk/src/test/php/net/stubbles/util/UtilTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/util/errorhandler/stubLogErrorHandlerTestCase.php (modified) (9 diffs)
- trunk/src/test/php/net/stubbles/util/log/stubLogDataFactoryTestCase.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/util/errorhandler/stubLogErrorHandler.php
r422 r1088 22 22 * @see http://php.net/set_error_handler 23 23 */ 24 class stubLogErrorHandler extends stubBaseObject implements stubErrorHandler , stubEventListener24 class stubLogErrorHandler extends stubBaseObject implements stubErrorHandler 25 25 { 26 /**27 * name of event triggered when session is created28 */29 const SESSION_EVENT_NAME = 'onsessionCreated';30 26 /** 31 27 * list of error levels and their string representation … … 33 29 * @var array<int,string> 34 30 */ 35 protected $levelStrings = array(E_ERROR => 'E_ERROR', 36 E_WARNING => 'E_WARNING', 37 E_PARSE => 'E_PARSE', 38 E_NOTICE => 'E_NOTICE', 39 E_CORE_ERROR => 'E_CORE_ERROR', 40 E_CORE_WARNING => 'E_CORE_WARNING', 41 E_COMPILE_ERROR => 'E_COMPILE_ERROR', 42 E_COMPILE_WARNING => 'E_COMPILE_WARNING', 43 E_USER_ERROR => 'E_USER_ERROR', 44 E_USER_WARNING => 'E_USER_WARNING', 45 E_USER_NOTICE => 'E_USER_NOTICE', 46 E_STRICT => 'E_STRICT', 47 E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', 48 E_ALL => 'E_ALL' 49 ); 50 /** 51 * instance of session 52 * 53 * @var stubSession 54 */ 55 protected $session = null; 56 /** 57 * list of unhandled errors 58 * 59 * @var array 60 */ 61 protected $errorStack = array(); 31 protected static $levelStrings = array(E_ERROR => 'E_ERROR', 32 E_WARNING => 'E_WARNING', 33 E_PARSE => 'E_PARSE', 34 E_NOTICE => 'E_NOTICE', 35 E_CORE_ERROR => 'E_CORE_ERROR', 36 E_CORE_WARNING => 'E_CORE_WARNING', 37 E_COMPILE_ERROR => 'E_COMPILE_ERROR', 38 E_COMPILE_WARNING => 'E_COMPILE_WARNING', 39 E_USER_ERROR => 'E_USER_ERROR', 40 E_USER_WARNING => 'E_USER_WARNING', 41 E_USER_NOTICE => 'E_USER_NOTICE', 42 E_STRICT => 'E_STRICT', 43 E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', 44 E_ALL => 'E_ALL' 45 ); 62 46 /** 63 47 * target of the log data … … 72 56 */ 73 57 protected $logLevel = stubLogger::LEVEL_ERROR; 74 75 /** 76 * constructor 77 * 78 * @param stubEventDispatcher $dispatcher optional 79 */ 80 public function __construct(stubEventDispatcher $dispatcher = null) 81 { 82 if (null == $dispatcher) { 83 $dispatcher = stubEventDispatcher::getInstance(); 84 } 85 86 $dispatcher->register($this, self::SESSION_EVENT_NAME); 87 } 88 89 /** 90 * sets the session instance 91 * 92 * @param stubSession $session 93 */ 94 public function setSession(stubSession $session) 95 { 96 $this->session = $session; 97 98 if (count($this->errorStack) > 0) { 99 foreach ($this->errorStack as $error) { 100 $this->log($error['level'], $error['message'], $error['file'], $error['line'], $error['context']); 101 } 102 103 $this->errorStack = array(); 104 } 105 } 106 58 107 59 /** 108 60 * sets the target of the log data … … 114 66 $this->logTarget = $logTarget; 115 67 } 116 68 117 69 /** 118 70 * sets the level of the log data … … 124 76 $this->logLevel = $logLevel; 125 77 } 126 127 /** 128 * handle the event 129 * 130 * If the context of the event is not an instance of stubSession a 131 * stubIllegalArgumentException will be thrown implicitly in case the 132 * net.stubbles.util.errorhandler.stubIllegalArgumentErrorHandler is 133 * registered as error handler, else an E_RECOVERABLE_ERROR is the 134 * consequence. 135 * 136 * @param stubEvent $event event that triggered this event listener 137 */ 138 public function handleEvent(stubEvent $event) 139 { 140 $this->setSession($event->getContext()); 141 } 142 143 /** 144 * instance can be removed as event listener if it received the session 145 * 146 * @return bool 147 */ 148 public function autoremove() 149 { 150 return (null != $this->session); 151 } 152 78 153 79 /** 154 80 * checks whether this error handler is responsible for the given error … … 167 93 return true; 168 94 } 169 95 170 96 /** 171 97 * checks whether this error is supressable … … 185 111 return false; 186 112 } 187 113 188 114 /** 189 115 * handles the given error … … 198 124 public function handle($level, $message, $file = null, $line = null, array $context = array()) 199 125 { 200 if (null == $this->session) { 201 $this->pushToErrorStack($level, $message, $file, $line, $context); 202 } else { 203 $this->log($level, $message, $file, $line, $context); 204 } 205 206 return true; 207 } 208 209 /** 210 * returns the stack of unlogged errors 211 * 212 * @return array 213 */ 214 public function getErrorStack() 215 { 216 return $this->errorStack; 217 } 218 219 /** 220 * logs the error 221 * 222 * @param int $level level of the raised error 223 * @param string $message error message 224 * @param string $file optional filename that the error was raised in 225 * @param int $line optional line number the error was raised at 226 * @param array $context optional array of every variable that existed in the scope the error was triggered in 227 */ 228 protected function log($level, $message, $file = null, $line = null, array $context = array()) 229 { 230 $logData = stubLogDataFactory::create($this->session, $this->logTarget, $this->logLevel); 126 $logData = stubLogDataFactory::create($this->logTarget, $this->logLevel); 231 127 $logData->addData($level); 232 $logData->addData((isset( $this->levelStrings[$level]) == true) ? ($this->levelStrings[$level]) : ('unknown'));128 $logData->addData((isset(self::$levelStrings[$level]) === true) ? (self::$levelStrings[$level]) : ('unknown')); 233 129 $logData->addData($message); 234 130 $logData->addData($file); 235 131 $logData->addData($line); 236 132 stubLogger::logToAll($logData); 237 } 238 239 /** 240 * pushes an error onto the stack 241 * 242 * @param int $level level of the raised error 243 * @param string $message error message 244 * @param string $file optional filename that the error was raised in 245 * @param int $line optional line number the error was raised at 246 * @param array $context optional array of every variable that existed in the scope the error was triggered in 247 */ 248 protected function pushToErrorStack($level, $message, $file = null, $line = null, array $context = array()) 249 { 250 $this->errorStack[] = array('level' => $level, 251 'message' => $message, 252 'file' => $file, 253 'line' => $line, 254 'context' => $context 255 ); 133 return true; 256 134 } 257 135 } trunk/src/test/php/net/stubbles/util/UtilTestSuite.php
r1087 r1088 42 42 $this->addTestFile($dir . '/errorhandler/stubCompositeErrorHandlerTestCase.php'); 43 43 $this->addTestFile($dir . '/errorhandler/stubIllegalArgumentErrorHandlerTestCase.php'); 44 #$this->addTestFile($dir . '/errorhandler/stubLogErrorHandlerTestCase.php');44 $this->addTestFile($dir . '/errorhandler/stubLogErrorHandlerTestCase.php'); 45 45 46 46 // logging api trunk/src/test/php/net/stubbles/util/errorhandler/stubLogErrorHandlerTestCase.php
r562 r1088 1 1 <?php 2 2 /** 3 * Tests for net .stubbles.util.errorhandler.stubLogErrorHandler3 * Tests for net::stubbles::util::errorhandler::stubLogErrorHandler. 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> … … 9 9 stubClassLoader::load('net.stubbles.util.errorhandler.stubLogErrorHandler'); 10 10 Mock::generate('stubSession'); 11 /** 12 * Mocked log appender to get access to the logged logdata object. 13 * 14 * @package stubbles 15 * @subpackage util_errorhandler_test 16 */ 11 17 class stubTestLogAppender extends stubBaseObject implements stubLogAppender 12 18 { 19 /** 20 * collected logdata objects 21 * 22 * @var array<stubLogData> 23 */ 13 24 protected $logData = array(); 14 25 26 /** 27 * sets the configuration data 28 * 29 * @param array $config 30 */ 15 31 public function setConfig(array $config) { } 16 32 33 /** 34 * returns the configuration 35 * 36 * @return array 37 */ 17 38 public function getConfig() 18 39 { … … 20 41 } 21 42 43 /** 44 * append the log data to the log target 45 * 46 * @param stubLogData $logData 47 */ 22 48 public function append(stubLogData $logData) 23 49 { … … 25 51 } 26 52 53 /** 54 * returns collected logdata objects 55 * 56 * @return array<stubLogData> 57 */ 27 58 public function getLogData() 28 59 { … … 30 61 } 31 62 63 /** 64 * finalize the log target 65 */ 32 66 public function finalize() {} 33 67 } 34 68 /** 35 * Tests for net .stubbles.util.errorhandler.stubLogErrorHandler69 * Tests for net::stubbles::util::errorhandler::stubLogErrorHandler. 36 70 * 37 71 * @package stubbles … … 54 88 $this->logErrorHandler = new stubLogErrorHandler(); 55 89 stubRegistry::setConfig('net.stubbles.util.log.class', 'net.stubbles.util.log.stubBaseLogData'); 90 $binder = new stubBinder(); 91 stubRegistry::set(stubBinder::REGISTRY_KEY, $binder); 56 92 } 57 93 … … 65 101 stubLogger::destroyInstance($loggerId); 66 102 } 67 } 68 69 /** 70 * test that event listener is removed if it has a session 71 */ 72 public function testAutoRemove() 73 { 74 $mockSession = new MockstubSession(); 75 $this->assertFalse($this->logErrorHandler->autoremove()); 76 $this->logErrorHandler->setSession($mockSession); 77 $this->assertTrue($this->logErrorHandler->autoremove()); 103 104 stubRegistry::remove(stubBinder::REGISTRY_KEY); 78 105 } 79 106 … … 95 122 96 123 /** 97 * assure that the error is put onto a stack of the session is set98 */99 public function testHandleWithoutSession()100 {101 $this->logErrorHandler->handle(E_NOTICE, 'foo', 'bar', 9, array('blub' => 'baz'));102 $this->assertEqual($this->logErrorHandler->getErrorStack(), array(array('level' => E_NOTICE,103 'message' => 'foo',104 'file' => 'bar',105 'line' => 9,106 'context' => array('blub' => 'baz')107 )108 )109 );110 }111 112 /**113 124 * assure that the error is logged if the session is set 114 125 */ 115 public function testHandle WithSession()126 public function testHandle() 116 127 { 117 $mockSession = new MockstubSession();118 $mockSession->setReturnValue('getId', 'foo');119 $this->logErrorHandler->setSession($mockSession);120 121 128 $logger = stubLogger::getInstance(__CLASS__); 122 129 $logAppender = new stubTestLogAppender(); … … 128 135 $this->assertEqual($logData[0]->getLevel(), stubLogger::LEVEL_ERROR); 129 136 $logDataContents = explode(stubLogData::SEPERATOR, $logData[0]->get()); 130 $this->assertEqual($logDataContents[2], E_USER_ERROR); 131 $this->assertEqual($logDataContents[3], 'E_USER_ERROR'); 132 $this->assertEqual($logDataContents[4], 'foo'); 133 $this->assertEqual($logDataContents[5], 'bar'); 134 $this->assertEqual($logDataContents[6], 9); 135 $this->assertEqual($this->logErrorHandler->getErrorStack(), array()); 136 stubLogger::destroyInstance(__CLASS__); 137 } 138 139 /** 140 * assure that the error is logged after the session is set 141 */ 142 public function testHandleWithSessionAddedAfterErrorOccured() 143 { 144 $mockSession = new MockstubSession(); 145 $mockSession->setReturnValue('getId', 'foo'); 146 $this->logErrorHandler->handle(E_RECOVERABLE_ERROR, 'foo', 'bar', 9, array('blub' => 'baz')); 147 $this->assertEqual($this->logErrorHandler->getErrorStack(), array(array('level' => E_RECOVERABLE_ERROR, 148 'message' => 'foo', 149 'file' => 'bar', 150 'line' => 9, 151 'context' => array('blub' => 'baz') 152 ) 153 ) 154 ); 155 $this->logErrorHandler->setLogTarget('exampleLogfile'); 156 $this->logErrorHandler->setLogLevel(stubLogger::LEVEL_DEBUG); 157 158 $logger = stubLogger::getInstance(__CLASS__); 159 $logAppender = new stubTestLogAppender(); 160 $logger->addLogAppender($logAppender); 161 162 $this->logErrorHandler->setSession($mockSession); 163 $logData = $logAppender->getLogData(); 164 $this->assertEqual(count($logData), 1); 165 $this->assertEqual($logData[0]->getTarget(), 'exampleLogfile'); 166 $this->assertEqual($logData[0]->getLevel(), stubLogger::LEVEL_DEBUG); 167 $logDataContents = explode(stubLogData::SEPERATOR, $logData[0]->get()); 168 $this->assertEqual($logDataContents[2], E_RECOVERABLE_ERROR); 169 $this->assertEqual($logDataContents[3], 'E_RECOVERABLE_ERROR'); 170 $this->assertEqual($logDataContents[4], 'foo'); 171 $this->assertEqual($logDataContents[5], 'bar'); 172 $this->assertEqual($logDataContents[6], 9); 173 $this->assertEqual($this->logErrorHandler->getErrorStack(), array()); 137 $this->assertEqual($logDataContents[1], E_USER_ERROR); 138 $this->assertEqual($logDataContents[2], 'E_USER_ERROR'); 139 $this->assertEqual($logDataContents[3], 'foo'); 140 $this->assertEqual($logDataContents[4], 'bar'); 141 $this->assertEqual($logDataContents[5], 9); 174 142 stubLogger::destroyInstance(__CLASS__); 175 143 } trunk/src/test/php/net/stubbles/util/log/stubLogDataFactoryTestCase.php
r1087 r1088 36 36 37 37 /** 38 * test that a missing binder throws an exception39 */40 public function testWithoutBinder()41 {42 stubRegistry::remove(stubBinder::REGISTRY_KEY);43 stubRegistry::setConfig('net.stubbles.util.log.class', 'MockstubLogData');44 $this->expectException('stubRuntimeException');45 stubLogDataFactory::create('foo');46 }47 48 /**49 38 * test creating a stubLogData object from a class that is already loaded 50 39 */
