2
0

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\Logger;
  12. class ElasticaFormatterTest extends \PHPUnit\Framework\TestCase
  13. {
  14. public function setUp(): void
  15. {
  16. if (!class_exists("Elastica\Document")) {
  17. $this->markTestSkipped("ruflin/elastica not installed");
  18. }
  19. }
  20. /**
  21. * @covers Monolog\Formatter\ElasticaFormatter::__construct
  22. * @covers Monolog\Formatter\ElasticaFormatter::format
  23. * @covers Monolog\Formatter\ElasticaFormatter::getDocument
  24. */
  25. public function testFormat()
  26. {
  27. // test log message
  28. $msg = [
  29. 'level' => Logger::ERROR,
  30. 'level_name' => 'ERROR',
  31. 'channel' => 'meh',
  32. 'context' => ['foo' => 7, 'bar', 'class' => new \stdClass],
  33. 'datetime' => new \DateTimeImmutable("@0"),
  34. 'extra' => [],
  35. 'message' => 'log',
  36. ];
  37. // expected values
  38. $expected = $msg;
  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. }