Przeglądaj źródła

Added Zend_Config support to Zend_Cache_Core - fixes ZF-7568

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18254 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic 16 lat temu
rodzic
commit
e89ccabd3a
2 zmienionych plików z 49 dodań i 6 usunięć
  1. 24 2
      library/Zend/Cache/Core.php
  2. 25 4
      tests/Zend/Cache/CoreTest.php

+ 24 - 2
library/Zend/Cache/Core.php

@@ -124,12 +124,19 @@ class Zend_Cache_Core
     /**
      * Constructor
      *
-     * @param  array $options Associative array of options
+     * @param  array|Zend_Config $options Associative array of options or Zend_Config instance
      * @throws Zend_Cache_Exception
      * @return void
      */
-    public function __construct(array $options = array())
+    public function __construct($options = array())
     {
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        }
+        if (!is_array($options)) {
+            Zend_Cache::throwException("Options passed were not an array"
+            . " or Zend_Config instance.");
+        }
         while (list($name, $value) = each($options)) {
             $this->setOption($name, $value);
         }
@@ -137,6 +144,21 @@ class Zend_Cache_Core
     }
 
     /**
+     * Set options using an instance of type Zend_Config
+     *
+     * @param Zend_Config $config
+     * @return Zend_Cache_Core
+     */
+    public function setConfig(Zend_Config $config)
+    {
+        $options = $config->toArray();
+        while (list($name, $value) = each($options)) {
+            $this->setOption($name, $value);
+        }
+        return $this;
+    }
+
+    /**
      * Set the backend
      *
      * @param  object $backendObject

+ 25 - 4
tests/Zend/Cache/CoreTest.php

@@ -33,6 +33,8 @@ require_once 'Zend/Cache/Backend/Test.php';
  */
 require_once 'PHPUnit/Framework/TestCase.php';
 
+require_once 'Zend/Config.php';
+
 /**
  * @category   Zend
  * @package    Zend_Cache
@@ -41,7 +43,7 @@ require_once 'PHPUnit/Framework/TestCase.php';
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  * @group      Zend_Cache
  */
-class Zend_Cache_CoreTest extends PHPUnit_Framework_TestCase 
+class Zend_Cache_CoreTest extends PHPUnit_Framework_TestCase
 {
     private $_instance;
 
@@ -64,6 +66,27 @@ class Zend_Cache_CoreTest extends PHPUnit_Framework_TestCase
         $test = new Zend_Cache_Core(array('lifetime' => 3600, 'caching' => true));
     }
 
+    /**
+     * @issue ZF-7568
+     */
+    public function testConstructorCorrectCallWithZendConfig()
+    {
+        $test = new Zend_Cache_Core(
+            new Zend_Config(array('lifetime' => 3600, 'caching' => true))
+        );
+    }
+
+    /**
+     * @issue ZF-7568
+     */
+    public function testSettingOptionsWithZendConfig()
+    {
+        $config = new Zend_Config(array('lifetime' => 3600, 'caching' => true));
+        $test = new Zend_Cache_Core();
+        $test->setConfig($config);
+        $this->assertEquals(3600, $test->getOption('lifetime'));
+    }
+
     public function testConstructorBadOption()
     {
         try {
@@ -110,7 +133,7 @@ class Zend_Cache_CoreTest extends PHPUnit_Framework_TestCase
     }
 
     /**
-     * Unknown options are okay and should be silently ignored. Non-string 
+     * Unknown options are okay and should be silently ignored. Non-string
      * options, however, should throw exceptions.
      *
      * @group ZF-5034
@@ -457,5 +480,3 @@ class Zend_Cache_CoreTest extends PHPUnit_Framework_TestCase
     }
 
 }
-
-