PageRenderTime 60ms CodeModel.GetById 36ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/Elastica/test/lib/Elastica/Test/LogTest.php

https://github.com/cceh/atom
PHP | 153 lines | 122 code | 30 blank | 1 comment | 1 complexity | d58ef084aadfaf2588d9e6f7445224c9 MD5 | raw file
  1<?php
  2
  3namespace Elastica\Test;
  4
  5use Elastica\Client;
  6use Elastica\Log;
  7use Elastica\Test\Base as BaseTest;
  8use Psr\Log\LogLevel;
  9
 10class LogTest extends BaseTest
 11{
 12    private $_context = array();
 13    private $_message = 'hello world';
 14
 15    protected function setUp()
 16    {
 17        if (!class_exists('Psr\Log\AbstractLogger')) {
 18            $this->markTestSkipped('The Psr extension is not available.');
 19        }
 20    }
 21
 22    public function testLogInterface()
 23    {
 24        $log = new Log();
 25        $this->assertInstanceOf('Psr\Log\LoggerInterface', $log);
 26    }
 27
 28    public function testSetLogConfigPath()
 29    {
 30        $logPath = '/tmp/php.log';
 31        $client = new Client(array('log' => $logPath));
 32        $this->assertEquals($logPath, $client->getConfig('log'));
 33    }
 34
 35    public function testSetLogConfigEnable()
 36    {
 37        $client = new Client(array('log' => true));
 38        $this->assertTrue($client->getConfig('log'));
 39    }
 40
 41    public function testSetLogConfigEnable1()
 42    {
 43        $client = new Client();
 44        $client->setLogger(new Log());
 45        $this->assertFalse($client->getConfig('log'));
 46    }
 47
 48    public function testEmptyLogConfig()
 49    {
 50        $client = $this->_getClient();
 51        $this->assertEmpty($client->getConfig('log'));
 52    }
 53
 54    public function testGetLastMessage()
 55    {
 56        $log = new Log('/tmp/php.log');
 57
 58        $log->log(LogLevel::DEBUG, $this->_message, $this->_context);
 59
 60        $this->_context['error_message'] = $this->_message;
 61        $message = json_encode($this->_context);
 62
 63        $this->assertEquals($message, $log->getLastMessage());
 64    }
 65
 66    public function testGetLastMessage2()
 67    {
 68        $client = new Client(array('log' => true));
 69        $log = new Log($client);
 70
 71        // Set log path temp path as otherwise test fails with output
 72        $errorLog = ini_get('error_log');
 73        ini_set('error_log', sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php.log');
 74
 75        $this->_context['error_message'] = $this->_message;
 76        $message = json_encode($this->_context);
 77
 78        $log->log(LogLevel::DEBUG, $this->_message, $this->_context);
 79        ini_set('error_log', $errorLog);
 80
 81        $this->assertEquals($message, $log->getLastMessage());
 82    }
 83
 84    public function testGetLastMessageInfo()
 85    {
 86        $log = $this->initLog();
 87        $log->info($this->_message, $this->_context);
 88        $this->assertEquals($this->getMessage(), $log->getLastMessage());
 89    }
 90
 91    public function testGetLastMessageCritical()
 92    {
 93        $log = $this->initLog();
 94        $log->critical($this->_message, $this->_context);
 95        $this->assertEquals($this->getMessage(), $log->getLastMessage());
 96    }
 97
 98    public function testGetLastMessageAlert()
 99    {
100        $log = $this->initLog();
101        $log->alert($this->_message, $this->_context);
102        $this->assertEquals($this->getMessage(), $log->getLastMessage());
103    }
104
105    public function testGetLastMessageDebug()
106    {
107        $log = $this->initLog();
108        $log->debug($this->_message, $this->_context);
109        $this->assertEquals($this->getMessage(), $log->getLastMessage());
110    }
111
112    public function testGetLastMessageEmergency()
113    {
114        $log = $this->initLog();
115        $log->emergency($this->_message, $this->_context);
116        $this->assertEquals($this->getMessage(), $log->getLastMessage());
117    }
118
119    public function testGetLastMessageError()
120    {
121        $log = $this->initLog();
122        $log->error($this->_message, $this->_context);
123        $this->assertEquals($this->getMessage(), $log->getLastMessage());
124    }
125
126    public function testGetLastMessageNotice()
127    {
128        $log = $this->initLog();
129        $log->notice($this->_message, $this->_context);
130        $this->assertEquals($this->getMessage(), $log->getLastMessage());
131    }
132
133    public function testGetLastMessageWarning()
134    {
135        $log = $this->initLog();
136        $log->warning($this->_message, $this->_context);
137        $this->assertEquals($this->getMessage(), $log->getLastMessage());
138    }
139
140    private function initLog()
141    {
142        $log = new Log('/tmp/php.log');
143
144        return $log;
145    }
146
147    private function getMessage()
148    {
149        $this->_context['error_message'] = $this->_message;
150
151        return json_encode($this->_context);
152    }
153}