Browse Source

Add config orFail() wrapper.

mscherer 6 years ago
parent
commit
0209b453da

+ 2 - 0
src/Core/Configure.php

@@ -428,6 +428,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(string $name, string $cacheConfig = 'default', ?array $data = null): bool
     {
@@ -448,6 +449,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(string $name, string $cacheConfig = 'default'): bool
     {

+ 22 - 2
src/Core/InstanceConfigTrait.php

@@ -18,6 +18,7 @@ namespace Cake\Core;
 
 use Cake\Core\Exception\Exception;
 use Cake\Utility\Hash;
+use InvalidArgumentException;
 
 /**
  * A trait for reading and writing instance config
@@ -111,8 +112,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(?string $key = null, $default = null)
     {
@@ -127,6 +128,25 @@ 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(string $key)
+    {
+        $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.
      *

+ 19 - 1
src/Core/StaticConfigTrait.php

@@ -112,7 +112,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(string $key)
     {
@@ -120,6 +120,24 @@ 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|null Configuration data at the named key.
+     * @throws \InvalidArgumentException If value does not exist.
+     */
+    public static function getConfigOrFail(string $key)
+    {
+        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,

+ 25 - 0
tests/TestCase/Core/InstanceConfigTraitTest.php

@@ -17,6 +17,7 @@ declare(strict_types=1);
 namespace Cake\Test\TestCase\Core;
 
 use Cake\TestSuite\TestCase;
+use InvalidArgumentException;
 use TestApp\Config\ReadOnlyTestInstanceConfig;
 use TestApp\Config\TestInstanceConfig;
 
@@ -237,6 +238,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

+ 31 - 0
tests/TestCase/Core/StaticConfigTraitTest.php

@@ -17,6 +17,7 @@ namespace Cake\Test\TestCase\Core;
 
 use Cake\Core\StaticConfigTrait;
 use Cake\TestSuite\TestCase;
+use InvalidArgumentException;
 use TestApp\Config\TestEmailStaticConfig;
 use TestApp\Config\TestLogStaticConfig;
 use TypeError;
@@ -27,6 +28,11 @@ use TypeError;
 class StaticConfigTraitTest extends TestCase
 {
     /**
+     * @var object
+     */
+    protected $subject;
+
+    /**
      * setup method
      *
      * @return void
@@ -72,6 +78,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

+ 5 - 0
tests/TestCase/View/StringTemplateTest.php

@@ -22,6 +22,11 @@ use Cake\View\StringTemplate;
 class StringTemplateTest extends TestCase
 {
     /**
+     * @var \Cake\View\StringTemplate
+     */
+    protected $template;
+
+    /**
      * setUp
      *
      * @return void

+ 2 - 2
tests/test_app/TestApp/Config/ReadOnlyTestInstanceConfig.php

@@ -28,11 +28,11 @@ class ReadOnlyTestInstanceConfig
      * Example of how to prevent modifying config at run time
      *
      * @throws \Exception
-     * @param mixed $key
+     * @param string|array $key
      * @param mixed $value
      * @return void
      */
-    protected function _configWrite($key, $value = null)
+    protected function _configWrite($key, $value): void
     {
         throw new Exception('This Instance is readonly');
     }