UtilsTest.php 5.3 KB

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