BaseLogTest.php 3.3 KB

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