web/lib/Zend/Test/PHPUnit/Db/Operation/Insert.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Test
       
    17  * @subpackage PHPUnit
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Insert.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see PHPUnit_Extensions_Database_Operation_IDatabaseOperation
       
    25  */
       
    26 require_once "PHPUnit/Extensions/Database/Operation/IDatabaseOperation.php";
       
    27 
       
    28 /**
       
    29  * @see PHPUnit_Extensions_Database_DB_IDatabaseConnection
       
    30  */
       
    31 require_once "PHPUnit/Extensions/Database/DB/IDatabaseConnection.php";
       
    32 
       
    33 /**
       
    34  * @see PHPUnit_Extensions_Database_DataSet_IDataSet
       
    35  */
       
    36 require_once "PHPUnit/Extensions/Database/DataSet/IDataSet.php";
       
    37 
       
    38 /**
       
    39  * @see PHPUnit_Extensions_Database_Operation_Exception
       
    40  */
       
    41 require_once "PHPUnit/Extensions/Database/Operation/Exception.php";
       
    42 
       
    43 /**
       
    44  * @see Zend_Test_PHPUnit_Db_Connection
       
    45  */
       
    46 require_once "Zend/Test/PHPUnit/Db/Connection.php";
       
    47 
       
    48 /**
       
    49  * Operation for Inserting on setup or teardown of a database tester.
       
    50  *
       
    51  * @uses       PHPUnit_Extensions_Database_Operation_IDatabaseOperation
       
    52  * @category   Zend
       
    53  * @package    Zend_Test
       
    54  * @subpackage PHPUnit
       
    55  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    56  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    57  */
       
    58 class Zend_Test_PHPUnit_Db_Operation_Insert implements PHPUnit_Extensions_Database_Operation_IDatabaseOperation
       
    59 {
       
    60     /**
       
    61      * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
       
    62      * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
       
    63      */
       
    64     public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
       
    65     {
       
    66         if(!($connection instanceof Zend_Test_PHPUnit_Db_Connection)) {
       
    67             require_once "Zend/Test/PHPUnit/Db/Exception.php";
       
    68             throw new Zend_Test_PHPUnit_Db_Exception("Not a valid Zend_Test_PHPUnit_Db_Connection instance, ".get_class($connection)." given!");
       
    69         }
       
    70 
       
    71         $databaseDataSet = $connection->createDataSet();
       
    72 
       
    73         $dsIterator = $dataSet->getIterator();
       
    74 
       
    75         foreach($dsIterator as $table) {
       
    76             $tableName = $table->getTableMetaData()->getTableName();
       
    77 
       
    78             $db = $connection->getConnection();
       
    79             for($i = 0; $i < $table->getRowCount(); $i++) {
       
    80                 $values = $this->buildInsertValues($table, $i);
       
    81                 try {
       
    82                     $db->insert($tableName, $values);
       
    83                 } catch (Exception $e) {
       
    84                     throw new PHPUnit_Extensions_Database_Operation_Exception("INSERT", "INSERT INTO ".$tableName." [..]", $values, $table, $e->getMessage());
       
    85                 }
       
    86             }
       
    87         }
       
    88     }
       
    89 
       
    90     /**
       
    91      *
       
    92      * @param PHPUnit_Extensions_Database_DataSet_ITable $table
       
    93      * @param int $rowNum
       
    94      * @return array
       
    95      */
       
    96     protected function buildInsertValues(PHPUnit_Extensions_Database_DataSet_ITable $table, $rowNum)
       
    97     {
       
    98         $values = array();
       
    99         foreach($table->getTableMetaData()->getColumns() as $columnName) {
       
   100             $values[$columnName] = $table->getValue($rowNum, $columnName);
       
   101         }
       
   102         return $values;
       
   103     }
       
   104 }