Explorar el Código

Added possibility to force the test adapter to fail. Added unit tests and docs as well

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17601 44c647ce-9c0f-0410-b52a-842ac1e357ba
cogo hace 16 años
padre
commit
de84bd3c71

+ 30 - 0
documentation/manual/en/module_specs/Zend_Http_Client-Adapters.xml

@@ -551,6 +551,36 @@ $adapter->addResponse(
             the <acronym>HTTP</acronym> client containing the adapter into your object under test
             and test its behavior.
         </para>
+        <para>
+            If you need the adapter to fail on demand you can use 
+            <methodname>setNextRequestWillFail($flag)</methodname>. The method will cause the next 
+            call to <methodname>connect()</methodname> to throw an 
+            <classname>Zend_Http_Client_Adapter_Exception</classname> exception. This can be useful 
+            when your application caches content from an external site (in case the site goes down)
+            and you want to test this feature.
+        </para>
+        <example id="zend.http.client.adapters.test.example-3">
+            <title>Forcing the adapter to fail</title>
+            <programlisting language="php"><![CDATA[
+// Instantiate a new adapter and client
+$adapter = new Zend_Http_Client_Adapter_Test();
+$client = new Zend_Http_Client('http://www.example.com', array(
+    'adapter' => $adapter
+));
+
+// Force the next request to fail with an exception
+$adapter->nextRequestWillFail(true);
+
+try {
+    // This call will result in a Zend_Http_Client_Adapter_Exception
+    $client->request(); 
+} catch (Zend_Http_Client_Adapter_Exception $e) {
+    // ... 
+}
+
+// Further requests will work as expected until you call setNextRequestWillFail(true) again
+]]></programlisting>
+        </example>
     </sect2>
 
     <sect2 id="zend.http.client.adapters.extending">

+ 28 - 1
library/Zend/Http/Client/Adapter/Test.php

@@ -72,6 +72,13 @@ class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interfac
     protected $responseIndex = 0;
 
     /**
+     * Wether or not the next request will fail with an exception
+     *
+     * @var boolean
+     */
+    protected $_nextRequestWillFail = false;
+
+    /**
      * Adapter constructor, currently empty. Config is set using setConfig()
      *
      */
@@ -79,6 +86,19 @@ class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interfac
     { }
 
     /**
+     * Set the nextRequestWillFail flag
+     *
+     * @param boolean $flag
+     * @return Zend_Http_Client_Adapter_Test
+     */
+    public function setNextRequestWillFail($flag)
+    {
+        $this->_nextRequestWillFail = (bool) $flag;
+
+        return $this;
+    }
+
+    /**
      * Set the configuration array for the adapter
      *
      * @param Zend_Config | array $config
@@ -108,9 +128,16 @@ class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interfac
      * @param int     $port
      * @param boolean $secure
      * @param int     $timeout
+     * @throws Zend_Http_Client_Adapter_Exception
      */
     public function connect($host, $port = 80, $secure = false)
-    { }
+    {
+        if ($this->_nextRequestWillFail) {
+            $this->_nextRequestWillFail = false;
+            require_once 'Zend/Http/Client/Adapter/Exception.php';
+            throw new Zend_Http_Client_Adapter_Exception('Request failed');
+        }
+    }
 
     /**
      * Send request to the remote server

+ 14 - 0
tests/Zend/Http/Client/TestAdapterTest.php

@@ -89,6 +89,20 @@ class Zend_Http_Client_TestAdapterTest extends PHPUnit_Framework_TestCase
         $this->adapter->close();
     }
 
+    public function testFailRequestOnDemand()
+    {
+        $this->adapter->setNextRequestWillFail(true);
+
+        try {
+            // Make a connection that will fail
+            $this->adapter->connect('http://foo');
+            $this->fail();
+        } catch (Zend_Http_Client_Adapter_Exception $e) {
+            // Connect again to see that the next request does not fail
+            $this->adapter->connect('http://foo');
+        }
+    }
+
     public function testReadDefaultResponse()
     {
         $expected = "HTTP/1.1 400 Bad Request\r\n\r\n";