UtilsTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  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 string $input
  16. * @dataProvider providePathsToCanonicalize
  17. */
  18. public function testCanonicalizePath($expected, $input)
  19. {
  20. $this->assertSame($expected, Utils::canonicalizePath($input));
  21. }
  22. public function providePathsToCanonicalize()
  23. {
  24. return array(
  25. array('/foo/bar', '/foo/bar'),
  26. array('file://'.getcwd().'/bla', 'file://bla'),
  27. array(getcwd().'/bla', 'bla'),
  28. array(getcwd().'/./bla', './bla'),
  29. array('file:///foo/bar', 'file:///foo/bar'),
  30. array('any://foo', 'any://foo'),
  31. array('\\\\network\path', '\\\\network\path'),
  32. );
  33. }
  34. /**
  35. * @param int $code
  36. * @param string $msg
  37. * @dataProvider providesHandleJsonErrorFailure
  38. */
  39. public function testHandleJsonErrorFailure($code, $msg)
  40. {
  41. $this->expectException('RuntimeException', $msg);
  42. Utils::handleJsonError($code, 'faked');
  43. }
  44. public function providesHandleJsonErrorFailure()
  45. {
  46. return [
  47. 'depth' => [JSON_ERROR_DEPTH, 'Maximum stack depth exceeded'],
  48. 'state' => [JSON_ERROR_STATE_MISMATCH, 'Underflow or the modes mismatch'],
  49. 'ctrl' => [JSON_ERROR_CTRL_CHAR, 'Unexpected control character found'],
  50. 'default' => [-1, 'Unknown error'],
  51. ];
  52. }
  53. /**
  54. * @param mixed $in Input
  55. * @param mixed $expect Expected output
  56. * @covers Monolog\Formatter\NormalizerFormatter::detectAndCleanUtf8
  57. * @dataProvider providesDetectAndCleanUtf8
  58. */
  59. public function testDetectAndCleanUtf8($in, $expect)
  60. {
  61. $reflMethod = new \ReflectionMethod(Utils::class, 'detectAndCleanUtf8');
  62. $reflMethod->setAccessible(true);
  63. $args = [&$in];
  64. $reflMethod->invokeArgs(null, $args);
  65. $this->assertSame($expect, $in);
  66. }
  67. public function providesDetectAndCleanUtf8()
  68. {
  69. $obj = new \stdClass;
  70. return [
  71. 'null' => [null, null],
  72. 'int' => [123, 123],
  73. 'float' => [123.45, 123.45],
  74. 'bool false' => [false, false],
  75. 'bool true' => [true, true],
  76. 'ascii string' => ['abcdef', 'abcdef'],
  77. 'latin9 string' => ["\xB1\x31\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE\xFF", '±1€ŠšŽžŒœŸÿ'],
  78. 'unicode string' => ['¤¦¨´¸¼½¾€ŠšŽžŒœŸ', '¤¦¨´¸¼½¾€ŠšŽžŒœŸ'],
  79. 'empty array' => [[], []],
  80. 'array' => [['abcdef'], ['abcdef']],
  81. 'object' => [$obj, $obj],
  82. ];
  83. }
  84. }