ConsoleLogTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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\n</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 (
  64. (DS === '\\' && !(bool)env('ANSICON')) ||
  65. (function_exists('posix_isatty') && !posix_isatty(null))
  66. ) {
  67. $expected = ConsoleOutput::PLAIN;
  68. } else {
  69. $expected = ConsoleOutput::COLOR;
  70. }
  71. $output = $this->getMock('Cake\Console\ConsoleOutput');
  72. $output->expects($this->at(0))
  73. ->method('outputAs')
  74. ->with($expected);
  75. $log = new ConsoleLog([
  76. 'stream' => $output,
  77. ]);
  78. $config = $log->config();
  79. $this->assertEquals($expected, $config['outputAs']);
  80. }
  81. }