404

[ Avaa Bypassed ]




Upload:

Command:

botdev@3.144.40.81: ~ $
<?php

/*
 * This file is part of the Monolog package.
 *
 * (c) Jordi Boggiano <j.boggiano@seld.be>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Monolog\Handler;

use Monolog\TestCase;
use PHPUnit_Framework_Error_Deprecated;

/**
 * @covers Monolog\Handler\RotatingFileHandler
 */
class RotatingFileHandlerTest extends TestCase
{
    /**
     * 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.
     */
    public $lastError;

    public function setUp()
    {
        $dir = __DIR__.'/Fixtures';
        chmod($dir, 0777);
        if (!is_writable($dir)) {
            $this->markTestSkipped($dir.' must be writable to test the RotatingFileHandler.');
        }
        $this->lastError = null;
        $self = $this;
        // workaround with &$self used for PHP 5.3
        set_error_handler(function($code, $message) use (&$self) {
            $self->lastError = array(
                'code' => $code,
                'message' => $message,
            );
        });
    }

    private function assertErrorWasTriggered($code, $message)
    {
        if (empty($this->lastError)) {
            $this->fail(
                sprintf(
                    'Failed asserting that error with code `%d` and message `%s` was triggered',
                    $code,
                    $message
                )
            );
        }
        $this->assertEquals($code, $this->lastError['code'], sprintf('Expected an error with code %d to be triggered, got `%s` instead', $code, $this->lastError['code']));
        $this->assertEquals($message, $this->lastError['message'], sprintf('Expected an error with message `%d` to be triggered, got `%s` instead', $message, $this->lastError['message']));
    }

    public function testRotationCreatesNewFile()
    {
        touch(__DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot');

        $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
        $handler->setFormatter($this->getIdentityFormatter());
        $handler->handle($this->getRecord());

        $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
        $this->assertTrue(file_exists($log));
        $this->assertEquals('test', file_get_contents($log));
    }

    /**
     * @dataProvider rotationTests
     */
    public function testRotation($createFile, $dateFormat, $timeCallback)
    {
        touch($old1 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-1)).'.rot');
        touch($old2 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-2)).'.rot');
        touch($old3 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-3)).'.rot');
        touch($old4 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-4)).'.rot');

        $log = __DIR__.'/Fixtures/foo-'.date($dateFormat).'.rot';

        if ($createFile) {
            touch($log);
        }

        $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
        $handler->setFormatter($this->getIdentityFormatter());
        $handler->setFilenameFormat('{filename}-{date}', $dateFormat);
        $handler->handle($this->getRecord());

        $handler->close();

        $this->assertTrue(file_exists($log));
        $this->assertTrue(file_exists($old1));
        $this->assertEquals($createFile, file_exists($old2));
        $this->assertEquals($createFile, file_exists($old3));
        $this->assertEquals($createFile, file_exists($old4));
        $this->assertEquals('test', file_get_contents($log));
    }

    public function rotationTests()
    {
        $now = time();
        $dayCallback = function($ago) use ($now) {
            return $now + 86400 * $ago;
        };
        $monthCallback = function($ago) {
            return gmmktime(0, 0, 0, date('n') + $ago, 1, date('Y'));
        };
        $yearCallback = function($ago) {
            return gmmktime(0, 0, 0, 1, 1, date('Y') + $ago);
        };

        return array(
            'Rotation is triggered when the file of the current day is not present'
                => array(true, RotatingFileHandler::FILE_PER_DAY, $dayCallback),
            'Rotation is not triggered when the file of the current day is already present'
                => array(false, RotatingFileHandler::FILE_PER_DAY, $dayCallback),

            'Rotation is triggered when the file of the current month is not present'
                => array(true, RotatingFileHandler::FILE_PER_MONTH, $monthCallback),
            'Rotation is not triggered when the file of the current month is already present'
                => array(false, RotatingFileHandler::FILE_PER_MONTH, $monthCallback),

            'Rotation is triggered when the file of the current year is not present'
                => array(true, RotatingFileHandler::FILE_PER_YEAR, $yearCallback),
            'Rotation is not triggered when the file of the current year is already present'
                => array(false, RotatingFileHandler::FILE_PER_YEAR, $yearCallback),
        );
    }

    /**
     * @dataProvider dateFormatProvider
     */
    public function testAllowOnlyFixedDefinedDateFormats($dateFormat, $valid)
    {
        $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
        $handler->setFilenameFormat('{filename}-{date}', $dateFormat);
        if (!$valid) {
            $this->assertErrorWasTriggered(
                E_USER_DEPRECATED,
                'Invalid date format - format must be one of RotatingFileHandler::FILE_PER_DAY ("Y-m-d"), '.
                'RotatingFileHandler::FILE_PER_MONTH ("Y-m") or RotatingFileHandler::FILE_PER_YEAR ("Y"), '.
                'or you can set one of the date formats using slashes, underscores and/or dots instead of dashes.'
            );
        }
    }

    public function dateFormatProvider()
    {
        return array(
            array(RotatingFileHandler::FILE_PER_DAY, true),
            array(RotatingFileHandler::FILE_PER_MONTH, true),
            array(RotatingFileHandler::FILE_PER_YEAR, true),
            array('m-d-Y', false),
            array('Y-m-d-h-i', false)
        );
    }

    /**
     * @dataProvider filenameFormatProvider
     */
    public function testDisallowFilenameFormatsWithoutDate($filenameFormat, $valid)
    {
        $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
        $handler->setFilenameFormat($filenameFormat, RotatingFileHandler::FILE_PER_DAY);
        if (!$valid) {
            $this->assertErrorWasTriggered(
                E_USER_DEPRECATED,
                'Invalid filename format - format should contain at least `{date}`, because otherwise rotating is impossible.'
            );
        }
    }

    public function filenameFormatProvider()
    {
        return array(
            array('{filename}', false),
            array('{filename}-{date}', true),
            array('{date}', true),
            array('foobar-{date}', true),
            array('foo-{date}-bar', true),
            array('{date}-foobar', true),
            array('foobar', false),
        );
    }

    /**
     * @dataProvider rotationWhenSimilarFilesExistTests
     */
    public function testRotationWhenSimilarFileNamesExist($dateFormat)
    {
        touch($old1 = __DIR__.'/Fixtures/foo-foo-'.date($dateFormat).'.rot');
        touch($old2 = __DIR__.'/Fixtures/foo-bar-'.date($dateFormat).'.rot');

        $log = __DIR__.'/Fixtures/foo-'.date($dateFormat).'.rot';

        $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
        $handler->setFormatter($this->getIdentityFormatter());
        $handler->setFilenameFormat('{filename}-{date}', $dateFormat);
        $handler->handle($this->getRecord());
        $handler->close();

        $this->assertTrue(file_exists($log));
    }

    public function rotationWhenSimilarFilesExistTests()
    {

        return array(
            'Rotation is triggered when the file of the current day is not present but similar exists'
                => array(RotatingFileHandler::FILE_PER_DAY),

            'Rotation is triggered when the file of the current month is not present but similar exists'
                => array(RotatingFileHandler::FILE_PER_MONTH),

            'Rotation is triggered when the file of the current year is not present but similar exists'
                => array(RotatingFileHandler::FILE_PER_YEAR),
        );
    }

    public function testReuseCurrentFile()
    {
        $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
        file_put_contents($log, "foo");
        $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot');
        $handler->setFormatter($this->getIdentityFormatter());
        $handler->handle($this->getRecord());
        $this->assertEquals('footest', file_get_contents($log));
    }

    public function tearDown()
    {
        foreach (glob(__DIR__.'/Fixtures/*.rot') as $file) {
            unlink($file);
        }
        restore_error_handler();
    }
}

