Author: dr
Date: Thu Aug  2 14:56:49 2007
New Revision: 5808

Log:
- Reorganize testing code, so that it's easier to add tests for multiple db
  backends.

Modified:
    trunk/Tree/tests/db_parent_child_tree.php
    trunk/Tree/tests/db_tree.php

Modified: trunk/Tree/tests/db_parent_child_tree.php
==============================================================================
--- trunk/Tree/tests/db_parent_child_tree.php [iso-8859-1] (original)
+++ trunk/Tree/tests/db_parent_child_tree.php [iso-8859-1] Thu Aug  2 14:56:49 
2007
@@ -19,19 +19,40 @@
 {
     private $tempDir;
 
-    protected function setUp()
+    protected $tables  = array( 'parent_child', 'data', 'datam' );
+    protected $schemaName = 'parent_child.dba';
+
+    public function insertData()
     {
-        parent::setUp();
+        // insert test data
+        $data = array(
+            // child -> parent
+            1 => 'null',
+            2 => 1,
+            3 => 1,
+            4 => 1,
+            6 => 4,
+            7 => 6,
+            8 => 6,
+            5 => 1,
+            9 => 5
+        );
+        foreach( $data as $childId => $parentId )
+        {
+            $this->dbh->exec( "INSERT INTO parent_child(id, parent_id) VALUES( 
$childId, $parentId )" );
+        }
+
+        // add data
+        for ( $i = 1; $i <= 8; $i++ )
+        {
+            $this->dbh->exec( "INSERT INTO data(id, data) values ( $i, 'Node 
$i' )" );
+        }
     }
 
-    protected function tearDown()
+    protected function setUpEmptyTestTree( $dataTable = 'data', $dataField = 
'data' )
     {
-    }
-
-    protected function setUpEmptyTestTree()
-    {
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 
'id', 'data' );
-        $tree = new ezcTreeDbParentChild(
+        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, $dataTable, 
'id', $dataField );
+        $tree = ezcTreeDbParentChild::create(
             $this->dbh,
             'parent_child',
             $store
@@ -40,9 +61,9 @@
         return $tree;
     }
 
-    protected function setUpTestTree()
+    protected function setUpTestTree( $dataTable = 'data', $dataField = 'data' 
)
     {
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 
'id', 'data' );
+        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, $dataTable, 
'id', $dataField );
         $tree = new ezcTreeDbParentChild(
             $this->dbh,
             'parent_child',
@@ -51,228 +72,10 @@
         return $tree;
     }
 
-    protected function emptyTables()
-    {
-        $db = $this->dbh;
-
-        $q = $db->createDeleteQuery();
-        $q->deleteFrom( 'parent_child' );
-        $s = $q->prepare();
-        $s->execute();
-
-        $q = $db->createDeleteQuery();
-        $q->deleteFrom( 'data' );
-        $s = $q->prepare();
-        $s->execute();
-    }
-
-    public function testCreateDbTree()
-    {
-        $this->emptyTables();
-
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 
'id', 'data' );
-        $tree = ezcTreeDbParentChild::create(
-            $this->dbh,
-            'parent_child',
-            $store
-        );
-        self::assertSame( false, $tree->nodeExists( '1' ) );
-        self::assertSame( false, $tree->nodeExists( '3' ) );
-
-        $node = $tree->createNode( 1, "Root Node" );
-        self::assertType( 'ezcTreeNode', $node );
-        self::assertSame( '1', $node->id );
-        $tree->setRootNode( $node );
-        self::assertSame( true, $tree->nodeExists( '1' ) );
-
-        $node2 = $tree->createNode( 2, "Node 2" );
-        $node->addChild( $node2 );
-        self::assertSame( true, $tree->nodeExists( '2' ) );
-
-        $node->addChild( $node3 = $tree->createNode( 3, "Node 3" ) );
-        $node3->addChild( $tree->createNode( 4, "Node 3.4" ) );
-        self::assertSame( true, $tree->nodeExists( '3' ) );
-        self::assertSame( true, $tree->nodeExists( '4' ) );
-    }
-
-    public function testCreateDbTreeStoreData()
-    {
-        $this->emptyTables();
-
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 
'id', 'data' );
-        $tree = ezcTreeDbParentChild::create(
-            $this->dbh,
-            'parent_child',
-            $store
-        );
-
-        $root = $tree->createNode( 1, "Pantherinae" );
-        $tree->setRootNode( $root );
-
-        $root->addChild( $panthera = $tree->createNode( 2, "Panthera" ) );
-        $root->addChild( $neofelis = $tree->createNode( 3, "Neofelis" ) );
-        $root->addChild( $uncia = $tree->createNode( 4, "Uncia" ) );
-
-        $panthera->addChild( $tree->createNode( 5, "Lion" ) );
-        $panthera->addChild( $tree->createNode( 6, "Jaguar" ) );
-        $panthera->addChild( $tree->createNode( 7, "Leopard" ) );
-        $panthera->addChild( $tree->createNode( 8, "Tiger" ) );
-
-        $neofelis->addChild( $tree->createNode( 9, "Clouded Leopard" ) );
-        $neofelis->addChild( $tree->createNode( 10, "Bornean Clouded Leopard" 
) );
-
-        $uncia->addChild( $tree->createNode( 11, "Snow Leopard" ) );
-
-        // start over
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 
'id', 'data' );
-        $tree = new ezcTreeDbParentChild(
-            $this->dbh,
-            'parent_child',
-            $store
-        );
-
-        self::assertSame( true, $tree->nodeExists( '1' ) );
-        self::assertSame( true, $tree->nodeExists( '2' ) );
-        self::assertSame( true, $tree->nodeExists( '3' ) );
-        self::assertSame( true, $tree->nodeExists( '4' ) );
-        self::assertSame( "Snow Leopard", $tree->fetchNodeById( '11' )->data );
-    }
-
-    public function testCreateDbTreeStoreDataPrefetch()
-    {
-        $this->emptyTables();
-
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 
'id', 'data' );
-        $tree = ezcTreeDbParentChild::create(
-            $this->dbh,
-            'parent_child',
-            $store
-        );
-
-        $root = $tree->createNode( 1, "Pantherinae" );
-        $tree->setRootNode( $root );
-
-        $root->addChild( $panthera = $tree->createNode( 2, "Panthera" ) );
-        $root->addChild( $neofelis = $tree->createNode( 3, "Neofelis" ) );
-        $root->addChild( $uncia = $tree->createNode( 4, "Uncia" ) );
-
-        $panthera->addChild( $tree->createNode( 5, "Lion" ) );
-        $panthera->addChild( $tree->createNode( 6, "Jaguar" ) );
-        $panthera->addChild( $tree->createNode( 7, "Leopard" ) );
-        $panthera->addChild( $tree->createNode( 8, "Tiger" ) );
-
-        $neofelis->addChild( $tree->createNode( 9, "Clouded Leopard" ) );
-        $neofelis->addChild( $tree->createNode( 10, "Bornean Clouded Leopard" 
) );
-
-        $uncia->addChild( $tree->createNode( 11, "Snow Leopard" ) );
-
-        // start over
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 
'id', 'data' );
-        $tree = new ezcTreeDbParentChild(
-            $this->dbh,
-            'parent_child',
-            $store
-        );
-
-        $nodeList = $tree->fetchSubtree( '3' );
-
-        $tree->prefetch = true;
-        $expected = "something's wrong";
-        foreach ( new ezcTreeNodeListIterator( $tree, $nodeList ) as $id => 
$data )
-        {
-            switch ( $id )
-            {
-                case 3:
-                    $expected = "Neofelis";
-                    break;
-                case 9:
-                    $expected = "Clouded Leopard";
-                    break;
-                case 10:
-                    $expected = "Bornean Clouded Leopard";
-                    break;
-            }
-            self::assertSame( $expected, $data );
-        }
-    }
-
-    public function testStoreUpdatedData()
-    {
-        $this->emptyTables();
-
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 
'id', 'data' );
-        $tree = ezcTreeDbParentChild::create(
-            $this->dbh,
-            'parent_child',
-            $store
-        );
-
-        $root = $tree->createNode( 1, "Camelinae" );
-        $tree->setRootNode( $root );
-
-        $root->addChild( $tree->createNode( 2, "Lama" ) );
-        $root->addChild( $tree->createNode( 3, "Vicugna" ) );
-        $root->addChild( $tree->createNode( 4, "Camelus" ) );
-
-        // start over
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 
'id', 'data' );
-        $tree = new ezcTreeDbParentChild(
-            $this->dbh,
-            'parent_child',
-            $store
-        );
-
-        $camelus = $tree->fetchNodeById( 4 );
-        self::assertSame( "Camelus", $camelus->data );
-        $camelus->data = "Something Wrong";
-        $camelus->data = "Camels";
-
-        // start over
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 
'id', 'data' );
-        $tree = new ezcTreeDbParentChild(
-            $this->dbh,
-            'parent_child',
-            $store
-        );
-
-        $camelus = $tree->fetchNodeById( 4 );
-        self::assertSame( "Camels", $camelus->data );
-    }
-
-    public function testCreateDbTreeWithTransaction()
-    {
-        $this->emptyTables();
-
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 
'id', 'data' );
-        $tree = ezcTreeDbParentChild::create(
-            $this->dbh,
-            'parent_child',
-            $store
-        );
-
-        $tree->setRootNode( $node = $tree->createNode( 1, "Root Node" ) );
-        self::assertSame( true, $tree->nodeExists( '1' ) );
-
-        $tree->beginTransaction();
-        $node->addChild( $tree->createNode( 2, "Node 2" ) );
-        $node->addChild( $node3 = $tree->createNode( 3, "Node 3" ) );
-        $node3->addChild( $tree->createNode( 4, "Node 3.4" ) );
-
-        self::assertSame( false, $tree->nodeExists( '3' ) );
-        
-        $tree->commit();
-        
-        self::assertSame( true, $tree->nodeExists( '3' ) );
-    }
-
     public function testMultipleDataFields()
     {
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'data', 'id' 
);
-        $tree = new ezcTreeDbParentChild(
-            $this->dbh,
-            'parent_child',
-            $store
-        );
+        $tree = $this->setUpTestTree( 'data', null );
+
         $node8 = $tree->fetchNodeById( 8 ); // returns 8
         self::assertType( 'ezcTreeNode', $node8 );
         self::assertSame( '8', $node8->id );
