Insert.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once "PHPUnit/Extensions/Database/Operation/IDatabaseOperation.php";
  23. require_once "PHPUnit/Extensions/Database/DB/IDatabaseConnection.php";
  24. require_once "PHPUnit/Extensions/Database/DataSet/IDataSet.php";
  25. require_once "PHPUnit/Extensions/Database/Operation/Exception.php";
  26. /**
  27. * @see Zend_Test_PHPUnit_Db_Connection
  28. */
  29. require_once "Zend/Test/PHPUnit/Db/Connection.php";
  30. /**
  31. * Operation for Inserting on setup or teardown of a database tester.
  32. *
  33. * @uses PHPUnit_Extensions_Database_Operation_IDatabaseOperation
  34. * @category Zend
  35. * @package Zend_Test
  36. * @subpackage PHPUnit
  37. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Test_PHPUnit_Db_Operation_Insert implements PHPUnit_Extensions_Database_Operation_IDatabaseOperation
  41. {
  42. /**
  43. * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
  44. * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
  45. */
  46. public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
  47. {
  48. if(!($connection instanceof Zend_Test_PHPUnit_Db_Connection)) {
  49. require_once "Zend/Test/PHPUnit/Db/Exception.php";
  50. throw new Zend_Test_PHPUnit_Db_Exception("Not a valid Zend_Test_PHPUnit_Db_Connection instance, ".get_class($connection)." given!");
  51. }
  52. $databaseDataSet = $connection->createDataSet();
  53. $dsIterator = $dataSet->getIterator();
  54. foreach($dsIterator as $table) {
  55. $tableName = $table->getTableMetaData()->getTableName();
  56. $db = $connection->getConnection();
  57. for($i = 0; $i < $table->getRowCount(); $i++) {
  58. $values = $this->buildInsertValues($table, $i);
  59. try {
  60. $db->insert($tableName, $values);
  61. } catch (Exception $e) {
  62. throw new PHPUnit_Extensions_Database_Operation_Exception("INSERT", "INSERT INTO ".$tableName." [..]", $values, $table, $e->getMessage());
  63. }
  64. }
  65. }
  66. }
  67. /**
  68. *
  69. * @param PHPUnit_Extensions_Database_DataSet_ITable $table
  70. * @param int $rowNum
  71. * @return array
  72. */
  73. protected function buildInsertValues(PHPUnit_Extensions_Database_DataSet_ITable $table, $rowNum)
  74. {
  75. $values = array();
  76. foreach($table->getTableMetaData()->getColumns() as $columnName) {
  77. $values[$columnName] = $table->getValue($rowNum, $columnName);
  78. }
  79. return $values;
  80. }
  81. }