Browse Source

Throw exception if json file is empty or result of decoding is not array.

ADmad 11 years ago
parent
commit
c3fd5ab1c9

+ 2 - 2
src/Core/Configure/Engine/JsonConfig.php

@@ -66,10 +66,10 @@ class JsonConfig implements ConfigEngineInterface
         $file = $this->_getFilePath($key, true);
 
         $values = json_decode(file_get_contents($file), true);
-        if (json_last_error() !== JSON_ERROR_NONE) {
+        if (!is_array($values) || json_last_error() !== JSON_ERROR_NONE) {
             throw new Exception(sprintf('Error parsing JSON string fetched from config file %s', $file));
         }
-        return (array)$values;
+        return $values;
     }
 
     /**

+ 1 - 1
tests/TestCase/Core/Configure/Engine/JsonConfigTest.php

@@ -97,13 +97,13 @@ class JsonConfigTest extends TestCase
     /**
      * Test reading an empty file.
      *
+     * @expectedException \Cake\Core\Exception\Exception
      * @return void
      */
     public function testReadEmptyFile()
     {
         $engine = new JsonConfig($this->path);
         $config = $engine->read('empty');
-        $this->assertEquals([], $config);
     }
 
     /**