Author: dr
Date: Tue Jul 31 11:03:20 2007
New Revision: 5770

Log:
- Implemented checked whether a transaction is started twice, or not at all
  when commit/rollback are called.

Added:
    trunk/Tree/src/exceptions/transaction_already_started.php   (with props)
    trunk/Tree/src/exceptions/transaction_not_started.php   (with props)
Modified:
    trunk/Tree/src/tree.php
    trunk/Tree/src/tree_autoload.php
    trunk/Tree/tests/tree.php

Added: trunk/Tree/src/exceptions/transaction_already_started.php
==============================================================================
--- trunk/Tree/src/exceptions/transaction_already_started.php (added)
+++ trunk/Tree/src/exceptions/transaction_already_started.php [iso-8859-1] Tue 
Jul 31 11:03:20 2007
@@ -1,0 +1,31 @@
+<?php
+/**
+ * File containing the ezcTreeTransactionAlreadyStartedException class
+ *
+ * @package Tree
+ * @version //autogen//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * Exception that is thrown when a transaction is active and
+ * "beginTransaction()" is called again.
+ *
+ * @package Tree
+ * @version //autogen//
+ */
+class ezcTreeTransactionAlreadyStartedException extends ezcTreeException
+{
+    /**
+     * Constructs a new ezcTreeTransactionAlreadyStartedException.
+     *
+     * @param string $id
+     * @return void
+     */
+    function __construct()
+    {
+        parent::__construct( "A transaction has already been started." );
+    }
+}
+?>

Propchange: trunk/Tree/src/exceptions/transaction_already_started.php
------------------------------------------------------------------------------
    svn:eol-style = native

Added: trunk/Tree/src/exceptions/transaction_not_started.php
==============================================================================
--- trunk/Tree/src/exceptions/transaction_not_started.php (added)
+++ trunk/Tree/src/exceptions/transaction_not_started.php [iso-8859-1] Tue Jul 
31 11:03:20 2007
@@ -1,0 +1,31 @@
+<?php
+/**
+ * File containing the ezcTreeTransactionNotStartedException class
+ *
+ * @package Tree
+ * @version //autogen//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * Exception that is thrown when "commit()" or "rollback()" are called without
+ * a matching "beginTransaction()" call.
+ *
+ * @package Tree
+ * @version //autogen//
+ */
+class ezcTreeTransactionNotStartedException extends ezcTreeException
+{
+    /**
+     * Constructs a new ezcTreeTransactionNotStartedException.
+     *
+     * @param string $id
+     * @return void
+     */
+    function __construct()
+    {
+        parent::__construct( "A transaction is not active." );
+    }
+}
+?>

