Author: kn
Date: Fri Sep  7 12:46:32 2007
New Revision: 6012

Log:
- Added basic tests for selection of transport me chanism depending on the
  client name

Added:
    experimental/Webdav/src/exceptions/
    experimental/Webdav/src/exceptions/exception.php   (with props)
    experimental/Webdav/src/exceptions/no_transport_handler.php   (with props)
Modified:
    experimental/Webdav/src/server.php
    experimental/Webdav/src/webdav_autoload.php
    experimental/Webdav/tests/server_test.php

Added: experimental/Webdav/src/exceptions/exception.php
==============================================================================
--- experimental/Webdav/src/exceptions/exception.php (added)
+++ experimental/Webdav/src/exceptions/exception.php [iso-8859-1] Fri Sep  7 
12:46:32 2007
@@ -1,0 +1,20 @@
+<?php
+/**
+ * Base exception for the Webdav package.
+ *
+ * @package Webdav
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * General exception container for the Webdav component.
+ *
+ * @package Webdav
+ * @version //autogentag//
+ */
+abstract class ezcWebdavException extends ezcBaseException
+{
+}
+?>

Propchange: experimental/Webdav/src/exceptions/exception.php
------------------------------------------------------------------------------
    svn:eol-style = native

Added: experimental/Webdav/src/exceptions/no_transport_handler.php
==============================================================================
--- experimental/Webdav/src/exceptions/no_transport_handler.php (added)
+++ experimental/Webdav/src/exceptions/no_transport_handler.php [iso-8859-1] 
Fri Sep  7 12:46:32 2007
@@ -1,0 +1,30 @@
+<?php
+/**
+ * File containing the ezcWebdavNotTransportHandlerException class
+ *
+ * @package Webdav
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+/**
+ * Exception thrown, when no [EMAIL PROTECTED] ezcWebdavTransport} could be 
found for the
+ * requesting client.
+ *
+ * @package Webdav
+ * @version //autogentag//
+ */
+class ezcWebdavNotTransportHandlerException extends ezcWebdavException
+{
+    /**
+     * Constructor
+     * 
+     * @param string $client
+     */
+    public function __construct( $client )
+    {
+        parent::__construct( "Could not find any ezcWebdavTransport for the 
client '{$client}'." );
+    }
+}
+
+?>

Propchange: experimental/Webdav/src/exceptions/no_transport_handler.php
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: experimental/Webdav/src/server.php
==============================================================================
--- experimental/Webdav/src/server.php [iso-8859-1] (original)
+++ experimental/Webdav/src/server.php [iso-8859-1] Fri Sep  7 12:46:32 2007
@@ -69,6 +69,13 @@
     );
 
     /**
+     * Selected transport handler 
+     * 
+     * @var ezcWebdavTransport
+     */
+    protected $transport = null;
+
+    /**
      * Array with properties of the basic server class.
      * 
      * @var array
@@ -88,6 +95,41 @@
     }
 
     /**
+     * Select a transport handler to use.
+     *
+     * The main server instance knows about available clients and has a regular
+     * expression for each of them, to identify the clients it communicates to
+     * by matching the regular expression against the client name provided in
+     * the HTTP headers.
+     *
+     * The protected property $transport will contain the best matching
+     * instantiated tarnsport handler after executing the method.
+     * 
+     * @throws ezcWebdavNotTransportHandlerException
+     *         If no matching handler could be found for the current user
+     *         agent.
+     * @return void
+     */
+    protected function selectTransportHandler()
+    {
+        foreach ( $this->transportHandlers as $expression => $class )
+        {
+            if ( preg_match( $expression, $_SERVER['HTTP_USER_AGENT'] ) )
+            {
+                // Transport handler matches
+                $this->transport = new $class();
+                break;
+            }
+        }
+
+        // If no handler matched throw an exception
+        if ( $this->transport === null )
+        {
+            throw new ezcWebdavNotTransportHandlerException( 
$_SERVER['HTTP_USER_AGENT'] );
+        }
+    }
+
+    /**
      * Actually handle request using the specified backend and matching
      * transport handlers.
      * 
@@ -101,6 +143,8 @@
      */
     public function handle()
     {
+        $this->selectTransportHandler();
+
         // @TODO: Implement
     }
 
