ConsoleLogTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.3.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Log\Engine;
  16. use Cake\Console\ConsoleOutput;
  17. use Cake\Log\Engine\ConsoleLog;
  18. use Cake\Log\Formatter\JsonFormatter;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * ConsoleLogTest class
  22. */
  23. class ConsoleLogTest extends TestCase
  24. {
  25. /**
  26. * Test writing to ConsoleOutput
  27. */
  28. public function testConsoleOutputlogs()
  29. {
  30. $output = $this->getMockBuilder('Cake\Console\ConsoleOutput')->getMock();
  31. $message = ' Error: oh noes</error>';
  32. $output->expects($this->once())
  33. ->method('write')
  34. ->with($this->stringContains($message));
  35. $log = new ConsoleLog([
  36. 'stream' => $output,
  37. ]);
  38. $log->log('error', 'oh noes');
  39. }
  40. /**
  41. * Test writing to a file stream
  42. *
  43. * @return void
  44. */
  45. public function testlogToFileStream()
  46. {
  47. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  48. $log = new ConsoleLog([
  49. 'stream' => $filename,
  50. ]);
  51. $log->log('error', 'oh noes');
  52. $fh = fopen($filename, 'r');
  53. $line = fgets($fh);
  54. $this->assertStringContainsString('Error: oh noes', $line);
  55. $this->assertMatchesRegularExpression('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: oh noes/', $line);
  56. }
  57. /**
  58. * test value of stream 'outputAs'
  59. */
  60. public function testDefaultOutputAs()
  61. {
  62. $output = $this->getMockBuilder(ConsoleOutput::class)->getMock();
  63. $output->expects($this->once())
  64. ->method('setOutputAs')
  65. ->with(ConsoleOutput::RAW);
  66. $log = new ConsoleLog([
  67. 'stream' => $output,
  68. 'outputAs' => ConsoleOutput::RAW,
  69. ]);
  70. $this->assertSame(ConsoleOutput::RAW, $log->getConfig('outputAs'));
  71. }
  72. /**
  73. * test dateFormat option
  74. *
  75. * @return void
  76. */
  77. public function testDateFormat()
  78. {
  79. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  80. $log = new ConsoleLog([
  81. 'stream' => $filename,
  82. 'formatter.dateFormat' => 'c',
  83. ]);
  84. $log->log('error', 'oh noes');
  85. $fh = fopen($filename, 'r');
  86. $line = fgets($fh);
  87. $this->assertMatchesRegularExpression('/2[0-9]{3}-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+\+\d{2}:\d{2} Error: oh noes/', $line);
  88. }
  89. /**
  90. * Test json formatter
  91. *
  92. * @return void
  93. */
  94. public function testJsonFormatter()
  95. {
  96. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  97. $log = new ConsoleLog([
  98. 'stream' => $filename,
  99. 'formatter' => [
  100. 'className' => JsonFormatter::class,
  101. 'flags' => JSON_HEX_QUOT,
  102. ],
  103. ]);
  104. $log->log('error', 'oh "{p1}"', ['p1' => 'noes']);
  105. $fh = fopen($filename, 'r');
  106. $line = fgets($fh);
  107. $this->assertStringContainsString('\u0022noes\u0022', $line);
  108. $entry = json_decode($line, true);
  109. $this->assertNotNull($entry['date']);
  110. $this->assertSame('error', $entry['level']);
  111. $this->assertSame('oh "noes"', $entry['message']);
  112. }
  113. /**
  114. * Test deprecated dateFormat option
  115. *
  116. * @return void
  117. */
  118. public function testDeprecatedDateFormat()
  119. {
  120. $this->deprecated(function () {
  121. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  122. $log = new ConsoleLog([
  123. 'stream' => $filename,
  124. 'dateFormat' => 'c',
  125. ]);
  126. $log->log('error', 'oh noes');
  127. $fh = fopen($filename, 'r');
  128. $line = fgets($fh);
  129. $this->assertMatchesRegularExpression('/2[0-9]{3}-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+\+\d{2}:\d{2} Error: oh noes/', $line);
  130. });
  131. }
  132. }