_format($message, $context); } } class BaseLogTest extends TestCase { private $testData = ['ä', 'ö', 'ü']; private $logger; /** * Setting up the test case. * Creates a stub logger implementing the log() function missing from abstract class BaseLog. */ public function setUp() { parent::setUp(); $this->logger = new BaseLogImpl(); } private function assertUnescapedUnicode(array $needles, $haystack) { foreach ($needles as $needle) { $this->assertContains( $needle, $haystack, 'Formatted log message does not contain unescaped unicode character.' ); } } /** * Tests the logging output of a single string containing unicode characters. */ public function testLogUnicodeString() { $logged = $this->logger->log(LogLevel::INFO, implode($this->testData)); $this->assertUnescapedUnicode($this->testData, $logged); } /** * Tests the logging output of an array containing unicode characters. */ public function testLogUnicodeArray() { $logged = $this->logger->log(LogLevel::INFO, $this->testData); $this->assertUnescapedUnicode($this->testData, $logged); } /** * Tests the logging output of an object implementing __toString(). * Note: __toString() will return a single string containing unicode characters. */ public function testLogUnicodeObjectToString() { $stub = $this->getMockBuilder(\stdClass::class) ->setMethods(['__toString']) ->getMock(); $stub->method('__toString') ->willReturn(implode($this->testData)); $logged = $this->logger->log(LogLevel::INFO, $stub); $this->assertUnescapedUnicode($this->testData, $logged); } /** * Tests the logging output of an object implementing jsonSerializable(). * Note: jsonSerializable() will return an array containing unicode characters. */ public function testLogUnicodeObjectJsonSerializable() { $stub = $this->createMock(\JsonSerializable::class); $stub->method('jsonSerialize') ->willReturn($this->testData); $logged = $this->logger->log(LogLevel::INFO, $stub); $this->assertUnescapedUnicode($this->testData, $logged); } /** * Tests the logging output of an entity with property value that contains unicode characters. */ public function testLogUnicodeEntity() { $entity = new Entity(['foo' => implode($this->testData)]); $logged = $this->logger->log(LogLevel::INFO, $entity); $this->assertUnescapedUnicode($this->testData, $logged); } }