ConsoleLogTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * ConsoleLogTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @since 1.3.0
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. namespace Cake\Test\TestCase\Log\Engine;
  19. use Cake\Console\ConsoleOutput;
  20. use Cake\Log\Engine\ConsoleLog;
  21. use Cake\Log\Log;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * ConsoleLogTest class
  25. *
  26. */
  27. class ConsoleLogTest extends TestCase {
  28. /**
  29. * Test writing to ConsoleOutput
  30. */
  31. public function testConsoleOutputWrites() {
  32. $output = $this->getMock('Cake\Console\ConsoleOutput');
  33. $output->expects($this->at(0))
  34. ->method('outputAs');
  35. $message = '<error>' . date('Y-m-d H:i:s') . " Error: oh noes\n</error>";
  36. $output->expects($this->at(1))
  37. ->method('write')
  38. ->with($message);
  39. $log = new ConsoleLog([
  40. 'stream' => $output
  41. ]);
  42. $log->write('error', 'oh noes');
  43. }
  44. public function testWriteToFileStream() {
  45. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  46. $log = new ConsoleLog([
  47. 'stream' => $filename
  48. ]);
  49. $log->write('error', 'oh noes');
  50. $fh = fopen($filename, 'r');
  51. $line = fgets($fh);
  52. $this->assertContains('Error: oh noes', $line);
  53. }
  54. /**
  55. * test default value of stream 'outputAs'
  56. */
  57. public function testDefaultOutputAs() {
  58. if (DS === '\\' && !(bool)env('ANSICON')) {
  59. $expected = ConsoleOutput::PLAIN;
  60. } else {
  61. $expected = ConsoleOutput::COLOR;
  62. }
  63. $output = $this->getMock('Cake\Console\ConsoleOutput');
  64. $output->expects($this->at(0))
  65. ->method('outputAs')
  66. ->with($expected);
  67. $log = new ConsoleLog([
  68. 'stream' => $output,
  69. ]);
  70. $config = $log->config();
  71. $this->assertEquals($expected, $config['outputAs']);
  72. }
  73. }