UtilsTest.php 5.2 KB

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