SyslogLogTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.4.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Log\Engine;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * SyslogLogTest class
  19. */
  20. class SyslogLogTest extends TestCase
  21. {
  22. /**
  23. * Tests that the connection to the logger is open with the right arguments
  24. *
  25. * @return void
  26. */
  27. public function testOpenLog()
  28. {
  29. $log = $this->getMockBuilder('Cake\Log\Engine\SyslogLog')
  30. ->setMethods(['_open', '_write'])
  31. ->getMock();
  32. $log->expects($this->once())->method('_open')->with('', LOG_ODELAY, LOG_USER);
  33. $log->log('debug', 'message');
  34. $log = $this->getMockBuilder('Cake\Log\Engine\SyslogLog')
  35. ->setMethods(['_open', '_write'])
  36. ->getMock();
  37. $log->setConfig([
  38. 'prefix' => 'thing',
  39. 'flag' => LOG_NDELAY,
  40. 'facility' => LOG_MAIL,
  41. 'format' => '%s: %s',
  42. ]);
  43. $log->expects($this->once())->method('_open')
  44. ->with('thing', LOG_NDELAY, LOG_MAIL);
  45. $log->log('debug', 'message');
  46. }
  47. /**
  48. * Tests that single lines are written to syslog
  49. *
  50. * @dataProvider typesProvider
  51. * @return void
  52. */
  53. public function testWriteOneLine($type, $expected)
  54. {
  55. $log = $this->getMockBuilder('Cake\Log\Engine\SyslogLog')
  56. ->setMethods(['_open', '_write'])
  57. ->getMock();
  58. $log->expects($this->once())->method('_write')->with($expected, $type . ': Foo');
  59. $log->log($type, 'Foo');
  60. }
  61. /**
  62. * Tests that multiple lines are split and logged separately
  63. *
  64. * @return void
  65. */
  66. public function testWriteMultiLine()
  67. {
  68. $log = $this->getMockBuilder('Cake\Log\Engine\SyslogLog')
  69. ->setMethods(['_open', '_write'])
  70. ->getMock();
  71. $log->expects($this->at(1))->method('_write')->with(LOG_DEBUG, 'debug: Foo');
  72. $log->expects($this->at(2))->method('_write')->with(LOG_DEBUG, 'debug: Bar');
  73. $log->expects($this->exactly(2))->method('_write');
  74. $log->log('debug', "Foo\nBar");
  75. }
  76. /**
  77. * Data provider for the write function test
  78. *
  79. * @return array
  80. */
  81. public function typesProvider()
  82. {
  83. return [
  84. ['emergency', LOG_EMERG],
  85. ['alert', LOG_ALERT],
  86. ['critical', LOG_CRIT],
  87. ['error', LOG_ERR],
  88. ['warning', LOG_WARNING],
  89. ['notice', LOG_NOTICE],
  90. ['info', LOG_INFO],
  91. ['debug', LOG_DEBUG],
  92. ];
  93. }
  94. }