summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/LogTest.php
blob: 188be933ee59c750a0054bac42260e85570ee9f7 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php

namespace Elastica\Test;

use Elastica\Client;
use Elastica\Log;
use Elastica\Test\Base as BaseTest;
use Psr\Log\LogLevel;

class LogTest extends BaseTest
{
    private $_context = array();
    private $_message = 'hello world';

    protected function setUp()
    {
        if (!class_exists('Psr\Log\AbstractLogger')) {
            $this->markTestSkipped('The Psr extension is not available.');
        }
    }

    public function testLogInterface()
    {
        $log = new Log();
        $this->assertInstanceOf('Psr\Log\LoggerInterface', $log);
    }

    public function testSetLogConfigPath()
    {
        $logPath = '/tmp/php.log';
        $client = new Client(array('log' => $logPath));
        $this->assertEquals($logPath, $client->getConfig('log'));
    }

    public function testSetLogConfigEnable()
    {
        $client = new Client(array('log' => true));
        $this->assertTrue($client->getConfig('log'));
    }

    public function testSetLogConfigEnable1()
    {
        $client = new Client();
        $client->setLogger(new Log());
        $this->assertFalse($client->getConfig('log'));
    }

    public function testEmptyLogConfig()
    {
        $client = $this->_getClient();
        $this->assertEmpty($client->getConfig('log'));
    }

    public function testGetLastMessage()
    {
        $log = new Log('/tmp/php.log');

        $log->log(LogLevel::DEBUG, $this->_message, $this->_context);

        $this->_context['error_message'] = $this->_message;
        $message = json_encode($this->_context);

        $this->assertEquals($message, $log->getLastMessage());
    }

    public function testGetLastMessage2()
    {
        $client = new Client(array('log' => true));
        $log = new Log($client);

        // Set log path temp path as otherwise test fails with output
        $errorLog = ini_get('error_log');
        ini_set('error_log', sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php.log');

        $this->_context['error_message'] = $this->_message;
        $message = json_encode($this->_context);

        $log->log(LogLevel::DEBUG, $this->_message, $this->_context);
        ini_set('error_log', $errorLog);

        $this->assertEquals($message, $log->getLastMessage());
    }

    public function testGetLastMessageInfo()
    {
        $log = $this->initLog();
        $log->info($this->_message, $this->_context);
        $this->assertEquals($this->getMessage(), $log->getLastMessage());
    }

    public function testGetLastMessageCritical()
    {
        $log = $this->initLog();
        $log->critical($this->_message, $this->_context);
        $this->assertEquals($this->getMessage(), $log->getLastMessage());
    }

    public function testGetLastMessageAlert()
    {
        $log = $this->initLog();
        $log->alert($this->_message, $this->_context);
        $this->assertEquals($this->getMessage(), $log->getLastMessage());
    }

    public function testGetLastMessageDebug()
    {
        $log = $this->initLog();
        $log->debug($this->_message, $this->_context);
        $this->assertEquals($this->getMessage(), $log->getLastMessage());
    }

    public function testGetLastMessageEmergency()
    {
        $log = $this->initLog();
        $log->emergency($this->_message, $this->_context);
        $this->assertEquals($this->getMessage(), $log->getLastMessage());
    }

    public function testGetLastMessageError()
    {
        $log = $this->initLog();
        $log->error($this->_message, $this->_context);
        $this->assertEquals($this->getMessage(), $log->getLastMessage());
    }

    public function testGetLastMessageNotice()
    {
        $log = $this->initLog();
        $log->notice($this->_message, $this->_context);
        $this->assertEquals($this->getMessage(), $log->getLastMessage());
    }

    public function testGetLastMessageWarning()
    {
        $log = $this->initLog();
        $log->warning($this->_message, $this->_context);
        $this->assertEquals($this->getMessage(), $log->getLastMessage());
    }

    private function initLog()
    {
        $log = new Log('/tmp/php.log');

        return $log;
    }

    private function getMessage()
    {
        $this->_context['error_message'] = $this->_message;

        return json_encode($this->_context);
    }
}