StreamHandlerTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php declare(strict_types=1);
  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\Test\TestCase;
  12. use Monolog\Level;
  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(Level::Warning, 'test'));
  25. $handler->handle($this->getRecord(Level::Warning, 'test2'));
  26. $handler->handle($this->getRecord(Level::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(Level::Warning, 'test'));
  48. $stream = $handler->getStream();
  49. $this->assertTrue(is_resource($stream));
  50. $handler->close();
  51. $this->assertFalse(is_resource($stream));
  52. }
  53. /**
  54. * @covers Monolog\Handler\StreamHandler::close
  55. * @covers Monolog\Handler\Handler::__sleep
  56. */
  57. public function testSerialization()
  58. {
  59. $handler = new StreamHandler('php://memory');
  60. $handler->handle($this->getRecord(Level::Warning, 'testfoo'));
  61. $stream = $handler->getStream();
  62. $this->assertTrue(is_resource($stream));
  63. fseek($stream, 0);
  64. $this->assertStringContainsString('testfoo', stream_get_contents($stream));
  65. $serialized = serialize($handler);
  66. $this->assertFalse(is_resource($stream));
  67. $handler = unserialize($serialized);
  68. $handler->handle($this->getRecord(Level::Warning, 'testbar'));
  69. $stream = $handler->getStream();
  70. $this->assertTrue(is_resource($stream));
  71. fseek($stream, 0);
  72. $contents = stream_get_contents($stream);
  73. $this->assertStringNotContainsString('testfoo', $contents);
  74. $this->assertStringContainsString('testbar', $contents);
  75. }
  76. /**
  77. * @covers Monolog\Handler\StreamHandler::write
  78. */
  79. public function testWriteCreatesTheStreamResource()
  80. {
  81. $handler = new StreamHandler('php://memory');
  82. $handler->handle($this->getRecord());
  83. }
  84. /**
  85. * @covers Monolog\Handler\StreamHandler::__construct
  86. * @covers Monolog\Handler\StreamHandler::write
  87. */
  88. public function testWriteLocking()
  89. {
  90. $temp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'monolog_locked_log';
  91. $handler = new StreamHandler($temp, Level::Debug, true, null, true);
  92. $handler->handle($this->getRecord());
  93. }
  94. /**
  95. * @covers Monolog\Handler\StreamHandler::__construct
  96. * @covers Monolog\Handler\StreamHandler::write
  97. */
  98. public function testWriteMissingResource()
  99. {
  100. $this->expectException(\LogicException::class);
  101. $handler = new StreamHandler(null);
  102. $handler->handle($this->getRecord());
  103. }
  104. public static function invalidArgumentProvider()
  105. {
  106. return [
  107. [1],
  108. [[]],
  109. [['bogus://url']],
  110. ];
  111. }
  112. /**
  113. * @dataProvider invalidArgumentProvider
  114. * @covers Monolog\Handler\StreamHandler::__construct
  115. */
  116. public function testWriteInvalidArgument($invalidArgument)
  117. {
  118. $this->expectException(\InvalidArgumentException::class);
  119. $handler = new StreamHandler($invalidArgument);
  120. }
  121. /**
  122. * @covers Monolog\Handler\StreamHandler::__construct
  123. * @covers Monolog\Handler\StreamHandler::write
  124. */
  125. public function testWriteInvalidResource()
  126. {
  127. $this->expectException(\UnexpectedValueException::class);
  128. $php7xMessage = <<<STRING
  129. The stream or file "bogus://url" could not be opened in append mode: failed to open stream: No such file or directory
  130. The exception occurred while attempting to log: test
  131. Context: {"foo":"bar"}
  132. Extra: [1,2,3]
  133. STRING;
  134. $php8xMessage = <<<STRING
  135. The stream or file "bogus://url" could not be opened in append mode: Failed to open stream: No such file or directory
  136. The exception occurred while attempting to log: test
  137. Context: {"foo":"bar"}
  138. Extra: [1,2,3]
  139. STRING;
  140. $phpVersionString = phpversion();
  141. $phpVersionComponents = explode('.', $phpVersionString);
  142. $majorVersion = (int) $phpVersionComponents[0];
  143. $this->expectExceptionMessage(($majorVersion >= 8) ? $php8xMessage : $php7xMessage);
  144. $handler = new StreamHandler('bogus://url');
  145. $record = $this->getRecord(
  146. context: ['foo' => 'bar'],
  147. extra: [1, 2, 3],
  148. );
  149. $handler->handle($record);
  150. }
  151. /**
  152. * @covers Monolog\Handler\StreamHandler::__construct
  153. * @covers Monolog\Handler\StreamHandler::write
  154. */
  155. public function testWriteNonExistingResource()
  156. {
  157. $this->expectException(\UnexpectedValueException::class);
  158. $handler = new StreamHandler('ftp://foo/bar/baz/'.rand(0, 10000));
  159. $handler->handle($this->getRecord());
  160. }
  161. /**
  162. * @covers Monolog\Handler\StreamHandler::__construct
  163. * @covers Monolog\Handler\StreamHandler::write
  164. */
  165. public function testWriteNonExistingPath()
  166. {
  167. $handler = new StreamHandler(sys_get_temp_dir().'/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
  168. $handler->handle($this->getRecord());
  169. }
  170. /**
  171. * @covers Monolog\Handler\StreamHandler::__construct
  172. * @covers Monolog\Handler\StreamHandler::write
  173. */
  174. public function testWriteNonExistingFileResource()
  175. {
  176. $handler = new StreamHandler('file://'.sys_get_temp_dir().'/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
  177. $handler->handle($this->getRecord());
  178. }
  179. /**
  180. * @covers Monolog\Handler\StreamHandler::__construct
  181. * @covers Monolog\Handler\StreamHandler::write
  182. * @dataProvider provideNonExistingAndNotCreatablePath
  183. */
  184. public function testWriteNonExistingAndNotCreatablePath($nonExistingAndNotCreatablePath)
  185. {
  186. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  187. $this->markTestSkipped('Permissions checks can not run on windows');
  188. }
  189. $handler = null;
  190. try {
  191. $handler = new StreamHandler($nonExistingAndNotCreatablePath);
  192. } catch (\Exception $fail) {
  193. $this->fail(
  194. 'A non-existing and not creatable path should throw an Exception earliest on first write.
  195. Not during instantiation.'
  196. );
  197. }
  198. $this->expectException(\UnexpectedValueException::class);
  199. $this->expectExceptionMessage('There is no existing directory at');
  200. $handler->handle($this->getRecord());
  201. }
  202. public static function provideNonExistingAndNotCreatablePath()
  203. {
  204. return [
  205. '/foo/bar/…' => [
  206. '/foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000),
  207. ],
  208. 'file:///foo/bar/…' => [
  209. 'file:///foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000),
  210. ],
  211. ];
  212. }
  213. public static function provideMemoryValues()
  214. {
  215. return [
  216. ['1M', (int) (1024*1024/10)],
  217. ['10M', (int) (1024*1024)],
  218. ['1024M', (int) (1024*1024*1024/10)],
  219. ['1G', (int) (1024*1024*1024/10)],
  220. ['2000M', (int) (2000*1024*1024/10)],
  221. ['2050M', (int) (2050*1024*1024/10)],
  222. ['2048M', (int) (2048*1024*1024/10)],
  223. ['3G', (int) (3*1024*1024*1024/10)],
  224. ['2560M', (int) (2560*1024*1024/10)],
  225. ];
  226. }
  227. /**
  228. * @dataProvider provideMemoryValues
  229. */
  230. public function testPreventOOMError($phpMemory, $expectedChunkSize): void
  231. {
  232. $previousValue = ini_set('memory_limit', $phpMemory);
  233. if ($previousValue === false) {
  234. $this->markTestSkipped('We could not set a memory limit that would trigger the error.');
  235. }
  236. try {
  237. $stream = tmpfile();
  238. if ($stream === false) {
  239. $this->markTestSkipped('We could not create a temp file to be use as a stream.');
  240. }
  241. $handler = new StreamHandler($stream);
  242. stream_get_contents($stream, 1024);
  243. $this->assertEquals($expectedChunkSize, $handler->getStreamChunkSize());
  244. } finally {
  245. ini_set('memory_limit', $previousValue);
  246. }
  247. }
  248. public function testSimpleOOMPrevention(): void
  249. {
  250. $previousValue = ini_set('memory_limit', '2048M');
  251. if ($previousValue === false) {
  252. $this->markTestSkipped('We could not set a memory limit that would trigger the error.');
  253. }
  254. try {
  255. $stream = tmpfile();
  256. new StreamHandler($stream);
  257. stream_get_contents($stream);
  258. $this->assertTrue(true);
  259. } finally {
  260. ini_set('memory_limit', $previousValue);
  261. }
  262. }
  263. }