123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- <?php
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- class LoggerTest extends PHPUnit_Framework_TestCase
- {
- private $client;
- public function setup()
- {
- $this->client = new Google_Client();
- }
- /**
- * @dataProvider logLevelsProvider
- */
- public function testPsrMethods($key)
- {
- $message = 'This is my log message';
- $context = array('some'=>'context');
- $logger = $this->getLogger('log');
- $logger->expects($this->once())
- ->method('log')
- ->with($key, $message, $context);
- call_user_func(array($logger, $key), $message, $context);
- }
- /**
- * @dataProvider invalidLevelsProvider
- * @expectedException Google_Logger_Exception
- * @expectedExceptionMessage Unknown LogLevel
- */
- public function testSetLabelWithBadValue($level)
- {
- $this->getLogger()->setLevel($level);
- }
- /**
- * @dataProvider invalidLevelsProvider
- * @expectedException Google_Logger_Exception
- * @expectedExceptionMessage Unknown LogLevel
- */
- public function testSetLabelWithBadValueFromConfig($level)
- {
- $this->client->setClassConfig('Google_Logger_Abstract', 'level', $level);
- $this->getLogger();
- }
- /**
- * @dataProvider filterProvider
- */
- public function testShouldHandle($setLevel, $handleLevel, $expected)
- {
- $logger = $this->getLogger();
- $logger->setLevel($setLevel);
- $this->assertEquals($expected, $logger->shouldHandle($handleLevel));
- }
- /**
- * @dataProvider filterProvider
- */
- public function testShouldHandleFromConfig($config, $handleLevel, $expected)
- {
- $this->client->setClassConfig('Google_Logger_Abstract', 'level', $config);
- $logger = $this->getLogger();
- $this->assertEquals($expected, $logger->shouldHandle($handleLevel));
- }
- /**
- * @dataProvider filterProvider
- */
- public function testShouldWrite($setLevel, $logLevel, $expected)
- {
- $logger = $this->getLogger();
- $logger->expects($expected ? $this->once() : $this->never())
- ->method('write');
- $logger->setLevel($setLevel);
- $logger->log($logLevel, 'This is my log message');
- }
- /**
- * @dataProvider filterProvider
- */
- public function testShouldWriteFromConfig($config, $logLevel, $expected)
- {
- $this->client->setClassConfig('Google_Logger_Abstract', 'level', $config);
- $logger = $this->getLogger();
- $logger->expects($expected ? $this->once() : $this->never())
- ->method('write');
- $logger->log($logLevel, 'This is my log message');
- }
- /**
- * @dataProvider formattingProvider
- */
- public function testMessageFormatting(
- $format,
- $date_format,
- $newlines,
- $message,
- $context,
- $expected
- ) {
- $this->client->setClassConfig(
- 'Google_Logger_Abstract',
- 'log_format',
- $format
- );
- $this->client->setClassConfig(
- 'Google_Logger_Abstract',
- 'date_format',
- $date_format
- );
- $this->client->setClassConfig(
- 'Google_Logger_Abstract',
- 'allow_newlines',
- $newlines
- );
- $logger = $this->getLogger();
- $logger->expects($this->once())
- ->method('write')
- ->with($expected);
- $logger->log('debug', $message, $context);
- }
- public function testNullLoggerNeverWrites()
- {
- $logger = $this->getLogger('write', 'Google_Logger_Null');
- $logger->expects($this->never())
- ->method('write');
- $logger->log(
- 'emergency',
- 'Should not be written',
- array('same' => 'for this')
- );
- }
- public function testNullLoggerNeverHandles()
- {
- $logger = $this->getLogger('write', 'Google_Logger_Null');
- $this->assertFalse($logger->shouldHandle('emergency'));
- $this->assertFalse($logger->shouldHandle(600));
- }
- public function testPsrNeverWrites()
- {
- $logger = $this->getLogger('write', 'Google_Logger_Psr');
- $logger->expects($this->never())
- ->method('write');
- $logger->log(
- 'emergency',
- 'Should not be written',
- array('same' => 'for this')
- );
- $logger->setLogger($this->getLogger());
- $logger->log(
- 'emergency',
- 'Should not be written',
- array('same' => 'for this')
- );
- }
- public function testPsrNeverShouldHandleWhenNoLoggerSet()
- {
- $logger = $this->getLogger(null, 'Google_Logger_Psr');
- $this->assertFalse($logger->shouldHandle('emergency'));
- $this->assertFalse($logger->shouldHandle(600));
- }
- public function testPsrShouldHandleWhenLoggerSet()
- {
- $logger = $this->getLogger(null, 'Google_Logger_Psr');
- $logger->setLogger($this->getLogger());
- $this->assertTrue($logger->shouldHandle('emergency'));
- $this->assertTrue($logger->shouldHandle(600));
- }
- /**
- * @dataProvider logLevelsProvider
- */
- public function testPsrDelegates($key)
- {
- $message = 'This is my log message';
- $context = array('some'=>'context');
- $delegate = $this->getLogger('log');
- $delegate->expects($this->once())
- ->method('log')
- ->with($key, $message, $context);
- $logger = $this->getLogger(null, 'Google_Logger_Psr');
- $logger->setLogger($delegate);
- call_user_func(array($logger, $key), $message, $context);
- }
- /**
- * @expectedException Google_Logger_Exception
- * @expectedExceptionMessage File logger requires a filename or a valid file pointer
- */
- public function testLoggerWithBadFileType()
- {
- $this->client->setClassConfig('Google_Logger_File', 'file', false);
- $logger = $this->getLogger(null, 'Google_Logger_File');
- }
- /**
- * @expectedException Google_Logger_Exception
- * @expectedExceptionMessage Logger Error
- */
- public function testLoggerWithBadFileValue()
- {
- $this->client->setClassConfig('Google_Logger_File', 'file', 'not://exist');
- $logger = $this->getLogger(null, 'Google_Logger_File');
- $logger->log('emergency', 'will fail');
- }
- /**
- * @expectedException Google_Logger_Exception
- * @expectedExceptionMessage File pointer is no longer available
- */
- public function testLoggerWithClosedFileReference()
- {
- $fp = fopen('php://memory', 'r+');
- $this->client->setClassConfig('Google_Logger_File', 'file', $fp);
- $logger = $this->getLogger(null, 'Google_Logger_File');
- fclose($fp);
- $logger->log('emergency', 'will fail');
- }
- public function testLoggerWithFileReference()
- {
- $fp = fopen('php://memory', 'r+');
- $this->client->setClassConfig('Google_Logger_File', 'file', $fp);
- $this->client->setClassConfig(
- 'Google_Logger_Abstract',
- 'log_format',
- "%level% - %message%\n"
- );
- $logger = $this->getLogger(null, 'Google_Logger_File');
- $logger->log('emergency', 'test one');
- $logger->log('alert', 'test two');
- $logger->log(500, 'test three');
- rewind($fp);
- $this->assertEquals(
- "EMERGENCY - test one\nALERT - test two\nCRITICAL - test three\n",
- stream_get_contents($fp)
- );
- fclose($fp);
- }
- public function testLoggerWithFile()
- {
- $this->expectOutputString(
- "EMERGENCY - test one\nALERT - test two\nCRITICAL - test three\n"
- );
- $this->client->setClassConfig(
- 'Google_Logger_File',
- 'file',
- 'php://output'
- );
- $this->client->setClassConfig(
- 'Google_Logger_Abstract',
- 'log_format',
- "%level% - %message%\n"
- );
- $logger = $this->getLogger(null, 'Google_Logger_File');
- $logger->log('emergency', 'test one');
- $logger->log('alert', 'test two');
- $logger->log(500, 'test three');
- }
- public function formattingProvider()
- {
- return array(
- 'no interpolation' => array(
- 'this is my format',
- 'd/M/Y:H:i:s O',
- false,
- 'you wont see this',
- array('or' => 'this'),
- 'this is my format',
- ),
- 'only message interpolation' => array(
- 'my format: %message%',
- 'd/M/Y:H:i:s O',
- false,
- 'you will see this',
- array('but not' => 'this'),
- 'my format: you will see this',
- ),
- 'message and level interpolation' => array(
- '%level% - my format: %message%',
- 'd/M/Y:H:i:s O',
- false,
- 'you will see this',
- array('but not' => 'this'),
- 'DEBUG - my format: you will see this',
- ),
- 'message, level, datetime interpolation' => array(
- '[%datetime%] %level% - my format: %message%',
- '\T\I\M\E',
- false,
- 'you will see this',
- array('but not' => 'this'),
- '[TIME] DEBUG - my format: you will see this',
- ),
- 'message, level, datetime, context interpolation' => array(
- '[%datetime%] %level% - my format: %message%: %context%',
- '\T\I\M\E',
- false,
- 'you will see this',
- array('and' => 'this'),
- '[TIME] DEBUG - my format: you will see this: {"and":"this"}',
- ),
- 'reverse JSON in context' => array(
- '%context%',
- 'd/M/Y:H:i:s O',
- false,
- 'you will not see this',
- array('reverse' => json_encode(array('this' => 'is cool'))),
- '{"reverse":{"this":"is cool"}}',
- ),
- 'remove newlines in message' => array(
- '%message%',
- 'd/M/Y:H:i:s O',
- false,
- "This \n\n\r\n is \r my \r\n newlines \r\r\r\n\n message",
- array('you wont' => 'see this'),
- 'This is my newlines message',
- ),
- 'allow newlines in message' => array(
- '%message%',
- 'd/M/Y:H:i:s O',
- true,
- "This \n\n\r\n is \r my \r\n newlines \r\r\r\n\n message",
- array('you wont' => 'see this'),
- "This \n\n\r\n is \r my \r\n newlines \r\r\r\n\n message",
- ),
- 'allow newlines in JSON' => array(
- '%context%',
- 'd/M/Y:H:i:s O',
- true,
- "wont see this",
- array('you will' => 'see this'),
- version_compare(PHP_VERSION, '5.4.0', '>=') ?
- "{\n \"you will\": \"see this\"\n}" :
- '{"you will":"see this"}'
- ),
- );
- }
- public function filterProvider()
- {
- return array(
- array('debug', 'debug', true),
- array(100, 'debug', true),
- array('info', 'debug', false),
- array('info', 'notice', true),
- array('notice', 'notice', true),
- array(250, 'notice', true),
- array('notice', 'debug', false),
- array(250, 'alert', true),
- array('error', 'error', true),
- array('error', 'warning', false),
- array('error', 'critical', true),
- array(600, 'alert', false),
- array(600, 'critical', false),
- array(600, 'emergency', true),
- array('emergency', 'emergency', true),
- );
- }
- public function invalidLevelsProvider()
- {
- return array(
- array('100'),
- array('DEBUG'),
- array(100.0),
- array(''),
- array(0),
- );
- }
- public function logLevelsProvider()
- {
- return array(
- array('emergency', 600),
- array('alert', 550),
- array('critical', 500),
- array('error', 400),
- array('warning', 300),
- array('notice', 250),
- array('info', 200),
- array('debug', 100),
- );
- }
- private function getLogger($methods = null, $type = 'Google_Logger_Abstract')
- {
- return $this->getMockBuilder($type)
- ->setMethods((array) $methods)
- ->setConstructorArgs(array($this->client))
- ->getMockForAbstractClass();
- }
- }
|