Author: ts
Date: Thu Oct 11 15:21:45 2007
New Revision: 6420

Log:
- Refactored ezcWebdavTransport for better mockability.
- Adjusted client test case temporarely.
# Work in progress for refactoring client test case.

Modified:
    trunk/Webdav/src/transport.php
    trunk/Webdav/tests/classes/transport_test_mock.php
    trunk/Webdav/tests/client_test.php

Modified: trunk/Webdav/src/transport.php
==============================================================================
--- trunk/Webdav/src/transport.php [iso-8859-1] (original)
+++ trunk/Webdav/src/transport.php [iso-8859-1] Thu Oct 11 15:21:45 2007
@@ -275,7 +275,7 @@
      */
     public final function handleResponse( ezcWebdavResponse $response )
     {
-        $this->sendResponse( $this->processResponse( $response ) );
+        $this->sendResponse( $this->flattenResponse( $this->processResponse( 
$response ) ) );
     }
 
     /**
@@ -308,75 +308,80 @@
      * self::$handlingMap} and throws an Exception if the given class could not
      * be dispatched.
      *
+     * The method internally calls one of the handle*Response() methods to get
+     * the repsonse object processed and returns an instance of [EMAIL 
PROTECTED]
+     * ezcWebdavDisplayInformation} to be displayed.
+     *
      * @param ezcWebdavResponse $response 
      * @return ezcWebdavDisplayInformation
-     *
+     * 
      * @throws RuntimeException
      *         if the class of the given object could not be dispatched.
-     *
-     * @todo Correct exception. Or better: Correct all exception mess!
-     */
-    private function processResponse( ezcWebdavResponse $response )
-    {
-        if ( isset( self::$handlingMap[( $responseClass = get_class( $response 
) )] ) === false )
-        {
-            // @todo: The processResponse plugin hook should be announced here.
-            throw new RuntimeException( "Serialization of class $responseClass 
not implemented, yet." );
-        }
-        return call_user_func( array( $this, self::$handlingMap[( 
$responseClass = get_class( $response ) )] ), $response );
-    }
-
-    /**
-     * Finally send out the response.
-     *
-     * This method is called to finally send the response to the browser. It
-     * can be overwritten in test cases to change the behaviour of printing out
-     * the result and sending the headers. The method automatically generates 
an
-     * appropriate Content-Type header for XML output, if an
-     * [EMAIL PROTECTED] ezcWebdavXmlDisplayInformation} is received. A header 
existent in the
-     * response object will not be affected and the method will silently go on.
-     *
-     * If an [EMAIL PROTECTED] ezcWebdavStringDisplayInformation} is submitted
-     * correct setting of the Content-Type header is checked and an [EMAIL 
PROTECTED]
-     * ezcWebdavMissingHeaderException} is thrown in negative case.
-     *
-     * If an [EMAIL PROTECTED] ezcWebdavEmptyDisplayInformation} is received, 
the method
-     * checks if Content-Type and Content-Length headers are not present, so
-     * they are not excplicitly send later on.
-     * 
-     * @param ezcWebdavDisplayInformation $info
-     * @return void
-     *
      * @throws ezcWebdavMissingHeaderException
-     *         if the submitted $info parameter is an [EMAIL PROTECTED]
+     *         if the generated result is an [EMAIL PROTECTED]
      *         ezcWebdavStringDisplayInformation} struct and the contained
      *         [EMAIL PROTECTED] ezcWebdavResponse} object has no Content-Type 
header set.
      * @throws ezcWebdavInvalidHeaderException
-     *         if the submitted $info parameter is an [EMAIL PROTECTED]
+     *         if the generated result is an [EMAIL PROTECTED]
      *         ezcWebdavEmptyDisplayInformation} and the contained [EMAIL 
PROTECTED]
      *         ezcWebdavResponse} object has a Content-Type or a Content-Length
      *         header set.
-     */
-    protected function sendResponse( ezcWebdavDisplayInformation $info )
-    {
+     *
+     * @todo Correct exception. Or better: Correct all exception mess!
+     */
+    private function processResponse( ezcWebdavResponse $response )
+    {
+        if ( isset( self::$handlingMap[( $responseClass = get_class( $response 
) )] ) === false )
+        {
+            // @todo: The processResponse plugin hook should be announced here.
+            throw new RuntimeException( "Serialization of class $responseClass 
not implemented, yet." );
+        }
+        
+        return call_user_func( array( $this, self::$handlingMap[( 
$responseClass = get_class( $response ) )] ), $response );
+    }
+
+    /**
+     * Flattens a processed response object to headers and body.
+     *
+     * Takes a given [EMAIL PROTECTED] ezcWebdavDisplayInformation} object and 
returns an
+     * array containg the headers and body it represents.
+     *
+     * <code>
+     *      array(
+     *          'headers' => array(
+     *              ''       => '<responsecodeandname>',
+     *              '<name>' => '<value>',
+     *              // ...
+     *          ),
+     *          'body' => '<string>'
+     *      )
+     * </code>
+     *
+     * The returned information can be processed (send out to the client) by
+     * [EMAIL PROTECTED] ezcWebdavTransport::sendResponse()}.
+     * 
+     * @param ezcWebdavDisplayInformation $info 
+     * @return array(string=>mixed)
+     */
+    protected function flattenResponse( ezcWebdavDisplayInformation $info )
+    {
+        $headers     = array_merge( $info->response->getHeaders() );
+        $headers[''] = (string) $info->response;
+        $body        = '';
+
         switch ( true )
         {
             case ( $info instanceof ezcWebdavXmlDisplayInformation ):
+                $headers['Content-Type']  = ( isset( $headers['Content-Type'] 
) ? $headers['Content-Type'] : 'text/xml; charset="utf-8"' );
                 $info->body->formatOutput = true;
-                // Explicitly set txt/xml content type
-                if ( $info->response->getHeader( 'Content-Type' ) === null )
-                {
-                    $info->response->setHeader( 'Content-Type', 'text/xml; 
charset="utf-8"' );
-                }
-                $result = $info->body->saveXML( $info->body );
+                $body                     = $info->body->saveXML( $info->body 
);
                 break;
-                
             case ( $info instanceof ezcWebdavStringDisplayInformation ):
                 if ( $info->response->getHeader( 'Content-Type' ) === null )
                 {
                     throw new ezcWebdavMissingHeaderException( 'ContentType' );
                 }
-                $result = $info->body;
+                $body = $info->body;
                 break;
 
             case ( $info instanceof ezcWebdavEmptyDisplayInformation ):
@@ -385,25 +390,50 @@
                 {
                     throw new ezcWebdavInvalidHeaderException( 'Content-Type', 
$contenTypeHeader, 'null' );
                 }
-                $result = null;
+                $body = '';
                 break;
         }
         
-        // Sends HTTP response code and description, 3rd param forces status
-        header( (string) $info->response, true, (int) $info->response->status 
);
-
-        // Send headers defined by response
-        $headers = $info->response->getHeaders();
-        foreach ( $headers as $name => $value )
-        {
-            header( "{$name}: {$value}" );
-        }
-
-        if ( $result !== null )
-        {
-            // Content-Length header automatically send
-            echo $result;
-        }
+        return array( 'headers' => $headers, 'body' => $body );
+    }
+
+    /**
+     * Finally send out the response.
+     *
+     * This method is called to finally send the response to the browser. It
+     * can be overwritten in test cases to change the behaviour of printing out
+     * the result and sending the headers. The method receives an array
+     * containg 2 elements: The key 'headers' is assigned to an array of
+     * headers to be send. This array is indexed by the header name, while
+     * there is usually 1 empty key, that contains the response code header.
+     * The second element has the key 'body' and contains the string to be send
+     * to the client.
+     *
+     * <code>
+     *      array(
+     *          'headers' => array(
+     *              ''       => '<responsecodeandname>',
+     *              '<name>' => '<value>',
+     *              // ...
+     *          ),
+     *          'body' => '<string>'
+     *      )
+     * </code>
+     *
+     * @param array(string=>mixed) $output
+     * @return void
+     *
+     */
+    protected function sendResponse( array $output )
+    {
+        // Sends HTTP response code and description
+        foreach( $output['headers'] as $name => $content )
+        {
+            header( ( $name === '' ? $content : "{$name}: {$content}" ) );
+        }
+
+        // Content-Length header automatically send
+        echo $result;
     }
 
     /**

Modified: trunk/Webdav/tests/classes/transport_test_mock.php
==============================================================================
--- trunk/Webdav/tests/classes/transport_test_mock.php [iso-8859-1] (original)
+++ trunk/Webdav/tests/classes/transport_test_mock.php [iso-8859-1] Thu Oct 11 
15:21:45 2007
@@ -4,43 +4,13 @@
 {
     protected function retreiveBody()
     {
-        return isset( $GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_BODY'] ) ? 
$GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_BODY'] : '';
+        return $GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_BODY'];
     }
 
-    protected function sendResponse( ezcWebdavDisplayInformation $info )
+    protected function sendResponse( array $output )
     {
-        $headers = array();
-
-        switch ( true )
-        {
-            case ( $info->body instanceof DOMDocument ):
-                $info->body->formatOutput = true;
-                $result = $info->body->saveXML( $info->body );
-                break;
-            case ( is_string( $info->body ) ):
-                $result = $info->body;
-                break;
-            case ( $info->body === null ):
-            default:
-                $result = '';
-                break;
-        }
-        
-        // Sends HTTP response code and description
-        $headers[] = (string) $info->response;
-
-        // Send headers defined by response
-        $responseHeaders = $info->response->getHeaders();
-        foreach ( $responseHeaders as $name => $value )
-        {
-            $headers[$name] = $value;
-        }
-
-        // Do we need to explictly send the Content-Length header here?
-        
-        $GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_RESPONSE_BODY']    = $result;
-        $GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_RESPONSE_HEADERS'] = $headers;
-        // All done
+        $GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_RESPONSE_HEADERS'] = 
$output['headers'];
+        $GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_RESPONSE_BODY']    = 
$output['body'];
     }
 }
 

Modified: trunk/Webdav/tests/client_test.php
==============================================================================
--- trunk/Webdav/tests/client_test.php [iso-8859-1] (original)
+++ trunk/Webdav/tests/client_test.php [iso-8859-1] Thu Oct 11 15:21:45 2007
@@ -179,9 +179,9 @@
         $responseObject = $this->backend->performRequest( $requestObject );
         
         $this->transport->handleResponse( $responseObject );
+        
         $responseBody    = $GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_RESPONSE_BODY'];
         $responseHeaders = 
$GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_RESPONSE_HEADERS'];
-
 
         if ( $response['result'] === false )
         {
@@ -220,6 +220,15 @@
         } 
         else
         {
+            // FIXME
+            $response['result']['headers'][''] = 
$response['result']['headers'][0];
+            unset( $response['result']['headers'][0] );
+            if ( !empty( $responseBody ) && !isset( 
$response['result']['headers']['Content-Type'] ) )
+            {
+                $response['result']['headers']['Content-Type'] = 'text/xml; 
charset="utf-8"';
+            }
+            // END FIXME
+
             $this->assertEquals(
                 $response['result']['headers'],
                 $responseHeaders,


-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to