eZ components PHP Cross Reference Developer Tools

Source: /TreeDatabaseTiein/tests/xml_tree_db_storage.php - 161 lines - 5007 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  /**
  12   * @package TreeDatabaseTiein
  13   * @subpackage Tests
  14   */
  15  class ezcTreeXmlWithDbStorageTest extends ezcTestCase
  16  {
  17      protected function setUp()
  18      {
  19          static $i = 0;
  20  
  21          $this->tempDir = $this->createTempDir( __CLASS__ . sprintf( '_%03d_', ++$i ) ) . '/';
  22          try
  23          {
  24              $this->dbh = ezcDbInstance::get();
  25              $this->removeTables();
  26              $this->loadSchemas();
  27          }
  28          catch ( Exception $e )
  29          {
  30              $this->markTestSkipped( $e->getMessage() );
  31          }
  32          $this->store = new ezcTreeDbExternalTableDataStore( $this->dbh, 'dataFrom', 'id', 'data' );
  33      }
  34  
  35      public function teardown()
  36      {
  37          $this->removeTempDir();
  38      }
  39  
  40      private function loadSchemas()
  41      {
  42          $schema = ezcDbSchema::createFromFile( 'array', dirname( __FILE__ ) . "/files/all-types.dba" );
  43          $schema->writeToDb( $this->dbh );
  44      }
  45  
  46      protected function removeTables()
  47      {
  48          try
  49          {
  50              foreach ( array( 'materialized_path', 'nested_set', 'parent_child', 'dataFrom', 'dataTo' ) as $table )
  51              {
  52                  $this->dbh->exec( "DROP TABLE $table" );
  53              }
  54          }
  55          catch ( Exception $e )
  56          {
  57              // ignore
  58          }
  59      }
  60  
  61      protected function addTestData( $tree )
  62      {
  63          $primates = array(
  64              'Hominoidea' => array(
  65                  'Hylobatidae' => array(
  66                      'Hylobates' => array(
  67                          'Lar Gibbon',
  68                          'Agile Gibbon',
  69                          'Müller\'s Bornean Gibbon',
  70                          'Silvery Gibbon',
  71                          'Pileated Gibbon',
  72                          'Kloss\'s Gibbon',
  73                      ),
  74                      'Hoolock' => array(
  75                          'Western Hoolock Gibbon',
  76                          'Eastern Hoolock Gibbon',
  77                      ),
  78                      'Symphalangus' => array(),
  79                      'Nomascus' => array(
  80                          'Black Crested Gibbon',
  81                          'Eastern Black Crested Gibbon',
  82                          'White-cheecked Crested Gibbon',
  83                          'Yellow-cheecked Gibbon',
  84                      ),
  85                  ),
  86                  'Hominidae' => array(
  87                      'Pongo' => array(
  88                          'Bornean Orangutan',
  89                          'Sumatran Orangutan',
  90                      ), 
  91                      'Gorilla' => array(
  92                          'Western Gorilla' => array(
  93                              'Western Lowland Gorilla',
  94                              'Cross River Gorilla',
  95                          ),
  96                          'Eastern Gorilla' => array(
  97                              'Mountain Gorilla',
  98                              'Eastern Lowland Gorilla',
  99                          ),
 100                      ), 
 101                      'Homo' => array(
 102                          'Homo Sapiens' => array(
 103                              'Homo Sapiens Sapiens',
 104                              'Homo Superior'
 105                          ),
 106                      ),
 107                      'Pan' => array(
 108                          'Common Chimpanzee',
 109                          'Bonobo',
 110                      ),
 111                  ),
 112              ),
 113          );
 114  
 115          $root = $tree->createNode( 'Hominoidea', 'Hominoidea' );
 116          $tree->setRootNode( $root );
 117  
 118          $this->addChildren( $root, $primates['Hominoidea'] );
 119      }
 120  
 121      private function fixId( $id )
 122      {
 123          return preg_replace( '/[^A-Z0-9_]/i', '_', $id );
 124      }
 125  
 126      private function addChildren( ezcTreeNode $node, array $children )
 127      {
 128          foreach( $children as $name => $child )
 129          {
 130              if ( is_array( $child ) )
 131              {
 132                  $newNode = $node->tree->createNode( $this->fixId( $name ), $name );
 133                  $node->addChild( $newNode );
 134                  $this->addChildren( $newNode, $child );
 135              }
 136              else
 137              {
 138                  $newNode = $node->tree->createNode( $this->fixId( $child ), $child );
 139                  $node->addChild( $newNode );
 140              }
 141          }
 142      }
 143  
 144      public function testTreeDbStorage()
 145      {
 146          $tree = ezcTreeXml::create( $this->tempDir . 'testDbStorage.xml', $this->store );
 147          $this->addTestData( $tree );
 148          self::assertSame( "Western Hoolock Gibbon", $tree->fetchNodeById( "Western_Hoolock_Gibbon" )->data );
 149  
 150          // start over
 151          $tree = new ezcTreeXml( $this->tempDir . 'testDbStorage.xml', $this->store );
 152          self::assertSame( "Western Hoolock Gibbon", $tree->fetchNodeById( "Western_Hoolock_Gibbon" )->data );
 153      }
 154  
 155      public static function suite()
 156      {
 157           return new PHPUnit_Framework_TestSuite( "ezcTreeXmlWithDbStorageTest" );
 158      }
 159  }
 160  
 161  ?>

title

Description

title

Description

title

Description

title

title

Body