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\Formatter\ElasticaFormatter;
use Monolog\Formatter\NormalizerFormatter;
use Monolog\TestCase;
use Monolog\Logger;
use Elastica\Client;
use Elastica\Request;
use Elastica\Response;

class ElasticSearchHandlerTest extends TestCase
{
    /**
     * @var Client mock
     */
    protected $client;

    /**
     * @var array Default handler options
     */
    protected $options = array(
        'index' => 'my_index',
        'type'  => 'doc_type',
    );

    public function setUp()
    {
        // Elastica lib required
        if (!class_exists("Elastica\Client")) {
            $this->markTestSkipped("ruflin/elastica not installed");
        }

        // base mock Elastica Client object
        $this->client = $this->getMockBuilder('Elastica\Client')
            ->setMethods(array('addDocuments'))
            ->disableOriginalConstructor()
            ->getMock();
    }

    /**
     * @covers Monolog\Handler\ElasticSearchHandler::write
     * @covers Monolog\Handler\ElasticSearchHandler::handleBatch
     * @covers Monolog\Handler\ElasticSearchHandler::bulkSend
     * @covers Monolog\Handler\ElasticSearchHandler::getDefaultFormatter
     */
    public function testHandle()
    {
        // log message
        $msg = array(
            'level' => Logger::ERROR,
            'level_name' => 'ERROR',
            'channel' => 'meh',
            'context' => array('foo' => 7, 'bar', 'class' => new \stdClass),
            'datetime' => new \DateTime("@0"),
            'extra' => array(),
            'message' => 'log',
        );

        // format expected result
        $formatter = new ElasticaFormatter($this->options['index'], $this->options['type']);
        $expected = array($formatter->format($msg));

        // setup ES client mock
        $this->client->expects($this->any())
            ->method('addDocuments')
            ->with($expected);

        // perform tests
        $handler = new ElasticSearchHandler($this->client, $this->options);
        $handler->handle($msg);
        $handler->handleBatch(array($msg));
    }

    /**
     * @covers Monolog\Handler\ElasticSearchHandler::setFormatter
     */
    public function testSetFormatter()
    {
        $handler = new ElasticSearchHandler($this->client);
        $formatter = new ElasticaFormatter('index_new', 'type_new');
        $handler->setFormatter($formatter);
        $this->assertInstanceOf('Monolog\Formatter\ElasticaFormatter', $handler->getFormatter());
        $this->assertEquals('index_new', $handler->getFormatter()->getIndex());
        $this->assertEquals('type_new', $handler->getFormatter()->getType());
    }

    /**
     * @covers                   Monolog\Handler\ElasticSearchHandler::setFormatter
     * @expectedException        InvalidArgumentException
     * @expectedExceptionMessage ElasticSearchHandler is only compatible with ElasticaFormatter
     */
    public function testSetFormatterInvalid()
    {
        $handler = new ElasticSearchHandler($this->client);
        $formatter = new NormalizerFormatter();
        $handler->setFormatter($formatter);
    }

    /**
     * @covers Monolog\Handler\ElasticSearchHandler::__construct
     * @covers Monolog\Handler\ElasticSearchHandler::getOptions
     */
    public function testOptions()
    {
        $expected = array(
            'index' => $this->options['index'],
            'type' => $this->options['type'],
            'ignore_error' => false,
        );
        $handler = new ElasticSearchHandler($this->client, $this->options);
        $this->assertEquals($expected, $handler->getOptions());
    }

    /**
     * @covers       Monolog\Handler\ElasticSearchHandler::bulkSend
     * @dataProvider providerTestConnectionErrors
     */
    public function testConnectionErrors($ignore, $expectedError)
    {
        $clientOpts = array('host' => '127.0.0.1', 'port' => 1);
        $client = new Client($clientOpts);
        $handlerOpts = array('ignore_error' => $ignore);
        $handler = new ElasticSearchHandler($client, $handlerOpts);

        if ($expectedError) {
            $this->setExpectedException($expectedError[0], $expectedError[1]);
            $handler->handle($this->getRecord());
        } else {
            $this->assertFalse($handler->handle($this->getRecord()));
        }
    }

    /**
     * @return array
     */
    public function providerTestConnectionErrors()
    {
        return array(
            array(false, array('RuntimeException', 'Error sending messages to Elasticsearch')),
            array(true, false),
        );
    }

    /**
     * Integration test using localhost Elastic Search server
     *
     * @covers Monolog\Handler\ElasticSearchHandler::__construct
     * @covers Monolog\Handler\ElasticSearchHandler::handleBatch
     * @covers Monolog\Handler\ElasticSearchHandler::bulkSend
     * @covers Monolog\Handler\ElasticSearchHandler::getDefaultFormatter
     */
    public function testHandleIntegration()
    {
        $msg = array(
            'level' => Logger::ERROR,
            'level_name' => 'ERROR',
            'channel' => 'meh',
            'context' => array('foo' => 7, 'bar', 'class' => new \stdClass),
            'datetime' => new \DateTime("@0"),
            'extra' => array(),
            'message' => 'log',
        );

        $expected = $msg;
        $expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601);
        $expected['context'] = array(
            'class' => '[object] (stdClass: {})',
            'foo' => 7,
            0 => 'bar',
        );

        $client = new Client();
        $handler = new ElasticSearchHandler($client, $this->options);
        try {
            $handler->handleBatch(array($msg));
        } catch (\RuntimeException $e) {
            $this->markTestSkipped("Cannot connect to Elastic Search server on localhost");
        }

        // check document id from ES server response
        $documentId = $this->getCreatedDocId($client->getLastResponse());
        $this->assertNotEmpty($documentId, 'No elastic document id received');

        // retrieve document source from ES and validate
        $document = $this->getDocSourceFromElastic(
            $client,
            $this->options['index'],
            $this->options['type'],
            $documentId
        );
        $this->assertEquals($expected, $document);

        // remove test index from ES
        $client->request("/{$this->options['index']}", Request::DELETE);
    }

    /**
     * Return last created document id from ES response
     * @param  Response    $response Elastica Response object
     * @return string|null
     */
    protected function getCreatedDocId(Response $response)
    {
        $data = $response->getData();
        if (!empty($data['items'][0]['create']['_id'])) {
            return $data['items'][0]['create']['_id'];
        }
    }

    /**
     * Retrieve document by id from Elasticsearch
     * @param  Client $client     Elastica client
     * @param  string $index
     * @param  string $type
     * @param  string $documentId
     * @return array
     */
    protected function getDocSourceFromElastic(Client $client, $index, $type, $documentId)
    {
        $resp = $client->request("/{$index}/{$type}/{$documentId}", Request::GET);
        $data = $resp->getData();
        if (!empty($data['_source'])) {
            return $data['_source'];
        }

        return array();
    }
}

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