-
-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathElasticsearchTargetTest.php
73 lines (58 loc) · 1.89 KB
/
ElasticsearchTargetTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* @author Eugene Terentev <eugene@terentev.net>
*/
namespace yiiunit\extensions\elasticsearch;
use yii\elasticsearch\ElasticsearchTarget;
use yii\elasticsearch\Query;
use yii\log\Dispatcher;
use yii\log\Logger;
class ElasticsearchTargetTest extends TestCase
{
public $logger;
public $index = 'yiilogtest';
public $type = 'log';
public function testExport()
{
$logger = $this->logger;
$logger->log('Test message', Logger::LEVEL_INFO, 'test-category');
$logger->flush(true);
$this->getConnection()->createCommand()->refreshIndex($this->index);
$query = new Query();
$query->from($this->index, $this->type);
$message = $query->one($this->getConnection());
$this->assertArrayHasKey('_source', $message);
$source = $message['_source'];
$this->assertArrayHasKey('@timestamp', $source);
$this->assertArrayHasKey('message', $source);
$this->assertArrayHasKey('level', $source);
$this->assertArrayHasKey('category', $source);
}
protected function setUp(): void
{
parent::setUp();
$command = $this->getConnection()->createCommand();
// delete index
if ($command->indexExists($this->index)) {
$command->deleteIndex($this->index);
}
$this->logger = new Logger();
$dispatcher = new Dispatcher([
'logger' => $this->logger,
'targets' => [
[
'class' => ElasticsearchTarget::className(),
'db' => $this->getConnection(),
'index' => $this->index,
'type' => $this->type,
]
]
]);
}
protected function tearDown(): void
{
$command = $this->getConnection()->createCommand();
$command->deleteIndex($this->index);
parent::tearDown();
}
}