UtilsTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog;
  11. use PHPUnit\Framework\Attributes\DataProvider;
  12. class UtilsTest extends \PHPUnit_Framework_TestCase
  13. {
  14. #[DataProvider('provideObjects')]
  15. public function testGetClass(string $expected, object $object)
  16. {
  17. $this->assertSame($expected, Utils::getClass($object));
  18. }
  19. public static function provideObjects()
  20. {
  21. return [
  22. ['stdClass', new \stdClass()],
  23. ['class@anonymous', new class {
  24. }],
  25. ['stdClass@anonymous', new class extends \stdClass {
  26. }],
  27. ];
  28. }
  29. #[DataProvider('providePathsToCanonicalize')]
  30. public function testCanonicalizePath(string $expected, string $input)
  31. {
  32. $this->assertSame($expected, Utils::canonicalizePath($input));
  33. }
  34. public static function providePathsToCanonicalize()
  35. {
  36. return [
  37. ['/foo/bar', '/foo/bar'],
  38. ['file://'.getcwd().'/bla', 'file://bla'],
  39. [getcwd().'/bla', 'bla'],
  40. [getcwd().'/./bla', './bla'],
  41. ['file:///foo/bar', 'file:///foo/bar'],
  42. ['any://foo', 'any://foo'],
  43. ['\\\\network\path', '\\\\network\path'],
  44. ];
  45. }
  46. #[DataProvider('providesHandleJsonErrorFailure')]
  47. public function testHandleJsonErrorFailure(int $code, string $msg)
  48. {
  49. $this->expectException('RuntimeException', $msg);
  50. Utils::handleJsonError($code, 'faked');
  51. }
  52. public static function providesHandleJsonErrorFailure()
  53. {
  54. return [
  55. 'depth' => [JSON_ERROR_DEPTH, 'Maximum stack depth exceeded'],
  56. 'state' => [JSON_ERROR_STATE_MISMATCH, 'Underflow or the modes mismatch'],
  57. 'ctrl' => [JSON_ERROR_CTRL_CHAR, 'Unexpected control character found'],
  58. 'default' => [-1, 'Unknown error'],
  59. ];
  60. }
  61. /**
  62. * @param mixed $in Input
  63. * @param mixed $expect Expected output
  64. * @covers Monolog\Formatter\NormalizerFormatter::detectAndCleanUtf8
  65. */
  66. #[DataProvider('providesDetectAndCleanUtf8')]
  67. public function testDetectAndCleanUtf8($in, $expect)
  68. {
  69. $reflMethod = new \ReflectionMethod(Utils::class, 'detectAndCleanUtf8');
  70. $reflMethod->setAccessible(true);
  71. $args = [&$in];
  72. $reflMethod->invokeArgs(null, $args);
  73. $this->assertSame($expect, $in);
  74. }
  75. public static function providesDetectAndCleanUtf8()
  76. {
  77. $obj = new \stdClass;
  78. return [
  79. 'null' => [null, null],
  80. 'int' => [123, 123],
  81. 'float' => [123.45, 123.45],
  82. 'bool false' => [false, false],
  83. 'bool true' => [true, true],
  84. 'ascii string' => ['abcdef', 'abcdef'],
  85. 'latin9 string' => ["\xB1\x31\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xFF", '±1€ŠšŽžŒœŸÿ'],
  86. 'unicode string' => ['¤¦¨´¸¼½¾€ŠšŽžŒœŸ', '¤¦¨´¸¼½¾€ŠšŽžŒœŸ'],
  87. 'empty array' => [[], []],
  88. 'array' => [['abcdef'], ['abcdef']],
  89. 'object' => [$obj, $obj],
  90. ];
  91. }
  92. #[DataProvider('providesPcreLastErrorMessage')]
  93. public function testPcreLastErrorMessage(int $code, string $msg)
  94. {
  95. if (PHP_VERSION_ID >= 80000) {
  96. $this->assertSame('No error', Utils::pcreLastErrorMessage($code));
  97. return;
  98. }
  99. $this->assertEquals($msg, Utils::pcreLastErrorMessage($code));
  100. }
  101. /**
  102. * @return array[]
  103. */
  104. public static function providesPcreLastErrorMessage(): array
  105. {
  106. return [
  107. [0, 'PREG_NO_ERROR'],
  108. [1, 'PREG_INTERNAL_ERROR'],
  109. [2, 'PREG_BACKTRACK_LIMIT_ERROR'],
  110. [3, 'PREG_RECURSION_LIMIT_ERROR'],
  111. [4, 'PREG_BAD_UTF8_ERROR'],
  112. [5, 'PREG_BAD_UTF8_OFFSET_ERROR'],
  113. [6, 'PREG_JIT_STACKLIMIT_ERROR'],
  114. [-1, 'UNDEFINED_ERROR'],
  115. ];
  116. }
  117. public static function provideIniValuesToConvertToBytes()
  118. {
  119. return [
  120. ['1', 1],
  121. ['2', 2],
  122. ['2.5', 2],
  123. ['2.9', 2],
  124. ['1B', false],
  125. ['1X', false],
  126. ['1K', 1024],
  127. ['1 K', 1024],
  128. [' 5 M ', 5*1024*1024],
  129. ['1G', 1073741824],
  130. ['', false],
  131. [null, false],
  132. ['A', false],
  133. ['AA', false],
  134. ['B', false],
  135. ['BB', false],
  136. ['G', false],
  137. ['GG', false],
  138. ['-1', -1],
  139. ['-123', -123],
  140. ['-1A', -1],
  141. ['-1B', -1],
  142. ['-123G', -123],
  143. ['-B', false],
  144. ['-A', false],
  145. ['-', false],
  146. [true, false],
  147. [false, false],
  148. ];
  149. }
  150. #[DataProvider('provideIniValuesToConvertToBytes')]
  151. public function testExpandIniShorthandBytes(string|null|bool $input, int|false $expected)
  152. {
  153. $result = Utils::expandIniShorthandBytes($input);
  154. $this->assertEquals($expected, $result);
  155. }
  156. }