/var/www/hkosl.com/imusiccircle/webadmin/libraies/php-console/php-console/tests/Test/Handler.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php

namespace PhpConsole\Test;

class 
Handler extends Test {

    
/** @var  \PhpConsole\Handler */
    
protected $handler;
    
/** @var \PhpConsole\Connector|\PHPUnit_Framework_MockObject_MockObject */
    
protected $connector;

    protected function 
setUp() {
        
parent::setUp();
        
$this->handler = \PhpConsole\Handler::getInstance();

        
$this->connector $this->getMockBuilder('\PhpConsole\Connector')
            ->
disableOriginalConstructor()
            ->
setMethods(array('sendMessage''onShutDown''isActiveClient'))
            ->
getMock();

        
$this->connector->expects($this->any())
            ->
method('isActiveClient')
            ->
will($this->returnValue(true));

        
$this->setProtectedProperty($this->handler'connector'$this->connector);
    }

    public function 
testGetConnector() {
        
$this->assertInstanceOf('\PhpConsole\Connector'$this->handler->getConnector());
    }

    public function 
testIsSingleton() {
        
$this->assertIsSingleton('\PhpConsole\Handler'$this->handler);
    }

    
/**
     * @expectedException \Exception
     */
    
public function testSetHandleErrorsBeforeStartThrowsException() {
        
$this->handler->start();
        
$this->handler->setHandleErrors(true);
    }

    
/**
     * @expectedException \Exception
     */
    
public function testSetHandleExceptionsBeforeStartThrowsException() {
        
$this->handler->start();
        
$this->handler->setHandleExceptions(true);
    }

    public function 
testInitErrorsHandler() {
        
$this->handler->start();
        
$this->assertEmpty(ini_get('display_errors'));
        
$this->assertEmpty(ini_get('html_errors'));
        
$this->assertEquals(E_ALL E_STRICTerror_reporting());
        
$this->assertEquals(array($this->handler'handleError'), set_error_handler($this->getProtectedProperty($this->handler'oldErrorsHandler')));
    }

    public function 
testInitExceptionsHandler() {
        
$this->handler->start();
        
$this->assertEquals(array($this->handler'handleException'), set_exception_handler($this->getProtectedProperty($this->handler'oldExceptionsHandler')));
    }

    
/**
     * @expectedException \Exception
     */
    
public function testDoubleStartThrowsException() {
        
$this->handler->start();
        
$this->handler->start();
    }

    public function 
testNoStartNoErrorsHandling() {
        
$this->connector->expects($this->never())->method('sendMessage');
        
$this->handler->handleError(1);
    }

    public function 
testNoStartNoExceptionsHandling() {
        
$this->connector->expects($this->never())->method('sendMessage');
        
$this->handler->handleException(new \Exception());
    }

    public function 
assertOldErrorsHandlerCalls($isEnabled) {
        
$oldErrorsHandlerCalls 0;
        
set_error_handler(function () use (&$oldErrorsHandlerCalls) {
            
$oldErrorsHandlerCalls++;
        });
        
$this->handler->start();
        
trigger_error(123);
        
$this->assertEquals($isEnabled 0$oldErrorsHandlerCalls);
    }

    public function 
assertOldExceptionsHandlerCalls($isEnabled) {
        
$oldExceptionsHandlerCalls 0;
        
set_exception_handler(function () use (&$oldExceptionsHandlerCalls) {
            
$oldExceptionsHandlerCalls++;
        });
        
$this->handler->start();
        
$this->handler->handleException(new \Exception());
        
$this->assertEquals($isEnabled 0$oldExceptionsHandlerCalls);
    }

    public function 
testOldErrorsHandlerIsCalledByDefault() {
        
$this->assertOldErrorsHandlerCalls(true);
    }

    public function 
testOldErrorsHandlerCallDisable() {
        
$this->handler->setCallOldHandlers(false);
        
$this->assertOldErrorsHandlerCalls(false);
    }

    public function 
testOldExceptionsHandlerIsCalledByDefault() {
        
$this->assertOldExceptionsHandlerCalls(true);
    }

    public function 
testOldExceptionsHandlerCallDisable() {
        
$this->handler->setCallOldHandlers(false);
        
$this->assertOldExceptionsHandlerCalls(false);
    }

    public function 
testHandleErrorData() {
        
$test $this;
        
$error null;
        
$this->connector->expects($this->once())
            ->
method('sendMessage')
            ->
with($this->callback(function (\PhpConsole\ErrorMessage $message) use ($test, &$error) {
                
$lastCall end($message->trace);
                
$test->assertContainsRecursive($error$message);
                
$test->assertContains(__CLASS__$lastCall->call);
                
$test->assertContains($test->getName(), $lastCall->call);
                return 
true;
            }));
        
$this->handler->start();
        
$error = array(
            
'type' => 'error',
            
'code' => E_USER_WARNING,
            
'class' => 'E_USER_WARNING',
            
'data' => 'err',
            
'file' => __FILE__,
            
'line' => __LINE__ 2,
        );
        
$this->handler->handleError($error['code'], $error['data'], __FILE____LINE__);
    }

    public function 
testHandleExceptionData() {
        
$test $this;
        
$exception = new \Exception('exception_test'100);
        
$this->connector->expects($this->once())
            ->
method('sendMessage')
            ->
with($this->callback(function (\PhpConsole\ErrorMessage $message) use ($test$exception) {
                
$lastCall end($message->trace);
                
$test->assertContainsRecursive(array(
                    
'type' => 'error',
                    
'code' => $exception->getCode(),
                    
'class' => get_class($exception),
                    
'data' => $exception->getMessage(),
                    
'file' => $exception->getFile(),
                    
'line' => $exception->getLine(),
                ), 
$message);
                
$test->assertContains(__CLASS__$lastCall->call);
                
$test->assertContains($test->getName(), $lastCall->call);

                return 
true;
            }));
        
$this->handler->start();
        
$this->handler->handleException($exception);
    }

    public function 
testHandleDebug() {
        
$debug null;
        
$test $this;

        
$this->connector->expects($this->once())
            ->
method('sendMessage')
            ->
with($this->callback(function (\PhpConsole\DebugMessage $message) use ($test, &$debug) {
                
$lastCall end($message->trace);
                
$test->assertContainsRecursive($debug$message);
                
$test->assertContains(__CLASS__$lastCall->call);
                
$test->assertContains($test->getName(), $lastCall->call);
                return 
true;
            }));
        
$this->handler->start();
        
$this->handler->getConnector()->getDebugDispatcher()->detectTraceAndSource true;
        
$debug = array(
            
'type' => 'debug',
            
'data' => 'data',
            
'tags' => array('t''a''g''s'),
            
'file' => __FILE__,
            
'line' => __LINE__ 2,
        );
        
$this->handler->debug($debug['data'], implode('.'$debug['tags']));
    }

    public function 
testRecursiveErrorsHandlingLimit() {
        
$handler $this->handler;
        
set_error_handler(function () use ($handler) {
            
$handler->handleError();
        });
        
$this->connector->getErrorsDispatcher()->ignoreRepeatedSource false;
        
$this->connector->expects($this->exactly(\PhpConsole\Handler::ERRORS_RECURSION_LIMIT))->method('sendMessage');
        
$this->handler->start();
        
$this->handler->handleError();
    }

    public function 
testRecursiveExceptionsHandlingLimit() {
        
$handler $this->handler;
        
set_exception_handler(function () use ($handler) {
            
$handler->handleException(new \Exception());
        });
        
$this->connector->getErrorsDispatcher()->ignoreRepeatedSource false;
        
$this->connector->expects($this->exactly(\PhpConsole\Handler::ERRORS_RECURSION_LIMIT))->method('sendMessage');
        
$this->handler->start();
        
$this->handler->handleException(new \Exception());
    }

    
/**
     * @expectedException \Exception
     */
    
public function testSetErrorsHandlerLevelBeforeStartThrowsException() {
        
$this->handler->start();
        
$this->handler->setErrorsHandlerLevel(E_USER_NOTICE);
    }

    public function 
testSetErrorsHandlerLevel() {
        
$this->handler->setErrorsHandlerLevel(E_ALL E_USER_NOTICE);
        
$this->handler->start();
        
trigger_error('hehe'E_USER_NOTICE);
        
$this->connector->expects($this->never())->method('sendMessage');
    }
}