BaseLogTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. *
  33. * @return mixed
  34. */
  35. public function log($level, $message, array $context = [])
  36. {
  37. return $this->_format($message, $context);
  38. }
  39. }
  40. class BaseLogTest extends TestCase
  41. {
  42. private $testData = ['ä', 'ö', 'ü'];
  43. private $logger;
  44. /**
  45. * Setting up the test case.
  46. * Creates a stub logger implementing the log() function missing from abstract class BaseLog.
  47. */
  48. public function setUp()
  49. {
  50. parent::setUp();
  51. $this->logger = new BaseLogImpl();
  52. }
  53. private function assertUnescapedUnicode(array $needles, $haystack)
  54. {
  55. foreach ($needles as $needle) {
  56. $this->assertContains(
  57. $needle,
  58. $haystack,
  59. 'Formatted log message does not contain unescaped unicode character.'
  60. );
  61. }
  62. }
  63. /**
  64. * Tests the logging output of a single string containing unicode characters.
  65. */
  66. public function testLogUnicodeString()
  67. {
  68. $logged = $this->logger->log(LogLevel::INFO, implode($this->testData));
  69. $this->assertUnescapedUnicode($this->testData, $logged);
  70. }
  71. /**
  72. * Tests the logging output of an array containing unicode characters.
  73. */
  74. public function testLogUnicodeArray()
  75. {
  76. $logged = $this->logger->log(LogLevel::INFO, $this->testData);
  77. $this->assertUnescapedUnicode($this->testData, $logged);
  78. }
  79. /**
  80. * Tests the logging output of an object implementing __toString().
  81. * Note: __toString() will return a single string containing unicode characters.
  82. */
  83. public function testLogUnicodeObjectToString()
  84. {
  85. $stub = $this->getMockBuilder(\stdClass::class)
  86. ->setMethods(['__toString'])
  87. ->getMock();
  88. $stub->method('__toString')
  89. ->willReturn(implode($this->testData));
  90. $logged = $this->logger->log(LogLevel::INFO, $stub);
  91. $this->assertUnescapedUnicode($this->testData, $logged);
  92. }
  93. /**
  94. * Tests the logging output of an object implementing jsonSerializable().
  95. * Note: jsonSerializable() will return an array containing unicode characters.
  96. */
  97. public function testLogUnicodeObjectJsonSerializable()
  98. {
  99. $stub = $this->createMock(\JsonSerializable::class);
  100. $stub->method('jsonSerialize')
  101. ->willReturn($this->testData);
  102. $logged = $this->logger->log(LogLevel::INFO, $stub);
  103. $this->assertUnescapedUnicode($this->testData, $logged);
  104. }
  105. /**
  106. * Tests the logging output of an entity with property value that contains unicode characters.
  107. */
  108. public function testLogUnicodeEntity()
  109. {
  110. $entity = new Entity(['foo' => implode($this->testData)]);
  111. $logged = $this->logger->log(LogLevel::INFO, $entity);
  112. $this->assertUnescapedUnicode($this->testData, $logged);
  113. }
  114. }