Selaa lähdekoodia

Added extra unit test from the example in the ticket.

Sandy Pleyte 11 vuotta sitten
vanhempi
commit
3a2815b778
2 muutettua tiedostoa jossa 19 lisäystä ja 3 poistoa
  1. 7 3
      src/Composer/Json/JsonFormatter.php
  2. 12 0
      tests/Composer/Test/Json/JsonFileTest.php

+ 7 - 3
src/Composer/Json/JsonFormatter.php

@@ -69,9 +69,13 @@ class JsonFormatter
                     $buffer = preg_replace_callback('/(\\\\+)u([0-9a-f]{4})/i', function($match) {
                         $l = strlen($match[1]);
 
-                        if ($l%2)
-                            return str_repeat ('\\', $l-1).mb_convert_encoding(pack('H*', $match[2]), 'UTF-8', 'UCS-2BE');
-
+                        if ($l % 2) {
+                            return str_repeat('\\', $l - 1) . mb_convert_encoding(
+                                pack('H*', $match[2]),
+                                'UTF-8',
+                                'UCS-2BE'
+                            );
+                        }
                         return $match[0];
                     }, $buffer);
                 }

+ 12 - 0
tests/Composer/Test/Json/JsonFileTest.php

@@ -198,6 +198,18 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
         $this->assertJsonFormat('"\\u018c"', $data, 0);
     }
 
+    public function testDoubleEscapedUnicode()
+    {
+        $jsonFile = new JsonFile('composer.json');
+        $data = array("Zdjęcia","hjkjhl\\u0119kkjk");
+        $encodedData = $jsonFile->encode($data);
+        $doubleEncodedData = $jsonFile->encode(array('t' => $encodedData));
+
+        $decodedData = json_decode($doubleEncodedData, true);
+        $doubleData = json_decode($decodedData['t'], true);
+        $this->assertEquals($data, $doubleData);
+    }
+
     private function expectParseException($text, $json)
     {
         try {