@@ -281,14 +84,7 @@
 
     public function testStoreUpdatedDataMultipleDataFields()
     {
-        $this->emptyTables();
-
-        $store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'datam', 
'id' );
-        $tree = ezcTreeDbParentChild::create(
-            $this->dbh,
-            'parent_child',
-            $store
-        );
+        $tree = $this->setUpEmptyTestTree( 'datam', null );
 
         $root = $tree->createNode( 1, array( 'name' => 'Harald V', 'born' => 
'1937' ) );
         $tree->setRootNode( $root );

Modified: trunk/Tree/tests/db_tree.php
==============================================================================
--- trunk/Tree/tests/db_tree.php [iso-8859-1] (original)
+++ trunk/Tree/tests/db_tree.php [iso-8859-1] Thu Aug  2 14:56:49 2007
@@ -14,71 +14,210 @@
  * @package Tree
  * @subpackage Tests
  */
-class ezcDbTreeTest extends ezcTreeTest
+abstract class ezcDbTreeTest extends ezcTreeTest
 {
-    private $tempDir;
+    protected $dbh;
 
     protected function setUp()
     {
         try
         {
             $this->dbh = ezcDbInstance::get();
-            $this->cleanupTables( $this->dbh );
-
-            // create the parent_child table
-            $schema = ezcDbSchema::createFromFile(
-                'array',
-                dirname( __FILE__ ) . '/files/parent_child.dba'
-            );
-            $schema->writeToDb( $this->dbh );
-
-            // insert test data
-            $data = array(
-                // child -> parent
-                1 => 'null',
-                2 => 1,
-                3 => 1,
-                4 => 1,
-                6 => 4,
-                7 => 6,
-                8 => 6,
-                5 => 1,
-                9 => 5
-            );
-            foreach( $data as $childId => $parentId )
+            $this->removeTables();
+            $this->loadSchema();
+            $this->insertData();
+        }
+        catch ( Exception $e )
+        {
+            $this->markTestSkipped( $e->getMessage() );
+        }
+    }
+
+    private function loadSchema()
+    {
+        // create the parent_child table
+        $schema = ezcDbSchema::createFromFile(
+            'array',
+            dirname( __FILE__ ) . '/files/' . $this->schemaName
+        );
+        $schema->writeToDb( $this->dbh );
+    }
+
+    protected function emptyTables()
+    {
+        $db = $this->dbh;
+
+        foreach ( $this->tables as $table )
+        {
+            $q = $db->createDeleteQuery();
+            $q->deleteFrom( $table );
+            $s = $q->prepare();
+            $s->execute();
+        }
+    }
+
+    protected function removeTables()
+    {
+        try
+        {
+            foreach ( $this->tables as $table )
             {
-                $this->dbh->exec( "INSERT INTO parent_child(id, parent_id) 
VALUES( $childId, $parentId )" );
+                $this->dbh->exec( "DROP TABLE $table" );
             }
-
-            // add data
-            for ( $i = 1; $i <= 8; $i++ )
+        }
+        catch ( Exception $e )
+        {
+            // ignore
+        }
+    }
+
+    abstract protected function insertData();
+    abstract protected function setUpEmptyTestTree();
+    abstract protected function setUpTestTree();
+
+    public function testCreateDbTree()
+    {
+        $tree = $this->setUpEmptyTestTree();
+
+        self::assertSame( false, $tree->nodeExists( '1' ) );
+        self::assertSame( false, $tree->nodeExists( '3' ) );
+
+        $node = $tree->createNode( 1, "Root Node" );
+        self::assertType( 'ezcTreeNode', $node );
+        self::assertSame( '1', $node->id );
+        $tree->setRootNode( $node );
+        self::assertSame( true, $tree->nodeExists( '1' ) );
+
+        $node2 = $tree->createNode( 2, "Node 2" );
+        $node->addChild( $node2 );
+        self::assertSame( true, $tree->nodeExists( '2' ) );
+
+        $node->addChild( $node3 = $tree->createNode( 3, "Node 3" ) );
+        $node3->addChild( $tree->createNode( 4, "Node 3.4" ) );
+        self::assertSame( true, $tree->nodeExists( '3' ) );
+        self::assertSame( true, $tree->nodeExists( '4' ) );
+    }
+
+    public function testCreateDbTreeStoreData()
+    {
+        $tree = $this->setUpEmptyTestTree();
+
+        $root = $tree->createNode( 1, "Pantherinae" );
+        $tree->setRootNode( $root );
+
+        $root->addChild( $panthera = $tree->createNode( 2, "Panthera" ) );
+        $root->addChild( $neofelis = $tree->createNode( 3, "Neofelis" ) );
+        $root->addChild( $uncia = $tree->createNode( 4, "Uncia" ) );
+
+        $panthera->addChild( $tree->createNode( 5, "Lion" ) );
+        $panthera->addChild( $tree->createNode( 6, "Jaguar" ) );
+        $panthera->addChild( $tree->createNode( 7, "Leopard" ) );
+        $panthera->addChild( $tree->createNode( 8, "Tiger" ) );
+
+        $neofelis->addChild( $tree->createNode( 9, "Clouded Leopard" ) );
+        $neofelis->addChild( $tree->createNode( 10, "Bornean Clouded Leopard" 
) );
+
+        $uncia->addChild( $tree->createNode( 11, "Snow Leopard" ) );
+
+        // start over
+        $tree = $this->setUpTestTree();
+
+        self::assertSame( true, $tree->nodeExists( '1' ) );
+        self::assertSame( true, $tree->nodeExists( '2' ) );
+        self::assertSame( true, $tree->nodeExists( '3' ) );
+        self::assertSame( true, $tree->nodeExists( '4' ) );
+        self::assertSame( "Snow Leopard", $tree->fetchNodeById( '11' )->data );
+    }
+
+    public function testCreateDbTreeStoreDataPrefetch()
+    {
+        $tree = $this->setUpEmptyTestTree();
+
+        $root = $tree->createNode( 1, "Pantherinae" );
+        $tree->setRootNode( $root );
+
+        $root->addChild( $panthera = $tree->createNode( 2, "Panthera" ) );
+        $root->addChild( $neofelis = $tree->createNode( 3, "Neofelis" ) );
+        $root->addChild( $uncia = $tree->createNode( 4, "Uncia" ) );
+
+        $panthera->addChild( $tree->createNode( 5, "Lion" ) );
+        $panthera->addChild( $tree->createNode( 6, "Jaguar" ) );
+        $panthera->addChild( $tree->createNode( 7, "Leopard" ) );
+        $panthera->addChild( $tree->createNode( 8, "Tiger" ) );
+
+        $neofelis->addChild( $tree->createNode( 9, "Clouded Leopard" ) );
+        $neofelis->addChild( $tree->createNode( 10, "Bornean Clouded Leopard" 
) );
+
+        $uncia->addChild( $tree->createNode( 11, "Snow Leopard" ) );
+
+        // start over
+        $tree = $this->setUpTestTree();
+
+        $nodeList = $tree->fetchSubtree( '3' );
+
+        $tree->prefetch = true;
+        $expected = "something's wrong";
+        foreach ( new ezcTreeNodeListIterator( $tree, $nodeList ) as $id => 
$data )
+        {
+            switch ( $id )
             {
-                $this->dbh->exec( "INSERT INTO data(id, data) values ( $i, 
'Node $i' )" );
+                case 3:
+                    $expected = "Neofelis";
+                    break;
+                case 9:
+                    $expected = "Clouded Leopard";
+                    break;
+                case 10:
+                    $expected = "Bornean Clouded Leopard";
+                    break;
             }
-        }
-        catch ( Exception $e )
-        {
-            $this->markTestSkipped( $e->getMessage() );
-        }
-
-    }
-
-    protected function cleanupTables()
-    {
-        try
-        {
-            $this->dbh->exec( 'DROP TABLE parent_child' );
-            $this->dbh->exec( 'DROP TABLE data' );
-            $this->dbh->exec( 'DROP TABLE datam' );
-        }
-        catch ( Exception $e )
-        {
-            // ignore
-        }
-    }
-
-    protected function tearDown()
-    {
+            self::assertSame( $expected, $data );
+        }
+    }
+
+    public function testStoreUpdatedData()
+    {
+        $tree = $this->setUpEmptyTestTree();
+
+        $root = $tree->createNode( 1, "Camelinae" );
+        $tree->setRootNode( $root );
+
+        $root->addChild( $tree->createNode( 2, "Lama" ) );
+        $root->addChild( $tree->createNode( 3, "Vicugna" ) );
+        $root->addChild( $tree->createNode( 4, "Camelus" ) );
+
+        // start over
+        $tree = $this->setUpTestTree();
+
+        $camelus = $tree->fetchNodeById( 4 );
+        self::assertSame( "Camelus", $camelus->data );
+        $camelus->data = "Something Wrong";
+        $camelus->data = "Camels";
+
+        // start over
+        $tree = $this->setUpTestTree();
+
+        $camelus = $tree->fetchNodeById( 4 );
+        self::assertSame( "Camels", $camelus->data );
+    }
+
+    public function testCreateDbTreeWithTransaction()
+    {
+        $tree = $this->setUpEmptyTestTree();
+
+        $tree->setRootNode( $node = $tree->createNode( 1, "Root Node" ) );
+        self::assertSame( true, $tree->nodeExists( '1' ) );
+
+        $tree->beginTransaction();
+        $node->addChild( $tree->createNode( 2, "Node 2" ) );
+        $node->addChild( $node3 = $tree->createNode( 3, "Node 3" ) );
+        $node3->addChild( $tree->createNode( 4, "Node 3.4" ) );
+
+        self::assertSame( false, $tree->nodeExists( '3' ) );
+        
+        $tree->commit();
+        
+        self::assertSame( true, $tree->nodeExists( '3' ) );
     }
 }
 


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

Reply via email to