Browse Source

Backport 4.x orFail() for config.

mscherer 6 years ago
parent
commit
9cd5fe0085

+ 2 - 0
src/Core/Configure.php

@@ -439,6 +439,7 @@ class Configure
      * @param string $cacheConfig The cache configuration to save into. Defaults to 'default'
      * @param array|null $data Either an array of data to store, or leave empty to store all values.
      * @return bool Success
+     * @throws \RuntimeException
      */
     public static function store($name, $cacheConfig = 'default', $data = null)
     {
@@ -459,6 +460,7 @@ class Configure
      * @param string $name Name of the stored config file to load.
      * @param string $cacheConfig Name of the Cache configuration to read from.
      * @return bool Success.
+     * @throws \RuntimeException
      */
     public static function restore($name, $cacheConfig = 'default')
     {

+ 26 - 2
src/Core/InstanceConfigTrait.php

@@ -16,6 +16,7 @@ namespace Cake\Core;
 
 use Cake\Core\Exception\Exception;
 use Cake\Utility\Hash;
+use InvalidArgumentException;
 
 /**
  * A trait for reading and writing instance config
@@ -109,8 +110,8 @@ trait InstanceConfigTrait
      * ```
      *
      * @param string|null $key The key to get or null for the whole config.
-     * @param mixed $default The return value when the key does not exist.
-     * @return mixed Configuration data at the named key or null if the key does not exist.
+     * @param mixed|null $default The return value when the key does not exist.
+     * @return mixed|null Configuration data at the named key or null if the key does not exist.
      */
     public function getConfig($key = null, $default = null)
     {
@@ -187,6 +188,29 @@ trait InstanceConfigTrait
     }
 
     /**
+     * Returns the config for this specific key.
+     *
+     * The config value for this key must exist, it can never be null.
+     *
+     * @param string $key The key to get.
+     * @return mixed Configuration data at the named key
+     * @throws \InvalidArgumentException
+     */
+    public function getConfigOrFail($key)
+    {
+        if (!isset($key)) {
+            throw new InvalidArgumentException('$key must not be null.');
+        }
+
+        $config = $this->getConfig($key);
+        if ($config === null) {
+            throw new InvalidArgumentException(sprintf('Expected configuration `%s` not found.', $key));
+        }
+
+        return $config;
+    }
+
+    /**
      * Merge provided config with existing config. Unlike `config()` which does
      * a recursive merge for nested keys, this method does a simple merge.
      *

+ 22 - 1
src/Core/StaticConfigTrait.php

@@ -109,7 +109,7 @@ trait StaticConfigTrait
      * Reads existing configuration.
      *
      * @param string $key The name of the configuration.
-     * @return mixed Configuration data at the named key or null if the key does not exist.
+     * @return mixed|null Configuration data at the named key or null if the key does not exist.
      */
     public static function getConfig($key)
     {
@@ -177,6 +177,27 @@ trait StaticConfigTrait
     }
 
     /**
+     * Reads existing configuration for a specific key.
+     *
+     * The config value for this key must exist, it can never be null.
+     *
+     * @param string $key The name of the configuration.
+     * @return mixed Configuration data at the named key.
+     * @throws \InvalidArgumentException If value does not exist.
+     */
+    public static function getConfigOrFail($key)
+    {
+        if (!isset($key)) {
+            throw new InvalidArgumentException('$key must not be null.');
+        }
+        if (!isset(static::$_config[$key])) {
+            throw new InvalidArgumentException(sprintf('Expected configuration `%s` not found.', $key));
+        }
+
+        return static::$_config[$key];
+    }
+
+    /**
      * Drops a constructed adapter.
      *
      * If you wish to modify an existing configuration, you should drop it,

+ 29 - 2
tests/TestCase/Core/InstanceConfigTraitTest.php

@@ -17,13 +17,13 @@ namespace Cake\Test\TestCase\Core;
 use Cake\Core\InstanceConfigTrait;
 use Cake\TestSuite\TestCase;
 use Exception;
+use InvalidArgumentException;
 
 /**
  * TestInstanceConfig
  */
 class TestInstanceConfig
 {
-
     use InstanceConfigTrait;
 
     /**
@@ -47,7 +47,6 @@ class TestInstanceConfig
  */
 class ReadOnlyTestInstanceConfig
 {
-
     use InstanceConfigTrait;
 
     /**
@@ -84,6 +83,10 @@ class ReadOnlyTestInstanceConfig
  */
 class InstanceConfigTraitTest extends TestCase
 {
+    /**
+     * @var TestInstanceConfig
+     */
+    protected $config;
 
     /**
      * setUp method
@@ -297,6 +300,30 @@ class InstanceConfigTraitTest extends TestCase
     }
 
     /**
+     * @return void
+     */
+    public function testGetConfigOrFail()
+    {
+        $this->object->setConfig(['foo' => 'bar']);
+        $this->assertSame(
+            'bar',
+            $this->object->getConfigOrFail('foo'),
+            'should return the same value just set'
+        );
+    }
+
+    /**
+     * @return void
+     */
+    public function testGetConfigOrFailException()
+    {
+        $this->expectException(InvalidArgumentException::class);
+        $this->expectExceptionMessage('Expected configuration `foo` not found.');
+
+        $this->object->getConfigOrFail('foo');
+    }
+
+    /**
      * test shallow merge
      *
      * @return void

+ 26 - 1
tests/TestCase/Core/StaticConfigTraitTest.php

@@ -15,13 +15,13 @@ namespace Cake\Test\TestCase\Core;
 
 use Cake\Core\StaticConfigTrait;
 use Cake\TestSuite\TestCase;
+use InvalidArgumentException;
 
 /**
  * TestCacheStaticConfig
  */
 class TestCacheStaticConfig
 {
-
     use StaticConfigTrait;
 
     /**
@@ -133,6 +133,31 @@ class StaticConfigTraitTest extends TestCase
     }
 
     /**
+     * @return void
+     */
+    public function testGetConfigOrFail()
+    {
+        $className = get_class($this->subject);
+        $className::setConfig('foo', 'bar');
+
+        $result = $className::getConfigOrFail('foo');
+        $this->assertSame('bar', $result);
+    }
+
+    /**
+     * @return void
+     */
+    public function testGetConfigOrFailException()
+    {
+        $this->expectException(InvalidArgumentException::class);
+        $this->expectExceptionMessage('Expected configuration `foo` not found.');
+
+        $className = get_class($this->subject);
+        $result = $className::getConfigOrFail('foo');
+        $this->assertSame('bar', $result);
+    }
+
+    /**
      * Tests parsing querystring values
      *
      * @return void