Автор: Mike Lively (m@digitalsandwich.com)
Наследование: extends IteratorAggregate
Пример #1
0
    /**
     * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
     * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
     */
    public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
    {
        if(!($connection instanceof Zend_Test_PHPUnit_Db_Connection)) {
            // require_once "Zend/Test/PHPUnit/Db/Exception.php";
            throw new Zend_Test_PHPUnit_Db_Exception("Not a valid Zend_Test_PHPUnit_Db_Connection instance, ".get_class($connection)." given!");
        }

        $databaseDataSet = $connection->createDataSet();

        $dsIterator = $dataSet->getIterator();

        foreach($dsIterator as $table) {
            $tableName = $table->getTableMetaData()->getTableName();

            $db = $connection->getConnection();
            for($i = 0; $i < $table->getRowCount(); $i++) {
                $values = $this->buildInsertValues($table, $i);
                try {
                    $db->insert($tableName, $values);
                } catch (Exception $e) {
                    throw new PHPUnit_Extensions_Database_Operation_Exception("INSERT", "INSERT INTO ".$tableName." [..]", $values, $table, $e->getMessage());
                }
            }
        }
    }
Пример #2
0
    /**
     * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
     * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
     */
    public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
    {
        $databaseDataSet = $connection->createDataSet();

        $dsIterator = $this->iteratorDirection == self::ITERATOR_TYPE_REVERSE ? $dataSet->getReverseIterator() : $dataSet->getIterator();

        foreach ($dsIterator as $table) {
            /* @var $table PHPUnit_Extensions_Database_DataSet_ITable */
            $databaseTableMetaData = $databaseDataSet->getTableMetaData($table->getTableMetaData()->getTableName());
            $query                 = $this->buildOperationQuery($databaseTableMetaData, $table, $connection);

            if ($query === FALSE && $table->getRowCount() > 0) {
                throw new PHPUnit_Extensions_Database_Operation_Exception($this->operationName, '', array(), $table, "Rows requested for insert, but no columns provided!");
            }

            $statement = $connection->getConnection()->prepare($query);
            $rowCount  = $table->getRowCount();

            for ($i = 0; $i < $rowCount; $i++) {
                $args = $this->buildOperationArguments($databaseTableMetaData, $table, $i);

                try {
                    $statement->execute($args);
                }

                catch (Exception $e) {
                    throw new PHPUnit_Extensions_Database_Operation_Exception(
                      $this->operationName, $query, $args, $table, $e->getMessage()
                    );
                }
            }
        }
    }
 /**
  * Adds a new data set to the composite.
  *
  * The dataset may not define tables that already exist in the composite.
  *
  * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
  */
 public function addDataSet(PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     foreach ($dataSet->getTableNames() as $tableName) {
         if (in_array($tableName, $this->getTableNames())) {
             throw new InvalidArgumentException("DataSet contains a table that already exists: {$tableName}");
         }
     }
     $this->dataSets[] = $dataSet;
 }
Пример #4
0
 public function execute(\PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, \PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     foreach ($dataSet->getTableNames() as $tableName) {
         $connection->getConnection()->query("Alter Table {$tableName} NOCHECK Constraint All");
     }
     parent::execute($connection, $dataSet);
     foreach ($dataSet->getTableNames() as $tableName) {
         $connection->getConnection()->query("Alter Table {$tableName} CHECK Constraint All");
     }
 }
Пример #5
0
 public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     foreach ($dataSet->getReverseIterator() as $table) {
         /* @var $table PHPUnit_Extensions_Database_DataSet_ITable */
         $query = "\n                DELETE FROM {$connection->quoteSchemaObject($table->getTableMetaData()->getTableName())}\n            ";
         try {
             $connection->getConnection()->query($query);
         } catch (PDOException $e) {
             throw new PHPUnit_Extensions_Database_Operation_Exception('DELETE_ALL', $query, array(), $table, $e->getMessage());
         }
     }
 }
Пример #6
0
 /**
  *
  * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
  * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
  * @return void
  */
 public function execute(\PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, \PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     if (!$connection instanceof \Zend\Test\PHPUnit\Db\Connection) {
         throw new \Zend\Test\PHPUnit\Db\Exception\InvalidArgumentException("Not a valid Zend_Test_PHPUnit_Db_Connection instance, " . get_class($connection) . " given!");
     }
     foreach ($dataSet->getReverseIterator() as $table) {
         try {
             $tableName = $table->getTableMetaData()->getTableName();
             $this->_truncate($connection->getConnection(), $tableName);
         } catch (\Exception $e) {
             throw new \PHPUnit_Extensions_Database_Operation_Exception('TRUNCATE', 'TRUNCATE ' . $tableName . '', array(), $table, $e->getMessage());
         }
     }
 }
