ElasticaFormatterTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 ElasticaFormatterTest extends MonologTestCase
  14. {
  15. public function setUp(): void
  16. {
  17. if (!class_exists("Elastica\Document")) {
  18. $this->markTestSkipped("ruflin/elastica not installed");
  19. }
  20. }
  21. /**
  22. * @covers Monolog\Formatter\ElasticaFormatter::__construct
  23. * @covers Monolog\Formatter\ElasticaFormatter::format
  24. * @covers Monolog\Formatter\ElasticaFormatter::getDocument
  25. */
  26. public function testFormat()
  27. {
  28. // test log message
  29. $msg = $this->getRecord(
  30. Level::Error,
  31. 'log',
  32. channel: 'meh',
  33. context: ['foo' => 7, 'bar', 'class' => new \stdClass],
  34. datetime: new \DateTimeImmutable("@0"),
  35. );
  36. // expected values
  37. $expected = $msg->toArray();
  38. $expected['datetime'] = '1970-01-01T00:00:00.000000+00:00';
  39. $expected['context'] = [
  40. 'class' => ['stdClass' => []],
  41. 'foo' => 7,
  42. 0 => 'bar',
  43. ];
  44. // format log message
  45. $formatter = new ElasticaFormatter('my_index', 'doc_type');
  46. $doc = $formatter->format($msg);
  47. $this->assertInstanceOf('Elastica\Document', $doc);
  48. // Document parameters
  49. $this->assertEquals('my_index', $doc->getIndex());
  50. if (method_exists($doc, 'getType')) {
  51. $this->assertEquals('doc_type', $doc->getType());
  52. }
  53. // Document data values
  54. $data = $doc->getData();
  55. foreach (array_keys($expected) as $key) {
  56. $this->assertEquals($expected[$key], $data[$key]);
  57. }
  58. }
  59. /**
  60. * @covers Monolog\Formatter\ElasticaFormatter::getIndex
  61. * @covers Monolog\Formatter\ElasticaFormatter::getType
  62. */
  63. public function testGetters()
  64. {
  65. $formatter = new ElasticaFormatter('my_index', 'doc_type');
  66. $this->assertEquals('my_index', $formatter->getIndex());
  67. $this->assertEquals('doc_type', $formatter->getType());
  68. }
  69. }