ElasticaHandlerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Formatter\ElasticaFormatter;
  12. use Monolog\Formatter\NormalizerFormatter;
  13. use Monolog\Level;
  14. use Elastica\Client;
  15. use Elastica\Request;
  16. use Elastica\Response;
  17. use PHPUnit\Framework\Attributes\DataProvider;
  18. use PHPUnit\Framework\Attributes\Group;
  19. #[Group('Elastica')]
  20. class ElasticaHandlerTest extends \Monolog\Test\MonologTestCase
  21. {
  22. /**
  23. * @var Client mock
  24. */
  25. protected Client $client;
  26. /**
  27. * @var array Default handler options
  28. */
  29. protected array $options = [
  30. 'index' => 'my_index',
  31. 'type' => 'doc_type',
  32. ];
  33. public function setUp(): void
  34. {
  35. // Elastica lib required
  36. if (!class_exists("Elastica\Client")) {
  37. $this->markTestSkipped("ruflin/elastica not installed");
  38. }
  39. // base mock Elastica Client object
  40. $this->client = $this->getMockBuilder('Elastica\Client')
  41. ->onlyMethods(['addDocuments'])
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. }
  45. public function tearDown(): void
  46. {
  47. parent::tearDown();
  48. unset($this->client);
  49. }
  50. /**
  51. * @covers Monolog\Handler\ElasticaHandler::write
  52. * @covers Monolog\Handler\ElasticaHandler::handleBatch
  53. * @covers Monolog\Handler\ElasticaHandler::bulkSend
  54. * @covers Monolog\Handler\ElasticaHandler::getDefaultFormatter
  55. */
  56. public function testHandle()
  57. {
  58. // log message
  59. $msg = $this->getRecord(Level::Error, 'log', context: ['foo' => 7, 'bar', 'class' => new \stdClass], datetime: new \DateTimeImmutable("@0"));
  60. // format expected result
  61. $formatter = new ElasticaFormatter($this->options['index'], $this->options['type']);
  62. $expected = [$formatter->format($msg)];
  63. // setup ES client mock
  64. $this->client->expects($this->any())
  65. ->method('addDocuments')
  66. ->with($expected);
  67. // perform tests
  68. $handler = new ElasticaHandler($this->client, $this->options);
  69. $handler->handle($msg);
  70. $handler->handleBatch([$msg]);
  71. }
  72. /**
  73. * @covers Monolog\Handler\ElasticaHandler::setFormatter
  74. */
  75. public function testSetFormatter()
  76. {
  77. $handler = new ElasticaHandler($this->client);
  78. $formatter = new ElasticaFormatter('index_new', 'type_new');
  79. $handler->setFormatter($formatter);
  80. $this->assertInstanceOf('Monolog\Formatter\ElasticaFormatter', $handler->getFormatter());
  81. $this->assertEquals('index_new', $handler->getFormatter()->getIndex());
  82. $this->assertEquals('type_new', $handler->getFormatter()->getType());
  83. }
  84. /**
  85. * @covers Monolog\Handler\ElasticaHandler::setFormatter
  86. */
  87. public function testSetFormatterInvalid()
  88. {
  89. $handler = new ElasticaHandler($this->client);
  90. $formatter = new NormalizerFormatter();
  91. $this->expectException(\InvalidArgumentException::class);
  92. $this->expectExceptionMessage('ElasticaHandler is only compatible with ElasticaFormatter');
  93. $handler->setFormatter($formatter);
  94. }
  95. /**
  96. * @covers Monolog\Handler\ElasticaHandler::__construct
  97. * @covers Monolog\Handler\ElasticaHandler::getOptions
  98. */
  99. public function testOptions()
  100. {
  101. $expected = [
  102. 'index' => $this->options['index'],
  103. 'type' => $this->options['type'],
  104. 'ignore_error' => false,
  105. ];
  106. $handler = new ElasticaHandler($this->client, $this->options);
  107. $this->assertEquals($expected, $handler->getOptions());
  108. }
  109. /**
  110. * @covers Monolog\Handler\ElasticaHandler::bulkSend
  111. */
  112. #[DataProvider('providerTestConnectionErrors')]
  113. public function testConnectionErrors($ignore, $expectedError)
  114. {
  115. $clientOpts = ['host' => '127.0.0.1', 'port' => 1];
  116. $client = new Client($clientOpts);
  117. $handlerOpts = ['ignore_error' => $ignore];
  118. $handler = new ElasticaHandler($client, $handlerOpts);
  119. if ($expectedError) {
  120. $this->expectException($expectedError[0]);
  121. $this->expectExceptionMessage($expectedError[1]);
  122. $handler->handle($this->getRecord());
  123. } else {
  124. $this->assertFalse($handler->handle($this->getRecord()));
  125. }
  126. }
  127. public static function providerTestConnectionErrors(): array
  128. {
  129. return [
  130. [false, ['RuntimeException', 'Error sending messages to Elasticsearch']],
  131. [true, false],
  132. ];
  133. }
  134. /**
  135. * Integration test using localhost Elastic Search server version 7+
  136. *
  137. * @covers Monolog\Handler\ElasticaHandler::__construct
  138. * @covers Monolog\Handler\ElasticaHandler::handleBatch
  139. * @covers Monolog\Handler\ElasticaHandler::bulkSend
  140. * @covers Monolog\Handler\ElasticaHandler::getDefaultFormatter
  141. */
  142. public function testHandleIntegrationNewESVersion()
  143. {
  144. $msg = $this->getRecord(Level::Error, 'log', context: ['foo' => 7, 'bar', 'class' => new \stdClass], datetime: new \DateTimeImmutable("@0"));
  145. $expected = (array) $msg;
  146. $expected['datetime'] = $msg['datetime']->format(\DateTime::ATOM);
  147. $expected['context'] = [
  148. 'class' => '[object] (stdClass: {})',
  149. 'foo' => 7,
  150. 0 => 'bar',
  151. ];
  152. $clientOpts = ['url' => 'http://elastic:changeme@127.0.0.1:9200'];
  153. $client = new Client($clientOpts);
  154. $handler = new ElasticaHandler($client, $this->options);
  155. try {
  156. $handler->handleBatch([$msg]);
  157. } catch (\RuntimeException $e) {
  158. $this->markTestSkipped("Cannot connect to Elastic Search server on localhost");
  159. }
  160. // check document id from ES server response
  161. $documentId = $this->getCreatedDocId($client->getLastResponse());
  162. $this->assertNotEmpty($documentId, 'No elastic document id received');
  163. // retrieve document source from ES and validate
  164. $document = $this->getDocSourceFromElastic(
  165. $client,
  166. $this->options['index'],
  167. null,
  168. $documentId
  169. );
  170. $this->assertEquals($expected, $document);
  171. // remove test index from ES
  172. $client->request("/{$this->options['index']}", Request::DELETE);
  173. }
  174. /**
  175. * Return last created document id from ES response
  176. * @param Response $response Elastica Response object
  177. */
  178. protected function getCreatedDocId(Response $response): ?string
  179. {
  180. $data = $response->getData();
  181. if (!empty($data['items'][0]['index']['_id'])) {
  182. return $data['items'][0]['index']['_id'];
  183. }
  184. var_dump('Unexpected response: ', $data);
  185. return null;
  186. }
  187. /**
  188. * Retrieve document by id from Elasticsearch
  189. * @param Client $client Elastica client
  190. * @param ?string $type
  191. */
  192. protected function getDocSourceFromElastic(Client $client, string $index, $type, string $documentId): array
  193. {
  194. if ($type === null) {
  195. $path = "/{$index}/_doc/{$documentId}";
  196. } else {
  197. $path = "/{$index}/{$type}/{$documentId}";
  198. }
  199. $resp = $client->request($path, Request::GET);
  200. $data = $resp->getData();
  201. if (!empty($data['_source'])) {
  202. return $data['_source'];
  203. }
  204. return [];
  205. }
  206. }