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