ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Monolog\Handler\RotatingFileHandlerTest Class Reference

@covers Monolog\Handler\RotatingFileHandler More...

+ Inheritance diagram for Monolog\Handler\RotatingFileHandlerTest:
+ Collaboration diagram for Monolog\Handler\RotatingFileHandlerTest:

Public Member Functions

 setUp ()
 
 testRotationCreatesNewFile ()
 
 testRotation ($createFile, $dateFormat, $timeCallback)
 @dataProvider rotationTests More...
 
 rotationTests ()
 
 testAllowOnlyFixedDefinedDateFormats ($dateFormat, $valid)
 @dataProvider dateFormatProvider More...
 
 dateFormatProvider ()
 
 testDisallowFilenameFormatsWithoutDate ($filenameFormat, $valid)
 @dataProvider filenameFormatProvider More...
 
 filenameFormatProvider ()
 
 testRotationWhenSimilarFileNamesExist ($dateFormat)
 @dataProvider rotationWhenSimilarFilesExistTests More...
 
 rotationWhenSimilarFilesExistTests ()
 
 testReuseCurrentFile ()
 
 tearDown ()
 

Data Fields

 $lastError
 This var should be private but then the anonymous function in the setUp method won't be able to set it. More...
 

Private Member Functions

 assertErrorWasTriggered ($code, $message)
 

Additional Inherited Members

- Protected Member Functions inherited from Monolog\TestCase
 getRecord ($level=Logger::WARNING, $message='test', $context=array())
 
 getMultipleRecords ()
 
 getIdentityFormatter ()
 

Detailed Description

@covers Monolog\Handler\RotatingFileHandler

Definition at line 20 of file RotatingFileHandlerTest.php.

Member Function Documentation

◆ assertErrorWasTriggered()

Monolog\Handler\RotatingFileHandlerTest::assertErrorWasTriggered (   $code,
  $message 
)
private

Definition at line 48 of file RotatingFileHandlerTest.php.

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,
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 }
$code
Definition: example_050.php:99
catch(Exception $e) $message

References $code, and $message.

Referenced by Monolog\Handler\RotatingFileHandlerTest\testAllowOnlyFixedDefinedDateFormats(), and Monolog\Handler\RotatingFileHandlerTest\testDisallowFilenameFormatsWithoutDate().

+ Here is the caller graph for this function:

◆ dateFormatProvider()

Monolog\Handler\RotatingFileHandlerTest::dateFormatProvider ( )

◆ filenameFormatProvider()

Monolog\Handler\RotatingFileHandlerTest::filenameFormatProvider ( )

Definition at line 181 of file RotatingFileHandlerTest.php.

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 }

◆ rotationTests()

Monolog\Handler\RotatingFileHandlerTest::rotationTests ( )

Definition at line 107 of file RotatingFileHandlerTest.php.

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, 1, date('Y'));
115 };
116 $yearCallback = function($ago) {
117 return gmmktime(0, 0, 0, 1, 1, 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 }

References Monolog\Handler\RotatingFileHandler\FILE_PER_DAY, Monolog\Handler\RotatingFileHandler\FILE_PER_MONTH, and Monolog\Handler\RotatingFileHandler\FILE_PER_YEAR.

◆ rotationWhenSimilarFilesExistTests()

Monolog\Handler\RotatingFileHandlerTest::rotationWhenSimilarFilesExistTests ( )

Definition at line 213 of file RotatingFileHandlerTest.php.

214 {
215
216 return array(
217 'Rotation is triggered when the file of the current day is not present but similar exists'
219
220 'Rotation is triggered when the file of the current month is not present but similar exists'
222
223 'Rotation is triggered when the file of the current year is not present but similar exists'
225 );
226 }

References Monolog\Handler\RotatingFileHandler\FILE_PER_DAY, Monolog\Handler\RotatingFileHandler\FILE_PER_MONTH, and Monolog\Handler\RotatingFileHandler\FILE_PER_YEAR.

◆ setUp()

Monolog\Handler\RotatingFileHandlerTest::setUp ( )

Definition at line 30 of file RotatingFileHandlerTest.php.

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 }

References $code, and $message.

◆ tearDown()

Monolog\Handler\RotatingFileHandlerTest::tearDown ( )

Definition at line 238 of file RotatingFileHandlerTest.php.

239 {
240 foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) {
241 unlink($file);
242 }
243 restore_error_handler();
244 }

