ConsoleLogTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  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. * Test writing to ConsoleOutput
  26. */
  27. public function testConsoleOutputWrites() {
  28. $output = $this->getMock('Cake\Console\ConsoleOutput');
  29. $output->expects($this->at(0))
  30. ->method('outputAs');
  31. $message = '<error>' . date('Y-m-d H:i:s') . " Error: oh noes\n</error>";
  32. $output->expects($this->at(1))
  33. ->method('write')
  34. ->with($message);
  35. $log = new ConsoleLog([
  36. 'stream' => $output
  37. ]);
  38. $log->write('error', 'oh noes');
  39. }
  40. public function testWriteToFileStream() {
  41. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  42. $log = new ConsoleLog([
  43. 'stream' => $filename
  44. ]);
  45. $log->write('error', 'oh noes');
  46. $fh = fopen($filename, 'r');
  47. $line = fgets($fh);
  48. $this->assertContains('Error: oh noes', $line);
  49. }
  50. /**
  51. * test default value of stream 'outputAs'
  52. */
  53. public function testDefaultOutputAs() {
  54. if (DS === '\\' && !(bool)env('ANSICON')) {
  55. $expected = ConsoleOutput::PLAIN;
  56. } else {
  57. $expected = ConsoleOutput::COLOR;
  58. }
  59. $output = $this->getMock('Cake\Console\ConsoleOutput');
  60. $output->expects($this->at(0))
  61. ->method('outputAs')
  62. ->with($expected);
  63. $log = new ConsoleLog([
  64. 'stream' => $output,
  65. ]);
  66. $config = $log->config();
  67. $this->assertEquals($expected, $config['outputAs']);
  68. }
  69. }