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. 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. ]);
  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. */
  52. public function testWriteOneLine(string $type, int $expected): void
  53. {
  54. /** @var \Cake\Log\Engine\SyslogLog|\PHPUnit\Framework\MockObject\MockObject $log */
  55. $log = $this->getMockBuilder(SyslogLog::class)
  56. ->onlyMethods(['_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. public function testWriteMultiLine(): void
  65. {
  66. /** @var \Cake\Log\Engine\SyslogLog|\PHPUnit\Framework\MockObject\MockObject $log */
  67. $log = $this->getMockBuilder(SyslogLog::class)
  68. ->onlyMethods(['_open', '_write'])
  69. ->getMock();
  70. $log->expects($this->exactly(2))
  71. ->method('_write')
  72. ->with(
  73. ...self::withConsecutive(
  74. [LOG_DEBUG, 'debug: Foo'],
  75. [LOG_DEBUG, 'debug: Bar']
  76. )
  77. );
  78. $log->log('debug', "Foo\nBar");
  79. }
  80. /**
  81. * Data provider for the write function test
  82. *
  83. * @return array
  84. */
  85. public static function typesProvider(): array
  86. {
  87. return [
  88. ['emergency', LOG_EMERG],
  89. ['alert', LOG_ALERT],
  90. ['critical', LOG_CRIT],
  91. ['error', LOG_ERR],
  92. ['warning', LOG_WARNING],
  93. ['notice', LOG_NOTICE],
  94. ['info', LOG_INFO],
  95. ['debug', LOG_DEBUG],
  96. ];
  97. }
  98. }