LogTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. /**
  3. * CakePHP(tm) <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  11. * @package Cake.Test.Case.Log
  12. * @since CakePHP(tm) v 1.2.0.5432
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\Log;
  16. use Cake\Core\App;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\Log\Engine\FileLog;
  20. use Cake\Log\Log;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * LogTest class
  24. *
  25. * @package Cake.Test.Case.Log
  26. */
  27. class LogTest extends TestCase {
  28. public function tearDown() {
  29. parent::tearDown();
  30. Log::reset();
  31. }
  32. /**
  33. * test importing loggers from app/libs and plugins.
  34. *
  35. * @return void
  36. */
  37. public function testImportingLoggers() {
  38. Configure::write('App.namespace', 'TestApp');
  39. Plugin::load('TestPlugin');
  40. Log::config('libtest', [
  41. 'engine' => 'TestApp'
  42. ]);
  43. Log::config('plugintest', [
  44. 'engine' => 'TestPlugin.TestPlugin'
  45. ]);
  46. $result = Log::engine('libtest');
  47. $this->assertInstanceOf('TestApp\Log\Engine\TestAppLog', $result);
  48. $this->assertContains('libtest', Log::configured());
  49. $result = Log::engine('plugintest');
  50. $this->assertInstanceOf('TestPlugin\Log\Engine\TestPluginLog', $result);
  51. $this->assertContains('libtest', Log::configured());
  52. $this->assertContains('plugintest', Log::configured());
  53. Log::write(LOG_INFO, 'TestPluginLog is not a BaseLog descendant');
  54. Plugin::unload();
  55. }
  56. /**
  57. * test all the errors from failed logger imports
  58. *
  59. * @expectedException Cake\Error\Exception
  60. * @return void
  61. */
  62. public function testImportingLoggerFailure() {
  63. Log::config('fail', []);
  64. Log::engine('fail');
  65. }
  66. /**
  67. * test config() with valid key name
  68. *
  69. * @return void
  70. */
  71. public function testValidKeyName() {
  72. Log::config('valid', array('engine' => 'File'));
  73. $stream = Log::engine('valid');
  74. $this->assertInstanceOf('Cake\Log\Engine\FileLog', $stream);
  75. }
  76. /**
  77. * test that loggers have to implement the correct interface.
  78. *
  79. * @expectedException Cake\Error\Exception
  80. * @return void
  81. */
  82. public function testNotImplementingInterface() {
  83. Log::config('fail', array('engine' => '\stdClass'));
  84. Log::engine('fail');
  85. }
  86. /**
  87. * explicit tests for drop()
  88. *
  89. * @return void
  90. **/
  91. public function testDrop() {
  92. Log::config('file', array(
  93. 'engine' => 'File',
  94. 'path' => LOGS
  95. ));
  96. $result = Log::configured();
  97. $this->assertContains('file', $result);
  98. $this->assertTrue(Log::drop('file'), 'Should be dropped');
  99. $this->assertFalse(Log::drop('file'), 'Already gone');
  100. $result = Log::configured();
  101. $this->assertNotContains('file', $result);
  102. }
  103. /**
  104. * Provider for config() tests.
  105. *
  106. * @return array
  107. */
  108. public static function configProvider() {
  109. return [
  110. 'Array of data using engine key.' => [[
  111. 'engine' => 'File',
  112. 'path' => TMP . 'tests',
  113. ]],
  114. 'Array of data using classname key.' => [[
  115. 'className' => 'File',
  116. 'path' => TMP . 'tests',
  117. ]],
  118. 'Direct instance' => [new FileLog()],
  119. ];
  120. }
  121. /**
  122. * Test the various config call signatures.
  123. *
  124. * @dataProvider configProvider
  125. * @return void
  126. */
  127. public function testConfigVariants($settings) {
  128. Log::config('test', $settings);
  129. $this->assertContains('test', Log::configured());
  130. $this->assertInstanceOf('Cake\Log\Engine\FileLog', Log::engine('test'));
  131. Log::drop('test');
  132. }
  133. /**
  134. * Test that config() throws an exception when adding an
  135. * adapter with the wrong type.
  136. *
  137. * @expectedException Cake\Error\Exception
  138. * @return void
  139. */
  140. public function testConfigInjectErrorOnWrongType() {
  141. Log::config('test', new \StdClass);
  142. Log::info('testing');
  143. }
  144. /**
  145. * Test that config() can read data back
  146. *
  147. * @return void
  148. */
  149. public function testConfigRead() {
  150. $config = [
  151. 'engine' => 'File',
  152. 'path' => LOGS
  153. ];
  154. Log::config('tests', $config);
  155. $expected = $config;
  156. $expected['className'] = $config['engine'];
  157. unset($expected['engine']);
  158. $this->assertSame($expected, Log::config('tests'));
  159. }
  160. /**
  161. * Ensure you cannot reconfigure a log adapter.
  162. *
  163. * @expectedException Cake\Error\Exception
  164. * @return void
  165. */
  166. public function testConfigErrorOnReconfigure() {
  167. Log::config('tests', ['engine' => 'File', 'path' => TMP]);
  168. Log::config('tests', ['engine' => 'Apc']);
  169. }
  170. /**
  171. * testLogFileWriting method
  172. *
  173. * @return void
  174. */
  175. public function testLogFileWriting() {
  176. $this->_resetLogConfig();
  177. if (file_exists(LOGS . 'error.log')) {
  178. unlink(LOGS . 'error.log');
  179. }
  180. $result = Log::write(LOG_WARNING, 'Test warning');
  181. $this->assertTrue($result);
  182. $this->assertTrue(file_exists(LOGS . 'error.log'));
  183. unlink(LOGS . 'error.log');
  184. Log::write(LOG_WARNING, 'Test warning 1');
  185. Log::write(LOG_WARNING, 'Test warning 2');
  186. $result = file_get_contents(LOGS . 'error.log');
  187. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1/', $result);
  188. $this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
  189. unlink(LOGS . 'error.log');
  190. }
  191. /**
  192. * test selective logging by level/type
  193. *
  194. * @return void
  195. */
  196. public function testSelectiveLoggingByLevel() {
  197. if (file_exists(LOGS . 'spam.log')) {
  198. unlink(LOGS . 'spam.log');
  199. }
  200. if (file_exists(LOGS . 'eggs.log')) {
  201. unlink(LOGS . 'eggs.log');
  202. }
  203. Log::config('spam', array(
  204. 'engine' => 'File',
  205. 'types' => 'debug',
  206. 'file' => 'spam',
  207. ));
  208. Log::config('eggs', array(
  209. 'engine' => 'File',
  210. 'types' => array('eggs', 'debug', 'error', 'warning'),
  211. 'file' => 'eggs',
  212. ));
  213. $testMessage = 'selective logging';
  214. Log::write(LOG_WARNING, $testMessage);
  215. $this->assertTrue(file_exists(LOGS . 'eggs.log'));
  216. $this->assertFalse(file_exists(LOGS . 'spam.log'));
  217. Log::write(LOG_DEBUG, $testMessage);
  218. $this->assertTrue(file_exists(LOGS . 'spam.log'));
  219. $contents = file_get_contents(LOGS . 'spam.log');
  220. $this->assertContains('Debug: ' . $testMessage, $contents);
  221. $contents = file_get_contents(LOGS . 'eggs.log');
  222. $this->assertContains('Debug: ' . $testMessage, $contents);
  223. if (file_exists(LOGS . 'spam.log')) {
  224. unlink(LOGS . 'spam.log');
  225. }
  226. if (file_exists(LOGS . 'eggs.log')) {
  227. unlink(LOGS . 'eggs.log');
  228. }
  229. }
  230. protected function _resetLogConfig() {
  231. Log::config('debug', array(
  232. 'engine' => 'File',
  233. 'types' => array('notice', 'info', 'debug'),
  234. 'file' => 'debug',
  235. ));
  236. Log::config('error', array(
  237. 'engine' => 'File',
  238. 'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
  239. 'file' => 'error',
  240. ));
  241. }
  242. protected function _deleteLogs() {
  243. if (file_exists(LOGS . 'shops.log')) {
  244. unlink(LOGS . 'shops.log');
  245. }
  246. if (file_exists(LOGS . 'error.log')) {
  247. unlink(LOGS . 'error.log');
  248. }
  249. if (file_exists(LOGS . 'debug.log')) {
  250. unlink(LOGS . 'debug.log');
  251. }
  252. if (file_exists(LOGS . 'bogus.log')) {
  253. unlink(LOGS . 'bogus.log');
  254. }
  255. if (file_exists(LOGS . 'spam.log')) {
  256. unlink(LOGS . 'spam.log');
  257. }
  258. if (file_exists(LOGS . 'eggs.log')) {
  259. unlink(LOGS . 'eggs.log');
  260. }
  261. }
  262. /**
  263. * test scoped logging
  264. *
  265. * @return void
  266. */
  267. public function testScopedLogging() {
  268. $this->_deleteLogs();
  269. $this->_resetLogConfig();
  270. Log::config('shops', array(
  271. 'engine' => 'File',
  272. 'types' => array('info', 'notice', 'warning'),
  273. 'scopes' => array('transactions', 'orders'),
  274. 'file' => 'shops',
  275. ));
  276. Log::write('info', 'info message', 'transactions');
  277. $this->assertFalse(file_exists(LOGS . 'error.log'));
  278. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  279. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  280. $this->_deleteLogs();
  281. Log::write('warning', 'warning message', 'orders');
  282. $this->assertTrue(file_exists(LOGS . 'error.log'));
  283. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  284. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  285. $this->_deleteLogs();
  286. Log::write('error', 'error message', 'orders');
  287. $this->assertTrue(file_exists(LOGS . 'error.log'));
  288. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  289. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  290. $this->_deleteLogs();
  291. Log::drop('shops');
  292. }
  293. /**
  294. * test scoped logging with convenience methods
  295. */
  296. public function testConvenienceScopedLogging() {
  297. if (file_exists(LOGS . 'shops.log')) {
  298. unlink(LOGS . 'shops.log');
  299. }
  300. if (file_exists(LOGS . 'error.log')) {
  301. unlink(LOGS . 'error.log');
  302. }
  303. if (file_exists(LOGS . 'debug.log')) {
  304. unlink(LOGS . 'debug.log');
  305. }
  306. $this->_resetLogConfig();
  307. Log::config('shops', array(
  308. 'engine' => 'File',
  309. 'types' => array('info', 'debug', 'notice', 'warning'),
  310. 'scopes' => array('transactions', 'orders'),
  311. 'file' => 'shops',
  312. ));
  313. Log::info('info message', 'transactions');
  314. $this->assertFalse(file_exists(LOGS . 'error.log'));
  315. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  316. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  317. $this->_deleteLogs();
  318. Log::error('error message', 'orders');
  319. $this->assertTrue(file_exists(LOGS . 'error.log'));
  320. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  321. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  322. $this->_deleteLogs();
  323. Log::warning('warning message', 'orders');
  324. $this->assertTrue(file_exists(LOGS . 'error.log'));
  325. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  326. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  327. $this->_deleteLogs();
  328. Log::drop('shops');
  329. }
  330. /**
  331. * Test that scopes are exclusive and don't bleed.
  332. *
  333. * @return void
  334. */
  335. public function testScopedLoggingExclusive() {
  336. $this->_deleteLogs();
  337. Log::config('shops', array(
  338. 'engine' => 'File',
  339. 'types' => array('info', 'notice', 'warning'),
  340. 'scopes' => array('transactions', 'orders'),
  341. 'file' => 'shops.log',
  342. ));
  343. Log::config('eggs', array(
  344. 'engine' => 'File',
  345. 'types' => array('info', 'notice', 'warning'),
  346. 'scopes' => array('eggs'),
  347. 'file' => 'eggs.log',
  348. ));
  349. Log::write('info', 'transactions message', 'transactions');
  350. $this->assertFalse(file_exists(LOGS . 'eggs.log'));
  351. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  352. $this->_deleteLogs();
  353. Log::write('info', 'eggs message', 'eggs');
  354. $this->assertTrue(file_exists(LOGS . 'eggs.log'));
  355. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  356. }
  357. /**
  358. * test convenience methods
  359. */
  360. public function testConvenienceMethods() {
  361. $this->_deleteLogs();
  362. Log::config('debug', array(
  363. 'engine' => 'File',
  364. 'types' => array('notice', 'info', 'debug'),
  365. 'file' => 'debug',
  366. ));
  367. Log::config('error', array(
  368. 'engine' => 'File',
  369. 'types' => array('emergency', 'alert', 'critical', 'error', 'warning'),
  370. 'file' => 'error',
  371. ));
  372. $testMessage = 'emergency message';
  373. Log::emergency($testMessage);
  374. $contents = file_get_contents(LOGS . 'error.log');
  375. $this->assertRegExp('/(Emergency|Critical): ' . $testMessage . '/', $contents);
  376. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  377. $this->_deleteLogs();
  378. $testMessage = 'alert message';
  379. Log::alert($testMessage);
  380. $contents = file_get_contents(LOGS . 'error.log');
  381. $this->assertRegExp('/(Alert|Critical): ' . $testMessage . '/', $contents);
  382. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  383. $this->_deleteLogs();
  384. $testMessage = 'critical message';
  385. Log::critical($testMessage);
  386. $contents = file_get_contents(LOGS . 'error.log');
  387. $this->assertContains('Critical: ' . $testMessage, $contents);
  388. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  389. $this->_deleteLogs();
  390. $testMessage = 'error message';
  391. Log::error($testMessage);
  392. $contents = file_get_contents(LOGS . 'error.log');
  393. $this->assertContains('Error: ' . $testMessage, $contents);
  394. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  395. $this->_deleteLogs();
  396. $testMessage = 'warning message';
  397. Log::warning($testMessage);
  398. $contents = file_get_contents(LOGS . 'error.log');
  399. $this->assertContains('Warning: ' . $testMessage, $contents);
  400. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  401. $this->_deleteLogs();
  402. $testMessage = 'notice message';
  403. Log::notice($testMessage);
  404. $contents = file_get_contents(LOGS . 'debug.log');
  405. $this->assertRegExp('/(Notice|Debug): ' . $testMessage . '/', $contents);
  406. $this->assertFalse(file_exists(LOGS . 'error.log'));
  407. $this->_deleteLogs();
  408. $testMessage = 'info message';
  409. Log::info($testMessage);
  410. $contents = file_get_contents(LOGS . 'debug.log');
  411. $this->assertRegExp('/(Info|Debug): ' . $testMessage . '/', $contents);
  412. $this->assertFalse(file_exists(LOGS . 'error.log'));
  413. $this->_deleteLogs();
  414. $testMessage = 'debug message';
  415. Log::debug($testMessage);
  416. $contents = file_get_contents(LOGS . 'debug.log');
  417. $this->assertContains('Debug: ' . $testMessage, $contents);
  418. $this->assertFalse(file_exists(LOGS . 'error.log'));
  419. $this->_deleteLogs();
  420. }
  421. /**
  422. * Test that write() returns false on an unhandled message.
  423. *
  424. * @return false
  425. */
  426. public function testWriteUnhandled() {
  427. Log::drop('error');
  428. Log::drop('debug');
  429. $result = Log::write('error', 'Bad stuff', 'unpossible');
  430. $this->assertFalse($result);
  431. }
  432. }