LoggerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\analysis;
  9. use lithium\core\Libraries;
  10. use lithium\analysis\Logger;
  11. use lithium\tests\mocks\analysis\MockLoggerAdapter;
  12. /**
  13. * Logger adapter test case
  14. */
  15. class LoggerTest extends \lithium\test\Unit {
  16. public function skip() {
  17. $path = Libraries::get(true, 'resources');
  18. if (is_writable($path)) {
  19. foreach (array("{$path}/tmp/tests", "{$path}/tmp/logs") as $dir) {
  20. if (!is_dir($dir)) {
  21. mkdir($dir, 0777, true);
  22. }
  23. }
  24. }
  25. $this->_testPath = "{$path}/tmp/tests";
  26. $this->skipIf(!is_writable($this->_testPath), "Path `{$this->_testPath}` is not writable.");
  27. }
  28. public function setUp() {
  29. Logger::config(array('default' => array('adapter' => new MockLoggerAdapter())));
  30. }
  31. public function tearDown() {
  32. Logger::reset();
  33. }
  34. public function testConfig() {
  35. $test = new MockLoggerAdapter();
  36. $config = array('logger' => array('adapter' => $test, 'filters' => array()));
  37. $result = Logger::config($config);
  38. $this->assertNull($result);
  39. $result = Logger::config();
  40. $config['logger'] += array('priority' => true);
  41. $expected = $config;
  42. $this->assertEqual($expected, $result);
  43. }
  44. public function testReset() {
  45. $test = new MockLoggerAdapter();
  46. $config = array('logger' => array('adapter' => $test, 'filters' => array()));
  47. $result = Logger::config($config);
  48. $result = Logger::reset();
  49. $this->assertNull($result);
  50. $result = Logger::config();
  51. $this->assertFalse($result);
  52. $this->assertFalse(Logger::write('info', 'Test message.'));
  53. }
  54. public function testWrite() {
  55. $result = Logger::write('info', 'value');
  56. $this->assertTrue($result);
  57. }
  58. public function testIntegrationWriteFile() {
  59. $base = Libraries::get(true, 'resources') . '/tmp/logs';
  60. $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
  61. $config = array('default' => array(
  62. 'adapter' => 'File', 'timestamp' => false, 'format' => "{:message}\n"
  63. ));
  64. Logger::config($config);
  65. $result = Logger::write('info', 'Message line 1');
  66. $this->assertTrue(file_exists($base . '/info.log'));
  67. $expected = "Message line 1\n";
  68. $result = file_get_contents($base . '/info.log');
  69. $this->assertEqual($expected, $result);
  70. $result = Logger::write('info', 'Message line 2');
  71. $this->assertTrue($result);
  72. $expected = "Message line 1\nMessage line 2\n";
  73. $result = file_get_contents($base . '/info.log');
  74. $this->assertEqual($expected, $result);
  75. unlink($base . '/info.log');
  76. }
  77. public function testWriteWithInvalidPriority() {
  78. $this->expectException("Attempted to write log message with invalid priority `foo`.");
  79. Logger::foo("Test message");
  80. }
  81. public function testWriteByName() {
  82. $base = Libraries::get(true, 'resources') . '/tmp/logs';
  83. $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
  84. Logger::config(array('default' => array(
  85. 'adapter' => 'File',
  86. 'timestamp' => false,
  87. 'priority' => false,
  88. 'format' => "{:message}\n"
  89. )));
  90. $this->assertFalse(file_exists($base . '/info.log'));
  91. $this->assertFalse(Logger::write('info', 'Message line 1'));
  92. $this->assertFalse(file_exists($base . '/info.log'));
  93. $this->assertTrue(Logger::write(null, 'Message line 1', array('name' => 'default')));
  94. $expected = "Message line 1\n";
  95. $result = file_get_contents($base . '/.log');
  96. $this->assertEqual($expected, $result);
  97. unlink($base . '/.log');
  98. }
  99. public function testMultipleAdaptersWriteByNameDefault() {
  100. $base = Libraries::get(true, 'resources') . '/tmp/logs';
  101. $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
  102. Logger::config(array(
  103. 'default' => array(
  104. 'adapter' => 'File',
  105. 'file' => function($data, $config) { return "{$data['priority']}_default.log"; },
  106. 'timestamp' => false,
  107. 'format' => "{:message}\n"
  108. ),
  109. 'secondary' => array(
  110. 'adapter' => 'File',
  111. 'file' => function($data, $config) { return "{$data['priority']}_secondary.log"; },
  112. 'timestamp' => false,
  113. 'format' => "{:message}\n"
  114. ),
  115. ));
  116. $this->assertFalse(file_exists($base . '/info_default.log'));
  117. $this->assertTrue(Logger::write('info', 'Default Message line 1', array(
  118. 'name' => 'default'
  119. )));
  120. $this->assertTrue(file_exists($base . '/info_default.log'));
  121. $expected = "Default Message line 1\n";
  122. $result = file_get_contents($base . '/info_default.log');
  123. $this->assertEqual($expected, $result);
  124. unlink($base . '/info_default.log');
  125. }
  126. public function testMultipleAdaptersWriteByNameSecondary() {
  127. $base = Libraries::get(true, 'resources') . '/tmp/logs';
  128. $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
  129. Logger::config(array(
  130. 'default' => array(
  131. 'adapter' => 'File',
  132. 'file' => function($data, $config) { return "{$data['priority']}_default.log"; },
  133. 'timestamp' => false,
  134. 'format' => "{:message}\n"
  135. ),
  136. 'secondary' => array(
  137. 'adapter' => 'File',
  138. 'file' => function($data, $config) { return "{$data['priority']}_secondary.log"; },
  139. 'timestamp' => false,
  140. 'format' => "{:message}\n"
  141. ),
  142. ));
  143. $this->assertFalse(file_exists($base . '/info_secondary.log'));
  144. $this->assertTrue(Logger::write('info', 'Secondary Message line 1', array(
  145. 'name' => 'secondary'
  146. )));
  147. $this->assertTrue(file_exists($base . '/info_secondary.log'));
  148. $expected = "Secondary Message line 1\n";
  149. $result = file_get_contents($base . '/info_secondary.log');
  150. $this->assertEqual($expected, $result);
  151. unlink($base . '/info_secondary.log');
  152. }
  153. public function testRespondsToParentCall() {
  154. $this->assertTrue(Logger::respondsTo('applyFilter'));
  155. $this->assertFalse(Logger::respondsTo('fooBarBaz'));
  156. }
  157. public function testRespondsToMagic() {
  158. $this->assertTrue(Logger::respondsTo('emergency'));
  159. $this->assertTrue(Logger::respondsTo('debug'));
  160. $this->assertFalse(Logger::respondsTo('foobar'));
  161. }
  162. }
  163. ?>