Insert.php 3.0 KB

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