Author: Sebastian Bergmann
Date: 2007-05-04 07:50:20 +0200 (Fri, 04 May 2007)
New Revision: 5034

Log:
- Refactor ezcWorkflowNode::__construct().
- Do not verify node constraints in {add|remove}{In|Out}Node().
- Let remove{In|Out}Node() return true on success and false on failure.
- Add tests.

Modified:
   trunk/Workflow/src/interfaces/node.php
   trunk/Workflow/src/nodes/action.php
   trunk/Workflow/src/nodes/variables/input.php
   trunk/Workflow/src/nodes/variables/set.php
   trunk/Workflow/src/nodes/variables/unset.php
   trunk/Workflow/tests/node_test.php

Modified: trunk/Workflow/src/interfaces/node.php
===================================================================
--- trunk/Workflow/src/interfaces/node.php      2007-05-04 04:39:31 UTC (rev 
5033)
+++ trunk/Workflow/src/interfaces/node.php      2007-05-04 05:50:20 UTC (rev 
5034)
@@ -149,24 +149,14 @@
     /**
      * Constructor.
      *
-     * @param mixed   $configuration
-     * @param integer $activationState
-     * @param mixed   $state
+     * @param mixed $configuration
      */
-    public function __construct( $configuration = '', $activationState = 
self::WAITING_FOR_ACTIVATION, $state = null )
+    public function __construct( $configuration = '' )
     {
         $this->configuration = $configuration;
 
-        if ( $state === null )
-        {
-            $this->initState();
-        }
-        else
-        {
-            $this->state = $state;
-        }
-
-        $this->setActivationState( $activationState );
+        $this->setActivationState( self::WAITING_FOR_ACTIVATION );
+        $this->initState();
     }
 
     /**
@@ -179,38 +169,27 @@
     public function addInNode( ezcWorkflowNode $node )
     {
         // Check whether the node is already an incoming node of this node.
-        if ( ezcWorkflowUtil::findObject( $this->inNodes, $node ) !== false )
+        if ( ezcWorkflowUtil::findObject( $this->inNodes, $node ) === false )
         {
-            return $this;
-        }
+            // Add the other node to the workflow.
+            $this->addNodeToWorkflow( $node );
 
-        // Check whether adding the other node to the incoming nodes
-        // of this node would violate this node's constraints.
-        if ( $this->maxInNodes !== FALSE && $this->numInNodes + 1 > 
$this->maxInNodes )
-        {
-            throw new ezcWorkflowInvalidDefinitionException(
-              'Adding an incoming node to this node would violate its 
constraints.'
-            );
-        }
+            // Add this node as an outgoing node to the other node.
+            if ( !self::$internalCall )
+            {
+                self::$internalCall = true;
+                $node->addOutNode( $this );
+            }
+            else
+            {
+                self::$internalCall = false;
+            }
 
-        // Add the other node to the workflow.
-        $this->addNodeToWorkflow( $node );
-
-        // Add this node as an outgoing node to the other node.
-        if ( !self::$internalCall )
-        {
-            self::$internalCall = true;
-            $node->addOutNode( $this );
+            // Add the other node as an incoming node to this node.
+            $this->inNodes[] = $node;
+            $this->numInNodes++;
         }
-        else
-        {
-            self::$internalCall = false;
-        }
 
-        // Add the other node as an incoming node to this node.
-        $this->inNodes[] = $node;
-        $this->numInNodes++;
-
         return $this;
     }
 
@@ -219,39 +198,32 @@
      *
      * @param  ezcWorkflowNode $node The node that is to be removed as 
incoming node.
      * @throws ezcWorkflowInvalidDefinitionException if the operation violates 
the constraints of the nodes involved.
-     * @return ezcWorkflowNode
+     * @return boolean
      */
     public function removeInNode( ezcWorkflowNode $node )
     {
         $index = ezcWorkflowUtil::findObject( $this->inNodes, $node );
 
-        if ( $index === false )
+        if ( $index !== false )
         {
-            return $this;
-        }
+            // Remove this node as an outgoing node from the other node.
+            if ( !self::$internalCall )
+            {
+                self::$internalCall = true;
+                $node->removeOutNode( $this );
+            }
+            else
+            {
+                self::$internalCall = false;
+            }
 
-        if ( $this->minInNodes !== FALSE && $this->numInNodes - 1 < 
$this->minInNodes )
-        {
-            throw new ezcWorkflowInvalidDefinitionException(
-              'Removing an incoming node from this node would violate its 
constraints.'
-            );
-        }
+            unset( $this->inNodes[$index] );
+            $this->numInNodes--;
 
-        // Remove this node as an outgoing node from the other node.
-        if ( !self::$internalCall )
-        {
-            self::$internalCall = true;
-            $node->removeOutNode( $this );
+            return true;
         }
-        else
-        {
-            self::$internalCall = false;
-        }
 
-        unset( $this->inNodes[$index] );
-        $this->numInNodes--;
-
-        return $this;
+        return false;
     }
 
     /**
@@ -264,38 +236,27 @@
     public function addOutNode( ezcWorkflowNode $node )
     {
         // Check whether the other node is already an outgoing node of this 
node.
-        if ( ezcWorkflowUtil::findObject( $this->outNodes, $node ) !== false )
+        if ( ezcWorkflowUtil::findObject( $this->outNodes, $node ) === false )
         {
-            return $this;
-        }
+            // Add the other node to the workflow.
+            $this->addNodeToWorkflow( $node );
 
-        // Check whether adding the other node to the outgoing nodes
-        // of this node would violate this node's constraints.
-        if ( $this->maxOutNodes !== FALSE && $this->numOutNodes + 1 > 
$this->maxOutNodes )
-        {
-            throw new ezcWorkflowInvalidDefinitionException(
-              'Adding an outgoing node to this node would violate its 
constraints.'
-            );
-        }
+            // Add this node as an incoming node to the other node.
+            if ( !self::$internalCall )
+            {
+                self::$internalCall = true;
+                $node->addInNode( $this );
+            }
+            else
+            {
+                self::$internalCall = false;
+            }
 
-        // Add the other node to the workflow.
-        $this->addNodeToWorkflow( $node );
-
-        // Add this node as an incoming node to the other node.
-        if ( !self::$internalCall )
-        {
-            self::$internalCall = true;
-            $node->addInNode( $this );
+            // Add the other node as an outgoing node to this node.
+            $this->outNodes[] = $node;
+            $this->numOutNodes++;
         }
-        else
-        {
-            self::$internalCall = false;
-        }
 
-        // Add the other node as an outgoing node to this node.
-        $this->outNodes[] = $node;
-        $this->numOutNodes++;
-
         return $this;
     }
 
@@ -304,39 +265,32 @@
      *
      * @param  ezcWorkflowNode $node The node that is to be removed as 
outgoing node.
      * @throws ezcWorkflowInvalidDefinitionException if the operation violates 
the constraints of the nodes involved.
-     * @return ezcWorkflowNode
+     * @return boolean
      */
     public function removeOutNode( ezcWorkflowNode $node )
     {
         $index = ezcWorkflowUtil::findObject( $this->outNodes, $node );
 
-        if ( $index === false )
+        if ( $index !== false )
         {
-            return $this;
-        }
+            // Remove this node as an incoming node from the other node.
+            if ( !self::$internalCall )
+            {
+                self::$internalCall = true;
+                $node->removeInNode( $this );
+            }
+            else
+            {
+                self::$internalCall = false;
+            }
 
-        if ( $this->minOutNodes !== FALSE && $this->numOutNodes - 1 < 
$this->minOutNodes )
-        {
-            throw new ezcWorkflowInvalidDefinitionException(
-              'Removing an outgoing node from this node would violate its 
constraints.'
-            );
-        }
+            unset( $this->outNodes[$index] );
+            $this->numOutNodes--;
 
-        // Remove this node as an incoming node from the other node.
-        if ( !self::$internalCall )
-        {
-            self::$internalCall = true;
-            $node->removeInNode( $this );
+            return true;
         }
-        else
-        {
-            self::$internalCall = false;
-        }
 
-        unset( $this->outNodes[$index] );
-        $this->numOutNodes--;
-
-        return $this;
+        return false;
     }
 
     /**
@@ -540,7 +494,7 @@
         if ( $this->activationState === self::WAITING_FOR_ACTIVATION )
         {
             $this->activationState = self::WAITING_FOR_EXECUTION;
-            $this->threadId = $threadId;
+            $this->setThreadId( $threadId );
 
             if ( $activatedFrom !== null )
             {

Modified: trunk/Workflow/src/nodes/action.php
===================================================================
--- trunk/Workflow/src/nodes/action.php 2007-05-04 04:39:31 UTC (rev 5033)
+++ trunk/Workflow/src/nodes/action.php 2007-05-04 05:50:20 UTC (rev 5034)
@@ -19,12 +19,10 @@
     /**
      * Constructor.
      *
-     * @param  mixed   $configuration
-     * @param  integer $activationState
-     * @param  mixed   $state
+     * @param mixed $configuration
      * @throws ezcWorkflowDefinitionException
      */
