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

Public Member Functions

 setUp ()
 
 testHandle ()
 Monolog::write Monolog::handleBatch Monolog::bulkSend Monolog::getDefaultFormatter More...
 
 testSetFormatter ()
 Monolog::setFormatter More...
 
 testSetFormatterInvalid ()
 Monolog::setFormatter InvalidArgumentException ElasticSearchHandler is only compatible with ElasticaFormatter More...
 
 testOptions ()
 Monolog::__construct Monolog::getOptions More...
 
 testConnectionErrors ($ignore, $expectedError)
 Monolog::bulkSend 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.

References $data.

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

214  {
215  $data = $response->getData();
216  if (!empty($data['items'][0]['create']['_id'])) {
217  return $data['items'][0]['create']['_id'];
218  }
219  }
$data
+ 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.

References $data.

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

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  }
$data
+ 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 
)

Monolog::bulkSend providerTestConnectionErrors

Definition at line 129 of file ElasticSearchHandlerTest.php.

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

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
+ Here is the call graph for this function:

◆ testHandle()

Monolog\Handler\ElasticSearchHandlerTest::testHandle ( )

Monolog::write Monolog::handleBatch Monolog::bulkSend Monolog::getDefaultFormatter

Definition at line 57 of file ElasticSearchHandlerTest.php.

References Monolog\Logger\ERROR.

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

◆ testHandleIntegration()

Monolog\Handler\ElasticSearchHandlerTest::testHandleIntegration ( )

Integration test using localhost Elastic Search server.

Monolog::__construct Monolog::handleBatch Monolog::bulkSend Monolog::getDefaultFormatter

Definition at line 163 of file ElasticSearchHandlerTest.php.

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

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  }
const ERROR
Runtime errors.
Definition: Logger.php:57
getDocSourceFromElastic(Client $client, $index, $type, $documentId)
Retrieve document by id from Elasticsearch.
getCreatedDocId(Response $response)
Return last created document id from ES response.
+ Here is the call graph for this function:

◆ testOptions()

Monolog\Handler\ElasticSearchHandlerTest::testOptions ( )

Monolog::__construct Monolog::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 ( )

Monolog::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 ( )

Monolog::setFormatter InvalidArgumentException 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

Monolog\Handler\ElasticSearchHandlerTest::$client
protected

◆ $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: