JsonFormatterTest.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. class JsonFormatterTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * Test if \u0119 (196+153) will get correctly formatted
  17. * See ticket #2613
  18. */
  19. public function testUnicodeWithPrependedSlash()
  20. {
  21. if (!extension_loaded('mbstring')) {
  22. $this->markTestSkipped('Test requires the mbstring extension');
  23. }
  24. $data = '"' . chr(92) . chr(92) . chr(92) . 'u0119"';
  25. $encodedData = JsonFormatter::format($data, true, true);
  26. $expected = '34+92+92+196+153+34';
  27. $this->assertEquals($expected, $this->getCharacterCodes($encodedData));
  28. }
  29. /**
  30. * Convert string to character codes split by a plus sign
  31. * @param string $string
  32. * @return string
  33. */
  34. protected function getCharacterCodes($string)
  35. {
  36. $codes = array();
  37. for ($i = 0; $i < strlen($string); $i++) {
  38. $codes[] = ord($string[$i]);
  39. }
  40. return implode('+', $codes);
  41. }
  42. }