From 76639ef02bbca5efa6dfb99e7cea6a269f14d421 Mon Sep 17 00:00:00 2001 From: patrickkusebauch Date: Fri, 11 Dec 2020 10:55:38 +0100 Subject: [PATCH] Elastica up to 7 support --- composer.json | 2 +- src/Monolog/Formatter/ElasticaFormatter.php | 7 +- src/Monolog/Handler/ElasticaHandler.php | 2 +- .../Formatter/ElasticaFormatterTest.php | 7 +- tests/Monolog/Handler/ElasticaHandlerTest.php | 66 ++++++++++++++++++- 5 files changed, 75 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 37a9ec58..27d43337 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "phpunit/phpunit": "^8.5", "predis/predis": "^1.1", "rollbar/rollbar": "^1.3", - "ruflin/elastica": ">=0.90 <3.0", + "ruflin/elastica": ">=0.90 <7.0.1", "swiftmailer/swiftmailer": "^5.3|^6.0", "phpstan/phpstan": "^0.12.59" }, diff --git a/src/Monolog/Formatter/ElasticaFormatter.php b/src/Monolog/Formatter/ElasticaFormatter.php index c6f3c8e1..d15ea5f2 100644 --- a/src/Monolog/Formatter/ElasticaFormatter.php +++ b/src/Monolog/Formatter/ElasticaFormatter.php @@ -58,6 +58,9 @@ class ElasticaFormatter extends NormalizerFormatter return $this->index; } + /** + * @deprecated since Elastica 7 type has no effect + */ public function getType(): string { return $this->type; @@ -72,7 +75,9 @@ class ElasticaFormatter extends NormalizerFormatter { $document = new Document(); $document->setData($record); - $document->setType($this->type); + if(method_exists($document, 'setType')) { + $document->setType($this->type); + } $document->setIndex($this->index); return $document; diff --git a/src/Monolog/Handler/ElasticaHandler.php b/src/Monolog/Handler/ElasticaHandler.php index 78a8d1ea..7af68fef 100644 --- a/src/Monolog/Handler/ElasticaHandler.php +++ b/src/Monolog/Handler/ElasticaHandler.php @@ -25,7 +25,7 @@ use Elastica\Exception\ExceptionInterface; * $client = new \Elastica\Client(); * $options = array( * 'index' => 'elastic_index_name', - * 'type' => 'elastic_doc_type', + * 'type' => 'elastic_doc_type', Types have been removed in Elastica 7 * ); * $handler = new ElasticaHandler($client, $options); * $log = new Logger('application'); diff --git a/tests/Monolog/Formatter/ElasticaFormatterTest.php b/tests/Monolog/Formatter/ElasticaFormatterTest.php index e2c97778..d42ca2a5 100644 --- a/tests/Monolog/Formatter/ElasticaFormatterTest.php +++ b/tests/Monolog/Formatter/ElasticaFormatterTest.php @@ -55,9 +55,10 @@ class ElasticaFormatterTest extends \PHPUnit\Framework\TestCase $this->assertInstanceOf('Elastica\Document', $doc); // Document parameters - $params = $doc->getParams(); - $this->assertEquals('my_index', $params['_index']); - $this->assertEquals('doc_type', $params['_type']); + $this->assertEquals('my_index', $doc->getIndex()); + if(method_exists($doc, 'getType')) { + $this->assertEquals('doc_type', $doc->getType()); + } // Document data values $data = $doc->getData(); diff --git a/tests/Monolog/Handler/ElasticaHandlerTest.php b/tests/Monolog/Handler/ElasticaHandlerTest.php index 453752f2..1bb92ded 100644 --- a/tests/Monolog/Handler/ElasticaHandlerTest.php +++ b/tests/Monolog/Handler/ElasticaHandlerTest.php @@ -156,7 +156,7 @@ class ElasticaHandlerTest extends TestCase } /** - * Integration test using localhost Elastic Search server + * Integration test using localhost Elastic Search server version <7 * * @covers Monolog\Handler\ElasticaHandler::__construct * @covers Monolog\Handler\ElasticaHandler::handleBatch @@ -209,6 +209,61 @@ class ElasticaHandlerTest extends TestCase $client->request("/{$this->options['index']}", Request::DELETE); } + /** + * Integration test using localhost Elastic Search server version 7+ + * + * @covers Monolog\Handler\ElasticaHandler::__construct + * @covers Monolog\Handler\ElasticaHandler::handleBatch + * @covers Monolog\Handler\ElasticaHandler::bulkSend + * @covers Monolog\Handler\ElasticaHandler::getDefaultFormatter + */ + public function testHandleIntegrationNewESVersion() + { + $msg = [ + 'level' => Logger::ERROR, + 'level_name' => 'ERROR', + 'channel' => 'meh', + 'context' => ['foo' => 7, 'bar', 'class' => new \stdClass], + 'datetime' => new \DateTimeImmutable("@0"), + 'extra' => [], + 'message' => 'log', + ]; + + $expected = $msg; + $expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601); + $expected['context'] = [ + 'class' => '[object] (stdClass: {})', + 'foo' => 7, + 0 => 'bar', + ]; + + $client = new Client(); + $handler = new ElasticaHandler($client, $this->options); + + try { + $handler->handleBatch([$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'], + null, + $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 @@ -226,13 +281,18 @@ class ElasticaHandlerTest extends TestCase * Retrieve document by id from Elasticsearch * @param Client $client Elastica client * @param string $index - * @param string $type + * @param ?string $type * @param string $documentId * @return array */ protected function getDocSourceFromElastic(Client $client, $index, $type, $documentId) { - $resp = $client->request("/{$index}/{$type}/{$documentId}", Request::GET); + if($type === null) { + $path = "/{$index}/{$documentId}"; + } else { + $path = "/{$index}/{$type}/{$documentId}"; + } + $resp = $client->request($path, Request::GET); $data = $resp->getData(); if (!empty($data['_source'])) { return $data['_source'];