◆ testAllowOnlyFixedDefinedDateFormats()

Monolog\Handler\RotatingFileHandlerTest::testAllowOnlyFixedDefinedDateFormats (   $dateFormat,
  $valid 
)

@dataProvider dateFormatProvider

Definition at line 141 of file RotatingFileHandlerTest.php.

142 {
143 $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
144 $handler->setFilenameFormat('{filename}-{date}', $dateFormat);
145 if (!$valid) {
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 }
$valid
$handler

References $handler, $valid, and Monolog\Handler\RotatingFileHandlerTest\assertErrorWasTriggered().

+ Here is the call graph for this function:

◆ testDisallowFilenameFormatsWithoutDate()

Monolog\Handler\RotatingFileHandlerTest::testDisallowFilenameFormatsWithoutDate (   $filenameFormat,
  $valid 
)

@dataProvider filenameFormatProvider

Definition at line 169 of file RotatingFileHandlerTest.php.

170 {
171 $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
172 $handler->setFilenameFormat($filenameFormat, RotatingFileHandler::FILE_PER_DAY);
173 if (!$valid) {
175 E_USER_DEPRECATED,
176 'Invalid filename format - format should contain at least `{date}`, because otherwise rotating is impossible.'
177 );
178 }
179 }

References $handler, $valid, Monolog\Handler\RotatingFileHandlerTest\assertErrorWasTriggered(), and Monolog\Handler\RotatingFileHandler\FILE_PER_DAY.

+ Here is the call graph for this function:

◆ testReuseCurrentFile()

Monolog\Handler\RotatingFileHandlerTest::testReuseCurrentFile ( )

Definition at line 228 of file RotatingFileHandlerTest.php.

229 {
230 $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
231 file_put_contents($log, "foo");
232 $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
233 $handler->setFormatter($this->getIdentityFormatter());
234 $handler->handle($this->getRecord());
235 $this->assertEquals('footest', file_get_contents($log));
236 }
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
$log
Definition: sabredav.php:21

References $handler, $log, Monolog\TestCase\getIdentityFormatter(), and Monolog\TestCase\getRecord().

+ Here is the call graph for this function:

◆ testRotation()

Monolog\Handler\RotatingFileHandlerTest::testRotation (   $createFile,
  $dateFormat,
  $timeCallback 
)

@dataProvider rotationTests

Definition at line 79 of file RotatingFileHandlerTest.php.

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 }

References $handler, $log, Monolog\TestCase\getIdentityFormatter(), and Monolog\TestCase\getRecord().

+ Here is the call graph for this function:

◆ testRotationCreatesNewFile()

Monolog\Handler\RotatingFileHandlerTest::testRotationCreatesNewFile ( )

Definition at line 63 of file RotatingFileHandlerTest.php.

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 }

References $handler, $log, Monolog\TestCase\getIdentityFormatter(), and Monolog\TestCase\getRecord().

+ Here is the call graph for this function:

◆ testRotationWhenSimilarFileNamesExist()

Monolog\Handler\RotatingFileHandlerTest::testRotationWhenSimilarFileNamesExist (   $dateFormat)

@dataProvider rotationWhenSimilarFilesExistTests

Definition at line 197 of file RotatingFileHandlerTest.php.

198 {
199 touch($old1 = __DIR__.'/Fixtures/foo-foo-'.date($dateFormat).'.rot');
200 touch($old2 = __DIR__.'/Fixtures/foo-bar-'.date($dateFormat).'.rot');
201
202 $log = __DIR__.'/Fixtures/foo-'.date($dateFormat).'.rot';
203
204 $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
205 $handler->setFormatter($this->getIdentityFormatter());
206 $handler->setFilenameFormat('{filename}-{date}', $dateFormat);
207 $handler->handle($this->getRecord());
208 $handler->close();
209
210 $this->assertTrue(file_exists($log));
211 }

References $handler, $log, Monolog\TestCase\getIdentityFormatter(), and Monolog\TestCase\getRecord().

+ Here is the call graph for this function:

Field Documentation

◆ $lastError

Monolog\Handler\RotatingFileHandlerTest::$lastError

This var should be private but then the anonymous function in the setUp method won't be able to set it.

$this cant't be used in the anonymous function in setUp because PHP 5.3 does not support it.

Definition at line 28 of file RotatingFileHandlerTest.php.


The documentation for this class was generated from the following file: