eZ components PHP Cross Reference Developer Tools

Source: /TreeDatabaseTiein/tests/db_nested_set_tree.php - 172 lines - 5785 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  require_once  'db_tree.php';
  13  
  14  /**
  15   * @package TreeDatabaseTiein
  16   * @subpackage Tests
  17   */
  18  class ezcTreeDbNestedSetTest extends ezcDbTreeTest
  19  {
  20      private $tempDir;
  21  
  22      protected $tables  = array( 'nested_set', 'data', 'datam' );
  23      protected $schemaName = 'nested_set.dba';
  24  
  25      public function insertData()
  26      {
  27          // insert test data
  28          $data = array(
  29              // child -> parent
  30              1 => array( 'null',  1, 18 ),
  31              2 => array(      1,  2,  3 ),
  32              3 => array(      1,  4,  5 ),
  33              4 => array(      1,  6, 13 ),
  34              6 => array(      4,  7, 12 ),
  35              7 => array(      6,  8,  9 ),
  36              8 => array(      6, 10, 11 ),
  37              5 => array(      1, 14, 17 ),
  38              9 => array(      5, 15, 16 ),
  39          );
  40          foreach( $data as $childId => $details )
  41          {
  42              list( $parentId, $left, $right ) = $details;
  43              $this->dbh->exec( "INSERT INTO nested_set(id, parent_id, lft, rgt) VALUES( $childId, $parentId, $left, $right )" );
  44          }
  45  
  46          // add data
  47          for ( $i = 1; $i <= 8; $i++ )
  48          {
  49              $this->dbh->exec( "INSERT INTO data(id, data) values ( $i, 'Node $i' )" );
  50          }
  51      }
  52  
  53      protected function setUpEmptyTestTree( $dataTable = 'data', $dataField = 'data', $indexTableSuffix = '' )
  54      {
  55          $store = new ezcTreeDbExternalTableDataStore( $this->dbh, $dataTable, 'id', $dataField );
  56          $tree = ezcTreeDbNestedSet::create(
  57              $this->dbh,
  58              'nested_set' . $indexTableSuffix,
  59              $store
  60          );
  61          $this->emptyTables();
  62          return $tree;
  63      }
  64  
  65      protected function setUpTestTree( $dataTable = 'data', $dataField = 'data', $indexTableSuffix = '' )
  66      {
  67          $store = new ezcTreeDbExternalTableDataStore( $this->dbh, $dataTable, 'id', $dataField );
  68          $tree = new ezcTreeDbNestedSet(
  69              $this->dbh,
  70              'nested_set' . $indexTableSuffix,
  71              $store
  72          );
  73          return $tree;
  74      }
  75  
  76      public function testVerifyLeftRightValues1()
  77      {
  78          $tree = $this->setUpEmptyTestTree();
  79  
  80          $node = $tree->createNode( 1, "één" );
  81          $tree->setRootNode( $node );
  82          $this->assertLeftRight( 1, 1, 2 );
  83  
  84          $node->addChild( $node2 = $tree->createNode( 2, "twee" ) );
  85          $this->assertLeftRight( 1, 1, 4 );
  86          $this->assertLeftRight( 2, 2, 3 );
  87  
  88          $node->addChild( $node3 = $tree->createNode( 3, "drie" ) );
  89          $this->assertLeftRight( 1, 1, 6 );
  90          $this->assertLeftRight( 2, 2, 3 );
  91          $this->assertLeftRight( 3, 4, 5 );
  92  
  93          $node3->addChild( $node4 = $tree->createNode( 4, "vier" ) );
  94          $this->assertLeftRight( 1, 1, 8 );
  95          $this->assertLeftRight( 2, 2, 3 );
  96          $this->assertLeftRight( 3, 4, 7 );
  97          $this->assertLeftRight( 4, 5, 6 );
  98  
  99          $node4->addChild( $tree->createNode( 5, "vijf" ) );
 100          $this->assertLeftRight( 1, 1, 10 );
 101          $this->assertLeftRight( 2, 2,  3 );
 102          $this->assertLeftRight( 3, 4,  9 );
 103          $this->assertLeftRight( 4, 5,  8 );
 104          $this->assertLeftRight( 5, 6,  7 );
 105  
 106          $node4->addChild( $tree->createNode( 6, "zes" ) );
 107          $this->assertLeftRight( 1, 1, 12 );
 108          $this->assertLeftRight( 2, 2,  3 );
 109          $this->assertLeftRight( 3, 4, 11 );
 110          $this->assertLeftRight( 4, 5, 10 );
 111          $this->assertLeftRight( 5, 6,  7 );
 112          $this->assertLeftRight( 6, 8,  9 );
 113  
 114          $node->addChild( $node7 = $tree->createNode( 7, "zeven" ) );
 115          $this->assertLeftRight( 1,  1, 14 );
 116          $this->assertLeftRight( 2,  2,  3 );
 117          $this->assertLeftRight( 3,  4, 11 );
 118          $this->assertLeftRight( 4,  5, 10 );
 119          $this->assertLeftRight( 5,  6,  7 );
 120          $this->assertLeftRight( 6,  8,  9 );
 121          $this->assertLeftRight( 7, 12, 13 );
 122  
 123          $node7->addChild( $node8 = $tree->createNode( 8, "acht" ) );
 124          $this->assertLeftRight( 1,  1, 16 );
 125          $this->assertLeftRight( 2,  2,  3 );
 126          $this->assertLeftRight( 3,  4, 11 );
 127          $this->assertLeftRight( 4,  5, 10 );
 128          $this->assertLeftRight( 5,  6,  7 );
 129          $this->assertLeftRight( 6,  8,  9 );
 130          $this->assertLeftRight( 7, 12, 15 );
 131          $this->assertLeftRight( 8, 13, 14 );
 132  
 133          $node8->addChild( $tree->createNode( 9, "negen" ) );
 134          $this->assertLeftRight( 1,  1, 18 );
 135          $this->assertLeftRight( 2,  2,  3 );
 136          $this->assertLeftRight( 3,  4, 11 );
 137          $this->assertLeftRight( 4,  5, 10 );
 138          $this->assertLeftRight( 5,  6,  7 );
 139          $this->assertLeftRight( 6,  8,  9 );
 140          $this->assertLeftRight( 7, 12, 17 );
 141          $this->assertLeftRight( 8, 13, 16 );
 142          $this->assertLeftRight( 9, 14, 15 );
 143      }
 144  
 145      private function assertLeftRight( $id, $left, $right )
 146      {
 147          $q = $this->dbh->createSelectQuery();
 148          $q->select( 'lft', 'rgt' )
 149            ->from( 'nested_set' )
 150            ->where( $q->expr->eq( 'id', $q->bindValue( $id ) ) );
 151          $s = $q->prepare();
 152          $s->execute();
 153  
 154          $r = $s->fetch( PDO::FETCH_ASSOC );
 155          if ( $r['lft'] != $left )
 156          {
 157              $this->fail( "Expected left value for node '$id' was not '$left', but '{$r['lft']}'." );
 158          }
 159          if ( $r['rgt'] != $right )
 160          {
 161              $this->fail( "Expected right value for node '$id' was not '$right', but '{$r['rgt']}'." );
 162          }
 163  
 164      }
 165  
 166      public static function suite()
 167      {
 168           return new PHPUnit_Framework_TestSuite( "ezcTreeDbNestedSetTest" );
 169      }
 170  }
 171  
 172  ?>

title

Description

title

Description

title

Description

title

title

Body