SyslogLogTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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. * Tests that the connection to the logger is open with the right arguments
  23. *
  24. * @return void
  25. */
  26. public function testOpenLog() {
  27. $log = $this->getMock('Cake\Log\Engine\SyslogLog', array('_open', '_write'));
  28. $log->expects($this->once())->method('_open')->with('', LOG_ODELAY, LOG_USER);
  29. $log->log('debug', 'message');
  30. $log = $this->getMock('Cake\Log\Engine\SyslogLog', array('_open', '_write'));
  31. $log->config(array(
  32. 'prefix' => 'thing',
  33. 'flag' => LOG_NDELAY,
  34. 'facility' => LOG_MAIL,
  35. 'format' => '%s: %s'
  36. ));
  37. $log->expects($this->once())->method('_open')
  38. ->with('thing', LOG_NDELAY, LOG_MAIL);
  39. $log->log('debug', 'message');
  40. }
  41. /**
  42. * Tests that single lines are written to syslog
  43. *
  44. * @dataProvider typesProvider
  45. * @return void
  46. */
  47. public function testWriteOneLine($type, $expected) {
  48. $log = $this->getMock('Cake\Log\Engine\SyslogLog', array('_open', '_write'));
  49. $log->expects($this->once())->method('_write')->with($expected, $type . ': Foo');
  50. $log->log($type, 'Foo');
  51. }
  52. /**
  53. * Tests that multiple lines are split and logged separately
  54. *
  55. * @return void
  56. */
  57. public function testWriteMultiLine() {
  58. $log = $this->getMock('Cake\Log\Engine\SyslogLog', array('_open', '_write'));
  59. $log->expects($this->at(1))->method('_write')->with(LOG_DEBUG, 'debug: Foo');
  60. $log->expects($this->at(2))->method('_write')->with(LOG_DEBUG, 'debug: Bar');
  61. $log->expects($this->exactly(2))->method('_write');
  62. $log->log('debug', "Foo\nBar");
  63. }
  64. /**
  65. * Data provider for the write function test
  66. *
  67. * @return array
  68. */
  69. public function typesProvider() {
  70. return array(
  71. array('emergency', LOG_EMERG),
  72. array('alert', LOG_ALERT),
  73. array('critical', LOG_CRIT),
  74. array('error', LOG_ERR),
  75. array('warning', LOG_WARNING),
  76. array('notice', LOG_NOTICE),
  77. array('info', LOG_INFO),
  78. array('debug', LOG_DEBUG)
  79. );
  80. }
  81. }