ElasticsearchFormatterTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Formatter;
  11. use Monolog\Level;
  12. use Monolog\Test\MonologTestCase;
  13. class ElasticsearchFormatterTest extends MonologTestCase
  14. {
  15. /**
  16. * @covers Monolog\Formatter\ElasticsearchFormatter::__construct
  17. * @covers Monolog\Formatter\ElasticsearchFormatter::format
  18. * @covers Monolog\Formatter\ElasticsearchFormatter::getDocument
  19. */
  20. public function testFormat()
  21. {
  22. // Test log message
  23. $msg = $this->getRecord(
  24. Level::Error,
  25. 'log',
  26. channel: 'meh',
  27. context: ['foo' => 7, 'bar', 'class' => new \stdClass],
  28. datetime: new \DateTimeImmutable("@0"),
  29. );
  30. // Expected values
  31. $expected = $msg->toArray();
  32. $expected['datetime'] = '1970-01-01T00:00:00+00:00';
  33. $expected['context'] = [
  34. 'class' => ['stdClass' => []],
  35. 'foo' => 7,
  36. 0 => 'bar',
  37. ];
  38. // Format log message
  39. $formatter = new ElasticsearchFormatter('my_index', 'doc_type');
  40. $doc = $formatter->format($msg);
  41. $this->assertIsArray($doc);
  42. // Record parameters
  43. $this->assertEquals('my_index', $doc['_index']);
  44. $this->assertEquals('doc_type', $doc['_type']);
  45. // Record data values
  46. foreach (array_keys($expected) as $key) {
  47. $this->assertEquals($expected[$key], $doc[$key]);
  48. }
  49. }
  50. /**
  51. * @covers Monolog\Formatter\ElasticsearchFormatter::getIndex
  52. * @covers Monolog\Formatter\ElasticsearchFormatter::getType
  53. */
  54. public function testGetters()
  55. {
  56. $formatter = new ElasticsearchFormatter('my_index', 'doc_type');
  57. $this->assertEquals('my_index', $formatter->getIndex());
  58. $this->assertEquals('doc_type', $formatter->getType());
  59. }
  60. }