Changeset 1088

Show
Ignore:
Timestamp:
11/29/07 18:25:56 (7 months ago)
Author:
mikey
Message:

fixed and simplified net::stubbles::util::errorhandler::stubLogErrorHandler (side effect of enhancement #112)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/util/errorhandler/stubLogErrorHandler.php

    r422 r1088  
    2222 * @see         http://php.net/set_error_handler 
    2323 */ 
    24 class stubLogErrorHandler extends stubBaseObject implements stubErrorHandler, stubEventListener 
     24class stubLogErrorHandler extends stubBaseObject implements stubErrorHandler 
    2525{ 
    26     /** 
    27      * name of event triggered when session is created 
    28      */ 
    29     const SESSION_EVENT_NAME = 'onsessionCreated'; 
    3026    /** 
    3127     * list of error levels and their string representation 
     
    3329     * @var  array<int,string> 
    3430     */ 
    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                                      ); 
    6246    /** 
    6347     * target of the log data 
     
    7256     */ 
    7357    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 
    10759    /** 
    10860     * sets the target of the log data 
     
    11466        $this->logTarget = $logTarget; 
    11567    } 
    116      
     68 
    11769    /** 
    11870     * sets the level of the log data 
     
    12476        $this->logLevel = $logLevel; 
    12577    } 
    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 
    15379    /** 
    15480     * checks whether this error handler is responsible for the given error 
     
    16793        return true; 
    16894    } 
    169      
     95 
    17096    /** 
    17197     * checks whether this error is supressable 
     
    185111        return false; 
    186112    } 
    187      
     113 
    188114    /** 
    189115     * handles the given error 
     
    198124    public function handle($level, $message, $file = null, $line = null, array $context = array()) 
    199125    { 
    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); 
    231127        $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')); 
    233129        $logData->addData($message); 
    234130        $logData->addData($file); 
    235131        $logData->addData($line); 
    236132        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; 
    256134    } 
    257135} 
  • trunk/src/test/php/net/stubbles/util/UtilTestSuite.php

    r1087 r1088  
    4242        $this->addTestFile($dir . '/errorhandler/stubCompositeErrorHandlerTestCase.php'); 
    4343        $this->addTestFile($dir . '/errorhandler/stubIllegalArgumentErrorHandlerTestCase.php'); 
    44         #$this->addTestFile($dir . '/errorhandler/stubLogErrorHandlerTestCase.php'); 
     44        $this->addTestFile($dir . '/errorhandler/stubLogErrorHandlerTestCase.php'); 
    4545 
    4646        // logging api 
  • trunk/src/test/php/net/stubbles/util/errorhandler/stubLogErrorHandlerTestCase.php

    r562 r1088  
    11<?php 
    22/** 
    3  * Tests for net.stubbles.util.errorhandler.stubLogErrorHandler 
     3 * Tests for net::stubbles::util::errorhandler::stubLogErrorHandler. 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    99stubClassLoader::load('net.stubbles.util.errorhandler.stubLogErrorHandler'); 
    1010Mock::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 */ 
    1117class stubTestLogAppender extends stubBaseObject implements stubLogAppender 
    1218{ 
     19    /** 
     20     * collected logdata objects 
     21     * 
     22     * @var  array<stubLogData> 
     23     */ 
    1324    protected $logData = array(); 
    1425 
     26    /** 
     27     * sets the configuration data 
     28     * 
     29     * @param  array  $config 
     30     */ 
    1531    public function setConfig(array $config) { } 
    1632 
     33    /** 
     34     * returns the configuration 
     35     * 
     36     * @return  array 
     37     */ 
    1738    public function getConfig() 
    1839    { 
     
    2041    } 
    2142 
     43    /** 
     44     * append the log data to the log target 
     45     * 
     46     * @param  stubLogData  $logData 
     47     */ 
    2248    public function append(stubLogData $logData) 
    2349    { 
     
    2551    } 
    2652 
     53    /** 
     54     * returns collected logdata objects 
     55     * 
     56     * @return  array<stubLogData> 
     57     */ 
    2758    public function getLogData() 
    2859    { 
     
    3061    } 
    3162 
     63    /** 
     64     * finalize the log target 
     65     */ 
    3266    public function finalize() {} 
    3367} 
    3468/** 
    35  * Tests for net.stubbles.util.errorhandler.stubLogErrorHandler 
     69 * Tests for net::stubbles::util::errorhandler::stubLogErrorHandler. 
    3670 * 
    3771 * @package     stubbles 
     
    5488        $this->logErrorHandler = new stubLogErrorHandler(); 
    5589        stubRegistry::setConfig('net.stubbles.util.log.class', 'net.stubbles.util.log.stubBaseLogData'); 
     90        $binder = new stubBinder(); 
     91        stubRegistry::set(stubBinder::REGISTRY_KEY, $binder); 
    5692    } 
    5793 
     
    65101            stubLogger::destroyInstance($loggerId); 
    66102        } 
    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); 
    78105    } 
    79106 
     
    95122 
    96123    /** 
    97      * assure that the error is put onto a stack of the session is set 
    98      */ 
    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     /** 
    113124     * assure that the error is logged if the session is set 
    114125     */ 
    115     public function testHandleWithSession() 
     126    public function testHandle() 
    116127    { 
    117         $mockSession = new MockstubSession(); 
    118         $mockSession->setReturnValue('getId', 'foo'); 
    119         $this->logErrorHandler->setSession($mockSession); 
    120          
    121128        $logger      = stubLogger::getInstance(__CLASS__); 
    122129        $logAppender = new stubTestLogAppender(); 
     
    128135        $this->assertEqual($logData[0]->getLevel(), stubLogger::LEVEL_ERROR); 
    129136        $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); 
    174142        stubLogger::destroyInstance(__CLASS__); 
    175143    } 
  • trunk/src/test/php/net/stubbles/util/log/stubLogDataFactoryTestCase.php

    r1087 r1088  
    3636 
    3737    /** 
    38      * test that a missing binder throws an exception 
    39      */ 
    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     /** 
    4938     * test creating a stubLogData object from a class that is already loaded 
    5039     */