ConsoleLogTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 1.3.0
  12. * @license http://www.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\Log\Log;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * ConsoleLogTest class
  21. *
  22. */
  23. class ConsoleLogTest extends TestCase
  24. {
  25. /**
  26. * Test writing to ConsoleOutput
  27. */
  28. public function testConsoleOutputlogs()
  29. {
  30. $output = $this->getMock('Cake\Console\ConsoleOutput');
  31. $output->expects($this->at(0))
  32. ->method('outputAs');
  33. $message = " Error: oh noes</error>";
  34. $output->expects($this->at(1))
  35. ->method('write')
  36. ->with($this->stringContains($message));
  37. $log = new ConsoleLog([
  38. 'stream' => $output
  39. ]);
  40. $log->log('error', 'oh noes');
  41. }
  42. /**
  43. * Test writing to a file stream
  44. *
  45. * @return void
  46. */
  47. public function testlogToFileStream()
  48. {
  49. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  50. $log = new ConsoleLog([
  51. 'stream' => $filename
  52. ]);
  53. $log->log('error', 'oh noes');
  54. $fh = fopen($filename, 'r');
  55. $line = fgets($fh);
  56. $this->assertContains('Error: oh noes', $line);
  57. }
  58. /**
  59. * test default value of stream 'outputAs'
  60. */
  61. public function testDefaultOutputAs()
  62. {
  63. if ((DS === '\\' && !(bool)env('ANSICON') && env('ConEmuANSI') !== 'ON') ||
  64. (function_exists('posix_isatty') && !posix_isatty(null))
  65. ) {
  66. $expected = ConsoleOutput::PLAIN;
  67. } else {
  68. $expected = ConsoleOutput::COLOR;
  69. }
  70. $output = $this->getMock('Cake\Console\ConsoleOutput');
  71. $output->expects($this->at(0))
  72. ->method('outputAs')
  73. ->with($expected);
  74. $log = new ConsoleLog([
  75. 'stream' => $output,
  76. ]);
  77. $config = $log->config();
  78. $this->assertEquals($expected, $config['outputAs']);
  79. }
  80. }