CakeLogTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * CakeLogTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Log
  16. * @since CakePHP(tm) v 1.2.0.5432
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeLog', 'Log');
  20. App::uses('FileLog', 'Log/Engine');
  21. /**
  22. * CakeLogTest class
  23. *
  24. * @package Cake.Test.Case.Log
  25. */
  26. class CakeLogTest extends CakeTestCase {
  27. /**
  28. * Start test callback, clears all streams enabled.
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. $streams = CakeLog::configured();
  35. foreach ($streams as $stream) {
  36. CakeLog::drop($stream);
  37. }
  38. }
  39. /**
  40. * test importing loggers from app/libs and plugins.
  41. *
  42. * @return void
  43. */
  44. public function testImportingLoggers() {
  45. App::build(array(
  46. 'libs' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  47. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  48. ), true);
  49. CakePlugin::load('TestPlugin');
  50. $result = CakeLog::config('libtest', array(
  51. 'engine' => 'TestAppLog'
  52. ));
  53. $this->assertTrue($result);
  54. $this->assertEqual(CakeLog::configured(), array('libtest'));
  55. $result = CakeLog::config('plugintest', array(
  56. 'engine' => 'TestPlugin.TestPluginLog'
  57. ));
  58. $this->assertTrue($result);
  59. $this->assertEqual(CakeLog::configured(), array('libtest', 'plugintest'));
  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 that loggers have to implement the correct interface.
  74. *
  75. * @expectedException CakeLogException
  76. * @return void
  77. */
  78. public function testNotImplementingInterface() {
  79. CakeLog::config('fail', array('engine' => 'stdClass'));
  80. }
  81. /**
  82. * Test that CakeLog autoconfigures itself to use a FileLogger with the LOGS dir.
  83. * When no streams are there.
  84. *
  85. * @return void
  86. */
  87. public function testAutoConfig() {
  88. if (file_exists(LOGS . 'error.log')) {
  89. unlink(LOGS . 'error.log');
  90. }
  91. CakeLog::write(LOG_WARNING, 'Test warning');
  92. $this->assertTrue(file_exists(LOGS . 'error.log'));
  93. $result = CakeLog::configured();
  94. $this->assertEqual($result, array('default'));
  95. unlink(LOGS . 'error.log');
  96. }
  97. /**
  98. * test configuring log streams
  99. *
  100. * @return void
  101. */
  102. public function testConfig() {
  103. CakeLog::config('file', array(
  104. 'engine' => 'FileLog',
  105. 'path' => LOGS
  106. ));
  107. $result = CakeLog::configured();
  108. $this->assertEqual($result, array('file'));
  109. if (file_exists(LOGS . 'error.log')) {
  110. @unlink(LOGS . 'error.log');
  111. }
  112. CakeLog::write(LOG_WARNING, 'Test warning');
  113. $this->assertTrue(file_exists(LOGS . 'error.log'));
  114. $result = file_get_contents(LOGS . 'error.log');
  115. $this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
  116. unlink(LOGS . 'error.log');
  117. }
  118. /**
  119. * explict tests for drop()
  120. *
  121. * @return void
  122. **/
  123. public function testDrop() {
  124. CakeLog::config('file', array(
  125. 'engine' => 'FileLog',
  126. 'path' => LOGS
  127. ));
  128. $result = CakeLog::configured();
  129. $this->assertEqual($result, array('file'));
  130. CakeLog::drop('file');
  131. $result = CakeLog::configured();
  132. $this->assertEqual($result, array());
  133. }
  134. /**
  135. * testLogFileWriting method
  136. *
  137. * @return void
  138. */
  139. public function testLogFileWriting() {
  140. if (file_exists(LOGS . 'error.log')) {
  141. unlink(LOGS . 'error.log');
  142. }
  143. $result = CakeLog::write(LOG_WARNING, 'Test warning');
  144. $this->assertTrue($result);
  145. $this->assertTrue(file_exists(LOGS . 'error.log'));
  146. unlink(LOGS . 'error.log');
  147. CakeLog::write(LOG_WARNING, 'Test warning 1');
  148. CakeLog::write(LOG_WARNING, 'Test warning 2');
  149. $result = file_get_contents(LOGS . 'error.log');
  150. $this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1/', $result);
  151. $this->assertPattern('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
  152. unlink(LOGS . 'error.log');
  153. }
  154. }