ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Monolog\Handler\ElasticSearchHandlerTest Class Reference
+ Inheritance diagram for Monolog\Handler\ElasticSearchHandlerTest:
+ Collaboration diagram for Monolog\Handler\ElasticSearchHandlerTest:

Public Member Functions

 setUp ()
 
 testHandle ()
 @covers Monolog\Handler\ElasticSearchHandler::write @covers Monolog\Handler\ElasticSearchHandler::handleBatch @covers Monolog\Handler\ElasticSearchHandler::bulkSend @covers Monolog\Handler\ElasticSearchHandler::getDefaultFormatter More...
 
 testSetFormatter ()
 @covers Monolog\Handler\ElasticSearchHandler::setFormatter More...
 
 testSetFormatterInvalid ()
 @covers Monolog\Handler\ElasticSearchHandler::setFormatter @expectedException InvalidArgumentException @expectedExceptionMessage ElasticSearchHandler is only compatible with ElasticaFormatter More...
 
 testOptions ()
 @covers Monolog\Handler\ElasticSearchHandler::__construct @covers Monolog\Handler\ElasticSearchHandler::getOptions More...
 
 testConnectionErrors ($ignore, $expectedError)
 @covers Monolog\Handler\ElasticSearchHandler::bulkSend @dataProvider providerTestConnectionErrors More...
 
 providerTestConnectionErrors ()
 
 testHandleIntegration ()
 Integration test using localhost Elastic Search server. More...
 

Protected Member Functions

 getCreatedDocId (Response $response)
 Return last created document id from ES response. More...
 
 getDocSourceFromElastic (Client $client, $index, $type, $documentId)
 Retrieve document by id from Elasticsearch. More...
 
- Protected Member Functions inherited from Monolog\TestCase
 getRecord ($level=Logger::WARNING, $message='test', $context=array())
 
 getMultipleRecords ()
 
 getIdentityFormatter ()
 

Protected Attributes

 $client
 
 $options
 

Detailed Description

Definition at line 22 of file ElasticSearchHandlerTest.php.

Member Function Documentation

◆ getCreatedDocId()

Monolog\Handler\ElasticSearchHandlerTest::getCreatedDocId ( Response  $response)
protected

Return last created document id from ES response.

Parameters
Response$responseElastica Response object
Returns
string|null

Definition at line 213 of file ElasticSearchHandlerTest.php.

214 {
215 $data = $response->getData();
216 if (!empty($data['items'][0]['create']['_id'])) {
217 return $data['items'][0]['create']['_id'];
218 }
219 }
$data

References $data.

Referenced by Monolog\Handler\ElasticSearchHandlerTest\testHandleIntegration().

+ Here is the caller graph for this function:

◆ getDocSourceFromElastic()

Monolog\Handler\ElasticSearchHandlerTest::getDocSourceFromElastic ( Client  $client,
  $index,
  $type,
  $documentId 
)
protected

Retrieve document by id from Elasticsearch.

Parameters
Client$clientElastica client
string$index
string$type
string$documentId
Returns
array

Definition at line 229 of file ElasticSearchHandlerTest.php.

230 {
231 $resp = $client->request("/{$index}/{$type}/{$documentId}", Request::GET);
232 $data = $resp->getData();
233 if (!empty($data['_source'])) {
234 return $data['_source'];
235 }
236
237 return array();
238 }

References Monolog\Handler\ElasticSearchHandlerTest\$client, and $data.

Referenced by Monolog\Handler\ElasticSearchHandlerTest\testHandleIntegration().

+ Here is the caller graph for this function:

◆ providerTestConnectionErrors()

Monolog\Handler\ElasticSearchHandlerTest::providerTestConnectionErrors ( )
Returns
array

Definition at line 147 of file ElasticSearchHandlerTest.php.

148 {
149 return array(
150 array(false, array('RuntimeException', 'Error sending messages to Elasticsearch')),
151 array(true, false),
152 );
153 }