Пример #7
0
 /**
  *
  * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
  * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
  * @return void
  */
 public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     if (!$connection instanceof Zend_Test_PHPUnit_Db_Connection) {
         require_once "Zend/Test/PHPUnit/Db/Exception.php";
         throw new Zend_Test_PHPUnit_Db_Exception("Not a valid Zend_Test_PHPUnit_Db_Connection instance, " . get_class($connection) . " given!");
     }
     foreach ($dataSet->getReverseIterator() as $table) {
         try {
             $tableName = $table->getTableMetaData()->getTableName();
             $this->_truncate($connection->getConnection(), $tableName);
         } catch (Exception $e) {
             throw new PHPUnit_Extensions_Database_Operation_Exception('TRUNCATE', 'TRUNCATE ' . $tableName . '', array(), $table, $e->getMessage());
         }
     }
 }
Пример #8
0
 public function execute(\PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, \PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     /* @var $connection DoctrineExtensions\PHPUnit\TestConnection */
     $conn = $connection->getConnection();
     foreach ($dataSet->getReverseIterator() as $table) {
         /* @var $table PHPUnit_Extensions_Database_DataSet_ITable */
         $tableName = $connection->quoteSchemaObject($table->getTableMetaData()->getTableName());
         $query = $conn->getDatabasePlatform()->getTruncateTableSql($tableName, $this->useCascade);
         try {
             $connection->getConnection()->executeUpdate($query);
         } catch (\Exception $e) {
             throw new \PHPUnit_Extensions_Database_Operation_Exception('TRUNCATE', $query, array(), $table, $e->getMessage());
         }
     }
 }
 /**
  * Adds a new data set to the composite.
  *
  * The dataset may not define tables that already exist in the composite.
  *
  * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
  */
 public function addDataSet(PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     foreach ($dataSet->getTableNames() as $tableName) {
         if (!in_array($tableName, $this->getTableNames())) {
             $this->motherDataset->addTable($dataSet->getTable($tableName));
         } else {
             $other = $dataSet->getTable($tableName);
             $table = $this->getTable($tableName);
             if (!$table->getTableMetaData()->matches($other->getTableMetaData())) {
                 throw new InvalidArgumentException("There is already a table named {$tableName} with different table definition");
             }
             $table->addTableRows($dataSet->getTable($tableName));
         }
     }
 }
Пример #10
0
 public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     foreach ($dataSet->getReverseIterator() as $table) {
         /* @var $table PHPUnit_Extensions_Database_DataSet_ITable */
         $query = "\n                {$connection->getTruncateCommand()} {$connection->quoteSchemaObject($table->getTableMetaData()->getTableName())}\n            ";
         if ($this->useCascade && $connection->allowsCascading()) {
             $query .= " CASCADE";
         }
         try {
             $connection->getConnection()->query($query);
         } catch (PDOException $e) {
             throw new PHPUnit_Extensions_Database_Operation_Exception('TRUNCATE', $query, array(), $table, $e->getMessage());
         }
     }
 }
Пример #11
0
 public function execute(\PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, \PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     foreach ($dataSet->getTableNames() as $tableName) {
         $connection->getConnection()->query("Alter Table {$tableName} NOCHECK Constraint All");
     }
     foreach ($dataSet->getTableNames() as $tableName) {
         $connection->getConnection()->query("Delete From {$tableName}");
     }
     foreach ($dataSet->getTableNames() as $tableName) {
         $connection->getConnection()->query("DBCC CHECKIDENT ({$tableName}, RESEED, 0)");
     }
     foreach ($dataSet->getTableNames() as $tableName) {
         $connection->getConnection()->query("Alter Table {$tableName} CHECK Constraint All");
     }
 }
