UtilsTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. $args = [&$in];
  71. $reflMethod->invokeArgs(null, $args);
  72. $this->assertSame($expect, $in);
  73. }
  74. public static function providesDetectAndCleanUtf8()
  75. {
  76. $obj = new \stdClass;
  77. return [
  78. 'null' => [null, null],
  79. 'int' => [123, 123],
  80. 'float' => [123.45, 123.45],
  81. 'bool false' => [false, false],
  82. 'bool true' => [true, true],
  83. 'ascii string' => ['abcdef', 'abcdef'],
  84. 'latin9 string' => ["\xB1\x31\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xFF", '±1€ŠšŽžŒœŸÿ'],
  85. 'unicode string' => ['¤¦¨´¸¼½¾€ŠšŽžŒœŸ', '¤¦¨´¸¼½¾€ŠšŽžŒœŸ'],
  86. 'empty array' => [[], []],
  87. 'array' => [['abcdef'], ['abcdef']],
  88. 'object' => [$obj, $obj],
  89. ];
  90. }
  91. public static function provideIniValuesToConvertToBytes()
  92. {
  93. return [
  94. ['1', 1],
  95. ['2', 2],
  96. ['2.5', 2],
  97. ['2.9', 2],
  98. ['1B', false],
  99. ['1X', false],
  100. ['1K', 1024],
  101. ['1 K', 1024],
  102. [' 5 M ', 5*1024*1024],
  103. ['1G', 1073741824],
  104. ['', false],
  105. [null, false],
  106. ['A', false],
  107. ['AA', false],
  108. ['B', false],
  109. ['BB', false],
  110. ['G', false],
  111. ['GG', false],
  112. ['-1', -1],
  113. ['-123', -123],
  114. ['-1A', -1],
  115. ['-1B', -1],
  116. ['-123G', -123],
  117. ['-B', false],
  118. ['-A', false],
  119. ['-', false],
  120. [true, false],
  121. [false, false],
  122. ];
  123. }
  124. #[DataProvider('provideIniValuesToConvertToBytes')]
  125. public function testExpandIniShorthandBytes(string|null|bool $input, int|false $expected)
  126. {
  127. $result = Utils::expandIniShorthandBytes($input);
  128. $this->assertEquals($expected, $result);
  129. }
  130. }