scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Monolog\Handler;
13  
14 use Monolog\TestCase;
15 use Monolog\Logger;
16  
17 class StreamHandlerTest extends TestCase
18 {
19 /**
20 * @covers Monolog\Handler\StreamHandler::__construct
21 * @covers Monolog\Handler\StreamHandler::write
22 */
23 public function testWrite()
24 {
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'));
31 fseek($handle, 0);
32 $this->assertEquals('testtest2test3', fread($handle, 100));
33 }
34  
35 /**
36 * @covers Monolog\Handler\StreamHandler::close
37 */
38 public function testCloseKeepsExternalHandlersOpen()
39 {
40 $handle = fopen('php://memory', 'a+');
41 $handler = new StreamHandler($handle);
42 $this->assertTrue(is_resource($handle));
43 $handler->close();
44 $this->assertTrue(is_resource($handle));
45 }
46  
47 /**
48 * @covers Monolog\Handler\StreamHandler::close
49 */
50 public function testClose()
51 {
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);
57  
58 $this->assertTrue(is_resource($handle));
59 $handler->close();
60 $this->assertFalse(is_resource($handle));
61 }
62  
63 /**
64 * @covers Monolog\Handler\StreamHandler::write
65 */
66 public function testWriteCreatesTheStreamResource()
67 {
68 $handler = new StreamHandler('php://memory');
69 $handler->handle($this->getRecord());
70 }
71  
72 /**
73 * @covers Monolog\Handler\StreamHandler::__construct
74 * @covers Monolog\Handler\StreamHandler::write
75 */
76 public function testWriteLocking()
77 {
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());
81 }
82  
83 /**
84 * @expectedException LogicException
85 * @covers Monolog\Handler\StreamHandler::__construct
86 * @covers Monolog\Handler\StreamHandler::write
87 */
88 public function testWriteMissingResource()
89 {
90 $handler = new StreamHandler(null);
91 $handler->handle($this->getRecord());
92 }
93  
94 public function invalidArgumentProvider()
95 {
96 return array(
97 array(1),
98 array(array()),
99 array(array('bogus://url')),
100 );
101 }
102  
103 /**
104 * @dataProvider invalidArgumentProvider
105 * @expectedException InvalidArgumentException
106 * @covers Monolog\Handler\StreamHandler::__construct
107 */
108 public function testWriteInvalidArgument($invalidArgument)
109 {
110 $handler = new StreamHandler($invalidArgument);
111 }
112  
113 /**
114 * @expectedException UnexpectedValueException
115 * @covers Monolog\Handler\StreamHandler::__construct
116 * @covers Monolog\Handler\StreamHandler::write
117 */
118 public function testWriteInvalidResource()
119 {
120 $handler = new StreamHandler('bogus://url');
121 $handler->handle($this->getRecord());
122 }
123  
124 /**
125 * @expectedException UnexpectedValueException
126 * @covers Monolog\Handler\StreamHandler::__construct
127 * @covers Monolog\Handler\StreamHandler::write
128 */
129 public function testWriteNonExistingResource()
130 {
131 $handler = new StreamHandler('ftp://foo/bar/baz/'.rand(0, 10000));
132 $handler->handle($this->getRecord());
133 }
134  
135 /**
136 * @covers Monolog\Handler\StreamHandler::__construct
137 * @covers Monolog\Handler\StreamHandler::write
138 */
139 public function testWriteNonExistingPath()
140 {
141 $handler = new StreamHandler(sys_get_temp_dir().'/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
142 $handler->handle($this->getRecord());
143 }
144  
145 /**
146 * @covers Monolog\Handler\StreamHandler::__construct
147 * @covers Monolog\Handler\StreamHandler::write
148 */
149 public function testWriteNonExistingFileResource()
150 {
151 $handler = new StreamHandler('file://'.sys_get_temp_dir().'/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
152 $handler->handle($this->getRecord());
153 }
154  
155 /**
156 * @expectedException Exception
157 * @expectedExceptionMessageRegExp /There is no existing directory at/
158 * @covers Monolog\Handler\StreamHandler::__construct
159 * @covers Monolog\Handler\StreamHandler::write
160 */
161 public function testWriteNonExistingAndNotCreatablePath()
162 {
163 if (defined('PHP_WINDOWS_VERSION_BUILD')) {
164 $this->markTestSkipped('Permissions checks can not run on windows');
165 }
166 $handler = new StreamHandler('/foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
167 $handler->handle($this->getRecord());
168 }
169  
170 /**
171 * @expectedException Exception
172 * @expectedExceptionMessageRegExp /There is no existing directory at/
173 * @covers Monolog\Handler\StreamHandler::__construct
174 * @covers Monolog\Handler\StreamHandler::write
175 */
176 public function testWriteNonExistingAndNotCreatableFileResource()
177 {
178 if (defined('PHP_WINDOWS_VERSION_BUILD')) {
179 $this->markTestSkipped('Permissions checks can not run on windows');
180 }
181 $handler = new StreamHandler('file:///foo/bar/'.rand(0, 10000).DIRECTORY_SEPARATOR.rand(0, 10000));
182 $handler->handle($this->getRecord());
183 }
184 }