◆ setUp()

Monolog\Handler\ElasticSearchHandlerTest::setUp ( )

Definition at line 37 of file ElasticSearchHandlerTest.php.

38 {
39 // Elastica lib required
40 if (!class_exists("Elastica\Client")) {
41 $this->markTestSkipped("ruflin/elastica not installed");
42 }
43
44 // base mock Elastica Client object
45 $this->client = $this->getMockBuilder('Elastica\Client')
46 ->setMethods(array('addDocuments'))
47 ->disableOriginalConstructor()
48 ->getMock();
49 }

◆ testConnectionErrors()

Monolog\Handler\ElasticSearchHandlerTest::testConnectionErrors (   $ignore,
  $expectedError 
)

@covers Monolog\Handler\ElasticSearchHandler::bulkSend @dataProvider providerTestConnectionErrors

Definition at line 129 of file ElasticSearchHandlerTest.php.

130 {
131 $clientOpts = array('host' => '127.0.0.1', 'port' => 1);
132 $client = new Client($clientOpts);
133 $handlerOpts = array('ignore_error' => $ignore);
134 $handler = new ElasticSearchHandler($client, $handlerOpts);
135
136 if ($expectedError) {
137 $this->setExpectedException($expectedError[0], $expectedError[1]);
138 $handler->handle($this->getRecord());
139 } else {
140 $this->assertFalse($handler->handle($this->getRecord()));
141 }
142 }
while(false !==($line=fgets($in))) if(! $columns) $ignore
Definition: Utf8Test.php:64
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19

References Monolog\Handler\ElasticSearchHandlerTest\$client, $ignore, and Monolog\TestCase\getRecord().

+ Here is the call graph for this function:

◆ testHandle()

Monolog\Handler\ElasticSearchHandlerTest::testHandle ( )

@covers Monolog\Handler\ElasticSearchHandler::write @covers Monolog\Handler\ElasticSearchHandler::handleBatch @covers Monolog\Handler\ElasticSearchHandler::bulkSend @covers Monolog\Handler\ElasticSearchHandler::getDefaultFormatter

Definition at line 57 of file ElasticSearchHandlerTest.php.

58 {
59 // log message
60 $msg = array(
61 'level' => Logger::ERROR,
62 'level_name' => 'ERROR',
63 'channel' => 'meh',
64 'context' => array('foo' => 7, 'bar', 'class' => new \stdClass),
65 'datetime' => new \DateTime("@0"),
66 'extra' => array(),
67 'message' => 'log',
68 );
69
70 // format expected result
71 $formatter = new ElasticaFormatter($this->options['index'], $this->options['type']);
72 $expected = array($formatter->format($msg));
73
74 // setup ES client mock
75 $this->client->expects($this->any())
76 ->method('addDocuments')
77 ->with($expected);
78
79 // perform tests
80 $handler = new ElasticSearchHandler($this->client, $this->options);
81 $handler->handle($msg);
82 $handler->handleBatch(array($msg));
83 }
const ERROR
Runtime errors.
Definition: Logger.php:57

References Monolog\Logger\ERROR.

◆ testHandleIntegration()

Monolog\Handler\ElasticSearchHandlerTest::testHandleIntegration ( )

Integration test using localhost Elastic Search server.

@covers Monolog\Handler\ElasticSearchHandler::__construct @covers Monolog\Handler\ElasticSearchHandler::handleBatch @covers Monolog\Handler\ElasticSearchHandler::bulkSend @covers Monolog\Handler\ElasticSearchHandler::getDefaultFormatter

Definition at line 163 of file ElasticSearchHandlerTest.php.

