ElasticaFormatterTest.php 2.3 KB

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