JsonFormatterTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Json;
  12. use Composer\Json\JsonFormatter;
  13. use PHPUnit\Framework\TestCase;
  14. class JsonFormatterTest extends TestCase
  15. {
  16. /**
  17. * Test if \u0119 (196+153) will get correctly formatted
  18. * See ticket #2613
  19. */
  20. public function testUnicodeWithPrependedSlash()
  21. {
  22. if (!extension_loaded('mbstring')) {
  23. $this->markTestSkipped('Test requires the mbstring extension');
  24. }
  25. $data = '"' . chr(92) . chr(92) . chr(92) . 'u0119"';
  26. $encodedData = JsonFormatter::format($data, true, true);
  27. $expected = '34+92+92+196+153+34';
  28. $this->assertEquals($expected, $this->getCharacterCodes($encodedData));
  29. }
  30. /**
  31. * Surrogate pairs are intentionally skipped and not unescaped
  32. * https://github.com/composer/composer/issues/7510
  33. */
  34. public function testUtf16SurrogatePair()
  35. {
  36. if (!extension_loaded('mbstring')) {
  37. $this->markTestSkipped('Test requires the mbstring extension');
  38. }
  39. $escaped = '"\ud83d\ude00"';
  40. $this->assertEquals($escaped, JsonFormatter::format($escaped, true, true));
  41. }
  42. /**
  43. * Convert string to character codes split by a plus sign
  44. * @param string $string
  45. * @return string
  46. */
  47. protected function getCharacterCodes($string)
  48. {
  49. $codes = array();
  50. for ($i = 0; $i < strlen($string); $i++) {
  51. $codes[] = ord($string[$i]);
  52. }
  53. return implode('+', $codes);
  54. }
  55. }