diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 1b20af274..458265e3e 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -18,16 +18,6 @@ use JsonSchema\Validator; use Seld\JsonLint\JsonParser; use Composer\Util\StreamContextFactory; -if (!defined('JSON_UNESCAPED_SLASHES')) { - define('JSON_UNESCAPED_SLASHES', 64); -} -if (!defined('JSON_PRETTY_PRINT')) { - define('JSON_PRETTY_PRINT', 128); -} -if (!defined('JSON_UNESCAPED_UNICODE')) { - define('JSON_UNESCAPED_UNICODE', 256); -} - /** * Reads/writes json files. * @@ -39,6 +29,10 @@ class JsonFile const LAX_SCHEMA = 1; const STRICT_SCHEMA = 2; + const JSON_UNESCAPED_SLASHES = 64; + const JSON_PRETTY_PRINT = 128; + const JSON_UNESCAPED_UNICODE = 256; + private $path; /** @@ -108,7 +102,7 @@ class JsonFile ); } } - file_put_contents($this->path, static::encode($hash, $options). ($options & JSON_PRETTY_PRINT ? "\n" : '')); + file_put_contents($this->path, static::encode($hash, $options). ($options & self::JSON_PRETTY_PRINT ? "\n" : '')); } /** @@ -170,9 +164,9 @@ class JsonFile $json = json_encode($data); - $prettyPrint = (Boolean) ($options & JSON_PRETTY_PRINT); - $unescapeUnicode = (Boolean) ($options & JSON_UNESCAPED_UNICODE); - $unescapeSlashes = (Boolean) ($options & JSON_UNESCAPED_SLASHES); + $prettyPrint = (Boolean) ($options & self::JSON_PRETTY_PRINT); + $unescapeUnicode = (Boolean) ($options & self::JSON_UNESCAPED_UNICODE); + $unescapeSlashes = (Boolean) ($options & self::JSON_UNESCAPED_SLASHES); if (!$prettyPrint && !$unescapeUnicode && !$unescapeSlashes) { return $json; diff --git a/tests/Composer/Test/Json/JsonFileTest.php b/tests/Composer/Test/Json/JsonFileTest.php index 3090a450f..4e30928cb 100644 --- a/tests/Composer/Test/Json/JsonFileTest.php +++ b/tests/Composer/Test/Json/JsonFileTest.php @@ -140,9 +140,10 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase public function testUnicode() { - if (!function_exists('mb_convert_encoding')) { + if (!function_exists('mb_convert_encoding') && version_compare(PHP_VERSION, '5.4', '<')) { $this->markTestSkipped('Test requires the mbstring extension'); } + $data = array("Žluťoučký \" kůň" => "úpěl ďábelské ódy za €"); $json = '{ "Žluťoučký \" kůň": "úpěl ďábelské ódy za €" @@ -151,14 +152,23 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase $this->assertJsonFormat($json, $data); } - public function testEscapedSlashes() + public function testOnlyUnicode() { - if (!function_exists('mb_convert_encoding')) { + if (!function_exists('mb_convert_encoding') && version_compare(PHP_VERSION, '5.4', '<')) { $this->markTestSkipped('Test requires the mbstring extension'); } - $data = "\\/fooƌ"; - $this->assertJsonFormat('"\\\\\\/fooƌ"', $data, JSON_UNESCAPED_UNICODE); + $data = "\\/ƌ"; + + $this->assertJsonFormat('"\\\\\\/ƌ"', $data, JsonFile::JSON_UNESCAPED_UNICODE); + } + + public function testEscapedSlashes() + { + + $data = "\\/foo"; + + $this->assertJsonFormat('"\\\\\\/foo"', $data, 0); } public function testEscapedUnicode()