-    public function __construct( $configuration, $activationState = 
self::WAITING_FOR_ACTIVATION, $state = null )
+    public function __construct( $configuration )
     {
         if ( is_string( $configuration ) )
         {
@@ -36,7 +34,7 @@
             $configuration['arguments'] = array();
         }
 
-        parent::__construct( $configuration, $activationState, $state );
+        parent::__construct( $configuration );
     }
 
     /**

Modified: trunk/Workflow/src/nodes/variables/input.php
===================================================================
--- trunk/Workflow/src/nodes/variables/input.php        2007-05-04 04:39:31 UTC 
(rev 5033)
+++ trunk/Workflow/src/nodes/variables/input.php        2007-05-04 05:50:20 UTC 
(rev 5034)
@@ -19,12 +19,10 @@
     /**
      * Constructor.
      *
-     * @param mixed   $configuration
-     * @param integer $activationState
-     * @param mixed   $state
+     * @param mixed $configuration
      * @throws InvalidArgumentException
      */
-    public function __construct( $configuration = '', $activationState = 
self::WAITING_FOR_ACTIVATION, $state = null )
+    public function __construct( $configuration = '' )
     {
         if ( !is_array( $configuration ) )
         {
@@ -54,7 +52,7 @@
             $tmp[$variable] = $condition;
         }
 
-        parent::__construct( $tmp, $activationState, $state );
+        parent::__construct( $tmp );
     }
 
     /**

Modified: trunk/Workflow/src/nodes/variables/set.php
===================================================================
--- trunk/Workflow/src/nodes/variables/set.php  2007-05-04 04:39:31 UTC (rev 
5033)
+++ trunk/Workflow/src/nodes/variables/set.php  2007-05-04 05:50:20 UTC (rev 
5034)
@@ -19,19 +19,17 @@
     /**
      * Constructor.
      *
-     * @param mixed   $configuration
-     * @param integer $activationState
-     * @param mixed   $state
+     * @param mixed $configuration
      * @throws InvalidArgumentException
      */
