No Description

ElasticaFormatterTest.php 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. class ElasticaFormatterTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function setUp()
  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 = array(
  29. 'level' => Logger::ERROR,
  30. 'level_name' => 'ERROR',
  31. 'channel' => 'meh',
  32. 'context' => array('foo' => 7, 'bar', 'class' => new \stdClass),
  33. 'datetime' => new \DateTime("@0"),
  34. 'extra' => array(),
  35. 'message' => 'log',
  36. );
  37. // expected values
  38. $expected = $msg;
  39. $expected['datetime'] = '1970-01-01T00:00:00.000000+00:00';
  40. $expected['context'] = array(
  41. 'class' => '[object] (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. $params = $doc->getParams();
  51. $this->assertEquals('my_index', $params['_index']);
  52. $this->assertEquals('doc_type', $params['_type']);
  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. }