ElasticsearchFormatterTest.php 2.1 KB

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