/vendor/monolog/monolog/tests/Monolog/Handler/StreamHandlerTest.php

https://gitlab.com/techniconline/kmc · PHP · 168 lines · 90 code · 16 blank · 62 comment · 2 complexity · e36ff80dd301095b5d7853218ba36046 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\TestCase;
  12. use Monolog\Logger;
  13. class StreamHandlerTest extends TestCase
  14. {
  15. /**
  16. * @covers Monolog\Handler\StreamHandler::__construct
  17. * @covers Monolog\Handler\StreamHandler::write
  18. */
  19. public function testWrite()
  20. {
  21. $handle = fopen('php://memory', 'a+');
  22. $handler = new StreamHandler($handle);
  23. $handler->setFormatter($this->getIdentityFormatter());
  24. $handler->handle($this->getRecord(Logger::WARNING, 'test'));
  25. $handler->handle($this->getRecord(Logger::WARNING, 'test2'));
  26. $handler->handle($this->getRecord(Logger::WARNING, 'test3'));
  27. fseek($handle, 0);
  28. $this->assertEquals('testtest2test3', fread($handle, 100));
  29. }
  30. /**
  31. * @covers Monolog\Handler\StreamHandler::close
  32. */
  33. public function testClose()
  34. {
  35. $handle = fopen('php://memory', 'a+');
  36. $handler = new StreamHandler($handle);
  37. $this->assertTrue(is_resource($handle));
  38. $handler->close();
  39. $this->assertFalse(is_resource($handle));
  40. }
  41. /**
  42. * @covers Monolog\Handler\StreamHandler::write
  43. */
  44. public function testWriteCreatesTheStreamResource()
  45. {
  46. $handler = new StreamHandler('php://memory');
  47. $handler->handle($this->getRecord());
  48. }
  49. /**
  50. * @covers Monolog\Handler\StreamHandler::__construct
  51. * @covers Monolog\Handler\StreamHandler::write
  52. */
  53. public function testWriteLocking()
  54. {
  55. $temp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'monolog_locked_log';
  56. $handler = new StreamHandler($temp, Logger::DEBUG, true, null, true);
  57. $handler->handle($this->getRecord());
  58. }
  59. /**
  60. * @expectedException LogicException
  61. * @covers Monolog\Handler\StreamHandler::__construct
  62. * @covers Monolog\Handler\StreamHandler::write
  63. */
  64. public function testWriteMissingResource()
  65. {
  66. $handler = new StreamHandler(null);
  67. $handler->handle($this->getRecord());
  68. }
  69. public function invalidArgumentProvider()
  70. {
  71. return array(
  72. array(1),
  73. array(array()),
  74. array(array('bogus://url')),
  75. );
  76. }
  77. /**
  78. * @dataProvider invalidArgumentProvider
  79. * @expectedException InvalidArgumentException
  80. * @covers Monolog\Handler\StreamHandler::__construct
  81. */
  82. public function testWriteInvalidArgument($invalidArgument)
  83. {
  84. $handler = new StreamHandler($invalidArgument);
  85. }
  86. /**
  87. * @expectedException UnexpectedValueException
  88. * @covers Monolog\Handler\StreamHandler::__construct
  89. * @covers Monolog\Handler\StreamHandler::write
  90. */
  91. public function testWriteInvalidResource()
  92. {
  93. $handler = new StreamHandler('bogus://url');
  94. $handler->handle($this->getRecord());
  95. }
  96. /**
  97. * @expectedException UnexpectedValueException
  98. * @covers Monolog\Handler\StreamHandler::__construct
  99. * @covers Monolog\Handler\StreamHandler::write
  100. */
  101. public function testWriteNonExistingResource()
  102. {
  103. $handler = new StreamHandler('ftp://foo/bar/baz/' . rand(0, 10000));
  104. $handler->handle($this->getRecord());
  105. }
  106. /**
  107. * @covers Monolog\Handler\StreamHandler::__construct
  108. * @covers Monolog\Handler\StreamHandler::write
  109. */
  110. public function testWriteNonExistingPath()
  111. {
  112. $handler = new StreamHandler(sys_get_temp_dir() . '/bar/' . rand(0, 10000) . DIRECTORY_SEPARATOR . rand(0, 10000));
  113. $handler->handle($this->getRecord());
  114. }
  115. /**
  116. * @covers Monolog\Handler\StreamHandler::__construct
  117. * @covers Monolog\Handler\StreamHandler::write
  118. */
  119. public function testWriteNonExistingFileResource()
  120. {
  121. $handler = new StreamHandler('file://' . sys_get_temp_dir() . '/bar/' . rand(0, 10000) . DIRECTORY_SEPARATOR . rand(0, 10000));
  122. $handler->handle($this->getRecord());
  123. }
  124. /**
  125. * @expectedException Exception
  126. * @expectedExceptionMessageRegExp /There is no existing directory at/
  127. * @covers Monolog\Handler\StreamHandler::__construct
  128. * @covers Monolog\Handler\StreamHandler::write
  129. */
  130. public function testWriteNonExistingAndNotCreatablePath()
  131. {
  132. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  133. $this->markTestSkipped('Permissions checks can not run on windows');
  134. }
  135. $handler = new StreamHandler('/foo/bar/' . rand(0, 10000) . DIRECTORY_SEPARATOR . rand(0, 10000));
  136. $handler->handle($this->getRecord());
  137. }
  138. /**
  139. * @expectedException Exception
  140. * @expectedExceptionMessageRegExp /There is no existing directory at/
  141. * @covers Monolog\Handler\StreamHandler::__construct
  142. * @covers Monolog\Handler\StreamHandler::write
  143. */
  144. public function testWriteNonExistingAndNotCreatableFileResource()
  145. {
  146. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  147. $this->markTestSkipped('Permissions checks can not run on windows');
  148. }
  149. $handler = new StreamHandler('file:///foo/bar/' . rand(0, 10000) . DIRECTORY_SEPARATOR . rand(0, 10000));
  150. $handler->handle($this->getRecord());
  151. }
  152. }