Пример #12
0
 /**
  * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
  * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
  */
 public function execute(\PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, \PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     if (!$connection instanceof \Zend\Test\PHPUnit\Db\Connection) {
         throw new \Zend\Test\PHPUnit\Db\Exception\InvalidArgumentException("Not a valid Zend_Test_PHPUnit_Db_Connection instance, " . get_class($connection) . " given!");
     }
     $databaseDataSet = $connection->createDataSet();
     $dsIterator = $dataSet->getIterator();
     foreach ($dsIterator as $table) {
         $tableName = $table->getTableMetaData()->getTableName();
         $db = $connection->getConnection();
         for ($i = 0; $i < $table->getRowCount(); $i++) {
             $values = $this->buildInsertValues($table, $i);
             try {
                 $db->insert($tableName, $values);
             } catch (\Exception $e) {
                 throw new \PHPUnit_Extensions_Database_Operation_Exception("INSERT", "INSERT INTO " . $tableName . " [..]", $values, $table, $e->getMessage());
             }
         }
     }
 }
Пример #13
0
 public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     foreach ($dataSet->getReverseIterator() as $table) {
         /* @var $table PHPUnit_Extensions_Database_DataSet_ITable */
         $query = "\n                {$connection->getTruncateCommand()} {$connection->quoteSchemaObject($table->getTableMetaData()->getTableName())}\n            ";
         if ($this->useCascade && $connection->allowsCascading()) {
             $query .= ' CASCADE';
         }
         try {
             $this->disableForeignKeyChecksForMysql($connection);
             $connection->getConnection()->query($query);
             $this->enableForeignKeyChecksForMysql($connection);
         } catch (\Exception $e) {
             $this->enableForeignKeyChecksForMysql($connection);
             if ($e instanceof PDOException) {
                 throw new PHPUnit_Extensions_Database_Operation_Exception('TRUNCATE', $query, [], $table, $e->getMessage());
             }
             throw $e;
         }
     }
 }
