eZ components PHP Cross Reference Developer Tools

Source: /Database/tests/sqlabstraction/query_delete_test.php - 188 lines - 5187 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
   4   * @license http://ez.no/licenses/new_bsd New BSD License
   5   * @version 1.4.7
   6   * @filesource
   7   * @package Database
   8   * @subpackage Tests
   9   */
  10  
  11  /**
  12   * Testing the SQL expression abstraction layer for INSERT queries.
  13   *
  14   * @package Database
  15   * @subpackage Tests
  16   */
  17  class ezcQueryDeleteTest extends ezcTestCase
  18  {
  19      private $q;
  20  
  21      protected function setUp()
  22      {
  23          try
  24          {
  25              $db = ezcDbInstance::get();
  26          }
  27          catch ( Exception $e )
  28          {
  29              $this->markTestSkipped();
  30          }
  31  
  32          $this->q = new ezcQueryDelete( $db );
  33          try
  34          {
  35              $db->exec( 'DROP TABLE query_test' );
  36          }
  37          catch ( Exception $e ) {} // eat
  38  
  39          // insert some data
  40          $db->exec( 'CREATE TABLE query_test ( id int, company VARCHAR(255), section VARCHAR(255), employees int )' );
  41  
  42      }
  43  
  44      protected function tearDown()
  45      {
  46          $db = ezcDbInstance::get();
  47          $db->exec( 'DROP TABLE query_test' );
  48      }
  49  
  50      public function testNoWhere()
  51      {
  52          $reference = "DELETE FROM legends";
  53          $this->q->deleteFrom( 'legends' );
  54          $this->assertEquals( $reference, $this->q->getQuery() );
  55      }
  56  
  57      public function testWithWhere()
  58      {
  59          $reference = "DELETE FROM legends WHERE id = 1";
  60          $this->q->deleteFrom( 'legends' )
  61              ->where( $this->q->expr->eq( 'id', 1 ) );
  62          $this->assertEquals( $reference, $this->q->getQuery() );
  63      }
  64  
  65      public function testWithSeveralWhere()
  66      {
  67          $reference = "DELETE FROM legends WHERE Gretzky = Lindros AND 1 = 1";
  68          
  69          $this->q->deleteFrom( 'legends' )
  70              ->where( $this->q->expr->eq( 'Gretzky', 'Lindros' ) )
  71              ->where( $this->q->expr->eq( 1, 1 ) );
  72          $this->assertEquals( $reference, $this->q->getQuery() );
  73      }
  74  
  75      public function testInvalidWhereCall()
  76      {
  77          try
  78          {
  79              $this->q->deleteFrom( 'legends' )
  80                  ->where();
  81          }
  82          catch ( ezcQueryException $e )
  83          {
  84              return;
  85          }
  86          $this->fail( "Got no exception when an exception was expected" );
  87      }
  88  
  89  
  90      public function testNoDeleteFrom()
  91      {
  92          try
  93          {
  94          $this->q->where( $this->q->expr->eq( 'id', 1 ) );
  95          $this->q->getQuery();
  96          }
  97          catch ( Exception $e )
  98          {
  99              return;
 100          }
 101          $this->fail( "Delete query with no table did not fail!" );
 102      }
 103  
 104      // test on a real database.
 105      public function testOnDatabaseWithoutWhere()
 106      {
 107          // fill database with some dummy data
 108          $q = new ezcQueryInsert( ezcDbInstance::get() );
 109          // insert some data we can update
 110          $company = 'eZ systems';
 111          $section = 'Norway';
 112          $q->insertInto( 'query_test' )
 113              ->set( 'id', 1 )
 114              ->set( 'company', $q->bindParam( $company ) )
 115              ->set( 'section', $q->bindParam( $section ) )
 116              ->set( 'employees', 20 );
 117          $stmt = $q->prepare();
 118          $stmt->execute();
 119  
 120          $q->insertInto( 'query_test' );
 121          $q->set( 'id', 2 );
 122          $q->set( 'employees', 70 );
 123          $company = 'trolltech';
 124          $section = 'Norway';
 125          $stmt = $q->prepare();
 126          $stmt->execute();
 127  
 128          // delete all
 129          $this->q->deleteFrom( 'query_test' );
 130          $stmt = $this->q->prepare();
 131          $stmt->execute();
 132  
 133          // test that table is empty
 134          $db = ezcDbInstance::get();
 135          $q = $db->createSelectQuery(); // get select query
 136          $q->select( '*' )->from( 'query_test' );
 137          $stmt = $q->prepare();
 138          $stmt->execute();
 139          $result = $stmt->fetchAll();
 140          $this->assertEquals( 0, count( $result ) );
 141      }
 142  
 143      public function testOnDatabaseWithWhere()
 144      {
 145          // fill database with some dummy data
 146          $q = new ezcQueryInsert( ezcDbInstance::get() );
 147  
 148          // insert some data we can update
 149          $company = 'eZ systems';
 150          $section = 'Norway';
 151          $q->insertInto( 'query_test' )
 152              ->set( 'id', 1 )
 153              ->set( 'company', $q->bindParam( $company ) )
 154              ->set( 'section', $q->bindParam( $section ) )
 155              ->set( 'employees', 20 );
 156          $stmt = $q->prepare();
 157          $stmt->execute();
 158  
 159          $q->insertInto( 'query_test' );
 160          $q->set( 'id', 2 );
 161          $q->set( 'employees', 70 );
 162          $company = 'trolltech';
 163          $section = 'Norway';
 164          $stmt = $q->prepare();
 165          $stmt->execute();
 166  
 167          // delete one
 168          $this->q->deleteFrom( 'query_test' )
 169              ->where( $this->q->expr->eq( 'id', 1 ) );
 170          $stmt = $this->q->prepare();
 171          $stmt->execute();
 172  
 173          // test that table has one row
 174          $db = ezcDbInstance::get();
 175          $q = $db->createSelectQuery(); // get select query
 176          $q->select( '*' )->from( 'query_test' );
 177          $stmt = $q->prepare();
 178          $stmt->execute();
 179          $result = $stmt->fetchAll();
 180          $this->assertEquals( 1, count( $result ) );
 181      }
 182  
 183      public static function suite()
 184      {
 185          return new PHPUnit_Framework_TestSuite( 'ezcQueryDeleteTest' );
 186      }
 187  }
 188  ?>

title

Description

title

Description

title

Description

title

title

Body