ConsoleLogTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 1.3.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Log\Engine;
  15. use Cake\Console\ConsoleOutput;
  16. use Cake\Log\Engine\ConsoleLog;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * ConsoleLogTest class
  20. */
  21. class ConsoleLogTest extends TestCase
  22. {
  23. /**
  24. * Test writing to ConsoleOutput
  25. */
  26. public function testConsoleOutputlogs()
  27. {
  28. $output = $this->getMockBuilder('Cake\Console\ConsoleOutput')->getMock();
  29. $output->expects($this->at(0))
  30. ->method('outputAs');
  31. $message = ' Error: oh noes</error>';
  32. $output->expects($this->at(1))
  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->assertContains('Error: oh noes', $line);
  55. }
  56. /**
  57. * test default value of stream 'outputAs'
  58. */
  59. public function testDefaultOutputAs()
  60. {
  61. if ((DS === '\\' && !(bool)env('ANSICON') && env('ConEmuANSI') !== 'ON') ||
  62. (function_exists('posix_isatty') && !posix_isatty(null))
  63. ) {
  64. $expected = ConsoleOutput::PLAIN;
  65. } else {
  66. $expected = ConsoleOutput::COLOR;
  67. }
  68. $output = $this->getMockBuilder('Cake\Console\ConsoleOutput')->getMock();
  69. $output->expects($this->at(0))
  70. ->method('setOutputAs')
  71. ->with($expected);
  72. $log = new ConsoleLog([
  73. 'stream' => $output,
  74. ]);
  75. $config = $log->getConfig();
  76. $this->assertEquals($expected, $config['outputAs']);
  77. }
  78. }