PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Test/Case/Log/CakeLogTest.php

https://bitbucket.org/daveschwan/ronin-group
PHP | 722 lines | 497 code | 92 blank | 133 comment | 16 complexity | 6b5e3e3bf0ebd2d25a99cf071f84a8d0 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT, BSD-3-Clause, Apache-2.0
  1. <?php
  2. /**
  3. * CakeLogTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.Log
  15. * @since CakePHP(tm) v 1.2.0.5432
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('CakeLog', 'Log');
  19. App::uses('FileLog', 'Log/Engine');
  20. /**
  21. * CakeLogTest class
  22. *
  23. * @package Cake.Test.Case.Log
  24. */
  25. class CakeLogTest extends CakeTestCase {
  26. /**
  27. * Start test callback, clears all streams enabled.
  28. *
  29. * @return void
  30. */
  31. public function setUp() {
  32. parent::setUp();
  33. $streams = CakeLog::configured();
  34. foreach ($streams as $stream) {
  35. CakeLog::drop($stream);
  36. }
  37. }
  38. /**
  39. * test importing loggers from app/libs and plugins.
  40. *
  41. * @return void
  42. */
  43. public function testImportingLoggers() {
  44. App::build(array(
  45. 'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  46. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  47. ), App::RESET);
  48. CakePlugin::load('TestPlugin');
  49. $result = CakeLog::config('libtest', array(
  50. 'engine' => 'TestAppLog'
  51. ));
  52. $this->assertTrue($result);
  53. $this->assertEquals(CakeLog::configured(), array('libtest'));
  54. $result = CakeLog::config('plugintest', array(
  55. 'engine' => 'TestPlugin.TestPluginLog'
  56. ));
  57. $this->assertTrue($result);
  58. $this->assertEquals(CakeLog::configured(), array('libtest', 'plugintest'));
  59. CakeLog::write(LOG_INFO, 'TestPluginLog is not a BaseLog descendant');
  60. App::build();
  61. CakePlugin::unload();
  62. }
  63. /**
  64. * test all the errors from failed logger imports
  65. *
  66. * @expectedException CakeLogException
  67. * @return void
  68. */
  69. public function testImportingLoggerFailure() {
  70. CakeLog::config('fail', array());
  71. }
  72. /**
  73. * test config() with valid key name
  74. *
  75. * @return void
  76. */
  77. public function testValidKeyName() {
  78. CakeLog::config('valid', array('engine' => 'File'));
  79. $stream = CakeLog::stream('valid');
  80. $this->assertInstanceOf('FileLog', $stream);
  81. CakeLog::drop('valid');
  82. }
  83. /**
  84. * test config() with valid key name including the deprecated Log suffix
  85. *
  86. * @return void
  87. */
  88. public function testValidKeyNameLogSuffix() {
  89. CakeLog::config('valid', array('engine' => 'FileLog'));
  90. $stream = CakeLog::stream('valid');
  91. $this->assertInstanceOf('FileLog', $stream);
  92. CakeLog::drop('valid');
  93. }
  94. /**
  95. * test config() with invalid key name
  96. *
  97. * @expectedException CakeLogException
  98. * @return void
  99. */
  100. public function testInvalidKeyName() {
  101. CakeLog::config('1nv', array('engine' => 'File'));
  102. }
  103. /**
  104. * test that loggers have to implement the correct interface.
  105. *
  106. * @expectedException CakeLogException
  107. * @return void
  108. */
  109. public function testNotImplementingInterface() {
  110. CakeLog::config('fail', array('engine' => 'stdClass'));
  111. }
  112. /**
  113. * Test that CakeLog autoconfigures itself to use a FileLogger with the LOGS dir.
  114. * When no streams are there.
  115. *
  116. * @return void
  117. */
  118. public function testAutoConfig() {
  119. if (file_exists(LOGS . 'error.log')) {
  120. unlink(LOGS . 'error.log');
  121. }
  122. CakeLog::write(LOG_WARNING, 'Test warning');
  123. $this->assertTrue(file_exists(LOGS . 'error.log'));
  124. $result = CakeLog::configured();
  125. $this->assertEquals(array('default'), $result);
  126. $testMessage = 'custom message';
  127. CakeLog::write('custom', $testMessage);
  128. $content = file_get_contents(LOGS . 'custom.log');
  129. $this->assertContains($testMessage, $content);
  130. unlink(LOGS . 'error.log');
  131. unlink(LOGS . 'custom.log');
  132. }
  133. /**
  134. * test configuring log streams
  135. *
  136. * @return void
  137. */
  138. public function testConfig() {
  139. CakeLog::config('file', array(
  140. 'engine' => 'File',
  141. 'path' => LOGS
  142. ));
  143. $result = CakeLog::configured();
  144. $this->assertEquals(array('file'), $result);
  145. if (file_exists(LOGS . 'error.log')) {
  146. unlink(LOGS . 'error.log');
  147. }
  148. CakeLog::write(LOG_WARNING, 'Test warning');
  149. $this->assertTrue(file_exists(LOGS . 'error.log'));
  150. $result = file_get_contents(LOGS . 'error.log');
  151. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
  152. unlink(LOGS . 'error.log');
  153. }
  154. /**
  155. * explicit tests for drop()
  156. *
  157. * @return void
  158. */
  159. public function testDrop() {
  160. CakeLog::config('file', array(
  161. 'engine' => 'File',
  162. 'path' => LOGS
  163. ));
  164. $result = CakeLog::configured();
  165. $this->assertEquals(array('file'), $result);
  166. CakeLog::drop('file');
  167. $result = CakeLog::configured();
  168. $this->assertSame(array(), $result);
  169. }
  170. /**
  171. * testLogFileWriting method
  172. *
  173. * @return void
  174. */
  175. public function testLogFileWriting() {
  176. if (file_exists(LOGS . 'error.log')) {
  177. unlink(LOGS . 'error.log');
  178. }
  179. $result = CakeLog::write(LOG_WARNING, 'Test warning');
  180. $this->assertTrue($result);
  181. $this->assertTrue(file_exists(LOGS . 'error.log'));
  182. unlink(LOGS . 'error.log');
  183. CakeLog::write(LOG_WARNING, 'Test warning 1');
  184. CakeLog::write(LOG_WARNING, 'Test warning 2');
  185. $result = file_get_contents(LOGS . 'error.log');
  186. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1/', $result);
  187. $this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
  188. unlink(LOGS . 'error.log');
  189. }
  190. /**
  191. * test selective logging by level/type
  192. *
  193. * @return void
  194. */
  195. public function testSelectiveLoggingByLevel() {
  196. if (file_exists(LOGS . 'spam.log')) {
  197. unlink(LOGS . 'spam.log');
  198. }
  199. if (file_exists(LOGS . 'eggs.log')) {
  200. unlink(LOGS . 'eggs.log');
  201. }
  202. CakeLog::config('spam', array(
  203. 'engine' => 'File',
  204. 'types' => 'debug',
  205. 'file' => 'spam',
  206. ));
  207. CakeLog::config('eggs', array(
  208. 'engine' => 'File',
  209. 'types' => array('eggs', 'debug', 'error', 'warning'),
  210. 'file' => 'eggs',
  211. ));
  212. $testMessage = 'selective logging';
  213. CakeLog::write(LOG_WARNING, $testMessage);
  214. $this->assertTrue(file_exists(LOGS . 'eggs.log'));
  215. $this->assertFalse(file_exists(LOGS . 'spam.log'));
  216. CakeLog::write(LOG_DEBUG, $testMessage);
  217. $this->assertTrue(file_exists(LOGS . 'spam.log'));
  218. $contents = file_get_contents(LOGS . 'spam.log');
  219. $this->assertContains('Debug: ' . $testMessage, $contents);
  220. $contents = file_get_contents(LOGS . 'eggs.log');
  221. $this->assertContains('Debug: ' . $testMessage, $contents);
  222. if (file_exists(LOGS . 'spam.log')) {
  223. unlink(LOGS . 'spam.log');
  224. }
  225. if (file_exists(LOGS . 'eggs.log')) {
  226. unlink(LOGS . 'eggs.log');
  227. }
  228. }
  229. /**
  230. * test enable
  231. *
  232. * @expectedException CakeLogException
  233. */
  234. public function testStreamEnable() {
  235. CakeLog::config('spam', array(
  236. 'engine' => 'File',
  237. 'file' => 'spam',
  238. ));
  239. $this->assertTrue(CakeLog::enabled('spam'));
  240. CakeLog::drop('spam');
  241. CakeLog::enable('bogus_stream');
  242. }
  243. /**
  244. * test disable
  245. *
  246. * @expectedException CakeLogException
  247. */
  248. public function testStreamDisable() {
  249. CakeLog::config('spam', array(
  250. 'engine' => 'File',
  251. 'file' => 'spam',
  252. ));
  253. $this->assertTrue(CakeLog::enabled('spam'));
  254. CakeLog::disable('spam');
  255. $this->assertFalse(CakeLog::enabled('spam'));
  256. CakeLog::drop('spam');
  257. CakeLog::enable('bogus_stream');
  258. }
  259. /**
  260. * test enabled() invalid stream
  261. *
  262. * @expectedException CakeLogException
  263. */
  264. public function testStreamEnabledInvalid() {
  265. CakeLog::enabled('bogus_stream');
  266. }
  267. /**
  268. * test disable invalid stream
  269. *
  270. * @expectedException CakeLogException
  271. */
  272. public function testStreamDisableInvalid() {
  273. CakeLog::disable('bogus_stream');
  274. }
  275. protected function _resetLogConfig() {
  276. CakeLog::config('debug', array(
  277. 'engine' => 'File',
  278. 'types' => array('notice', 'info', 'debug'),
  279. 'file' => 'debug',
  280. ));
  281. CakeLog::config('error', array(
  282. 'engine' => 'File',
  283. 'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
  284. 'file' => 'error',
  285. ));
  286. }
  287. protected function _deleteLogs() {
  288. if (file_exists(LOGS . 'shops.log')) {
  289. unlink(LOGS . 'shops.log');
  290. }
  291. if (file_exists(LOGS . 'error.log')) {
  292. unlink(LOGS . 'error.log');
  293. }
  294. if (file_exists(LOGS . 'debug.log')) {
  295. unlink(LOGS . 'debug.log');
  296. }
  297. if (file_exists(LOGS . 'bogus.log')) {
  298. unlink(LOGS . 'bogus.log');
  299. }
  300. if (file_exists(LOGS . 'spam.log')) {
  301. unlink(LOGS . 'spam.log');
  302. }
  303. if (file_exists(LOGS . 'eggs.log')) {
  304. unlink(LOGS . 'eggs.log');
  305. }
  306. }
  307. /**
  308. * test backward compatible scoped logging
  309. *
  310. * @return void
  311. */
  312. public function testScopedLoggingBC() {
  313. $this->_resetLogConfig();
  314. CakeLog::config('shops', array(
  315. 'engine' => 'File',
  316. 'types' => array('info', 'notice', 'warning'),
  317. 'scopes' => array('transactions', 'orders'),
  318. 'file' => 'shops',
  319. ));
  320. $this->_deleteLogs();
  321. CakeLog::write('info', 'info message');
  322. $this->assertFalse(file_exists(LOGS . 'error.log'));
  323. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  324. $this->_deleteLogs();
  325. CakeLog::write('transactions', 'transaction message');
  326. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  327. $this->assertFalse(file_exists(LOGS . 'transactions.log'));
  328. $this->assertFalse(file_exists(LOGS . 'error.log'));
  329. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  330. $this->_deleteLogs();
  331. CakeLog::write('error', 'error message');
  332. $this->assertTrue(file_exists(LOGS . 'error.log'));
  333. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  334. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  335. $this->_deleteLogs();
  336. CakeLog::write('orders', 'order message');
  337. $this->assertFalse(file_exists(LOGS . 'error.log'));
  338. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  339. $this->assertFalse(file_exists(LOGS . 'orders.log'));
  340. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  341. $this->_deleteLogs();
  342. CakeLog::write('warning', 'warning message');
  343. $this->assertTrue(file_exists(LOGS . 'error.log'));
  344. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  345. $this->_deleteLogs();
  346. CakeLog::drop('shops');
  347. }
  348. /**
  349. * Test that scopes are exclusive and don't bleed.
  350. *
  351. * @return void
  352. */
  353. public function testScopedLoggingExclusive() {
  354. $this->_deleteLogs();
  355. CakeLog::config('shops', array(
  356. 'engine' => 'File',
  357. 'types' => array('info', 'notice', 'warning'),
  358. 'scopes' => array('transactions', 'orders'),
  359. 'file' => 'shops.log',
  360. ));
  361. CakeLog::config('eggs', array(
  362. 'engine' => 'File',
  363. 'types' => array('info', 'notice', 'warning'),
  364. 'scopes' => array('eggs'),
  365. 'file' => 'eggs.log',
  366. ));
  367. CakeLog::write('info', 'transactions message', 'transactions');
  368. $this->assertFalse(file_exists(LOGS . 'eggs.log'));
  369. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  370. $this->_deleteLogs();
  371. CakeLog::write('info', 'eggs message', 'eggs');
  372. $this->assertTrue(file_exists(LOGS . 'eggs.log'));
  373. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  374. }
  375. /**
  376. * test scoped logging
  377. *
  378. * @return void
  379. */
  380. public function testScopedLogging() {
  381. $this->_resetLogConfig();
  382. $this->_deleteLogs();
  383. CakeLog::config('string-scope', array(
  384. 'engine' => 'File',
  385. 'types' => array('info', 'notice', 'warning'),
  386. 'scopes' => 'string-scope',
  387. 'file' => 'string-scope.log'
  388. ));
  389. CakeLog::write('info', 'info message', 'string-scope');
  390. $this->assertTrue(file_exists(LOGS . 'string-scope.log'));
  391. CakeLog::drop('string-scope');
  392. CakeLog::config('shops', array(
  393. 'engine' => 'File',
  394. 'types' => array('info', 'notice', 'warning'),
  395. 'scopes' => array('transactions', 'orders'),
  396. 'file' => 'shops.log',
  397. ));
  398. CakeLog::write('info', 'info message', 'transactions');
  399. $this->assertFalse(file_exists(LOGS . 'error.log'));
  400. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  401. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  402. $this->_deleteLogs();
  403. CakeLog::write('transactions', 'transaction message', 'orders');
  404. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  405. $this->assertFalse(file_exists(LOGS . 'transactions.log'));
  406. $this->assertFalse(file_exists(LOGS . 'error.log'));
  407. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  408. $this->_deleteLogs();
  409. CakeLog::write('error', 'error message', 'orders');
  410. $this->assertTrue(file_exists(LOGS . 'error.log'));
  411. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  412. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  413. $this->_deleteLogs();
  414. CakeLog::write('orders', 'order message', 'transactions');
  415. $this->assertFalse(file_exists(LOGS . 'error.log'));
  416. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  417. $this->assertFalse(file_exists(LOGS . 'orders.log'));
  418. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  419. $this->_deleteLogs();
  420. CakeLog::write('warning', 'warning message', 'orders');
  421. $this->assertTrue(file_exists(LOGS . 'error.log'));
  422. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  423. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  424. $this->_deleteLogs();
  425. CakeLog::drop('shops');
  426. }
  427. /**
  428. * test bogus type and scope
  429. *
  430. */
  431. public function testBogusTypeAndScope() {
  432. $this->_resetLogConfig();
  433. $this->_deleteLogs();
  434. CakeLog::write('bogus', 'bogus message');
  435. $this->assertTrue(file_exists(LOGS . 'bogus.log'));
  436. $this->assertFalse(file_exists(LOGS . 'error.log'));
  437. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  438. $this->_deleteLogs();
  439. CakeLog::write('bogus', 'bogus message', 'bogus');
  440. $this->assertTrue(file_exists(LOGS . 'bogus.log'));
  441. $this->assertFalse(file_exists(LOGS . 'error.log'));
  442. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  443. $this->_deleteLogs();
  444. CakeLog::write('error', 'bogus message', 'bogus');
  445. $this->assertFalse(file_exists(LOGS . 'bogus.log'));
  446. $this->assertTrue(file_exists(LOGS . 'error.log'));
  447. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  448. $this->_deleteLogs();
  449. }
  450. /**
  451. * test scoped logging with convenience methods
  452. */
  453. public function testConvenienceScopedLogging() {
  454. if (file_exists(LOGS . 'shops.log')) {
  455. unlink(LOGS . 'shops.log');
  456. }
  457. if (file_exists(LOGS . 'error.log')) {
  458. unlink(LOGS . 'error.log');
  459. }
  460. if (file_exists(LOGS . 'debug.log')) {
  461. unlink(LOGS . 'debug.log');
  462. }
  463. $this->_resetLogConfig();
  464. CakeLog::config('shops', array(
  465. 'engine' => 'File',
  466. 'types' => array('info', 'debug', 'notice', 'warning'),
  467. 'scopes' => array('transactions', 'orders'),
  468. 'file' => 'shops',
  469. ));
  470. CakeLog::info('info message', 'transactions');
  471. $this->assertFalse(file_exists(LOGS . 'error.log'));
  472. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  473. $this->assertTrue(file_exists(LOGS . 'debug.log'));
  474. $this->_deleteLogs();
  475. CakeLog::error('error message', 'orders');
  476. $this->assertTrue(file_exists(LOGS . 'error.log'));
  477. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  478. $this->assertFalse(file_exists(LOGS . 'shops.log'));
  479. $this->_deleteLogs();
  480. CakeLog::warning('warning message', 'orders');
  481. $this->assertTrue(file_exists(LOGS . 'error.log'));
  482. $this->assertTrue(file_exists(LOGS . 'shops.log'));
  483. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  484. $this->_deleteLogs();
  485. CakeLog::drop('shops');
  486. }
  487. /**
  488. * test convenience methods
  489. */
  490. public function testConvenienceMethods() {
  491. $this->_deleteLogs();
  492. CakeLog::config('debug', array(
  493. 'engine' => 'File',
  494. 'types' => array('notice', 'info', 'debug'),
  495. 'file' => 'debug',
  496. ));
  497. CakeLog::config('error', array(
  498. 'engine' => 'File',
  499. 'types' => array('emergency', 'alert', 'critical', 'error', 'warning'),
  500. 'file' => 'error',
  501. ));
  502. $testMessage = 'emergency message';
  503. CakeLog::emergency($testMessage);
  504. $contents = file_get_contents(LOGS . 'error.log');
  505. $this->assertRegExp('/(Emergency|Critical): ' . $testMessage . '/', $contents);
  506. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  507. $this->_deleteLogs();
  508. $testMessage = 'alert message';
  509. CakeLog::alert($testMessage);
  510. $contents = file_get_contents(LOGS . 'error.log');
  511. $this->assertRegExp('/(Alert|Critical): ' . $testMessage . '/', $contents);
  512. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  513. $this->_deleteLogs();
  514. $testMessage = 'critical message';
  515. CakeLog::critical($testMessage);
  516. $contents = file_get_contents(LOGS . 'error.log');
  517. $this->assertContains('Critical: ' . $testMessage, $contents);
  518. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  519. $this->_deleteLogs();
  520. $testMessage = 'error message';
  521. CakeLog::error($testMessage);
  522. $contents = file_get_contents(LOGS . 'error.log');
  523. $this->assertContains('Error: ' . $testMessage, $contents);
  524. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  525. $this->_deleteLogs();
  526. $testMessage = 'warning message';
  527. CakeLog::warning($testMessage);
  528. $contents = file_get_contents(LOGS . 'error.log');
  529. $this->assertContains('Warning: ' . $testMessage, $contents);
  530. $this->assertFalse(file_exists(LOGS . 'debug.log'));
  531. $this->_deleteLogs();
  532. $testMessage = 'notice message';
  533. CakeLog::notice($testMessage);
  534. $contents = file_get_contents(LOGS . 'debug.log');
  535. $this->assertRegExp('/(Notice|Debug): ' . $testMessage . '/', $contents);
  536. $this->assertFalse(file_exists(LOGS . 'error.log'));
  537. $this->_deleteLogs();
  538. $testMessage = 'info message';
  539. CakeLog::info($testMessage);
  540. $contents = file_get_contents(LOGS . 'debug.log');
  541. $this->assertRegExp('/(Info|Debug): ' . $testMessage . '/', $contents);
  542. $this->assertFalse(file_exists(LOGS . 'error.log'));
  543. $this->_deleteLogs();
  544. $testMessage = 'debug message';
  545. CakeLog::debug($testMessage);
  546. $contents = file_get_contents(LOGS . 'debug.log');
  547. $this->assertContains('Debug: ' . $testMessage, $contents);
  548. $this->assertFalse(file_exists(LOGS . 'error.log'));
  549. $this->_deleteLogs();
  550. }
  551. /**
  552. * test levels customization
  553. */
  554. public function testLevelCustomization() {
  555. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Log level tests not supported on Windows.');
  556. $levels = CakeLog::defaultLevels();
  557. $this->assertNotEmpty($levels);
  558. $result = array_keys($levels);
  559. $this->assertEquals(array(0, 1, 2, 3, 4, 5, 6, 7), $result);
  560. $levels = CakeLog::levels(array('foo', 'bar'));
  561. CakeLog::defaultLevels();
  562. $this->assertEquals('foo', $levels[8]);
  563. $this->assertEquals('bar', $levels[9]);
  564. $levels = CakeLog::levels(array(11 => 'spam', 'bar' => 'eggs'));
  565. CakeLog::defaultLevels();
  566. $this->assertEquals('spam', $levels[8]);
  567. $this->assertEquals('eggs', $levels[9]);
  568. $levels = CakeLog::levels(array(11 => 'spam', 'bar' => 'eggs'), false);
  569. CakeLog::defaultLevels();
  570. $this->assertEquals(array('spam', 'eggs'), $levels);
  571. $levels = CakeLog::levels(array('ham', 9 => 'spam', '12' => 'fam'), false);
  572. CakeLog::defaultLevels();
  573. $this->assertEquals(array('ham', 'spam', 'fam'), $levels);
  574. }
  575. /**
  576. * Test writing log files with custom levels
  577. */
  578. public function testCustomLevelWrites() {
  579. $this->_deleteLogs();
  580. $this->_resetLogConfig();
  581. CakeLog::levels(array('spam', 'eggs'));
  582. $testMessage = 'error message';
  583. CakeLog::write('error', $testMessage);
  584. CakeLog::defaultLevels();
  585. $this->assertTrue(file_exists(LOGS . 'error.log'));
  586. $contents = file_get_contents(LOGS . 'error.log');
  587. $this->assertContains('Error: ' . $testMessage, $contents);
  588. CakeLog::config('spam', array(
  589. 'engine' => 'File',
  590. 'file' => 'spam.log',
  591. 'types' => 'spam',
  592. ));
  593. CakeLog::config('eggs', array(
  594. 'engine' => 'File',
  595. 'file' => 'eggs.log',
  596. 'types' => array('spam', 'eggs'),
  597. ));
  598. $testMessage = 'spam message';
  599. CakeLog::write('spam', $testMessage);
  600. CakeLog::defaultLevels();
  601. $this->assertTrue(file_exists(LOGS . 'spam.log'));
  602. $this->assertTrue(file_exists(LOGS . 'eggs.log'));
  603. $contents = file_get_contents(LOGS . 'spam.log');
  604. $this->assertContains('Spam: ' . $testMessage, $contents);
  605. $testMessage = 'egg message';
  606. CakeLog::write('eggs', $testMessage);
  607. CakeLog::defaultLevels();
  608. $contents = file_get_contents(LOGS . 'spam.log');
  609. $this->assertNotContains('Eggs: ' . $testMessage, $contents);
  610. $contents = file_get_contents(LOGS . 'eggs.log');
  611. $this->assertContains('Eggs: ' . $testMessage, $contents);
  612. CakeLog::drop('spam');
  613. CakeLog::drop('eggs');
  614. $this->_deleteLogs();
  615. }
  616. }