ConsoleLogTest.php 2.4 KB

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