SyslogLogTest.php 2.6 KB

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