SyslogLogTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.4.0
  13. * @license http://www.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->getMock('Cake\Log\Engine\SyslogLog', ['_open', '_write']);
  30. $log->expects($this->once())->method('_open')->with('', LOG_ODELAY, LOG_USER);
  31. $log->log('debug', 'message');
  32. $log = $this->getMock('Cake\Log\Engine\SyslogLog', ['_open', '_write']);
  33. $log->config([
  34. 'prefix' => 'thing',
  35. 'flag' => LOG_NDELAY,
  36. 'facility' => LOG_MAIL,
  37. 'format' => '%s: %s'
  38. ]);
  39. $log->expects($this->once())->method('_open')
  40. ->with('thing', LOG_NDELAY, LOG_MAIL);
  41. $log->log('debug', 'message');
  42. }
  43. /**
  44. * Tests that single lines are written to syslog
  45. *
  46. * @dataProvider typesProvider
  47. * @return void
  48. */
  49. public function testWriteOneLine($type, $expected)
  50. {
  51. $log = $this->getMock('Cake\Log\Engine\SyslogLog', ['_open', '_write']);
  52. $log->expects($this->once())->method('_write')->with($expected, $type . ': Foo');
  53. $log->log($type, 'Foo');
  54. }
  55. /**
  56. * Tests that multiple lines are split and logged separately
  57. *
  58. * @return void
  59. */
  60. public function testWriteMultiLine()
  61. {
  62. $log = $this->getMock('Cake\Log\Engine\SyslogLog', ['_open', '_write']);
  63. $log->expects($this->at(1))->method('_write')->with(LOG_DEBUG, 'debug: Foo');
  64. $log->expects($this->at(2))->method('_write')->with(LOG_DEBUG, 'debug: Bar');
  65. $log->expects($this->exactly(2))->method('_write');
  66. $log->log('debug', "Foo\nBar");
  67. }
  68. /**
  69. * Data provider for the write function test
  70. *
  71. * @return array
  72. */
  73. public function typesProvider()
  74. {
  75. return [
  76. ['emergency', LOG_EMERG],
  77. ['alert', LOG_ALERT],
  78. ['critical', LOG_CRIT],
  79. ['error', LOG_ERR],
  80. ['warning', LOG_WARNING],
  81. ['notice', LOG_NOTICE],
  82. ['info', LOG_INFO],
  83. ['debug', LOG_DEBUG]
  84. ];
  85. }
  86. }