ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
PHPConsoleHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog\Handler;
13 
14 use Exception;
16 use Monolog\Logger;
19 use PhpConsole\Dispatcher\Debug as DebugDispatcher;
20 use PhpConsole\Dispatcher\Errors as ErrorDispatcher;
23 
29 {
30 
32  protected $connector;
34  protected $debugDispatcher;
36  protected $errorDispatcher;
37 
38  protected function setUp()
39  {
40  if (!class_exists('PhpConsole\Connector')) {
41  $this->markTestSkipped('PHP Console library not found. See https://github.com/barbushin/php-console#installation');
42  }
43  $this->connector = $this->initConnectorMock();
44 
45  $this->debugDispatcher = $this->initDebugDispatcherMock($this->connector);
46  $this->connector->setDebugDispatcher($this->debugDispatcher);
47 
48  $this->errorDispatcher = $this->initErrorDispatcherMock($this->connector);
49  $this->connector->setErrorsDispatcher($this->errorDispatcher);
50  }
51 
52  protected function initDebugDispatcherMock(Connector $connector)
53  {
54  return $this->getMockBuilder('PhpConsole\Dispatcher\Debug')
55  ->disableOriginalConstructor()
56  ->setMethods(array('dispatchDebug'))
57  ->setConstructorArgs(array($connector, $connector->getDumper()))
58  ->getMock();
59  }
60 
61  protected function initErrorDispatcherMock(Connector $connector)
62  {
63  return $this->getMockBuilder('PhpConsole\Dispatcher\Errors')
64  ->disableOriginalConstructor()
65  ->setMethods(array('dispatchError', 'dispatchException'))
66  ->setConstructorArgs(array($connector, $connector->getDumper()))
67  ->getMock();
68  }
69 
70  protected function initConnectorMock()
71  {
72  $connector = $this->getMockBuilder('PhpConsole\Connector')
73  ->disableOriginalConstructor()
74  ->setMethods(array(
75  'sendMessage',
76  'onShutDown',
77  'isActiveClient',
78  'setSourcesBasePath',
79  'setServerEncoding',
80  'setPassword',
81  'enableSslOnlyMode',
82  'setAllowedIpMasks',
83  'setHeadersLimit',
84  'startEvalRequestsListener',
85  ))
86  ->getMock();
87 
88  $connector->expects($this->any())
89  ->method('isActiveClient')
90  ->will($this->returnValue(true));
91 
92  return $connector;
93  }
94 
95  protected function getHandlerDefaultOption($name)
96  {
97  $handler = new PHPConsoleHandler(array(), $this->connector);
98  $options = $handler->getOptions();
99 
100  return $options[$name];
101  }
102 
103  protected function initLogger($handlerOptions = array(), $level = Logger::DEBUG)
104  {
105  return new Logger('test', array(
106  new PHPConsoleHandler($handlerOptions, $this->connector, $level)
107  ));
108  }
109 
111  {
112  $handler = new PHPConsoleHandler();
113  $this->assertEquals(spl_object_hash(Connector::getInstance()), spl_object_hash($handler->getConnector()));
114  }
115 
116  public function testInitWithCustomConnector()
117  {
118  $handler = new PHPConsoleHandler(array(), $this->connector);
119  $this->assertEquals(spl_object_hash($this->connector), spl_object_hash($handler->getConnector()));
120  }
121 
122  public function testDebug()
123  {
124  $this->debugDispatcher->expects($this->once())->method('dispatchDebug')->with($this->equalTo('test'));
125  $this->initLogger()->addDebug('test');
126  }
127 
128  public function testDebugContextInMessage()
129  {
130  $message = 'test';
131  $tag = 'tag';
132  $context = array($tag, 'custom' => mt_rand());
133  $expectedMessage = $message . ' ' . json_encode(array_slice($context, 1));
134  $this->debugDispatcher->expects($this->once())->method('dispatchDebug')->with(
135  $this->equalTo($expectedMessage),
136  $this->equalTo($tag)
137  );
138  $this->initLogger()->addDebug($message, $context);
139  }
140 
141  public function testDebugTags($tagsContextKeys = null)
142  {
143  $expectedTags = mt_rand();
144  $logger = $this->initLogger($tagsContextKeys ? array('debugTagsKeysInContext' => $tagsContextKeys) : array());
145  if (!$tagsContextKeys) {
146  $tagsContextKeys = $this->getHandlerDefaultOption('debugTagsKeysInContext');
147  }
148  foreach ($tagsContextKeys as $key) {
149  $debugDispatcher = $this->initDebugDispatcherMock($this->connector);
150  $debugDispatcher->expects($this->once())->method('dispatchDebug')->with(
151  $this->anything(),
152  $this->equalTo($expectedTags)
153  );
154  $this->connector->setDebugDispatcher($debugDispatcher);
155  $logger->addDebug('test', array($key => $expectedTags));
156  }
157  }
158 
159  public function testError($classesPartialsTraceIgnore = null)
160  {
161  $code = E_USER_NOTICE;
162  $message = 'message';
163  $file = __FILE__;
164  $line = __LINE__;
165  $this->errorDispatcher->expects($this->once())->method('dispatchError')->with(
166  $this->equalTo($code),
167  $this->equalTo($message),
168  $this->equalTo($file),
169  $this->equalTo($line),
170  $classesPartialsTraceIgnore ?: $this->equalTo($this->getHandlerDefaultOption('classesPartialsTraceIgnore'))
171  );
172  $errorHandler = ErrorHandler::register($this->initLogger($classesPartialsTraceIgnore ? array('classesPartialsTraceIgnore' => $classesPartialsTraceIgnore) : array()), false);
173  $errorHandler->registerErrorHandler(array(), false, E_USER_WARNING);
174  $errorHandler->handleError($code, $message, $file, $line);
175  }
176 
177  public function testException()
178  {
179  $e = new Exception();
180  $this->errorDispatcher->expects($this->once())->method('dispatchException')->with(
181  $this->equalTo($e)
182  );
183  $handler = $this->initLogger();
184  $handler->log(
186  sprintf('Uncaught Exception %s: "%s" at %s line %s', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()),
187  array('exception' => $e)
188  );
189  }
190 
195  {
196  new PHPConsoleHandler(array('xxx' => 1));
197  }
198 
199  public function testOptionEnabled()
200  {
201  $this->debugDispatcher->expects($this->never())->method('dispatchDebug');
202  $this->initLogger(array('enabled' => false))->addDebug('test');
203  }
204 
206  {
207  $this->testError(array('Class', 'Namespace\\'));
208  }
209 
211  {
212  $this->testDebugTags(array('key1', 'key2'));
213  }
214 
216  {
217  $this->initLogger(array('useOwnErrorsHandler' => true, 'useOwnExceptionsHandler' => true));
218  $this->assertEquals(array(Handler::getInstance(), 'handleError'), set_error_handler(function () {
219  }));
220  $this->assertEquals(array(Handler::getInstance(), 'handleException'), set_exception_handler(function () {
221  }));
222  }
223 
224  public static function provideConnectorMethodsOptionsSets()
225  {
226  return array(
227  array('sourcesBasePath', 'setSourcesBasePath', __DIR__),
228  array('serverEncoding', 'setServerEncoding', 'cp1251'),
229  array('password', 'setPassword', '******'),
230  array('enableSslOnlyMode', 'enableSslOnlyMode', true, false),
231  array('ipMasks', 'setAllowedIpMasks', array('127.0.0.*')),
232  array('headersLimit', 'setHeadersLimit', 2500),
233  array('enableEvalListener', 'startEvalRequestsListener', true, false),
234  );
235  }
236 
240  public function testOptionCallsConnectorMethod($option, $method, $value, $isArgument = true)
241  {
242  $expectCall = $this->connector->expects($this->once())->method($method);
243  if ($isArgument) {
244  $expectCall->with($value);
245  }
246  new PHPConsoleHandler(array($option => $value), $this->connector);
247  }
248 
250  {
251  new PHPConsoleHandler(array('detectDumpTraceAndSource' => true), $this->connector);
252  $this->assertTrue($this->connector->getDebugDispatcher()->detectTraceAndSource);
253  }
254 
255  public static function provideDumperOptionsValues()
256  {
257  return array(
258  array('dumperLevelLimit', 'levelLimit', 1001),
259  array('dumperItemsCountLimit', 'itemsCountLimit', 1002),
260  array('dumperItemSizeLimit', 'itemSizeLimit', 1003),
261  array('dumperDumpSizeLimit', 'dumpSizeLimit', 1004),
262  array('dumperDetectCallbacks', 'detectCallbacks', true),
263  );
264  }
265 
269  public function testDumperOptions($option, $dumperProperty, $value)
270  {
271  new PHPConsoleHandler(array($option => $value), $this->connector);
272  $this->assertEquals($value, $this->connector->getDumper()->$dumperProperty);
273  }
274 }
print $file
const DEBUG
Detailed debug information.
Definition: Logger.php:32
initLogger($handlerOptions=array(), $level=Logger::DEBUG)
$code
Definition: example_050.php:99
Monolog log channel.
Definition: Logger.php:27
testError($classesPartialsTraceIgnore=null)
Monolog handler for Google Chrome extension "PHP Console".
testOptionCallsConnectorMethod($option, $method, $value, $isArgument=true)
provideConnectorMethodsOptionsSets
$Header$
if(!is_array($argv)) $options
testDumperOptions($option, $dumperProperty, $value)
provideDumperOptionsValues
static register(LoggerInterface $logger, $errorLevelMap=array(), $exceptionLevel=null, $fatalLevel=null)
Registers a new ErrorHandler for a given Logger.