10119ca25SAndreas Gohr<?php 20119ca25SAndreas Gohr 30119ca25SAndreas Gohrnamespace Psr\Log\Test; 40119ca25SAndreas Gohr 50119ca25SAndreas Gohruse Psr\Log\LoggerInterface; 60119ca25SAndreas Gohruse Psr\Log\LogLevel; 7*dc4d9dc6SAnna Dabrowskause PHPUnit\Framework\TestCase; 80119ca25SAndreas Gohr 90119ca25SAndreas Gohr/** 100119ca25SAndreas Gohr * Provides a base test class for ensuring compliance with the LoggerInterface. 110119ca25SAndreas Gohr * 120119ca25SAndreas Gohr * Implementors can extend the class and implement abstract methods to run this 130119ca25SAndreas Gohr * as part of their test suite. 140119ca25SAndreas Gohr */ 15*dc4d9dc6SAnna Dabrowskaabstract class LoggerInterfaceTest extends TestCase 160119ca25SAndreas Gohr{ 170119ca25SAndreas Gohr /** 180119ca25SAndreas Gohr * @return LoggerInterface 190119ca25SAndreas Gohr */ 200119ca25SAndreas Gohr abstract public function getLogger(); 210119ca25SAndreas Gohr 220119ca25SAndreas Gohr /** 230119ca25SAndreas Gohr * This must return the log messages in order. 240119ca25SAndreas Gohr * 250119ca25SAndreas Gohr * The simple formatting of the messages is: "<LOG LEVEL> <MESSAGE>". 260119ca25SAndreas Gohr * 270119ca25SAndreas Gohr * Example ->error('Foo') would yield "error Foo". 280119ca25SAndreas Gohr * 290119ca25SAndreas Gohr * @return string[] 300119ca25SAndreas Gohr */ 310119ca25SAndreas Gohr abstract public function getLogs(); 320119ca25SAndreas Gohr 330119ca25SAndreas Gohr public function testImplements() 340119ca25SAndreas Gohr { 350119ca25SAndreas Gohr $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger()); 360119ca25SAndreas Gohr } 370119ca25SAndreas Gohr 380119ca25SAndreas Gohr /** 390119ca25SAndreas Gohr * @dataProvider provideLevelsAndMessages 400119ca25SAndreas Gohr */ 410119ca25SAndreas Gohr public function testLogsAtAllLevels($level, $message) 420119ca25SAndreas Gohr { 430119ca25SAndreas Gohr $logger = $this->getLogger(); 440119ca25SAndreas Gohr $logger->{$level}($message, array('user' => 'Bob')); 450119ca25SAndreas Gohr $logger->log($level, $message, array('user' => 'Bob')); 460119ca25SAndreas Gohr 470119ca25SAndreas Gohr $expected = array( 480119ca25SAndreas Gohr $level.' message of level '.$level.' with context: Bob', 490119ca25SAndreas Gohr $level.' message of level '.$level.' with context: Bob', 500119ca25SAndreas Gohr ); 510119ca25SAndreas Gohr $this->assertEquals($expected, $this->getLogs()); 520119ca25SAndreas Gohr } 530119ca25SAndreas Gohr 540119ca25SAndreas Gohr public function provideLevelsAndMessages() 550119ca25SAndreas Gohr { 560119ca25SAndreas Gohr return array( 570119ca25SAndreas Gohr LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'), 580119ca25SAndreas Gohr LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'), 590119ca25SAndreas Gohr LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'), 600119ca25SAndreas Gohr LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'), 610119ca25SAndreas Gohr LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'), 620119ca25SAndreas Gohr LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'), 630119ca25SAndreas Gohr LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'), 640119ca25SAndreas Gohr LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'), 650119ca25SAndreas Gohr ); 660119ca25SAndreas Gohr } 670119ca25SAndreas Gohr 680119ca25SAndreas Gohr /** 690119ca25SAndreas Gohr * @expectedException \Psr\Log\InvalidArgumentException 700119ca25SAndreas Gohr */ 710119ca25SAndreas Gohr public function testThrowsOnInvalidLevel() 720119ca25SAndreas Gohr { 730119ca25SAndreas Gohr $logger = $this->getLogger(); 740119ca25SAndreas Gohr $logger->log('invalid level', 'Foo'); 750119ca25SAndreas Gohr } 760119ca25SAndreas Gohr 770119ca25SAndreas Gohr public function testContextReplacement() 780119ca25SAndreas Gohr { 790119ca25SAndreas Gohr $logger = $this->getLogger(); 800119ca25SAndreas Gohr $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar')); 810119ca25SAndreas Gohr 820119ca25SAndreas Gohr $expected = array('info {Message {nothing} Bob Bar a}'); 830119ca25SAndreas Gohr $this->assertEquals($expected, $this->getLogs()); 840119ca25SAndreas Gohr } 850119ca25SAndreas Gohr 860119ca25SAndreas Gohr public function testObjectCastToString() 870119ca25SAndreas Gohr { 880119ca25SAndreas Gohr if (method_exists($this, 'createPartialMock')) { 890119ca25SAndreas Gohr $dummy = $this->createPartialMock('Psr\Log\Test\DummyTest', array('__toString')); 900119ca25SAndreas Gohr } else { 910119ca25SAndreas Gohr $dummy = $this->getMock('Psr\Log\Test\DummyTest', array('__toString')); 920119ca25SAndreas Gohr } 930119ca25SAndreas Gohr $dummy->expects($this->once()) 940119ca25SAndreas Gohr ->method('__toString') 950119ca25SAndreas Gohr ->will($this->returnValue('DUMMY')); 960119ca25SAndreas Gohr 970119ca25SAndreas Gohr $this->getLogger()->warning($dummy); 980119ca25SAndreas Gohr 990119ca25SAndreas Gohr $expected = array('warning DUMMY'); 1000119ca25SAndreas Gohr $this->assertEquals($expected, $this->getLogs()); 1010119ca25SAndreas Gohr } 1020119ca25SAndreas Gohr 1030119ca25SAndreas Gohr public function testContextCanContainAnything() 1040119ca25SAndreas Gohr { 105*dc4d9dc6SAnna Dabrowska $closed = fopen('php://memory', 'r'); 106*dc4d9dc6SAnna Dabrowska fclose($closed); 107*dc4d9dc6SAnna Dabrowska 1080119ca25SAndreas Gohr $context = array( 1090119ca25SAndreas Gohr 'bool' => true, 1100119ca25SAndreas Gohr 'null' => null, 1110119ca25SAndreas Gohr 'string' => 'Foo', 1120119ca25SAndreas Gohr 'int' => 0, 1130119ca25SAndreas Gohr 'float' => 0.5, 1140119ca25SAndreas Gohr 'nested' => array('with object' => new DummyTest), 1150119ca25SAndreas Gohr 'object' => new \DateTime, 1160119ca25SAndreas Gohr 'resource' => fopen('php://memory', 'r'), 117*dc4d9dc6SAnna Dabrowska 'closed' => $closed, 1180119ca25SAndreas Gohr ); 1190119ca25SAndreas Gohr 1200119ca25SAndreas Gohr $this->getLogger()->warning('Crazy context data', $context); 1210119ca25SAndreas Gohr 1220119ca25SAndreas Gohr $expected = array('warning Crazy context data'); 1230119ca25SAndreas Gohr $this->assertEquals($expected, $this->getLogs()); 1240119ca25SAndreas Gohr } 1250119ca25SAndreas Gohr 1260119ca25SAndreas Gohr public function testContextExceptionKeyCanBeExceptionOrOtherValues() 1270119ca25SAndreas Gohr { 1280119ca25SAndreas Gohr $logger = $this->getLogger(); 1290119ca25SAndreas Gohr $logger->warning('Random message', array('exception' => 'oops')); 1300119ca25SAndreas Gohr $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail'))); 1310119ca25SAndreas Gohr 1320119ca25SAndreas Gohr $expected = array( 1330119ca25SAndreas Gohr 'warning Random message', 1340119ca25SAndreas Gohr 'critical Uncaught Exception!' 1350119ca25SAndreas Gohr ); 1360119ca25SAndreas Gohr $this->assertEquals($expected, $this->getLogs()); 1370119ca25SAndreas Gohr } 1380119ca25SAndreas Gohr} 139