Quellcode durchsuchen

Fixing ZF-5577: Adding full Zend_Config support to all adapters
- Future: might want to consider moving config handling to a base abstract class - but that would introduce BC


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17030 44c647ce-9c0f-0410-b52a-842ac1e357ba

shahar vor 16 Jahren
Ursprung
Commit
f50a23b94e

+ 8 - 3
library/Zend/Http/Client/Adapter/Curl.php

@@ -110,14 +110,19 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
      * Set the configuration array for the adapter
      *
      * @throws Zend_Http_Client_Adapter_Exception
-     * @param array $config
+     * @param  Zend_Config | array $config
      * @return Zend_Http_Client_Adapter_Curl
      */
     public function setConfig($config = array())
     {
-        if (!is_array($config)) {
+        if ($config instanceof Zend_Config) {
+            $config = $config->toArray();
+
+        } elseif (! is_array($config)) {
             require_once 'Zend/Http/Client/Adapter/Exception.php';
-            throw new Zend_Http_Client_Adapter_Exception('Http Adapter configuration expects an array, ' . gettype($config) . ' recieved.');
+            throw new Zend_Http_Client_Adapter_Exception(
+                'Array or Zend_Config object expected, got ' . gettype($config)
+            );
         }
 
         if(isset($config['proxy_user']) && isset($config['proxy_pass'])) {

+ 7 - 3
library/Zend/Http/Client/Adapter/Socket.php

@@ -93,14 +93,18 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
     /**
      * Set the configuration array for the adapter
      *
-     * @param array $config
+     * @param Zend_Config | array $config
      */
     public function setConfig($config = array())
     {
-        if (! is_array($config)) {
+        if ($config instanceof Zend_Config) {
+            $config = $config->toArray();
+
+        } elseif (! is_array($config)) {
             require_once 'Zend/Http/Client/Adapter/Exception.php';
             throw new Zend_Http_Client_Adapter_Exception(
-                '$concig expects an array, ' . gettype($config) . ' recieved.');
+                'Array or Zend_Config object expected, got ' . gettype($config)
+            );
         }
 
         foreach ($config as $k => $v) {

+ 8 - 3
library/Zend/Http/Client/Adapter/Test.php

@@ -81,14 +81,18 @@ class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interfac
     /**
      * Set the configuration array for the adapter
      *
-     * @param array $config
+     * @param Zend_Config | array $config
      */
     public function setConfig($config = array())
     {
-        if (! is_array($config)) {
+        if ($config instanceof Zend_Config) {
+            $config = $config->toArray();
+
+        } elseif (! is_array($config)) {
             require_once 'Zend/Http/Client/Adapter/Exception.php';
             throw new Zend_Http_Client_Adapter_Exception(
-                '$config expects an array, ' . gettype($config) . ' recieved.');
+                'Array or Zend_Config object expected, got ' . gettype($config)
+            );
         }
 
         foreach ($config as $k => $v) {
@@ -96,6 +100,7 @@ class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interfac
         }
     }
 
+
     /**
      * Connect to the remote server
      *

+ 27 - 0
tests/Zend/Http/Client/CommonHttpTests.php

@@ -52,6 +52,13 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
     protected $client = null;
 
     /**
+     * Common HTTP client adapter
+     *
+     * @var Zend_Http_Client_Adapter_Interface
+     */
+    protected $_adapter = null;
+
+    /**
      * Configuration array
      *
      * @var array
@@ -78,7 +85,10 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
             }
 
             $uri = $this->baseuri . $name . '.php';
+
+            $this->_adapter = new $this->config['adapter'];
             $this->client = new Zend_Http_Client($uri, $this->config);
+            $this->client->setAdapter($this->_adapter);
 
         } else {
             // Skip tests
@@ -93,6 +103,7 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
     protected function tearDown()
     {
         $this->client = null;
+        $this->_adapter = null;
     }
 
     /**
@@ -824,4 +835,20 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
             )
         );
     }
+
+    /**
+     * Data provider for invalid configuration containers
+     *
+     * @return array
+     */
+    static public function invalidConfigProvider()
+    {
+        return array(
+            array(false),
+            array('foo => bar'),
+            array(null),
+            array(new stdClass),
+            array(55)
+        );
+    }
 }

+ 59 - 0
tests/Zend/Http/Client/CurlTest.php

@@ -6,6 +6,8 @@ if (!defined('PHPUnit_MAIN_METHOD')) {
 
 require_once dirname(__FILE__) . '/CommonHttpTests.php';
 
+require_once 'Zend/Http/Client/Adapter/Curl.php';
+
 /**
  * This Testsuite includes all Zend_Http_Client that require a working web
  * server to perform. It was designed to be extendable, so that several
@@ -52,6 +54,63 @@ class Zend_Http_Client_CurlTest extends Zend_Http_Client_CommonHttpTests
     }
 
     /**
+     * Off-line common adapter tests
+     */
+
+    /**
+     * Test that we can set a valid configuration array with some options
+     *
+     */
+    public function testConfigSetAsArray()
+    {
+        $config = array(
+            'timeout'    => 500,
+            'someoption' => 'hasvalue'
+        );
+
+        $this->_adapter->setConfig($config);
+
+        $hasConfig = $this->getObjectAttribute($this->_adapter, '_config');
+        foreach($config as $k => $v) {
+            $this->assertEquals($v, $hasConfig[$k]);
+        }
+    }
+
+    /**
+     * Test that a Zend_Config object can be used to set configuration
+     *
+     * @link http://framework.zend.com/issues/browse/ZF-5577
+     */
+    public function testConfigSetAsZendConfig()
+    {
+        require_once 'Zend/Config.php';
+
+        $config = new Zend_Config(array(
+            'timeout'  => 400,
+            'nested'   => array(
+                'item' => 'value',
+            )
+        ));
+
+        $this->_adapter->setConfig($config);
+
+        $hasConfig = $this->getObjectAttribute($this->_adapter, '_config');
+        $this->assertEquals($config->timeout, $hasConfig['timeout']);
+        $this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
+    }
+
+    /**
+     * Check that an exception is thrown when trying to set invalid config
+     *
+     * @expectedException Zend_Http_Client_Adapter_Exception
+     * @dataProvider invalidConfigProvider
+     */
+    public function testSetConfigInvalidConfig($config)
+    {
+        $this->_adapter->setConfig($config);
+    }
+
+    /**
      * CURLOPT_CLOSEPOLICY never worked and returns false on setopt always:
      * @link http://de2.php.net/manual/en/function.curl-setopt.php#84277
      *

+ 57 - 0
tests/Zend/Http/Client/SocketTest.php

@@ -36,6 +36,63 @@ class Zend_Http_Client_SocketTest extends Zend_Http_Client_CommonHttpTests
     );
 
     /**
+     * Off-line common adapter tests
+     */
+
+    /**
+     * Test that we can set a valid configuration array with some options
+     *
+     */
+    public function testConfigSetAsArray()
+    {
+        $config = array(
+            'timeout'    => 500,
+            'someoption' => 'hasvalue'
+        );
+
+        $this->_adapter->setConfig($config);
+
+        $hasConfig = $this->getObjectAttribute($this->_adapter, 'config');
+        foreach($config as $k => $v) {
+            $this->assertEquals($v, $hasConfig[$k]);
+        }
+    }
+
+    /**
+     * Test that a Zend_Config object can be used to set configuration
+     *
+     * @link http://framework.zend.com/issues/browse/ZF-5577
+     */
+    public function testConfigSetAsZendConfig()
+    {
+        require_once 'Zend/Config.php';
+
+        $config = new Zend_Config(array(
+            'timeout'  => 400,
+            'nested'   => array(
+                'item' => 'value',
+            )
+        ));
+
+        $this->_adapter->setConfig($config);
+
+        $hasConfig = $this->getObjectAttribute($this->_adapter, 'config');
+        $this->assertEquals($config->timeout, $hasConfig['timeout']);
+        $this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
+    }
+
+    /**
+     * Check that an exception is thrown when trying to set invalid config
+     *
+     * @expectedException Zend_Http_Client_Adapter_Exception
+     * @dataProvider invalidConfigProvider
+     */
+    public function testSetConfigInvalidConfig($config)
+    {
+        $this->_adapter->setConfig($config);
+    }
+
+    /**
      * Stream context related tests
      */
 

+ 6 - 7
tests/Zend/Http/Client/TestAdapterTest.php

@@ -41,15 +41,14 @@ class Zend_Http_Client_TestAdapterTest extends PHPUnit_Framework_TestCase
         $this->adapter = null;
     }
 
+    /**
+     * Make sure an exception is thrown on invalid cofiguration
+     *
+     * @expectedException Zend_Http_Client_Adapter_Exception
+     */
     public function testSetConfigThrowsOnInvalidConfig()
     {
-        try {
-            $this->adapter->setConfig('foo');
-        } catch (Exception $e) {
-            $class = 'Zend_Http_Client_Adapter_Exception';
-            $this->assertType($class, $e);
-            $this->assertRegexp('/expects an array/i', $e->getMessage());
-        }
+        $this->adapter->setConfig('foo');
     }
 
     public function testSetConfigReturnsQuietly()