ElasticaFormatterTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  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. use Monolog\Formatter\ElasticaFormatter;
  13. class ElasticaFormatterTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function setUp()
  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 = array(
  30. 'level' => Logger::ERROR,
  31. 'level_name' => 'ERROR',
  32. 'channel' => 'meh',
  33. 'context' => array('foo' => 7, 'bar', 'class' => new \stdClass),
  34. 'datetime' => new \DateTime("@0"),
  35. 'extra' => array(),
  36. 'message' => 'log',
  37. );
  38. // expected values
  39. $expected = $msg;
  40. $expected['datetime'] = '1970-01-01T00:00:00+0000';
  41. $expected['context'] = array(
  42. 'class' => '[object] (stdClass: {})',
  43. 'foo' => 7,
  44. 0 => 'bar',
  45. );
  46. // format log message
  47. $formatter = new ElasticaFormatter('my_index', 'doc_type');
  48. $doc = $formatter->format($msg);
  49. $this->assertInstanceOf('Elastica\Document', $doc);
  50. // Document parameters
  51. $params = $doc->getParams();
  52. $this->assertEquals('my_index', $params['_index']);
  53. $this->assertEquals('doc_type', $params['_type']);
  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. }