-    public function __construct( $configuration = '', $activationState = 
self::WAITING_FOR_ACTIVATION, $state = null )
+    public function __construct( $configuration = '' )
     {
         if ( !is_array( $configuration ) )
         {
             throw new InvalidArgumentException;
         }
 
-        parent::__construct( $configuration, $activationState, $state );
+        parent::__construct( $configuration );
     }
 
     /**

Modified: trunk/Workflow/src/nodes/variables/unset.php
===================================================================
--- trunk/Workflow/src/nodes/variables/unset.php        2007-05-04 04:39:31 UTC 
(rev 5033)
+++ trunk/Workflow/src/nodes/variables/unset.php        2007-05-04 05:50:20 UTC 
(rev 5034)
@@ -19,12 +19,10 @@
     /**
      * Constructor.
      *
-     * @param mixed   $configuration
-     * @param integer $activationState
-     * @param mixed   $state
+     * @param mixed $configuration
      * @throws InvalidArgumentException
      */
-    public function __construct( $configuration = '', $activationState = 
self::WAITING_FOR_ACTIVATION, $state = null )
+    public function __construct( $configuration = '' )
     {
         if ( is_string( $configuration ) )
         {
@@ -36,7 +34,7 @@
             throw new InvalidArgumentException;
         }
 
-        parent::__construct( $configuration, $activationState, $state );
+        parent::__construct( $configuration );
     }
 
     /**

Modified: trunk/Workflow/tests/node_test.php
===================================================================
--- trunk/Workflow/tests/node_test.php  2007-05-04 04:39:31 UTC (rev 5033)
+++ trunk/Workflow/tests/node_test.php  2007-05-04 05:50:20 UTC (rev 5034)
@@ -52,6 +52,74 @@
         $this->fail();
     }
 
+    public function testVerifyTooManyIncomingNodes()
+    {
+        try
+        {
+            $a = new ezcWorkflowNodeVariableSet(
+              array( 'foo' => 'bar' )
+            );
+
+            $b = new ezcWorkflowNodeVariableSet(
+              array( 'foo' => 'bar' )
+            );
+
+            $c = new ezcWorkflowNodeVariableSet(
+              array( 'foo' => 'bar' )
+            );
+
+            $d = new ezcWorkflowNodeVariableSet(
+              array( 'foo' => 'bar' )
+            );
+
+            $c->addInNode( $a );
+            $c->addInNode( $b );
+            $c->addOutNode( $d );
+
+            $c->verify();
+        }
+        catch ( ezcWorkflowInvalidDefinitionException $e )
+        {
+            return;
+        }
+
+        $this->fail();
+    }
+
+    public function testVerifyTooManyOutgoingNodes()
+    {
+        try
+        {
+            $a = new ezcWorkflowNodeVariableSet(
+              array( 'foo' => 'bar' )
+            );
+
+            $b = new ezcWorkflowNodeVariableSet(
+              array( 'foo' => 'bar' )
+            );
+
+            $c = new ezcWorkflowNodeVariableSet(
+              array( 'foo' => 'bar' )
+            );
+
+            $d = new ezcWorkflowNodeVariableSet(
+              array( 'foo' => 'bar' )
+            );
+
+            $b->addOutNode( $c );
+            $b->addOutNode( $d );
+            $b->addInNode( $a );
+
+            $b->verify();
+        }
+        catch ( ezcWorkflowInvalidDefinitionException $e )
+        {
+            return;
+        }
+
+        $this->fail();
+    }
+
     public function testGetInNodes()
     {
         $this->setUpStartEnd();
@@ -84,34 +152,16 @@
     {
         $this->setUpStartEnd();
 
-        try
-        {
-            $this->endNode->removeInNode( $this->startNode );
-        }
-
-        catch ( ezcWorkflowInvalidDefinitionException $e )
-        {
-            return;
-        }
-
-        $this->fail();
+        $this->assertTrue( $this->endNode->removeInNode( $this->startNode ) );
+        $this->assertFalse( $this->endNode->removeInNode( $this->startNode ) );
     }
 
     public function testRemoveOutNode()
     {
         $this->setUpStartEnd();
 
-        try
-        {
-            $this->startNode->removeOutNode( $this->endNode );
-        }
-
-        catch ( ezcWorkflowInvalidDefinitionException $e )
-        {
-            return;
-        }
-
-        $this->fail();
+        $this->assertTrue( $this->startNode->removeOutNode( $this->endNode ) );
+        $this->assertFalse( $this->startNode->removeOutNode( $this->endNode ) 
);
     }
 
     public function testToString()

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

Reply via email to