PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/Log/test/Horde/Log/Handler/StreamTest.php

https://github.com/sgtcarneiro/horde
PHP | 125 lines | 88 code | 15 blank | 22 comment | 0 complexity | 7bc7c19858bbaf2d426b7dd7ab100a9c MD5 | raw file
  1. <?php
  2. /**
  3. * Horde Log package
  4. *
  5. * This package is based on Zend_Log from the Zend Framework
  6. * (http://framework.zend.com). Both that package and this
  7. * one were written by Mike Naberezny and Chuck Hagenbuch.
  8. *
  9. * @author Mike Naberezny <mike@maintainable.com>
  10. * @author Chuck Hagenbuch <chuck@horde.org>
  11. * @category Horde
  12. * @license http://opensource.org/licenses/bsd-license.php BSD
  13. * @package Log
  14. * @subpackage UnitTests
  15. */
  16. /**
  17. * @author Mike Naberezny <mike@maintainable.com>
  18. * @author Chuck Hagenbuch <chuck@horde.org>
  19. * @category Horde
  20. * @license http://opensource.org/licenses/bsd-license.php BSD
  21. * @package Log
  22. * @subpackage UnitTests
  23. */
  24. class Horde_Log_Handler_StreamTest extends PHPUnit_Framework_TestCase
  25. {
  26. public function setUp()
  27. {
  28. date_default_timezone_set('America/New_York');
  29. }
  30. public function testConstructorThrowsWhenResourceIsNotStream()
  31. {
  32. $resource = xml_parser_create();
  33. try {
  34. new Horde_Log_Handler_Stream($resource);
  35. $this->fail();
  36. } catch (Exception $e) {
  37. $this->assertInstanceOf('Horde_Log_Exception', $e);
  38. $this->assertRegExp('/not a stream/i', $e->getMessage());
  39. }
  40. xml_parser_free($resource);
  41. }
  42. public function testConstructorWithValidStream()
  43. {
  44. $stream = fopen('php://memory', 'a');
  45. new Horde_Log_Handler_Stream($stream);
  46. }
  47. public function testConstructorWithValidUrl()
  48. {
  49. new Horde_Log_Handler_Stream('php://memory');
  50. }
  51. public function testConstructorThrowsWhenModeSpecifiedForExistingStream()
  52. {
  53. $stream = fopen('php://memory', 'a');
  54. try {
  55. new Horde_Log_Handler_Stream($stream, 'w');
  56. $this->fail();
  57. } catch (Exception $e) {
  58. $this->assertInstanceOf('Horde_Log_Exception', $e);
  59. $this->assertRegExp('/existing stream/i', $e->getMessage());
  60. }
  61. }
  62. public function testConstructorThrowsWhenStreamCannotBeOpened()
  63. {
  64. try {
  65. new Horde_Log_Handler_Stream('');
  66. $this->fail();
  67. } catch (Exception $e) {
  68. $this->assertInstanceOf('Horde_Log_Exception', $e);
  69. $this->assertRegExp('/cannot be opened/i', $e->getMessage());
  70. }
  71. }
  72. public function testSettingBadOptionThrows()
  73. {
  74. try {
  75. $handler = new Horde_Log_Handler_Stream('php://memory');
  76. $handler->setOption('foo', 42);
  77. $this->fail();
  78. } catch (Exception $e) {
  79. $this->assertInstanceOf('Horde_Log_Exception', $e);
  80. $this->assertRegExp('/unknown option/i', $e->getMessage());
  81. }
  82. }
  83. public function testWrite()
  84. {
  85. $stream = fopen('php://memory', 'a');
  86. $handler = new Horde_Log_Handler_Stream($stream);
  87. $handler->write(array('message' => $message = 'message-to-log',
  88. 'level' => $level = Horde_Log::ALERT,
  89. 'levelName' => $levelName = 'ALERT',
  90. 'timestamp' => date('c')));
  91. rewind($stream);
  92. $contents = stream_get_contents($stream);
  93. fclose($stream);
  94. $date = '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}-\d{2}:\d{2}';
  95. $this->assertRegExp("/$date $levelName: $message/", $contents);
  96. }
  97. public function testWriteThrowsWhenStreamWriteFails()
  98. {
  99. $stream = fopen('php://memory', 'a');
  100. $handler = new Horde_Log_Handler_Stream($stream);
  101. fclose($stream);
  102. try {
  103. $handler->write(array('message' => 'foo', 'level' => 1));
  104. $this->fail();
  105. } catch (Exception $e) {
  106. $this->assertInstanceOf('Horde_Log_Exception', $e);
  107. $this->assertRegExp('/unable to write/i', $e->getMessage());
  108. }
  109. }
  110. }