PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/ealexis.t/trends
PHP | 184 lines | 101 code | 18 blank | 65 comment | 2 complexity | bb191bfb22bf31c5ed5eea712a3919ef 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 testCloseKeepsExternalHandlersOpen()
  34. {
  35. $handle = fopen('php://memory', 'a+');
  36. $handler = new StreamHandler($handle);
  37. $this->assertTrue(is_resource($handle));
  38. $handler->close();
  39. $this->assertTrue(is_resource($handle));
  40. }
  41. /**
  42. * @covers Monolog\Handler\StreamHandler::close
  43. */
  44. public function testClose()
  45. {
  46. $handler = new StreamHandler('php://memory');
  47. $handler->handle($this->getRecord(Logger::WARNING, 'test'));
  48. $streamProp = new \ReflectionProperty('Monolog\Handler\StreamHandler', 'stream');
  49. $streamProp->setAccessible(true);
  50. $handle = $streamProp->getValue($handler);
  51. $this->assertTrue(is_resource($handle));
  52. $handler->close();
  53. $this->assertFalse(is_resource($handle));
  54. }
  55. /**
  56. * @covers Monolog\Handler\StreamHandler::write
  57. */
  58. public function testWriteCreatesTheStreamResource()
  59. {
  60. $handler = new StreamHandler('php://memory');
  61. $handler->handle($this->getRecord());
  62. }
  63. /**
  64. * @covers Monolog\Handler\StreamHandler::__construct
  65. * @covers Monolog\Handler\StreamHandler::write
  66. */
  67. public function testWriteLocking()
  68. {
  69. $temp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'monolog_locked_log';
  70. $handler = new StreamHandler($temp, Logger::DEBUG, true, null, true);
  71. $handler->handle($this->getRecord());
  72. }
  73. /**
  74. * @expectedException LogicException
  75. * @covers Monolog\Handler\StreamHandler::__construct
  76. * @covers Monolog\Handler\StreamHandler::write
  77. */
  78. public function testWriteMissingResource()
  79. {
  80. $handler = new StreamHandler(null);
  81. $handler->handle($this->getRecord());
  82. }
  83. public function invalidArgumentProvider()
  84. {
  85. return array(
  86. array(1),
  87. array(array()),
  88. array(array('bogus://url')),
  89. );
  90. }
  91. /**
  92. * @dataProvider invalidArgumentProvider
  93. * @expectedException InvalidArgumentException
  94. * @covers Monolog\Handler\StreamHandler::__construct
  95. */
  96. public function testWriteInvalidArgument($invalidArgument)
  97. {
  98. $handler = new StreamHandler($invalidArgument);
  99. }
  100. /**
  101. * @expectedException UnexpectedValueException
  102. * @covers Monolog\Handler\StreamHandler::__construct
  103. * @covers Monolog\Handler\StreamHandler::write
  104. */
  105. public function testWriteInvalidResource()
  106. {
  107. $handler = new StreamHandler('bogus://url');
  108. $handler->handle($this->getRecord());
  109. }
  110. /**
  111. * @expectedException UnexpectedValueException
  112. * @covers Monolog\Handler\StreamHandler::__construct
  113. * @covers Monolog\Handler\StreamHandler::write
  114. */
  115. public function testWriteNonExistingResource()
  116. {
  117. $handler = new StreamHandler('ftp://foo/bar/baz/'.rand(0, 10000));
  118. $handler->handle($this->getRecord());
  119. }
  120. /**
  121. * @covers Monolog\Handler\StreamHandler::__construct
  122. * @covers Monolog\Handler\StreamHandler::write
  123. */
  124. public function testWriteNonExistingPath()
  125. {
  126. $handler = new StreamHandler(sys_get_temp_dir().'/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
  127. $handler->handle($this->getRecord());
  128. }
  129. /**
  130. * @covers Monolog\Handler\StreamHandler::__construct
  131. * @covers Monolog\Handler\StreamHandler::write
  132. */
  133. public function testWriteNonExistingFileResource()
  134. {
  135. $handler = new StreamHandler('file://'.sys_get_temp_dir().'/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 testWriteNonExistingAndNotCreatablePath()
  145. {
  146. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  147. $this->markTestSkipped('Permissions checks can not run on windows');
  148. }
  149. $handler = new StreamHandler('/foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
  150. $handler->handle($this->getRecord());
  151. }
  152. /**
  153. * @expectedException Exception
  154. * @expectedExceptionMessageRegExp /There is no existing directory at/
  155. * @covers Monolog\Handler\StreamHandler::__construct
  156. * @covers Monolog\Handler\StreamHandler::write
  157. */
  158. public function testWriteNonExistingAndNotCreatableFileResource()
  159. {
  160. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  161. $this->markTestSkipped('Permissions checks can not run on windows');
  162. }
  163. $handler = new StreamHandler('file:///foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
  164. $handler->handle($this->getRecord());
  165. }
  166. }