PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/general/LoggerTest.php

https://gitlab.com/oytunistrator/google-api-php-client
PHP | 445 lines | 332 code | 52 blank | 61 comment | 0 complexity | 857a4bfaff2b9a0a7d5c42274d92d4d2 MD5 | raw file
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class LoggerTest extends PHPUnit_Framework_TestCase
  21. {
  22. private $client;
  23. public function setup()
  24. {
  25. $this->client = new Google_Client();
  26. }
  27. /**
  28. * @dataProvider logLevelsProvider
  29. */
  30. public function testPsrMethods($key)
  31. {
  32. $message = 'This is my log message';
  33. $context = array('some'=>'context');
  34. $logger = $this->getLogger('log');
  35. $logger->expects($this->once())
  36. ->method('log')
  37. ->with($key, $message, $context);
  38. call_user_func(array($logger, $key), $message, $context);
  39. }
  40. /**
  41. * @dataProvider invalidLevelsProvider
  42. * @expectedException Google_Logger_Exception
  43. * @expectedExceptionMessage Unknown LogLevel
  44. */
  45. public function testSetLabelWithBadValue($level)
  46. {
  47. $this->getLogger()->setLevel($level);
  48. }
  49. /**
  50. * @dataProvider invalidLevelsProvider
  51. * @expectedException Google_Logger_Exception
  52. * @expectedExceptionMessage Unknown LogLevel
  53. */
  54. public function testSetLabelWithBadValueFromConfig($level)
  55. {
  56. $this->client->setClassConfig('Google_Logger_Abstract', 'level', $level);
  57. $this->getLogger();
  58. }
  59. /**
  60. * @dataProvider filterProvider
  61. */
  62. public function testShouldHandle($setLevel, $handleLevel, $expected)
  63. {
  64. $logger = $this->getLogger();
  65. $logger->setLevel($setLevel);
  66. $this->assertEquals($expected, $logger->shouldHandle($handleLevel));
  67. }
  68. /**
  69. * @dataProvider filterProvider
  70. */
  71. public function testShouldHandleFromConfig($config, $handleLevel, $expected)
  72. {
  73. $this->client->setClassConfig('Google_Logger_Abstract', 'level', $config);
  74. $logger = $this->getLogger();
  75. $this->assertEquals($expected, $logger->shouldHandle($handleLevel));
  76. }
  77. /**
  78. * @dataProvider filterProvider
  79. */
  80. public function testShouldWrite($setLevel, $logLevel, $expected)
  81. {
  82. $logger = $this->getLogger();
  83. $logger->expects($expected ? $this->once() : $this->never())
  84. ->method('write');
  85. $logger->setLevel($setLevel);
  86. $logger->log($logLevel, 'This is my log message');
  87. }
  88. /**
  89. * @dataProvider filterProvider
  90. */
  91. public function testShouldWriteFromConfig($config, $logLevel, $expected)
  92. {
  93. $this->client->setClassConfig('Google_Logger_Abstract', 'level', $config);
  94. $logger = $this->getLogger();
  95. $logger->expects($expected ? $this->once() : $this->never())
  96. ->method('write');
  97. $logger->log($logLevel, 'This is my log message');
  98. }
  99. /**
  100. * @dataProvider formattingProvider
  101. */
  102. public function testMessageFormatting(
  103. $format,
  104. $date_format,
  105. $newlines,
  106. $message,
  107. $context,
  108. $expected
  109. ) {
  110. $this->client->setClassConfig(
  111. 'Google_Logger_Abstract',
  112. 'log_format',
  113. $format
  114. );
  115. $this->client->setClassConfig(
  116. 'Google_Logger_Abstract',
  117. 'date_format',
  118. $date_format
  119. );
  120. $this->client->setClassConfig(
  121. 'Google_Logger_Abstract',
  122. 'allow_newlines',
  123. $newlines
  124. );
  125. $logger = $this->getLogger();
  126. $logger->expects($this->once())
  127. ->method('write')
  128. ->with($expected);
  129. $logger->log('debug', $message, $context);
  130. }
  131. public function testNullLoggerNeverWrites()
  132. {
  133. $logger = $this->getLogger('write', 'Google_Logger_Null');
  134. $logger->expects($this->never())
  135. ->method('write');
  136. $logger->log(
  137. 'emergency',
  138. 'Should not be written',
  139. array('same' => 'for this')
  140. );
  141. }
  142. public function testNullLoggerNeverHandles()
  143. {
  144. $logger = $this->getLogger('write', 'Google_Logger_Null');
  145. $this->assertFalse($logger->shouldHandle('emergency'));
  146. $this->assertFalse($logger->shouldHandle(600));
  147. }
  148. public function testPsrNeverWrites()
  149. {
  150. $logger = $this->getLogger('write', 'Google_Logger_Psr');
  151. $logger->expects($this->never())
  152. ->method('write');
  153. $logger->log(
  154. 'emergency',
  155. 'Should not be written',
  156. array('same' => 'for this')
  157. );
  158. $logger->setLogger($this->getLogger());
  159. $logger->log(
  160. 'emergency',
  161. 'Should not be written',
  162. array('same' => 'for this')
  163. );
  164. }
  165. public function testPsrNeverShouldHandleWhenNoLoggerSet()
  166. {
  167. $logger = $this->getLogger(null, 'Google_Logger_Psr');
  168. $this->assertFalse($logger->shouldHandle('emergency'));
  169. $this->assertFalse($logger->shouldHandle(600));
  170. }
  171. public function testPsrShouldHandleWhenLoggerSet()
  172. {
  173. $logger = $this->getLogger(null, 'Google_Logger_Psr');
  174. $logger->setLogger($this->getLogger());
  175. $this->assertTrue($logger->shouldHandle('emergency'));
  176. $this->assertTrue($logger->shouldHandle(600));
  177. }
  178. /**
  179. * @dataProvider logLevelsProvider
  180. */
  181. public function testPsrDelegates($key)
  182. {
  183. $message = 'This is my log message';
  184. $context = array('some'=>'context');
  185. $delegate = $this->getLogger('log');
  186. $delegate->expects($this->once())
  187. ->method('log')
  188. ->with($key, $message, $context);
  189. $logger = $this->getLogger(null, 'Google_Logger_Psr');
  190. $logger->setLogger($delegate);
  191. call_user_func(array($logger, $key), $message, $context);
  192. }
  193. /**
  194. * @expectedException Google_Logger_Exception
  195. * @expectedExceptionMessage File logger requires a filename or a valid file pointer
  196. */
  197. public function testLoggerWithBadFileType()
  198. {
  199. $this->client->setClassConfig('Google_Logger_File', 'file', false);
  200. $logger = $this->getLogger(null, 'Google_Logger_File');
  201. }
  202. /**
  203. * @expectedException Google_Logger_Exception
  204. * @expectedExceptionMessage Logger Error
  205. */
  206. public function testLoggerWithBadFileValue()
  207. {
  208. $this->client->setClassConfig('Google_Logger_File', 'file', 'not://exist');
  209. $logger = $this->getLogger(null, 'Google_Logger_File');
  210. $logger->log('emergency', 'will fail');
  211. }
  212. /**
  213. * @expectedException Google_Logger_Exception
  214. * @expectedExceptionMessage File pointer is no longer available
  215. */
  216. public function testLoggerWithClosedFileReference()
  217. {
  218. $fp = fopen('php://memory', 'r+');
  219. $this->client->setClassConfig('Google_Logger_File', 'file', $fp);
  220. $logger = $this->getLogger(null, 'Google_Logger_File');
  221. fclose($fp);
  222. $logger->log('emergency', 'will fail');
  223. }
  224. public function testLoggerWithFileReference()
  225. {
  226. $fp = fopen('php://memory', 'r+');
  227. $this->client->setClassConfig('Google_Logger_File', 'file', $fp);
  228. $this->client->setClassConfig(
  229. 'Google_Logger_Abstract',
  230. 'log_format',
  231. "%level% - %message%\n"
  232. );
  233. $logger = $this->getLogger(null, 'Google_Logger_File');
  234. $logger->log('emergency', 'test one');
  235. $logger->log('alert', 'test two');
  236. $logger->log(500, 'test three');
  237. rewind($fp);
  238. $this->assertEquals(
  239. "EMERGENCY - test one\nALERT - test two\nCRITICAL - test three\n",
  240. stream_get_contents($fp)
  241. );
  242. fclose($fp);
  243. }
  244. public function testLoggerWithFile()
  245. {
  246. $this->expectOutputString(
  247. "EMERGENCY - test one\nALERT - test two\nCRITICAL - test three\n"
  248. );
  249. $this->client->setClassConfig(
  250. 'Google_Logger_File',
  251. 'file',
  252. 'php://output'
  253. );
  254. $this->client->setClassConfig(
  255. 'Google_Logger_Abstract',
  256. 'log_format',
  257. "%level% - %message%\n"
  258. );
  259. $logger = $this->getLogger(null, 'Google_Logger_File');
  260. $logger->log('emergency', 'test one');
  261. $logger->log('alert', 'test two');
  262. $logger->log(500, 'test three');
  263. }
  264. public function formattingProvider()
  265. {
  266. return array(
  267. 'no interpolation' => array(
  268. 'this is my format',
  269. 'd/M/Y:H:i:s O',
  270. false,
  271. 'you wont see this',
  272. array('or' => 'this'),
  273. 'this is my format',
  274. ),
  275. 'only message interpolation' => array(
  276. 'my format: %message%',
  277. 'd/M/Y:H:i:s O',
  278. false,
  279. 'you will see this',
  280. array('but not' => 'this'),
  281. 'my format: you will see this',
  282. ),
  283. 'message and level interpolation' => array(
  284. '%level% - my format: %message%',
  285. 'd/M/Y:H:i:s O',
  286. false,
  287. 'you will see this',
  288. array('but not' => 'this'),
  289. 'DEBUG - my format: you will see this',
  290. ),
  291. 'message, level, datetime interpolation' => array(
  292. '[%datetime%] %level% - my format: %message%',
  293. '\T\I\M\E',
  294. false,
  295. 'you will see this',
  296. array('but not' => 'this'),
  297. '[TIME] DEBUG - my format: you will see this',
  298. ),
  299. 'message, level, datetime, context interpolation' => array(
  300. '[%datetime%] %level% - my format: %message%: %context%',
  301. '\T\I\M\E',
  302. false,
  303. 'you will see this',
  304. array('and' => 'this'),
  305. '[TIME] DEBUG - my format: you will see this: {"and":"this"}',
  306. ),
  307. 'reverse JSON in context' => array(
  308. '%context%',
  309. 'd/M/Y:H:i:s O',
  310. false,
  311. 'you will not see this',
  312. array('reverse' => json_encode(array('this' => 'is cool'))),
  313. '{"reverse":{"this":"is cool"}}',
  314. ),
  315. 'remove newlines in message' => array(
  316. '%message%',
  317. 'd/M/Y:H:i:s O',
  318. false,
  319. "This \n\n\r\n is \r my \r\n newlines \r\r\r\n\n message",
  320. array('you wont' => 'see this'),
  321. 'This is my newlines message',
  322. ),
  323. 'allow newlines in message' => array(
  324. '%message%',
  325. 'd/M/Y:H:i:s O',
  326. true,
  327. "This \n\n\r\n is \r my \r\n newlines \r\r\r\n\n message",
  328. array('you wont' => 'see this'),
  329. "This \n\n\r\n is \r my \r\n newlines \r\r\r\n\n message",
  330. ),
  331. 'allow newlines in JSON' => array(
  332. '%context%',
  333. 'd/M/Y:H:i:s O',
  334. true,
  335. "wont see this",
  336. array('you will' => 'see this'),
  337. version_compare(PHP_VERSION, '5.4.0', '>=') ?
  338. "{\n \"you will\": \"see this\"\n}" :
  339. '{"you will":"see this"}'
  340. ),
  341. );
  342. }
  343. public function filterProvider()
  344. {
  345. return array(
  346. array('debug', 'debug', true),
  347. array(100, 'debug', true),
  348. array('info', 'debug', false),
  349. array('info', 'notice', true),
  350. array('notice', 'notice', true),
  351. array(250, 'notice', true),
  352. array('notice', 'debug', false),
  353. array(250, 'alert', true),
  354. array('error', 'error', true),
  355. array('error', 'warning', false),
  356. array('error', 'critical', true),
  357. array(600, 'alert', false),
  358. array(600, 'critical', false),
  359. array(600, 'emergency', true),
  360. array('emergency', 'emergency', true),
  361. );
  362. }
  363. public function invalidLevelsProvider()
  364. {
  365. return array(
  366. array('100'),
  367. array('DEBUG'),
  368. array(100.0),
  369. array(''),
  370. array(0),
  371. );
  372. }
  373. public function logLevelsProvider()
  374. {
  375. return array(
  376. array('emergency', 600),
  377. array('alert', 550),
  378. array('critical', 500),
  379. array('error', 400),
  380. array('warning', 300),
  381. array('notice', 250),
  382. array('info', 200),
  383. array('debug', 100),
  384. );
  385. }
  386. private function getLogger($methods = null, $type = 'Google_Logger_Abstract')
  387. {
  388. return $this->getMockBuilder($type)
  389. ->setMethods((array) $methods)
  390. ->setConstructorArgs(array($this->client))
  391. ->getMockForAbstractClass();
  392. }
  393. }