Filemanager

Name Type Size Permission Actions
Fixtures Folder 0755
Slack Folder 0755
AbstractHandlerTest.php File 3.99 KB 0644
AbstractProcessingHandlerTest.php File 2.59 KB 0644
AmqpHandlerTest.php File 4.09 KB 0644
BrowserConsoleHandlerTest.php File 4.11 KB 0644
BufferHandlerTest.php File 5.23 KB 0644
ChromePHPHandlerTest.php File 4.78 KB 0644
CouchDBHandlerTest.php File 751 B 0644
DeduplicationHandlerTest.php File 5.99 KB 0644
DoctrineCouchDBHandlerTest.php File 1.44 KB 0644
DynamoDbHandlerTest.php File 2.46 KB 0644
ElasticSearchHandlerTest.php File 7.51 KB 0644
ErrorLogHandlerTest.php File 2.1 KB 0644
FilterHandlerTest.php File 6.26 KB 0644
FingersCrossedHandlerTest.php File 10.84 KB 0644
FirePHPHandlerTest.php File 2.9 KB 0644
FleepHookHandlerTest.php File 2.19 KB 0644
FlowdockHandlerTest.php File 2.53 KB 0644
GelfHandlerLegacyTest.php File 3.12 KB 0644
GelfHandlerTest.php File 3.31 KB 0644
GelfMockMessagePublisher.php File 500 B 0644
GroupHandlerTest.php File 3.7 KB 0644
HandlerWrapperTest.php File 3.22 KB 0644
HipChatHandlerTest.php File 10.23 KB 0644
InsightOpsHandlerTest.php File 2.33 KB 0644
LogEntriesHandlerTest.php File 2.34 KB 0644
MailHandlerTest.php File 2.15 KB 0644
MockRavenClient.php File 573 B 0644
MongoDBHandlerTest.php File 1.71 KB 0644
NativeMailerHandlerTest.php File 3.65 KB 0644
NewRelicHandlerTest.php File 5.92 KB 0644
NullHandlerTest.php File 747 B 0644
PHPConsoleHandlerTest.php File 9.66 KB 0644
PsrHandlerTest.php File 1.25 KB 0644
PushoverHandlerTest.php File 5.13 KB 0644
RavenHandlerTest.php File 8.69 KB 0644
RedisHandlerTest.php File 3.83 KB 0644
RollbarHandlerTest.php File 2.14 KB 0644
RotatingFileHandlerTest.php File 8.95 KB 0644
SamplingHandlerTest.php File 845 B 0644
SlackHandlerTest.php File 5.31 KB 0644
SlackWebhookHandlerTest.php File 3.13 KB 0644
SlackbotHandlerTest.php File 1.13 KB 0644
SocketHandlerTest.php File 9.33 KB 0644
StreamHandlerTest.php File 5.8 KB 0644
SwiftMailerHandlerTest.php File 3.74 KB 0644
SyslogHandlerTest.php File 1.25 KB 0644
SyslogUdpHandlerTest.php File 2.26 KB 0644
TestHandlerTest.php File 3.95 KB 0644
UdpSocketTest.php File 1.63 KB 0644
WhatFailureGroupHandlerTest.php File 4.73 KB 0644
ZendMonitorHandlerTest.php File 1.99 KB 0644