eZ components PHP Cross Reference Developer Tools

Source: /GraphDatabaseTiein/tests/dataset_pdo_test.php - 374 lines - 9423 bytes - Summary - Text - Print

Description: ezcGraphDatabaseTest

   1  <?php
   2  /**
   3   * ezcGraphDatabaseTest 
   4   * 
   5   * @package Graph
   6   * @version 1.0.1
   7   * @subpackage Tests
   8   * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
   9   * @license http://ez.no/licenses/new_bsd New BSD License
  10   */
  11  
  12  /**
  13   * Tests for ezcGraph class.
  14   * 
  15   * @package ImageAnalysis
  16   * @subpackage Tests
  17   */
  18  class ezcGraphDatabaseTest extends ezcTestCase
  19  {
  20      protected $basePath;
  21  
  22      protected $tempDir;
  23  
  24  	public static function suite()
  25      {
  26          return new PHPUnit_Framework_TestSuite( "ezcGraphDatabaseTest" );
  27      }
  28  
  29      protected function setUp()
  30      {
  31          static $i = 0;
  32          $this->tempDir = $this->createTempDir( __CLASS__ . sprintf( '_%03d_', ++$i ) ) . '/';
  33          $this->basePath = dirname( __FILE__ ) . '/data/';
  34  
  35          // Try to build up database connection
  36          try
  37          {
  38              $db = ezcDbInstance::get();
  39          }
  40          catch ( Exception $e )
  41          {
  42              $this->markTestSkipped( 'Database connection required for PDO statement tests.' );
  43          }
  44  
  45          $this->q = new ezcQueryInsert( $db );
  46          try
  47          {
  48              $db->exec( 'DROP TABLE graph_pdo_test' );
  49          }
  50          catch ( Exception $e ) {} // eat
  51  
  52          // Create test table
  53          $db->exec( 'CREATE TABLE graph_pdo_test ( id INT, browser VARCHAR(255), hits INT )' );
  54  
  55          // Insert some data
  56          $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'Firefox', 2567 )" );
  57          $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'Opera', 543 )" );
  58          $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'Safari', 23 )" );
  59          $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'Konquror', 812 )" );
  60          $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'Lynx', 431 )" );
  61          $db->exec( "INSERT INTO graph_pdo_test VALUES ( 0, 'wget', 912 )" );
  62      }
  63  
  64      protected function tearDown()
  65      {
  66          if ( !$this->hasFailed() )
  67          {
  68              $this->removeTempDir();
  69          }
  70  
  71          $db = ezcDbInstance::get();
  72          $db->exec( 'DROP TABLE graph_pdo_test' );
  73      }
  74  
  75      public function testAutomaticDataSetUsage()
  76      {
  77          $db = ezcDbInstance::get();
  78  
  79          $statement = $db->prepare( 'SELECT browser, hits FROM graph_pdo_test' );
  80          $statement->execute();
  81  
  82          $dataset = new ezcGraphDatabaseDataSet( $statement );
  83  
  84          $dataSetArray = array(
  85              'Firefox' => 2567,
  86              'Opera' => 543,
  87              'Safari' => 23,
  88              'Konquror' => 812,
  89              'Lynx' => 431,
  90              'wget' => 912,
  91          );
  92  
  93          $count = 0;
  94          foreach ( $dataset as $key => $value )
  95          {
  96              list( $compareKey, $compareValue ) = each( $dataSetArray );
  97  
  98              $this->assertEquals(
  99                  $compareKey,
 100                  $key,
 101                  'Unexpected key for dataset value.'
 102              );
 103  
 104              $this->assertEquals(
 105                  $compareValue,
 106                  $value,
 107                  'Unexpected value for dataset.'
 108              );
 109  
 110              ++$count;
 111          }
 112  
 113          $this->assertEquals(
 114              $count,
 115              count( $dataSetArray ),
 116              'Too few datasets found.'
 117          );
 118      }
 119  
 120      public function testAutomaticDataSetUsageSingleColumn()
 121      {
 122          $db = ezcDbInstance::get();
 123  
 124          $statement = $db->prepare( 'SELECT hits FROM graph_pdo_test' );
 125          $statement->execute();
 126  
 127          $dataset = new ezcGraphDatabaseDataSet( $statement );
 128  
 129          $dataSetArray = array(
 130              'Firefox' => 2567,
 131              'Opera' => 543,
 132              'Safari' => 23,
 133              'Konquror' => 812,
 134              'Lynx' => 431,
 135              'wget' => 912,
 136          );
 137  
 138          $count = 0;
 139          foreach ( $dataset as $key => $value )
 140          {
 141              list( $compareKey, $compareValue ) = each( $dataSetArray );
 142  
 143              $this->assertEquals(
 144                  $count,
 145                  $key,
 146                  'Unexpected key for dataset value.'
 147              );
 148  
 149              $this->assertEquals(
 150                  $compareValue,
 151                  $value,
 152                  'Unexpected value for dataset.'
 153              );
 154  
 155              ++$count;
 156          }
 157  
 158          $this->assertEquals(
 159              $count,
 160              count( $dataSetArray ),
 161              'Too few datasets found.'
 162          );
 163      }
 164  
 165      public function testAutomaticDataSetUsageTooManyRows()
 166      {
 167          $db = ezcDbInstance::get();
 168  
 169          $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
 170          $statement->execute();
 171  
 172          try
 173          {
 174              $dataset = new ezcGraphDatabaseDataSet( $statement );
 175          }
 176          catch ( ezcGraphDatabaseTooManyColumnsException $e )
 177          {
 178              return true;
 179          }
 180  
 181          $this->fail( 'Expected ezcGraphDatabaseTooManyColumnsException.' );
 182      }
 183  
 184      public function testSpecifiedDataSetUsage()
 185      {
 186          $db = ezcDbInstance::get();
 187  
 188          $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
 189          $statement->execute();
 190  
 191          $dataset = new ezcGraphDatabaseDataSet(
 192              $statement,
 193              array(
 194                  ezcGraph::KEY => 'browser',
 195                  ezcGraph::VALUE => 'hits',
 196              )
 197          );
 198  
 199          $dataSetArray = array(
 200              'Firefox' => 2567,
 201              'Opera' => 543,
 202              'Safari' => 23,
 203              'Konquror' => 812,
 204              'Lynx' => 431,
 205              'wget' => 912,
 206          );
 207  
 208          $count = 0;
 209          foreach ( $dataset as $key => $value )
 210          {
 211              list( $compareKey, $compareValue ) = each( $dataSetArray );
 212  
 213              $this->assertEquals(
 214                  $compareKey,
 215                  $key,
 216                  'Unexpected key for dataset value.'
 217              );
 218  
 219              $this->assertEquals(
 220                  $compareValue,
 221                  $value,
 222                  'Unexpected value for dataset.'
 223              );
 224  
 225              ++$count;
 226          }
 227  
 228          $this->assertEquals(
 229              $count,
 230              count( $dataSetArray ),
 231              'Too few datasets found.'
 232          );
 233      }
 234  
 235      public function testSpecifiedDataSetUsageSingleColumn()
 236      {
 237          $db = ezcDbInstance::get();
 238  
 239          $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
 240          $statement->execute();
 241  
 242          $dataset = new ezcGraphDatabaseDataSet(
 243              $statement,
 244              array(
 245                  ezcGraph::VALUE => 'hits',
 246              )
 247          );
 248  
 249          $dataSetArray = array(
 250              'Firefox' => 2567,
 251              'Opera' => 543,
 252              'Safari' => 23,
 253              'Konquror' => 812,
 254              'Lynx' => 431,
 255              'wget' => 912,
 256          );
 257  
 258          $count = 0;
 259          foreach ( $dataset as $key => $value )
 260          {
 261              list( $compareKey, $compareValue ) = each( $dataSetArray );
 262  
 263              $this->assertEquals(
 264                  $count,
 265                  $key,
 266                  'Unexpected key for dataset value.'
 267              );
 268  
 269              $this->assertEquals(
 270                  $compareValue,
 271                  $value,
 272                  'Unexpected value for dataset.'
 273              );
 274  
 275              ++$count;
 276          }
 277  
 278          $this->assertEquals(
 279              $count,
 280              count( $dataSetArray ),
 281              'Too few datasets found.'
 282          );
 283      }
 284  
 285      public function testSpecifiedDataSetUsageBrokenKey()
 286      {
 287          $db = ezcDbInstance::get();
 288  
 289          $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
 290          $statement->execute();
 291  
 292          try
 293          {
 294              $dataset = new ezcGraphDatabaseDataSet(
 295                  $statement,
 296                  array(
 297                      ezcGraph::KEY => 'nonexistant',
 298                      ezcGraph::VALUE => 'hits',
 299                  )
 300              );
 301          }
 302          catch ( ezcGraphDatabaseMissingColumnException $e )
 303          {
 304              return true;
 305          }
 306  
 307          $this->fail( 'Expected ezcGraphDatabaseMissingColumnException.' );
 308      }
 309  
 310      public function testSpecifiedDataSetUsageBrokenValue()
 311      {
 312          $db = ezcDbInstance::get();
 313  
 314          $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
 315          $statement->execute();
 316  
 317          try
 318          {
 319              $dataset = new ezcGraphDatabaseDataSet(
 320                  $statement,
 321                  array(
 322                      ezcGraph::VALUE => 'nonexistant',
 323                  )
 324              );
 325          }
 326          catch ( ezcGraphDatabaseMissingColumnException $e )
 327          {
 328              return true;
 329          }
 330  
 331          $this->fail( 'Expected ezcGraphDatabaseMissingColumnException.' );
 332      }
 333  
 334      public function testNonExceutedQuery()
 335      {
 336          $db = ezcDbInstance::get();
 337  
 338          $statement = $db->prepare( 'SELECT browser, hits FROM graph_pdo_test' );
 339  
 340          try
 341          {
 342              $dataset = new ezcGraphDatabaseDataSet( $statement );
 343          }
 344          catch ( ezcGraphDatabaseStatementNotExecutedException $e )
 345          {
 346              return true;
 347          }
 348          
 349          $this->fail( 'Expected ezcGraphDatabaseStatementNotExecutedException.' );
 350      }
 351  
 352      public function testDataSetCount()
 353      {
 354          $db = ezcDbInstance::get();
 355  
 356          $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
 357          $statement->execute();
 358  
 359          $dataset = new ezcGraphDatabaseDataSet(
 360              $statement,
 361              array(
 362                  ezcGraph::VALUE => 'hits',
 363              )
 364          );
 365  
 366          $this->assertEquals(
 367              count( $dataset ),
 368              6,
 369              'Wrong data set item count returned'
 370          );
 371      }
 372  }
 373  
 374  ?>

title

Description

title

Description

title

Description

title

title

Body