ConsoleLogTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * 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: oh noes\n</error>";
  32. $output->expects($this->at(1))
  33. ->method('write')
  34. ->with($this->stringContains($message));
  35. $log = new ConsoleLog([
  36. 'stream' => $output
  37. ]);
  38. $log->write('error', 'oh noes');
  39. }
  40. /**
  41. * Test writing to a file stream
  42. *
  43. * @return void
  44. */
  45. public function testWriteToFileStream() {
  46. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  47. $log = new ConsoleLog([
  48. 'stream' => $filename
  49. ]);
  50. $log->write('error', 'oh noes');
  51. $fh = fopen($filename, 'r');
  52. $line = fgets($fh);
  53. $this->assertContains('Error: oh noes', $line);
  54. }
  55. /**
  56. * test default value of stream 'outputAs'
  57. */
  58. public function testDefaultOutputAs() {
  59. if (DS === '\\' && !(bool)env('ANSICON')) {
  60. $expected = ConsoleOutput::PLAIN;
  61. } else {
  62. $expected = ConsoleOutput::COLOR;
  63. }
  64. $output = $this->getMock('Cake\Console\ConsoleOutput');
  65. $output->expects($this->at(0))
  66. ->method('outputAs')
  67. ->with($expected);
  68. $log = new ConsoleLog([
  69. 'stream' => $output,
  70. ]);
  71. $config = $log->config();
  72. $this->assertEquals($expected, $config['outputAs']);
  73. }
  74. }