Propchange: trunk/Tree/src/exceptions/transaction_not_started.php
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: trunk/Tree/src/tree.php
==============================================================================
--- trunk/Tree/src/tree.php [iso-8859-1] (original)
+++ trunk/Tree/src/tree.php [iso-8859-1] Tue Jul 31 11:03:20 2007
@@ -305,6 +305,10 @@
      */
     public function commit()
     {
+        if ( !$this->inTransaction )
+        {
+            throw new ezcTreeTransactionNotStartedException;
+        }
         $this->inTransaction = false;
         $this->inTransactionCommit = true;
         foreach ( $this->transactionList as $transactionItem )
@@ -335,18 +339,18 @@
      */
     protected function addTransactionItem( ezcTreeTransactionItem $item )
     {
+        $this->transactionList[] = $item;
+    }
+
+    /**
+     * Rolls back the transaction by clearing all stored instructions.
+     */
+    public function rollback()
+    {
         if ( !$this->inTransaction )
         {
             throw new ezcTreeTransactionNotStartedException;
         }
-        $this->transactionList[] = $item;
-    }
-
-    /**
-     * Rolls back the transaction by clearing all stored instructions.
-     */
-    public function rollback()
-    {
         $this->inTransaction = false;
         $this->transactionList = array();
     }

Modified: trunk/Tree/src/tree_autoload.php
==============================================================================
--- trunk/Tree/src/tree_autoload.php [iso-8859-1] (original)
+++ trunk/Tree/src/tree_autoload.php [iso-8859-1] Tue Jul 31 11:03:20 2007
@@ -10,25 +10,27 @@
  */
 
 return array(
-    'ezcTreeException'                => 'Tree/exceptions/exception.php',
-    'ezcTreeIdsDoNotMatchException'   => 
'Tree/exceptions/ids_do_not_match.php',
-    'ezcTreeInvalidClassException'    => 'Tree/exceptions/invalid_class.php',
-    'ezcTreeInvalidIdException'       => 'Tree/exceptions/invalid_id.php',
-    'ezcTree'                         => 'Tree/tree.php',
-    'ezcTreeDataStore'                => 'Tree/interfaces/data_store.php',
-    'ezcTreeDb'                       => 'Tree/backends/db.php',
-    'ezcTreeDbDataStore'              => 'Tree/stores/db.php',
-    'ezcTreeXmlDataStore'             => 'Tree/stores/xml.php',
-    'ezcTreeDbExternalTableDataStore' => 'Tree/stores/db_external.php',
-    'ezcTreeDbParentChild'            => 'Tree/backends/db_parent_child.php',
-    'ezcTreeMemory'                   => 'Tree/backends/memory.php',
-    'ezcTreeMemoryDataStore'          => 'Tree/stores/memory.php',
-    'ezcTreeMemoryNode'               => 'Tree/structs/memory_node.php',
-    'ezcTreeNode'                     => 'Tree/tree_node.php',
-    'ezcTreeNodeList'                 => 'Tree/tree_node_list.php',
-    'ezcTreeNodeListIterator'         => 'Tree/tree_node_list_iterator.php',
-    'ezcTreeTransactionItem'          => 'Tree/structs/transaction_item.php',
-    'ezcTreeXml'                      => 'Tree/backends/xml.php',
-    'ezcTreeXmlInternalDataStore'     => 'Tree/stores/xml_internal.php',
+    'ezcTreeException'                          => 
'Tree/exceptions/exception.php',
+    'ezcTreeIdsDoNotMatchException'             => 
'Tree/exceptions/ids_do_not_match.php',
+    'ezcTreeInvalidClassException'              => 
'Tree/exceptions/invalid_class.php',
+    'ezcTreeInvalidIdException'                 => 
'Tree/exceptions/invalid_id.php',
+    'ezcTreeTransactionAlreadyStartedException' => 
'Tree/exceptions/transaction_already_started.php',
+    'ezcTreeTransactionNotStartedException'     => 
'Tree/exceptions/transaction_not_started.php',
+    'ezcTree'                                   => 'Tree/tree.php',
+    'ezcTreeDataStore'                          => 
'Tree/interfaces/data_store.php',
+    'ezcTreeDb'                                 => 'Tree/backends/db.php',
+    'ezcTreeDbDataStore'                        => 'Tree/stores/db.php',
+    'ezcTreeXmlDataStore'                       => 'Tree/stores/xml.php',
+    'ezcTreeDbExternalTableDataStore'           => 
'Tree/stores/db_external.php',
+    'ezcTreeDbParentChild'                      => 
'Tree/backends/db_parent_child.php',
+    'ezcTreeMemory'                             => 'Tree/backends/memory.php',
+    'ezcTreeMemoryDataStore'                    => 'Tree/stores/memory.php',
+    'ezcTreeMemoryNode'                         => 
'Tree/structs/memory_node.php',
+    'ezcTreeNode'                               => 'Tree/tree_node.php',
+    'ezcTreeNodeList'                           => 'Tree/tree_node_list.php',
+    'ezcTreeNodeListIterator'                   => 
'Tree/tree_node_list_iterator.php',
+    'ezcTreeTransactionItem'                    => 
'Tree/structs/transaction_item.php',
+    'ezcTreeXml'                                => 'Tree/backends/xml.php',
+    'ezcTreeXmlInternalDataStore'               => 
'Tree/stores/xml_internal.php',
 );
 ?>

Modified: trunk/Tree/tests/tree.php
==============================================================================
--- trunk/Tree/tests/tree.php [iso-8859-1] (original)
+++ trunk/Tree/tests/tree.php [iso-8859-1] Tue Jul 31 11:03:20 2007
@@ -556,6 +556,52 @@
         self::assertSame( 8, $tree->getChildCountRecursive( '1' ) );
     }
 
+    public function testTreeTransactionDoubleStart()
+    {
+        $tree = $this->setUpTestTree();
+
+        $tree->beginTransaction();
+        try
+        {
+            $tree->beginTransaction();
+            self::fail( "Expected exception not thrown" );
+        }
+        catch ( ezcTreeTransactionAlreadyStartedException $e )
+        {
+            self::assertSame( "A transaction has already been started.", 
$e->getMessage() );
+        }
+    }
+
+    public function testTreeTransactionCommitWithoutBegin()
+    {
+        $tree = $this->setUpTestTree();
+
+        try
+        {
+            $tree->commit();
+            self::fail( "Expected exception not thrown" );
+        }
+        catch ( ezcTreeTransactionNotStartedException $e )
+        {
+            self::assertSame( "A transaction is not active.", $e->getMessage() 
);
+        }
+    }
+
+    public function testTreeTransactionRollbackWithoutBegin()
+    {
+        $tree = $this->setUpTestTree();
+
+        try
+        {
+            $tree->rollback();
+            self::fail( "Expected exception not thrown" );
+        }
+        catch ( ezcTreeTransactionNotStartedException $e )
+        {
+            self::assertSame( "A transaction is not active.", $e->getMessage() 
);
+        }
+    }
+
     public function testTreeNodeListIterator()
     {
         $tree = $this->setUpTestTree();


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

Reply via email to