ConsoleLogTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(): void
  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. public function testLogToFileStream(): void
  44. {
  45. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  46. $log = new ConsoleLog([
  47. 'stream' => $filename,
  48. ]);
  49. $log->log('error', 'oh noes');
  50. $fh = fopen($filename, 'r');
  51. $line = fgets($fh);
  52. $this->assertStringContainsString('error: oh noes', $line);
  53. $this->assertMatchesRegularExpression('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ error: oh noes/', $line);
  54. }
  55. /**
  56. * test value of stream 'outputAs'
  57. */
  58. public function testDefaultOutputAs(): void
  59. {
  60. $output = $this->getMockBuilder(ConsoleOutput::class)->getMock();
  61. $output->expects($this->once())
  62. ->method('setOutputAs')
  63. ->with(ConsoleOutput::RAW);
  64. $log = new ConsoleLog([
  65. 'stream' => $output,
  66. 'outputAs' => ConsoleOutput::RAW,
  67. ]);
  68. $this->assertSame(ConsoleOutput::RAW, $log->getConfig('outputAs'));
  69. }
  70. /**
  71. * test dateFormat option
  72. */
  73. public function testDateFormat(): void
  74. {
  75. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  76. $log = new ConsoleLog([
  77. 'stream' => $filename,
  78. 'formatter.dateFormat' => 'c',
  79. ]);
  80. $log->log('error', 'oh noes');
  81. $fh = fopen($filename, 'r');
  82. $line = fgets($fh);
  83. $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);
  84. }
  85. /**
  86. * Test json formatter
  87. */
  88. public function testJsonFormatter(): void
  89. {
  90. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  91. $log = new ConsoleLog([
  92. 'stream' => $filename,
  93. 'formatter' => [
  94. 'className' => JsonFormatter::class,
  95. ],
  96. ]);
  97. $log->log('error', 'test with newline');
  98. $fh = fopen($filename, 'r');
  99. $line = fgets($fh);
  100. $this->assertSame(strlen($line) - 1, strpos($line, "\n"));
  101. $entry = json_decode($line, true);
  102. $this->assertNotNull($entry['date']);
  103. $this->assertSame('error', $entry['level']);
  104. $this->assertSame('test with newline', $entry['message']);
  105. }
  106. /**
  107. * Test json formatter custom flags
  108. */
  109. public function testJsonFormatterFlags(): void
  110. {
  111. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  112. $log = new ConsoleLog([
  113. 'stream' => $filename,
  114. 'formatter' => [
  115. 'className' => JsonFormatter::class,
  116. 'flags' => JSON_HEX_QUOT,
  117. ],
  118. ]);
  119. $log->log('error', 'oh "{p1}"', ['p1' => 'noes']);
  120. $fh = fopen($filename, 'r');
  121. $line = fgets($fh);
  122. $this->assertStringContainsString('\u0022noes\u0022', $line);
  123. $entry = json_decode($line, true);
  124. $this->assertSame('oh "noes"', $entry['message']);
  125. }
  126. }