summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/LogTest.php
blob: fdfc5c00c3fddad670b9df4b93729af21b756ee0 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace Elastica\Test;

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

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

    public static function setUpBeforeClass()
    {
        if (!class_exists('Psr\Log\AbstractLogger')) {
            self::markTestSkipped('The Psr extension is not available.');
        }
    }

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

    /**
     * @group unit
     */
    public function testSetLogConfigPath()
    {
        $logPath = '/tmp/php.log';
        $client = $this->_getClient(array('log' => $logPath));
        $this->assertEquals($logPath, $client->getConfig('log'));
    }

    /**
     * @group unit
     */
    public function testSetLogConfigEnable()
    {
        $client = $this->_getClient(array('log' => true));
        $this->assertTrue($client->getConfig('log'));
    }

    /**
     * @group unit
     */
    public function testSetLogConfigEnable1()
    {
        $client = $this->_getClient();
        $client->setLogger(new Log());
        $this->assertFalse($client->getConfig('log'));
    }

    /**
     * @group unit
     */
    public function testEmptyLogConfig()
    {
        $client = $this->_getClient();
        $this->assertEmpty($client->getConfig('log'));
    }

    /**
     * @group unit
     */
    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());
    }

    /**
     * @group unit
     */
    public function testGetLastMessage2()
    {
        $client = $this->_getClient(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());
    }

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

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

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

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

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

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

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

    /**
     * @group unit
     */
    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);
    }
}