ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
RotatingFileHandlerTest.php
Go to the documentation of this file.
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 
15 
20 {
21  public function setUp()
22  {
23  $dir = __DIR__.'/Fixtures';
24  chmod($dir, 0777);
25  if (!is_writable($dir)) {
26  $this->markTestSkipped($dir.' must be writeable to test the RotatingFileHandler.');
27  }
28  }
29 
30  public function testRotationCreatesNewFile()
31  {
32  touch(__DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
33 
34  $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
35  $handler->setFormatter($this->getIdentityFormatter());
36  $handler->handle($this->getRecord());
37 
38  $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
39  $this->assertTrue(file_exists($log));
40  $this->assertEquals('test', file_get_contents($log));
41  }
42 
46  public function testRotation($createFile)
47  {
48  touch($old1 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');
49  touch($old2 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 2).'.rot');
50  touch($old3 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 3).'.rot');
51  touch($old4 = __DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400 * 4).'.rot');
52 
53  $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
54 
55  if ($createFile) {
56  touch($log);
57  }
58 
59  $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
60  $handler->setFormatter($this->getIdentityFormatter());
61  $handler->handle($this->getRecord());
62 
63  $handler->close();
64 
65  $this->assertTrue(file_exists($log));
66  $this->assertTrue(file_exists($old1));
67  $this->assertEquals($createFile, file_exists($old2));
68  $this->assertEquals($createFile, file_exists($old3));
69  $this->assertEquals($createFile, file_exists($old4));
70  $this->assertEquals('test', file_get_contents($log));
71  }
72 
73  public function rotationTests()
74  {
75  return array(
76  'Rotation is triggered when the file of the current day is not present'
77  => array(true),
78  'Rotation is not triggered when the file is already present'
79  => array(false),
80  );
81  }
82 
83  public function testReuseCurrentFile()
84  {
85  $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
86  file_put_contents($log, "foo");
87  $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
88  $handler->setFormatter($this->getIdentityFormatter());
89  $handler->handle($this->getRecord());
90  $this->assertEquals('footest', file_get_contents($log));
91  }
92 
93  public function tearDown()
94  {
95  foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) {
96  unlink($file);
97  }
98  }
99 }
print $file
Stores logs to files that are rotated every day and a limited number of files are kept...
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19