ConsoleLogTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\TestSuite\TestCase;
  19. /**
  20. * ConsoleLogTest class
  21. */
  22. class ConsoleLogTest extends TestCase
  23. {
  24. /**
  25. * Test writing to ConsoleOutput
  26. */
  27. public function testConsoleOutputlogs()
  28. {
  29. $output = $this->getMockBuilder('Cake\Console\ConsoleOutput')->getMock();
  30. $message = ' Error: oh noes</error>';
  31. $output->expects($this->at(0))
  32. ->method('write')
  33. ->with($this->stringContains($message));
  34. $log = new ConsoleLog([
  35. 'stream' => $output,
  36. ]);
  37. $log->log('error', 'oh noes');
  38. }
  39. /**
  40. * Test writing to a file stream
  41. *
  42. * @return void
  43. */
  44. public function testlogToFileStream()
  45. {
  46. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  47. $log = new ConsoleLog([
  48. 'stream' => $filename,
  49. ]);
  50. $log->log('error', 'oh noes');
  51. $fh = fopen($filename, 'r');
  52. $line = fgets($fh);
  53. $this->assertStringContainsString('Error: oh noes', $line);
  54. $this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: oh noes/', $line);
  55. }
  56. /**
  57. * test value of stream 'outputAs'
  58. */
  59. public function testDefaultOutputAs()
  60. {
  61. $output = $this->getMockBuilder(ConsoleOutput::class)->getMock();
  62. $output->expects($this->at(0))
  63. ->method('setOutputAs')
  64. ->with(ConsoleOutput::RAW);
  65. $log = new ConsoleLog([
  66. 'stream' => $output,
  67. 'outputAs' => ConsoleOutput::RAW,
  68. ]);
  69. $this->assertEquals(ConsoleOutput::RAW, $log->getConfig('outputAs'));
  70. }
  71. /**
  72. * test dateFormat option
  73. *
  74. * @return void
  75. */
  76. public function testDateFormat()
  77. {
  78. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  79. $log = new ConsoleLog([
  80. 'stream' => $filename,
  81. 'dateFormat' => 'c',
  82. ]);
  83. $log->log('error', 'oh noes');
  84. $fh = fopen($filename, 'r');
  85. $line = fgets($fh);
  86. $this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+\+\d{2}:\d{2} Error: oh noes/', $line);
  87. }
  88. }