SyslogLogTest.php 3.3 KB

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