@@ -114,26 +158,51 @@
      * The given handler class should extend the basic webdav transport class
      * ezcWebdavTransport.
      * 
+     * @throws ezcBaseValueException
+     *         If a handler class name has been provided not implementing
+     *         ezcWebdavTransport.
      * @param string $expression 
      * @param string $handler 
-     * @return bool
+     * @return void
      */
     public function registerTransportHandler( $expression, $handler )
     {
-        // @TODO: Implement
+        // Check if handler class is an extension of the base transport
+        // mechanism
+        if ( !is_subclass_of( $handler, 'ezcWebdavTransport' ) )
+        {
+            throw new ezcBaseValueException( 'transportHandler', $handler, 
'ezcWebdavTransport' );
+        }
+
+        // Use array_unshift to prepend new registrations to transport handler
+        // array
+        $this->transportHandlers = array_merge(
+            array( $expression => $handler ),
+            $this->transportHandlers
+        );
     }
 
     /**
      * Unregister a new transport handler for the webdav server.
      *
-     * Remove a handler from the handler list by its regular expression.
+     * Remove a handler from the handler list by its regular expression. If no
+     * handler with theis expression has been registerd before, the method will
+     * return false.
      * 
      * @param string $expression 
      * @return bool
      */
     public function unregisterTransportHandler( $expression )
     {
-        // @TODO: Implement
+        if ( array_key_exists( $expression, $this->transportHandlers ) )
+        {
+            unset( $this->transportHandlers[$expression] );
+            return true;
+        }
+        else
+        {
+            return false;
+        }
     }
 }
 

Modified: experimental/Webdav/src/webdav_autoload.php
==============================================================================
--- experimental/Webdav/src/webdav_autoload.php [iso-8859-1] (original)
+++ experimental/Webdav/src/webdav_autoload.php [iso-8859-1] Fri Sep  7 
12:46:32 2007
@@ -10,17 +10,19 @@
  */
 
 return array(
-    'ezcWebdavBackend'               => 'Webdav/interfaces/backend.php',
-    'ezcWebdavBackendChange'         => 'Webdav/interfaces/backend/change.php',
-    'ezcWebdavBackendMakeCollection' => 
'Webdav/interfaces/backend/make_collection.php',
-    'ezcWebdavBackendPut'            => 'Webdav/interfaces/backend/put.php',
-    'ezcWebdavFileBackend'           => 'Webdav/backend/file.php',
-    'ezcWebdavGetRequest'            => 'Webdav/request/get.php',
-    'ezcWebdavPathFactory'           => 'Webdav/path_factory.php',
-    'ezcWebdavRequest'               => 'Webdav/request.php',
-    'ezcWebdavResponse'              => 'Webdav/response.php',
-    'ezcWebdavServer'                => 'Webdav/server.php',
-    'ezcWebdavTransport'             => 'Webdav/transport.php',
-    'ezcWebdavXmlBase'               => 'Webdav/interfaces/xml_base.php',
+    'ezcWebdavException'                    => 
'Webdav/exceptions/exception.php',
+    'ezcWebdavNotTransportHandlerException' => 
'Webdav/exceptions/no_transport_handler.php',
+    'ezcWebdavBackend'                      => 'Webdav/interfaces/backend.php',
+    'ezcWebdavBackendChange'                => 
'Webdav/interfaces/backend/change.php',
+    'ezcWebdavBackendMakeCollection'        => 
'Webdav/interfaces/backend/make_collection.php',
+    'ezcWebdavBackendPut'                   => 
'Webdav/interfaces/backend/put.php',
+    'ezcWebdavFileBackend'                  => 'Webdav/backend/file.php',
+    'ezcWebdavGetRequest'                   => 'Webdav/request/get.php',
+    'ezcWebdavPathFactory'                  => 'Webdav/path_factory.php',
+    'ezcWebdavRequest'                      => 'Webdav/request.php',
+    'ezcWebdavResponse'                     => 'Webdav/response.php',
+    'ezcWebdavServer'                       => 'Webdav/server.php',
+    'ezcWebdavTransport'                    => 'Webdav/transport.php',
+    'ezcWebdavXmlBase'                      => 
'Webdav/interfaces/xml_base.php',
 );
 ?>

