SyslogLogTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. public function testOpenLog(): void
  28. {
  29. /** @var \Cake\Log\Engine\SyslogLog|\PHPUnit\Framework\MockObject\MockObject $log */
  30. $log = $this->getMockBuilder(SyslogLog::class)
  31. ->onlyMethods(['_open', '_write'])
  32. ->getMock();
  33. $log->expects($this->once())->method('_open')->with('', LOG_ODELAY, LOG_USER);
  34. $log->log('debug', 'message');
  35. $log = $this->getMockBuilder(SyslogLog::class)
  36. ->onlyMethods(['_open', '_write'])
  37. ->getMock();
  38. $log->setConfig([
  39. 'prefix' => 'thing',
  40. 'flag' => LOG_NDELAY,
  41. 'facility' => LOG_MAIL,
  42. 'format' => '%s: %s',
  43. ]);
  44. $log->expects($this->once())->method('_open')
  45. ->with('thing', LOG_NDELAY, LOG_MAIL);
  46. $log->log('debug', 'message');
  47. }
  48. /**
  49. * Tests that single lines are written to syslog
  50. *
  51. * @dataProvider typesProvider
  52. */
  53. public function testWriteOneLine(string $type, int $expected): void
  54. {
  55. /** @var \Cake\Log\Engine\SyslogLog|\PHPUnit\Framework\MockObject\MockObject $log */
  56. $log = $this->getMockBuilder(SyslogLog::class)
  57. ->onlyMethods(['_open', '_write'])
  58. ->getMock();
  59. $log->expects($this->once())->method('_write')->with($expected, $type . ': Foo');
  60. $log->log($type, 'Foo');
  61. }
  62. /**
  63. * Tests that multiple lines are split and logged separately
  64. */
  65. public function testWriteMultiLine(): void
  66. {
  67. /** @var \Cake\Log\Engine\SyslogLog|\PHPUnit\Framework\MockObject\MockObject $log */
  68. $log = $this->getMockBuilder(SyslogLog::class)
  69. ->onlyMethods(['_open', '_write'])
  70. ->getMock();
  71. $log->expects($this->exactly(2))
  72. ->method('_write')
  73. ->withConsecutive(
  74. [LOG_DEBUG, 'debug: Foo'],
  75. [LOG_DEBUG, 'debug: Bar']
  76. );
  77. $log->log('debug', "Foo\nBar");
  78. }
  79. /**
  80. * Test deprecated format option
  81. */
  82. public function testDeprecatedFormat(): void
  83. {
  84. $this->deprecated(function (): void {
  85. $log = $this->getMockBuilder(SyslogLog::class)
  86. ->setConstructorArgs(['config' => ['format' => 'custom %s: %s']])
  87. ->onlyMethods(['_open', '_write'])
  88. ->getMock();
  89. $log->expects($this->exactly(2))
  90. ->method('_write')
  91. ->withConsecutive(
  92. [LOG_DEBUG, 'custom debug: Foo'],
  93. [LOG_DEBUG, 'custom debug: Bar']
  94. );
  95. $log->log('debug', "Foo\nBar");
  96. });
  97. }
  98. /**
  99. * Test deprecated format option
  100. */
  101. public function testDeprecatedFormatMessage(): void
  102. {
  103. $this->expectDeprecation();
  104. $this->expectDeprecationMessage('`format` option is now deprecated in favor of custom formatters');
  105. $this->expectDeprecationMessage('SyslogLog.php');
  106. new SyslogLog(['format' => 'custom %s: %s']);
  107. }
  108. /**
  109. * Data provider for the write function test
  110. *
  111. * @return array
  112. */
  113. public function typesProvider(): array
  114. {
  115. return [
  116. ['emergency', LOG_EMERG],
  117. ['alert', LOG_ALERT],
  118. ['critical', LOG_CRIT],
  119. ['error', LOG_ERR],
  120. ['warning', LOG_WARNING],
  121. ['notice', LOG_NOTICE],
  122. ['info', LOG_INFO],
  123. ['debug', LOG_DEBUG],
  124. ];
  125. }
  126. }