ConsoleLogTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 1.3.0
  12. * @license https://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. class ConsoleLogTest extends TestCase
  22. {
  23. /**
  24. * Test writing to ConsoleOutput
  25. */
  26. public function testConsoleOutputlogs()
  27. {
  28. $output = $this->getMockBuilder('Cake\Console\ConsoleOutput')->getMock();
  29. $message = ' Error: oh noes</error>';
  30. $output->expects($this->at(0))
  31. ->method('write')
  32. ->with($this->stringContains($message));
  33. $log = new ConsoleLog([
  34. 'stream' => $output,
  35. ]);
  36. $log->log('error', 'oh noes');
  37. }
  38. /**
  39. * Test writing to a file stream
  40. *
  41. * @return void
  42. */
  43. public function testlogToFileStream()
  44. {
  45. $filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
  46. $log = new ConsoleLog([
  47. 'stream' => $filename,
  48. ]);
  49. $log->log('error', 'oh noes');
  50. $fh = fopen($filename, 'r');
  51. $line = fgets($fh);
  52. $this->assertContains('Error: oh noes', $line);
  53. }
  54. /**
  55. * test value of stream 'outputAs'
  56. */
  57. public function testDefaultOutputAs()
  58. {
  59. $output = $this->getMockBuilder(ConsoleOutput::class)->getMock();
  60. $output->expects($this->at(0))
  61. ->method('setOutputAs')
  62. ->with(ConsoleOutput::RAW);
  63. $log = new ConsoleLog([
  64. 'stream' => $output,
  65. 'outputAs' => ConsoleOutput::RAW,
  66. ]);
  67. $this->assertEquals(ConsoleOutput::RAW, $log->getConfig('outputAs'));
  68. }
  69. }