Пример #14
0
 /**
  * Disable foreign key constraint checking prior to running the stock PHPUnit
  * truncate operation and then re-enable it.
  *
  * @param \PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
  * @param \PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet
  * @return void
  */
 public function execute(\PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, \PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
 {
     if (defined('WPINC')) {
         $connection->getConnection()->query("SET foreign_key_checks = 0");
     }
     foreach ($dataSet->getReverseIterator() as $table) {
         /* @var $table PHPUnit_Extensions_Database_DataSet_ITable */
         $query = "\n                {$connection->getTruncateCommand()}\n                {$connection->quoteSchemaObject($table->getTableMetaData()->getTableName())}\n            ";
         if (defined('WPINC')) {
             $query .= "";
         } else {
             $query .= " RESTART IDENTITY CASCADE";
         }
         try {
             $connection->getConnection()->query($query);
         } catch (PDOException $e) {
             throw new PHPUnit_Extensions_Database_Operation_Exception('TRUNCATE', $query, array(), $table, $e->getMessage());
         }
     }
     if (defined('WPINC')) {
         $connection->getConnection()->query("SET foreign_key_checks = 1");
     }
 }
Пример #15
0
 /**
  * Creates an iterator over the tables in the data set. If $reverse is
  * true a reverse iterator will be returned.
  *
  * @param  bool                                               $reverse
  * @return PHPUnit_Extensions_Database_DataSet_ITableIterator
  */
 protected function createIterator($reverse = FALSE)
 {
     $original_tables = $this->originalDataSet->getIterator($reverse);
     $new_tables = [];
     foreach ($original_tables as $table) {
         /* @var $table PHPUnit_Extensions_Database_DataSet_ITable */
         $tableName = $table->getTableMetaData()->getTableName();
         if (!in_array($tableName, $this->includeTables) && !empty($this->includeTables) || in_array($tableName, $this->excludeTables)) {
             continue;
         } elseif (!empty($this->excludeColumns[$tableName]) || !empty($this->includeColumns[$tableName])) {
             $new_table = new PHPUnit_Extensions_Database_DataSet_TableFilter($table);
             if (!empty($this->includeColumns[$tableName])) {
                 $new_table->addIncludeColumns($this->includeColumns[$tableName]);
             }
             if (!empty($this->excludeColumns[$tableName])) {
                 $new_table->addExcludeColumns($this->excludeColumns[$tableName]);
             }
             $new_tables[] = $new_table;
         } else {
             $new_tables[] = $table;
         }
     }
     return new PHPUnit_Extensions_Database_DataSet_DefaultTableIterator($new_tables);
 }
Пример #16
0
 /**
  * Creates an iterator over the tables in the data set. If $reverse is
  * true a reverse iterator will be returned.
  *
  * @param bool $reverse
  * @return PHPUnit_Extensions_Database_DataSet_ITableIterator
  */
 protected function createIterator($reverse = FALSE)
 {
     $original_tables = $this->originalDataSet->getIterator($reverse);
     $new_tables = array();
     foreach ($original_tables as $table) {
         /* @var $table PHPUnit_Extensions_Database_DataSet_ITable */
         $tableName = $table->getTableMetaData()->getTableName();
         if (in_array($tableName, $this->excludeTables)) {
             continue;
         } elseif (array_key_exists($tableName, $this->excludeColumns)) {
             $new_tables[] = new PHPUnit_Extensions_Database_DataSet_TableFilter($table, $this->excludeColumns[$tableName]);
         } else {
             $new_tables[] = $table;
         }
     }
     return new PHPUnit_Extensions_Database_DataSet_DefaultTableIterator($new_tables);
 }
Пример #17
0
 /**
  * Creates an iterator over the tables in the data set. If $reverse is
  * true a reverse iterator will be returned.
  *
  * @param bool $reverse
  * @return PHPUnit_Extensions_Database_DataSet_ITableIterator
  */
 protected function createIterator($reverse = FALSE)
 {
     $innerIterator = $reverse ? $this->dataSet->getReverseIterator() : $this->dataSet->getIterator();
     return new PHPUnit_Extensions_Database_DataSet_ReplacementTableIterator($innerIterator, $this->fullReplacements, $this->subStrReplacements);
 }
 /**
  * Asserts that the given data set matches this data set.
  *
  * @param PHPUnit_Extensions_Database_DataSet_IDataSet $other
  */
 public function matches(PHPUnit_Extensions_Database_DataSet_IDataSet $other)
 {
     $thisTableNames = $this->getTableNames();
     $otherTableNames = $other->getTableNames();
     sort($thisTableNames);
     sort($otherTableNames);
     if ($thisTableNames != $otherTableNames) {
         return FALSE;
     }
     foreach ($thisTableNames as $tableName) {
         $table = $this->getTable($tableName);
         if (!$table->matches($other->getTable($tableName))) {
             return FALSE;
         }
     }
     return TRUE;
 }
Пример #19
0
 /**
  * Asserts that the given data set matches this data set.
  *
  * @param PHPUnit_Extensions_Database_DataSet_IDataSet $other
  */
 public function assertEquals(PHPUnit_Extensions_Database_DataSet_IDataSet $other)
 {
     $thisTableNames = $this->getTableNames();
     $otherTableNames = $other->getTableNames();
     sort($thisTableNames);
     sort($otherTableNames);
     if ($thisTableNames != $otherTableNames) {
         throw new Exception("Expected following tables: " . implode(', ', $thisTableNames) . "; has columns: " . implode(', ', $otherTableNames));
     }
     foreach ($thisTableNames as $tableName) {
         $this->getTable($tableName)->assertEquals($other->getTable($tableName));
     }
     return TRUE;
 }
Пример #20
0
 /**
  * Returns a string representation of the constraint.
  *
  * @return string
  */
 public function toString()
 {
     return sprintf('is equal to expected %s', $this->value->__toString());
 }
Пример #21
0
    /**
     * Load date into moodle database tables from standard PHPUnit data set.
     *
     * Note: it is usually better to use data generators
     *
     * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataset
     * @return void
     */
    protected function loadDataSet(PHPUnit_Extensions_Database_DataSet_IDataSet $dataset) {
        global $DB;

        $structure = phpunit_util::get_tablestructure();

        foreach($dataset->getTableNames() as $tablename) {
            $table = $dataset->getTable($tablename);
            $metadata = $dataset->getTableMetaData($tablename);
            $columns = $metadata->getColumns();

            $doimport = false;
            if (isset($structure[$tablename]['id']) and $structure[$tablename]['id']->auto_increment) {
                $doimport = in_array('id', $columns);
            }

            for($r=0; $r<$table->getRowCount(); $r++) {
                $record = $table->getRow($r);
                if ($doimport) {
                    $DB->import_record($tablename, $record);
                } else {
                    $DB->insert_record($tablename, $record);
                }
            }
            if ($doimport) {
                $DB->get_manager()->reset_sequence(new xmldb_table($tablename));
            }
        }
    }