eZ components PHP Cross Reference Developer Tools

Source: /TreeDatabaseTiein/tests/db_tree.php - 269 lines - 8584 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
   4   * @license http://ez.no/licenses/new_bsd New BSD License
   5   * @version 1.1.1
   6   * @filesource
   7   * @package TreeDatabaseTiein
   8   * @subpackage Tests
   9   */
  10  
  11  require_once  'Tree/tests/tree.php';
  12  
  13  /**
  14   * @package TreeDatabaseTiein
  15   * @subpackage Tests
  16   */
  17  abstract class ezcDbTreeTest extends ezcTreeTest
  18  {
  19      protected $dbh;
  20  
  21      protected function setUp()
  22      {
  23          try
  24          {
  25              $this->dbh = ezcDbInstance::get();
  26              $this->removeTables();
  27              $this->loadSchema();
  28              $this->insertData();
  29          }
  30          catch ( Exception $e )
  31          {
  32              $this->markTestSkipped( $e->getMessage() );
  33          }
  34      }
  35  
  36      private function loadSchema()
  37      {
  38          // create the parent_child table
  39          $schema = ezcDbSchema::createFromFile(
  40              'array',
  41              dirname( __FILE__ ) . '/files/' . $this->schemaName
  42          );
  43          $schema->writeToDb( $this->dbh );
  44      }
  45  
  46      protected function emptyTables()
  47      {
  48          $db = $this->dbh;
  49  
  50          foreach ( $this->tables as $table )
  51          {
  52              $q = $db->createDeleteQuery();
  53              $q->deleteFrom( $table );
  54              $s = $q->prepare();
  55              $s->execute();
  56          }
  57      }
  58  
  59      protected function removeTables()
  60      {
  61          try
  62          {
  63              foreach ( $this->tables as $table )
  64              {
  65                  $this->dbh->exec( "DROP TABLE $table" );
  66              }
  67          }
  68          catch ( Exception $e )
  69          {
  70              // ignore
  71          }
  72      }
  73  
  74      abstract protected function insertData();
  75      abstract protected function setUpEmptyTestTree();
  76      abstract protected function setUpTestTree();
  77  
  78      public function testCreateDbTree()
  79      {
  80          $tree = $this->setUpEmptyTestTree();
  81  
  82          self::assertSame( false, $tree->nodeExists( '1' ) );
  83          self::assertSame( false, $tree->nodeExists( '3' ) );
  84  
  85          $node = $tree->createNode( 1, "Root Node" );
  86          self::assertType( 'ezcTreeNode', $node );
  87          self::assertSame( '1', $node->id );
  88          $tree->setRootNode( $node );
  89          self::assertSame( true, $tree->nodeExists( '1' ) );
  90  
  91          $node2 = $tree->createNode( 2, "Node 2" );
  92          $node->addChild( $node2 );
  93          self::assertSame( true, $tree->nodeExists( '2' ) );
  94  
  95          $node->addChild( $node3 = $tree->createNode( 3, "Node 3" ) );
  96          $node3->addChild( $tree->createNode( 4, "Node 3.4" ) );
  97          self::assertSame( true, $tree->nodeExists( '3' ) );
  98          self::assertSame( true, $tree->nodeExists( '4' ) );
  99      }
 100  
 101      public function testCreateDbTreeStoreData()
 102      {
 103          $tree = $this->setUpEmptyTestTree();
 104  
 105          $root = $tree->createNode( 1, "Pantherinae" );
 106          $tree->setRootNode( $root );
 107  
 108          $root->addChild( $panthera = $tree->createNode( 2, "Panthera" ) );
 109          $root->addChild( $neofelis = $tree->createNode( 3, "Neofelis" ) );
 110          $root->addChild( $uncia = $tree->createNode( 4, "Uncia" ) );
 111  
 112          $panthera->addChild( $tree->createNode( 5, "Lion" ) );
 113          $panthera->addChild( $tree->createNode( 6, "Jaguar" ) );
 114          $panthera->addChild( $tree->createNode( 7, "Leopard" ) );
 115          $panthera->addChild( $tree->createNode( 8, "Tiger" ) );
 116  
 117          $neofelis->addChild( $tree->createNode( 9, "Clouded Leopard" ) );
 118          $neofelis->addChild( $tree->createNode( 10, "Bornean Clouded Leopard" ) );
 119  
 120          $uncia->addChild( $tree->createNode( 11, "Snow Leopard" ) );
 121  
 122          // start over
 123          $tree = $this->setUpTestTree();
 124  
 125          self::assertSame( true, $tree->nodeExists( '1' ) );
 126          self::assertSame( true, $tree->nodeExists( '2' ) );
 127          self::assertSame( true, $tree->nodeExists( '3' ) );
 128          self::assertSame( true, $tree->nodeExists( '4' ) );
 129          self::assertSame( "Snow Leopard", $tree->fetchNodeById( '11' )->data );
 130      }
 131  
 132      public function testCreateDbTreeStoreDataPrefetch()
 133      {
 134          $tree = $this->setUpEmptyTestTree();
 135  
 136          $root = $tree->createNode( 1, "Pantherinae" );
 137          $tree->setRootNode( $root );
 138  
 139          $root->addChild( $panthera = $tree->createNode( 2, "Panthera" ) );
 140          $root->addChild( $neofelis = $tree->createNode( 3, "Neofelis" ) );
 141          $root->addChild( $uncia = $tree->createNode( 4, "Uncia" ) );
 142  
 143          $panthera->addChild( $tree->createNode( 5, "Lion" ) );
 144          $panthera->addChild( $tree->createNode( 6, "Jaguar" ) );
 145          $panthera->addChild( $tree->createNode( 7, "Leopard" ) );
 146          $panthera->addChild( $tree->createNode( 8, "Tiger" ) );
 147  
 148          $neofelis->addChild( $tree->createNode( 9, "Clouded Leopard" ) );
 149          $neofelis->addChild( $tree->createNode( 10, "Bornean Clouded Leopard" ) );
 150  
 151          $uncia->addChild( $tree->createNode( 11, "Snow Leopard" ) );
 152  
 153          // start over
 154          $tree = $this->setUpTestTree();
 155  
 156          $nodeList = $tree->fetchSubtree( '3' );
 157  
 158          $expected = "something's wrong";
 159          foreach ( new ezcTreeNodeListIterator( $tree, $nodeList, true ) as $id => $data )
 160          {
 161              switch ( $id )
 162              {
 163                  case 3:
 164                      $expected = "Neofelis";
 165                      break;
 166                  case 9:
 167                      $expected = "Clouded Leopard";
 168                      break;
 169                  case 10:
 170                      $expected = "Bornean Clouded Leopard";
 171                      break;
 172              }
 173              self::assertSame( $expected, $data );
 174          }
 175      }
 176  
 177      public function testStoreUpdatedData()
 178      {
 179          $tree = $this->setUpEmptyTestTree();
 180  
 181          $root = $tree->createNode( 1, "Camelinae" );
 182          $tree->setRootNode( $root );
 183  
 184          $root->addChild( $tree->createNode( 2, "Lama" ) );
 185          $root->addChild( $tree->createNode( 3, "Vicugna" ) );
 186          $root->addChild( $tree->createNode( 4, "Camelus" ) );
 187  
 188          // start over
 189          $tree = $this->setUpTestTree();
 190  
 191          $camelus = $tree->fetchNodeById( 4 );
 192          self::assertSame( "Camelus", $camelus->data );
 193          $camelus->data = "Something Wrong";
 194          $camelus->data = "Camels";
 195  
 196          // start over
 197          $tree = $this->setUpTestTree();
 198  
 199          $camelus = $tree->fetchNodeById( 4 );
 200          self::assertSame( "Camels", $camelus->data );
 201      }
 202  
 203      public function testCreateDbTreeWithTransaction()
 204      {
 205          $tree = $this->setUpEmptyTestTree();
 206  
 207          $tree->setRootNode( $node = $tree->createNode( 1, "Root Node" ) );
 208          self::assertSame( true, $tree->nodeExists( '1' ) );
 209  
 210          $tree->beginTransaction();
 211          $node->addChild( $tree->createNode( 2, "Node 2" ) );
 212          $node->addChild( $node3 = $tree->createNode( 3, "Node 3" ) );
 213          $node3->addChild( $tree->createNode( 4, "Node 3.4" ) );
 214  
 215          self::assertSame( false, $tree->nodeExists( '3' ) );
 216          
 217          $tree->commit();
 218          
 219          self::assertSame( true, $tree->nodeExists( '3' ) );
 220      }
 221  
 222      public function testMultipleDataFields()
 223      {
 224          $tree = $this->setUpTestTree( 'data', null );
 225  
 226          $node8 = $tree->fetchNodeById( 8 ); // returns 8
 227          self::assertType( 'ezcTreeNode', $node8 );
 228          self::assertSame( '8', $node8->id );
 229          self::assertSame( array( 'data' => 'Node 8' ), $node8->data );
 230      }
 231  
 232      public function testStoreUpdatedDataMultipleDataFields()
 233      {
 234          $tree = $this->setUpEmptyTestTree( 'datam', null );
 235  
 236          $root = $tree->createNode( 1, array( 'name' => 'Harald V', 'born' => '1937' ) );
 237          $tree->setRootNode( $root );
 238  
 239          $root->addChild( $tree->createNode( 2, array( 'name' => 'Haakon', 'born' => '1973' ) ) );
 240          $root->addChild( $tree->createNode( 3, array( 'name' => 'Märtha Louise', 'born' => '1971' ) ) );
 241  
 242          // start over
 243          $tree = $this->setUpTestTree( 'datam', null );
 244  
 245          $haakon = $tree->fetchNodeById( 2 );
 246          self::assertEquals( array( 'name' => 'Haakon', 'born' => '1973' ), $haakon->data );
 247          $haakon->data = array( 'name' => 'Haakon', 'born' => 1981 );
 248          $haakon->data = array( 'name' => 'Haakon', 'born' => 1983 );
 249  
 250          // start over
 251          $tree = $this->setUpTestTree( 'datam', null );
 252  
 253          $haakon = $tree->fetchNodeById( 2 );
 254          self::assertEquals( array( 'name' => 'Haakon', 'born' => '1983' ), $haakon->data );
 255      }
 256  
 257      public function testNodeListIteratorEmptyList()
 258      {
 259          $tree = $this->setUpEmptyTestTree();
 260          $list = new ezcTreeNodeList;
 261  
 262          foreach ( new ezcTreeNodeListIterator( $tree, $list, true ) as $key => $node )
 263          {
 264              self::fail( "The list is not empty." );
 265          }
 266      }
 267  }
 268  
 269  ?>

title

Description

title

Description

title

Description

title

title

Body