JsonFormatterTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. * Convert string to character codes split by a plus sign
  32. * @param string $string
  33. * @return string
  34. */
  35. protected function getCharacterCodes($string)
  36. {
  37. $codes = array();
  38. for ($i = 0; $i < strlen($string); $i++) {
  39. $codes[] = ord($string[$i]);
  40. }
  41. return implode('+', $codes);
  42. }
  43. }