164 {
165 $msg = array(
166 'level' => Logger::ERROR,
167 'level_name' => 'ERROR',
168 'channel' => 'meh',
169 'context' => array('foo' => 7, 'bar', 'class' => new \stdClass),
170 'datetime' => new \DateTime("@0"),
171 'extra' => array(),
172 'message' => 'log',
173 );
174
175 $expected = $msg;
176 $expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601);
177 $expected['context'] = array(
178 'class' => '[object] (stdClass: {})',
179 'foo' => 7,
180 0 => 'bar',
181 );
182
183 $client = new Client();
184 $handler = new ElasticSearchHandler($client, $this->options);
185 try {
186 $handler->handleBatch(array($msg));
187 } catch (\RuntimeException $e) {
188 $this->markTestSkipped("Cannot connect to Elastic Search server on localhost");
189 }
190
191 // check document id from ES server response
192 $documentId = $this->getCreatedDocId($client->getLastResponse());
193 $this->assertNotEmpty($documentId, 'No elastic document id received');
194
195 // retrieve document source from ES and validate
196 $document = $this->getDocSourceFromElastic(
197 $client,
198 $this->options['index'],
199 $this->options['type'],
200 $documentId
201 );
202 $this->assertEquals($expected, $document);
203
204 // remove test index from ES
205 $client->request("/{$this->options['index']}", Request::DELETE);
206 }
getDocSourceFromElastic(Client $client, $index, $type, $documentId)
Retrieve document by id from Elasticsearch.
getCreatedDocId(Response $response)
Return last created document id from ES response.

References Monolog\Handler\ElasticSearchHandlerTest\$client, Monolog\Logger\ERROR, Monolog\Handler\ElasticSearchHandlerTest\getCreatedDocId(), and Monolog\Handler\ElasticSearchHandlerTest\getDocSourceFromElastic().

+ Here is the call graph for this function:

◆ testOptions()

Monolog\Handler\ElasticSearchHandlerTest::testOptions ( )

@covers Monolog\Handler\ElasticSearchHandler::__construct @covers Monolog\Handler\ElasticSearchHandler::getOptions

Definition at line 114 of file ElasticSearchHandlerTest.php.

115 {
116 $expected = array(
117 'index' => $this->options['index'],
118 'type' => $this->options['type'],
119 'ignore_error' => false,
120 );
121 $handler = new ElasticSearchHandler($this->client, $this->options);
122 $this->assertEquals($expected, $handler->getOptions());
123 }

◆ testSetFormatter()

Monolog\Handler\ElasticSearchHandlerTest::testSetFormatter ( )

@covers Monolog\Handler\ElasticSearchHandler::setFormatter

Definition at line 88 of file ElasticSearchHandlerTest.php.

89 {
90 $handler = new ElasticSearchHandler($this->client);
91 $formatter = new ElasticaFormatter('index_new', 'type_new');
92 $handler->setFormatter($formatter);
93 $this->assertInstanceOf('Monolog\Formatter\ElasticaFormatter', $handler->getFormatter());
94 $this->assertEquals('index_new', $handler->getFormatter()->getIndex());
95 $this->assertEquals('type_new', $handler->getFormatter()->getType());
96 }

◆ testSetFormatterInvalid()

Monolog\Handler\ElasticSearchHandlerTest::testSetFormatterInvalid ( )

@covers Monolog\Handler\ElasticSearchHandler::setFormatter @expectedException InvalidArgumentException @expectedExceptionMessage ElasticSearchHandler is only compatible with ElasticaFormatter

Definition at line 103 of file ElasticSearchHandlerTest.php.

104 {
105 $handler = new ElasticSearchHandler($this->client);
106 $formatter = new NormalizerFormatter();
107 $handler->setFormatter($formatter);
108 }

Field Documentation

◆ $client

◆ $options

Monolog\Handler\ElasticSearchHandlerTest::$options
protected
Initial value:
= array(
'index' => 'my_index',
'type' => 'doc_type',
)

Definition at line 32 of file ElasticSearchHandlerTest.php.


The documentation for this class was generated from the following file: