BaseLogTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since t.b.d.
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Log\Engine;
  15. use Cake\Log\Engine\BaseLog;
  16. use Cake\ORM\Entity;
  17. use Cake\TestSuite\TestCase;
  18. use Psr\Log\LogLevel;
  19. /**
  20. * Class BaseLogImpl
  21. * Implementation of abstract class {@see \Cake\Log\Engine\BaseLog},
  22. * required by test case {@see \Cake\Test\TestCase\Log\Engine\BaseLogTest}.
  23. */
  24. class BaseLogImpl extends BaseLog
  25. {
  26. /**
  27. * Logs with an arbitrary level.
  28. *
  29. * @param mixed $level
  30. * @param mixed $message
  31. * @param array $context
  32. * @return mixed
  33. */
  34. public function log($level, $message, array $context = [])
  35. {
  36. return $this->_format($message, $context);
  37. }
  38. }
  39. class BaseLogTest extends TestCase
  40. {
  41. private $testData = ['ä', 'ö', 'ü'];
  42. private $logger;
  43. /**
  44. * Setting up the test case.
  45. * Creates a stub logger implementing the log() function missing from abstract class BaseLog.
  46. */
  47. public function setUp()
  48. {
  49. parent::setUp();
  50. $this->logger = new BaseLogImpl();
  51. }
  52. private function assertUnescapedUnicode(array $needles, $haystack)
  53. {
  54. foreach ($needles as $needle) {
  55. $this->assertContains(
  56. $needle,
  57. $haystack,
  58. 'Formatted log message does not contain unescaped unicode character.'
  59. );
  60. }
  61. }
  62. /**
  63. * Tests the logging output of a single string containing unicode characters.
  64. */
  65. public function testLogUnicodeString()
  66. {
  67. $logged = $this->logger->log(LogLevel::INFO, implode($this->testData));
  68. $this->assertUnescapedUnicode($this->testData, $logged);
  69. }
  70. /**
  71. * Tests the logging output of an array containing unicode characters.
  72. */
  73. public function testLogUnicodeArray()
  74. {
  75. $logged = $this->logger->log(LogLevel::INFO, $this->testData);
  76. $this->assertUnescapedUnicode($this->testData, $logged);
  77. }
  78. /**
  79. * Tests the logging output of an object implementing __toString().
  80. * Note: __toString() will return a single string containing unicode characters.
  81. */
  82. public function testLogUnicodeObjectToString()
  83. {
  84. $stub = $this->getMockBuilder(\stdClass::class)
  85. ->setMethods(['__toString'])
  86. ->getMock();
  87. $stub->method('__toString')
  88. ->willReturn(implode($this->testData));
  89. $logged = $this->logger->log(LogLevel::INFO, $stub);
  90. $this->assertUnescapedUnicode($this->testData, $logged);
  91. }
  92. /**
  93. * Tests the logging output of an object implementing jsonSerializable().
  94. * Note: jsonSerializable() will return an array containing unicode characters.
  95. */
  96. public function testLogUnicodeObjectJsonSerializable()
  97. {
  98. $stub = $this->createMock(\JsonSerializable::class);
  99. $stub->method('jsonSerialize')
  100. ->willReturn($this->testData);
  101. $logged = $this->logger->log(LogLevel::INFO, $stub);
  102. $this->assertUnescapedUnicode($this->testData, $logged);
  103. }
  104. /**
  105. * Tests the logging output of an entity with property value that contains unicode characters.
  106. */
  107. public function testLogUnicodeEntity()
  108. {
  109. $entity = new Entity(['foo' => implode($this->testData)]);
  110. $logged = $this->logger->log(LogLevel::INFO, $entity);
  111. $this->assertUnescapedUnicode($this->testData, $logged);
  112. }
  113. }