|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Psr\Log\Test; |
| 4 | + |
| 5 | +use Psr\Log\InvalidArgumentException; |
| 6 | +use Psr\Log\LoggerInterface; |
| 7 | +use Psr\Log\LogLevel; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +/** |
| 11 | + * Provides a base test class for ensuring compliance with the LoggerInterface. |
| 12 | + * |
| 13 | + * Implementors can extend the class and implement abstract methods to run this |
| 14 | + * as part of their test suite. |
| 15 | + */ |
| 16 | +abstract class LoggerInterfaceTest extends TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Return an instance of the logger to be tested. |
| 20 | + * |
| 21 | + * @return LoggerInterface |
| 22 | + */ |
| 23 | + abstract public function getLogger(); |
| 24 | + |
| 25 | + /** |
| 26 | + * This must return the log messages in order. |
| 27 | + * |
| 28 | + * The simple formatting of the messages is: "<LOG LEVEL> <MESSAGE>". |
| 29 | + * |
| 30 | + * Example ->error('Foo') would yield "error Foo". |
| 31 | + * |
| 32 | + * @return string[] |
| 33 | + */ |
| 34 | + abstract public function getLogs(); |
| 35 | + |
| 36 | + public function testImplements() |
| 37 | + { |
| 38 | + $this->assertInstanceOf(LoggerInterface::class, $this->getLogger()); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @dataProvider provideLevelsAndMessages |
| 43 | + */ |
| 44 | + public function testLogsAtAllLevels($level, $message) |
| 45 | + { |
| 46 | + $logger = $this->getLogger(); |
| 47 | + $logger->{$level}($message, ['user' => 'Bob']); |
| 48 | + $logger->log($level, $message, ['user' => 'Bob']); |
| 49 | + |
| 50 | + $expected = [ |
| 51 | + $level . ' message of level ' . $level . ' with context: Bob', |
| 52 | + $level . ' message of level ' . $level . ' with context: Bob', |
| 53 | + ]; |
| 54 | + $this->assertEquals($expected, $this->getLogs()); |
| 55 | + } |
| 56 | + |
| 57 | + public function provideLevelsAndMessages() |
| 58 | + { |
| 59 | + return [ |
| 60 | + LogLevel::EMERGENCY => [LogLevel::EMERGENCY, 'message of level emergency with context: {user}'], |
| 61 | + LogLevel::ALERT => [LogLevel::ALERT, 'message of level alert with context: {user}'], |
| 62 | + LogLevel::CRITICAL => [LogLevel::CRITICAL, 'message of level critical with context: {user}'], |
| 63 | + LogLevel::ERROR => [LogLevel::ERROR, 'message of level error with context: {user}'], |
| 64 | + LogLevel::WARNING => [LogLevel::WARNING, 'message of level warning with context: {user}'], |
| 65 | + LogLevel::NOTICE => [LogLevel::NOTICE, 'message of level notice with context: {user}'], |
| 66 | + LogLevel::INFO => [LogLevel::INFO, 'message of level info with context: {user}'], |
| 67 | + LogLevel::DEBUG => [LogLevel::DEBUG, 'message of level debug with context: {user}'], |
| 68 | + ]; |
| 69 | + } |
| 70 | + |
| 71 | + public function testThrowsOnInvalidLevel() |
| 72 | + { |
| 73 | + $this->expectException(InvalidArgumentException::class); |
| 74 | + $logger = $this->getLogger(); |
| 75 | + $logger->log('invalid level', 'Foo'); |
| 76 | + } |
| 77 | + |
| 78 | + public function testContextReplacement() |
| 79 | + { |
| 80 | + $logger = $this->getLogger(); |
| 81 | + $logger->info('{Message {nothing} {user} {foo.bar} a}', ['user' => 'Bob', 'foo.bar' => 'Bar']); |
| 82 | + |
| 83 | + $expected = ['info {Message {nothing} Bob Bar a}']; |
| 84 | + $this->assertEquals($expected, $this->getLogs()); |
| 85 | + } |
| 86 | + |
| 87 | + public function testObjectCastToString() |
| 88 | + { |
| 89 | + $dummy = $this->createPartialMock(DummyTest::class, ['__toString']); |
| 90 | + $dummy->expects($this->once()) |
| 91 | + ->method('__toString') |
| 92 | + ->will($this->returnValue('DUMMY')); |
| 93 | + |
| 94 | + $this->getLogger()->warning($dummy); |
| 95 | + |
| 96 | + $expected = ['warning DUMMY']; |
| 97 | + $this->assertEquals($expected, $this->getLogs()); |
| 98 | + } |
| 99 | + |
| 100 | + public function testContextCanContainAnything() |
| 101 | + { |
| 102 | + $closed = fopen('php://memory', 'r'); |
| 103 | + fclose($closed); |
| 104 | + |
| 105 | + $context = [ |
| 106 | + 'bool' => true, |
| 107 | + 'null' => null, |
| 108 | + 'string' => 'Foo', |
| 109 | + 'int' => 0, |
| 110 | + 'float' => 0.5, |
| 111 | + 'nested' => ['with object' => new DummyTest()], |
| 112 | + 'object' => new \DateTime(), |
| 113 | + 'resource' => fopen('php://memory', 'r'), |
| 114 | + 'closed' => $closed, |
| 115 | + ]; |
| 116 | + |
| 117 | + $this->getLogger()->warning('Crazy context data', $context); |
| 118 | + |
| 119 | + $expected = ['warning Crazy context data']; |
| 120 | + $this->assertEquals($expected, $this->getLogs()); |
| 121 | + } |
| 122 | + |
| 123 | + public function testContextExceptionKeyCanBeExceptionOrOtherValues() |
| 124 | + { |
| 125 | + $logger = $this->getLogger(); |
| 126 | + $logger->warning('Random message', ['exception' => 'oops']); |
| 127 | + $logger->critical('Uncaught Exception!', ['exception' => new \LogicException('Fail')]); |
| 128 | + |
| 129 | + $expected = [ |
| 130 | + 'warning Random message', |
| 131 | + 'critical Uncaught Exception!' |
| 132 | + ]; |
| 133 | + $this->assertEquals($expected, $this->getLogs()); |
| 134 | + } |
| 135 | +} |
0 commit comments