Modified: experimental/Webdav/tests/server_test.php
==============================================================================
--- experimental/Webdav/tests/server_test.php [iso-8859-1] (original)
+++ experimental/Webdav/tests/server_test.php [iso-8859-1] Fri Sep  7 12:46:32 
2007
@@ -27,12 +27,99 @@
                return new PHPUnit_Framework_TestSuite( 
'ezcWebdavBasicServerTest' );
        }
 
-    public function testTrue()
+    public function testDefaultHandlerWithUnknowClient()
     {
-        $this->assertTrue(
-            true,
-            'true should always stay true...'
+        $_SERVER['HTTP_USER_AGENT'] = 'ezcUnknownClient';
+
+        $webdav = new ezcWebdavServer();
+        $webdav->handle();
+
+        $this->assertEquals(
+            $this->readAttribute( $webdav, 'transport' ),
+            new ezcWebdavTransport(),
+            'Fallback to ezcWebdavTransport expected.'
         );
+    }
+
+    public function testAddMockedTransport()
+    {
+        $_SERVER['HTTP_USER_AGENT'] = 'ezcMockedClient';
+
+        $mockedTransport = $this->getMock( 'ezcWebdavTransport' );
+
+        $webdav = new ezcWebdavServer();
+
+        $count = count( $this->readAttribute( $webdav, 'transportHandlers' ) );
+        $webdav->registerTransportHandler( 
+            '(^ezcMockedClient$)', 
+            get_class( $mockedTransport )
+        );
+
+        $this->assertEquals(
+            $count + 1,
+            count( $this->readAttribute( $webdav, 'transportHandlers' ) ),
+            'Expected increased count of transport handlers.'
+        );
+
+        $transportHandlers = $this->readAttribute( $webdav, 
'transportHandlers' );
+        $this->assertEquals(
+            reset( $transportHandlers ),
+            get_class( $mockedTransport ),
+            'Expected mocked transport handler to be first in list.'
+        );
+
+        $webdav->handle();
+
+        $this->assertEquals(
+            $this->readAttribute( $webdav, 'transport' ),
+            $mockedTransport,
+            'Mocked transport handler expected.'
+        );
+    }
+
+    public function testRemoveTransport()
+    {
+        $_SERVER['HTTP_USER_AGENT'] = 'ezcUnknownClient';
+
+        $webdav = new ezcWebdavServer();
+
+        $count = count( $this->readAttribute( $webdav, 'transportHandlers' ) );
+        $webdav->unregisterTransportHandler( 
+            '(.*)'
+        );
+
+        $this->assertEquals(
+            $count - 1,
+            count( $this->readAttribute( $webdav, 'transportHandlers' ) ),
+            'Expected decreased count of transport handlers.'
+        );
+    }
+
+    public function testExceptionForUnhandledClient()
+    {
+        $_SERVER['HTTP_USER_AGENT'] = 'ezcUnknownClient';
+
+        $webdav = new ezcWebdavServer();
+
+        $webdav->unregisterTransportHandler( 
+            '(.*)'
+        );
+
+        try
+        {
+            $webdav->handle();
+        }
+        catch ( ezcWebdavNotTransportHandlerException $e )
+        {
+            $this->assertEquals(
+                $e->getMessage(),
+                "Could not find any ezcWebdavTransport for the client 
'ezcUnknownClient'."
+            );
+
+            return true;
+        }
+
+        $this->fail( 'Expected ezcWebdavNotTransportHandlerException.' );
     }
 }
 ?>


-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to