]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/monolog/monolog/tests/Monolog/Handler/RotatingFileHandlerTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / monolog / monolog / tests / Monolog / Handler / RotatingFileHandlerTest.php
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 PHPUnit_Framework_Error_Deprecated;
16
17 /**
18  * @covers Monolog\Handler\RotatingFileHandler
19  */
20 class RotatingFileHandlerTest extends TestCase
21 {
22     /**
23      * This var should be private but then the anonymous function
24      * in the `setUp` method won't be able to set it. `$this` cant't
25      * be used in the anonymous function in `setUp` because PHP 5.3
26      * does not support it.
27      */
28     public $lastError;
29
30     public function setUp()
31     {
32         $dir = __DIR__.'/Fixtures';
33         chmod($dir, 0777);
34         if (!is_writable($dir)) {
35             $this->markTestSkipped($dir.' must be writable to test the RotatingFileHandler.');
36         }
37         $this->lastError = null;
38         $self = $this;
39         // workaround with &$self used for PHP 5.3
40         set_error_handler(function($code, $message) use (&$self) {
41             $self->lastError = array(
42                 'code' => $code,
43                 'message' => $message,
44             );
45         });
46     }
47
48     private function assertErrorWasTriggered($code, $message)
49     {
50         if (empty($this->lastError)) {
51             $this->fail(
52                 sprintf(
53                     'Failed asserting that error with code `%d` and message `%s` was triggered',
54                     $code,
55                     $message
56                 )
57             );
58         }
59         $this->assertEquals($code, $this->lastError['code'], sprintf('Expected an error with code %d to be triggered, got `%s` instead', $code, $this->lastError['code']));
60         $this->assertEquals($message, $this->lastError['message'], sprintf('Expected an error with message `%d` to be triggered, got `%s` instead', $message, $this->lastError['message']));
61     }
62
63     public function testRotationCreatesNewFile()
64     {
65         touch(__DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
66
67         $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
68         $handler->setFormatter($this->getIdentityFormatter());
69         $handler->handle($this->getRecord());
70
71         $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
72         $this->assertTrue(file_exists($log));
73         $this->assertEquals('test', file_get_contents($log));
74     }
75
76     /**
77      * @dataProvider rotationTests
78      */
79     public function testRotation($createFile, $dateFormat, $timeCallback)
80     {
81         touch($old1 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-1)).'.rot');
82         touch($old2 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-2)).'.rot');
83         touch($old3 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-3)).'.rot');
84         touch($old4 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-4)).'.rot');
85
86         $log = __DIR__.'/Fixtures/foo-'.date($dateFormat).'.rot';
87
88         if ($createFile) {
89             touch($log);
90         }
91
92         $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
93         $handler->setFormatter($this->getIdentityFormatter());
94         $handler->setFilenameFormat('{filename}-{date}', $dateFormat);
95         $handler->handle($this->getRecord());
96
97         $handler->close();
98
99         $this->assertTrue(file_exists($log));
100         $this->assertTrue(file_exists($old1));
101         $this->assertEquals($createFile, file_exists($old2));
102         $this->assertEquals($createFile, file_exists($old3));
103         $this->assertEquals($createFile, file_exists($old4));
104         $this->assertEquals('test', file_get_contents($log));
105     }
106
107     public function rotationTests()
108     {
109         $now = time();
110         $dayCallback = function($ago) use ($now) {
111             return $now + 86400 * $ago;
112         };
113         $monthCallback = function($ago) {
114             return gmmktime(0, 0, 0, date('n') + $ago, date('d'), date('Y'));
115         };
116         $yearCallback = function($ago) {
117             return gmmktime(0, 0, 0, date('n'), date('d'), date('Y') + $ago);
118         };
119
120         return array(
121             'Rotation is triggered when the file of the current day is not present'
122                 => array(true, RotatingFileHandler::FILE_PER_DAY, $dayCallback),
123             'Rotation is not triggered when the file of the current day is already present'
124                 => array(false, RotatingFileHandler::FILE_PER_DAY, $dayCallback),
125
126             'Rotation is triggered when the file of the current month is not present'
127                 => array(true, RotatingFileHandler::FILE_PER_MONTH, $monthCallback),
128             'Rotation is not triggered when the file of the current month is already present'
129                 => array(false, RotatingFileHandler::FILE_PER_MONTH, $monthCallback),
130
131             'Rotation is triggered when the file of the current year is not present'
132                 => array(true, RotatingFileHandler::FILE_PER_YEAR, $yearCallback),
133             'Rotation is not triggered when the file of the current year is already present'
134                 => array(false, RotatingFileHandler::FILE_PER_YEAR, $yearCallback),
135         );
136     }
137
138     /**
139      * @dataProvider dateFormatProvider
140      */
141     public function testAllowOnlyFixedDefinedDateFormats($dateFormat, $valid)
142     {
143         $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
144         $handler->setFilenameFormat('{filename}-{date}', $dateFormat);
145         if (!$valid) {
146             $this->assertErrorWasTriggered(
147                 E_USER_DEPRECATED,
148                 'Invalid date format - format must be one of RotatingFileHandler::FILE_PER_DAY ("Y-m-d"), '.
149                 'RotatingFileHandler::FILE_PER_MONTH ("Y-m") or RotatingFileHandler::FILE_PER_YEAR ("Y"), '.
150                 'or you can set one of the date formats using slashes, underscores and/or dots instead of dashes.'
151             );
152         }
153     }
154
155     public function dateFormatProvider()
156     {
157         return array(
158             array(RotatingFileHandler::FILE_PER_DAY, true),
159             array(RotatingFileHandler::FILE_PER_MONTH, true),
160             array(RotatingFileHandler::FILE_PER_YEAR, true),
161             array('m-d-Y', false),
162             array('Y-m-d-h-i', false)
163         );
164     }
165
166     /**
167      * @dataProvider filenameFormatProvider
168      */
169     public function testDisallowFilenameFormatsWithoutDate($filenameFormat, $valid)
170     {
171         $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
172         $handler->setFilenameFormat($filenameFormat, RotatingFileHandler::FILE_PER_DAY);
173         if (!$valid) {
174             $this->assertErrorWasTriggered(
175                 E_USER_DEPRECATED,
176                 'Invalid filename format - format should contain at least `{date}`, because otherwise rotating is impossible.'
177             );
178         }
179     }
180
181     public function filenameFormatProvider()
182     {
183         return array(
184             array('{filename}', false),
185             array('{filename}-{date}', true),
186             array('{date}', true),
187             array('foobar-{date}', true),
188             array('foo-{date}-bar', true),
189             array('{date}-foobar', true),
190             array('foobar', false),
191         );
192     }
193
194     public function testReuseCurrentFile()
195     {
196         $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
197         file_put_contents($log, "foo");
198         $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
199         $handler->setFormatter($this->getIdentityFormatter());
200         $handler->handle($this->getRecord());
201         $this->assertEquals('footest', file_get_contents($log));
202     }
203
204     public function tearDown()
205     {
206         foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) {
207             unlink($file);
208         }
209         restore_error_handler();
210     }
211 }