404

[ Avaa Bypassed ]




Upload:

Command:

botdev@18.189.188.157: ~ $
<?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 Monolog\Logger;

/**
 * @author Pablo de Leon Belloc <pablolb@gmail.com>
 */
class SocketHandlerTest extends TestCase
{
    /**
     * @var Monolog\Handler\SocketHandler
     */
    private $handler;

    /**
     * @var resource
     */
    private $res;

    /**
     * @expectedException UnexpectedValueException
     */
    public function testInvalidHostname()
    {
        $this->createHandler('garbage://here');
        $this->writeRecord('data');
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testBadConnectionTimeout()
    {
        $this->createHandler('localhost:1234');
        $this->handler->setConnectionTimeout(-1);
    }

    public function testSetConnectionTimeout()
    {
        $this->createHandler('localhost:1234');
        $this->handler->setConnectionTimeout(10.1);
        $this->assertEquals(10.1, $this->handler->getConnectionTimeout());
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testBadTimeout()
    {
        $this->createHandler('localhost:1234');
        $this->handler->setTimeout(-1);
    }

    public function testSetTimeout()
    {
        $this->createHandler('localhost:1234');
        $this->handler->setTimeout(10.25);
        $this->assertEquals(10.25, $this->handler->getTimeout());
    }

    public function testSetWritingTimeout()
    {
        $this->createHandler('localhost:1234');
        $this->handler->setWritingTimeout(10.25);
        $this->assertEquals(10.25, $this->handler->getWritingTimeout());
    }

    public function testSetChunkSize()
    {
        $this->createHandler('localhost:1234');
        $this->handler->setChunkSize(1025);
        $this->assertEquals(1025, $this->handler->getChunkSize());
    }

    public function testSetConnectionString()
    {
        $this->createHandler('tcp://localhost:9090');
        $this->assertEquals('tcp://localhost:9090', $this->handler->getConnectionString());
    }

    /**
     * @expectedException UnexpectedValueException
     */
    public function testExceptionIsThrownOnFsockopenError()
    {
        $this->setMockHandler(array('fsockopen'));
        $this->handler->expects($this->once())
            ->method('fsockopen')
            ->will($this->returnValue(false));
        $this->writeRecord('Hello world');
    }

    /**
     * @expectedException UnexpectedValueException
     */
    public function testExceptionIsThrownOnPfsockopenError()
    {
        $this->setMockHandler(array('pfsockopen'));
        $this->handler->expects($this->once())
            ->method('pfsockopen')
            ->will($this->returnValue(false));
        $this->handler->setPersistent(true);
        $this->writeRecord('Hello world');
    }

    /**
     * @expectedException UnexpectedValueException
     */
    public function testExceptionIsThrownIfCannotSetTimeout()
    {
        $this->setMockHandler(array('streamSetTimeout'));
        $this->handler->expects($this->once())
            ->method('streamSetTimeout')
            ->will($this->returnValue(false));
        $this->writeRecord('Hello world');
    }

    /**
     * @expectedException UnexpectedValueException
     */
    public function testExceptionIsThrownIfCannotSetChunkSize()
    {
        $this->setMockHandler(array('streamSetChunkSize'));
        $this->handler->setChunkSize(8192);
        $this->handler->expects($this->once())
            ->method('streamSetChunkSize')
            ->will($this->returnValue(false));
        $this->writeRecord('Hello world');
    }

    /**
     * @expectedException RuntimeException
     */
    public function testWriteFailsOnIfFwriteReturnsFalse()
    {
        $this->setMockHandler(array('fwrite'));

        $callback = function ($arg) {
            $map = array(
                'Hello world' => 6,
                'world' => false,
            );

            return $map[$arg];
        };

        $this->handler->expects($this->exactly(2))
            ->method('fwrite')
            ->will($this->returnCallback($callback));

        $this->writeRecord('Hello world');
    }

    /**
     * @expectedException RuntimeException
     */
    public function testWriteFailsIfStreamTimesOut()
    {
        $this->setMockHandler(array('fwrite', 'streamGetMetadata'));

        $callback = function ($arg) {
            $map = array(
                'Hello world' => 6,
                'world' => 5,
            );

            return $map[$arg];
        };

        $this->handler->expects($this->exactly(1))
            ->method('fwrite')
            ->will($this->returnCallback($callback));
        $this->handler->expects($this->exactly(1))
            ->method('streamGetMetadata')
            ->will($this->returnValue(array('timed_out' => true)));

        $this->writeRecord('Hello world');
    }

    /**
     * @expectedException RuntimeException
     */
    public function testWriteFailsOnIncompleteWrite()
    {
        $this->setMockHandler(array('fwrite', 'streamGetMetadata'));

        $res = $this->res;
        $callback = function ($string) use ($res) {
            fclose($res);

            return strlen('Hello');
        };

        $this->handler->expects($this->exactly(1))
            ->method('fwrite')
            ->will($this->returnCallback($callback));
        $this->handler->expects($this->exactly(1))
            ->method('streamGetMetadata')
            ->will($this->returnValue(array('timed_out' => false)));

        $this->writeRecord('Hello world');
    }

    public function testWriteWithMemoryFile()
    {
        $this->setMockHandler();
        $this->writeRecord('test1');
        $this->writeRecord('test2');
        $this->writeRecord('test3');
        fseek($this->res, 0);
        $this->assertEquals('test1test2test3', fread($this->res, 1024));
    }

    public function testWriteWithMock()
    {
        $this->setMockHandler(array('fwrite'));

        $callback = function ($arg) {
            $map = array(
                'Hello world' => 6,
                'world' => 5,
            );

            return $map[$arg];
        };

        $this->handler->expects($this->exactly(2))
            ->method('fwrite')
            ->will($this->returnCallback($callback));

        $this->writeRecord('Hello world');
    }

    public function testClose()
    {
        $this->setMockHandler();
        $this->writeRecord('Hello world');
        $this->assertInternalType('resource', $this->res);
        $this->handler->close();
        $this->assertFalse(is_resource($this->res), "Expected resource to be closed after closing handler");
    }

    public function testCloseDoesNotClosePersistentSocket()
    {
        $this->setMockHandler();
        $this->handler->setPersistent(true);
        $this->writeRecord('Hello world');
        $this->assertTrue(is_resource($this->res));
        $this->handler->close();
        $this->assertTrue(is_resource($this->res));
    }

    /**
     * @expectedException \RuntimeException
     */
    public function testAvoidInfiniteLoopWhenNoDataIsWrittenForAWritingTimeoutSeconds()
    {
        $this->setMockHandler(array('fwrite', 'streamGetMetadata'));

        $this->handler->expects($this->any())
            ->method('fwrite')
            ->will($this->returnValue(0));

        $this->handler->expects($this->any())
            ->method('streamGetMetadata')
            ->will($this->returnValue(array('timed_out' => false)));

        $this->handler->setWritingTimeout(1);

        $this->writeRecord('Hello world');
    }

    private function createHandler($connectionString)
    {
        $this->handler = new SocketHandler($connectionString);
        $this->handler->setFormatter($this->getIdentityFormatter());
    }

    private function writeRecord($string)
    {
        $this->handler->handle($this->getRecord(Logger::WARNING, $string));
    }

    private function setMockHandler(array $methods = array())
    {
        $this->res = fopen('php://memory', 'a');

        $defaultMethods = array('fsockopen', 'pfsockopen', 'streamSetTimeout');
        $newMethods = array_diff($methods, $defaultMethods);

        $finalMethods = array_merge($defaultMethods, $newMethods);

        $this->handler = $this->getMock(
            '\Monolog\Handler\SocketHandler', $finalMethods, array('localhost:1234')
        );

        if (!in_array('fsockopen', $methods)) {
            $this->handler->expects($this->any())
                ->method('fsockopen')
                ->will($this->returnValue($this->res));
        }

        if (!in_array('pfsockopen', $methods)) {
            $this->handler->expects($this->any())
                ->method('pfsockopen')
                ->will($this->returnValue($this->res));
        }

        if (!in_array('streamSetTimeout', $methods)) {
            $this->handler->expects($this->any())
                ->method('streamSetTimeout')
                ->will($this->returnValue(true));
        }

        if (!in_array('streamSetChunkSize', $methods)) {
            $this->handler->expects($this->any())
                ->method('streamSetChunkSize')
                ->will($this->returnValue(8192));
        }

        $this->handler->setFormatter($this->getIdentityFormatter());
    }
}

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