UtilsTest.php 5.4 KB

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