4 * This file is part of the Monolog package.
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Monolog\Handler;
17 class StreamHandlerTest extends TestCase
20 * @covers Monolog\Handler\StreamHandler::__construct
21 * @covers Monolog\Handler\StreamHandler::write
23 public function testWrite()
25 $handle = fopen('php://memory', 'a+');
26 $handler = new StreamHandler($handle);
27 $handler->setFormatter($this->getIdentityFormatter());
28 $handler->handle($this->getRecord(Logger::WARNING, 'test'));
29 $handler->handle($this->getRecord(Logger::WARNING, 'test2'));
30 $handler->handle($this->getRecord(Logger::WARNING, 'test3'));
32 $this->assertEquals('testtest2test3', fread($handle, 100));
36 * @covers Monolog\Handler\StreamHandler::close
38 public function testCloseKeepsExternalHandlersOpen()
40 $handle = fopen('php://memory', 'a+');
41 $handler = new StreamHandler($handle);
42 $this->assertTrue(is_resource($handle));
44 $this->assertTrue(is_resource($handle));
48 * @covers Monolog\Handler\StreamHandler::close
50 public function testClose()
52 $handler = new StreamHandler('php://memory');
53 $handler->handle($this->getRecord(Logger::WARNING, 'test'));
54 $streamProp = new \ReflectionProperty('Monolog\Handler\StreamHandler', 'stream');
55 $streamProp->setAccessible(true);
56 $handle = $streamProp->getValue($handler);
58 $this->assertTrue(is_resource($handle));
60 $this->assertFalse(is_resource($handle));
64 * @covers Monolog\Handler\StreamHandler::write
66 public function testWriteCreatesTheStreamResource()
68 $handler = new StreamHandler('php://memory');
69 $handler->handle($this->getRecord());
73 * @covers Monolog\Handler\StreamHandler::__construct
74 * @covers Monolog\Handler\StreamHandler::write
76 public function testWriteLocking()
78 $temp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'monolog_locked_log';
79 $handler = new StreamHandler($temp, Logger::DEBUG, true, null, true);
80 $handler->handle($this->getRecord());
84 * @expectedException LogicException
85 * @covers Monolog\Handler\StreamHandler::__construct
86 * @covers Monolog\Handler\StreamHandler::write
88 public function testWriteMissingResource()
90 $handler = new StreamHandler(null);
91 $handler->handle($this->getRecord());
94 public function invalidArgumentProvider()
99 array(array('bogus://url')),
104 * @dataProvider invalidArgumentProvider
105 * @expectedException InvalidArgumentException
106 * @covers Monolog\Handler\StreamHandler::__construct
108 public function testWriteInvalidArgument($invalidArgument)
110 $handler = new StreamHandler($invalidArgument);
114 * @expectedException UnexpectedValueException
115 * @covers Monolog\Handler\StreamHandler::__construct
116 * @covers Monolog\Handler\StreamHandler::write
118 public function testWriteInvalidResource()
120 $handler = new StreamHandler('bogus://url');
121 $handler->handle($this->getRecord());
125 * @expectedException UnexpectedValueException
126 * @covers Monolog\Handler\StreamHandler::__construct
127 * @covers Monolog\Handler\StreamHandler::write
129 public function testWriteNonExistingResource()
131 $handler = new StreamHandler('ftp://foo/bar/baz/'.rand(0, 10000));
132 $handler->handle($this->getRecord());
136 * @covers Monolog\Handler\StreamHandler::__construct
137 * @covers Monolog\Handler\StreamHandler::write
139 public function testWriteNonExistingPath()
141 $handler = new StreamHandler(sys_get_temp_dir().'/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
142 $handler->handle($this->getRecord());
146 * @covers Monolog\Handler\StreamHandler::__construct
147 * @covers Monolog\Handler\StreamHandler::write
149 public function testWriteNonExistingFileResource()
151 $handler = new StreamHandler('file://'.sys_get_temp_dir().'/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
152 $handler->handle($this->getRecord());
156 * @expectedException Exception
157 * @expectedExceptionMessageRegExp /There is no existing directory at/
158 * @covers Monolog\Handler\StreamHandler::__construct
159 * @covers Monolog\Handler\StreamHandler::write
161 public function testWriteNonExistingAndNotCreatablePath()
163 if (defined('PHP_WINDOWS_VERSION_BUILD')) {
164 $this->markTestSkipped('Permissions checks can not run on windows');
166 $handler = new StreamHandler('/foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
167 $handler->handle($this->getRecord());
171 * @expectedException Exception
172 * @expectedExceptionMessageRegExp /There is no existing directory at/
173 * @covers Monolog\Handler\StreamHandler::__construct
174 * @covers Monolog\Handler\StreamHandler::write
176 public function testWriteNonExistingAndNotCreatableFileResource()
178 if (defined('PHP_WINDOWS_VERSION_BUILD')) {
179 $this->markTestSkipped('Permissions checks can not run on windows');
181 $handler = new StreamHandler('file:///foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
182 $handler->handle($this->getRecord());