PHPUnit and MySQL truncation error

后端 未结 3 2150
臣服心动
臣服心动 2021-02-07 13:48

I am getting a headache with PHPUnit\'s behavior of always running TRUNCATE prior to inserting fixtures without first setting foreign key checks off:

3条回答
  •  滥情空心
    2021-02-07 13:58

    Alternatively, you could simulate truncation with a combination of deleting all records and then re-setting the auto-increment counter.

    First, you would have to create new database operation class that will handle resetting the table's auto-increment value.

    /**
     * Resets all AUTO_INCREMENT counters on all tables in a dataset.
     * @see PHPUnit_Extensions_Database_Operation_IDatabaseOperation
     */
    class ResetAutoincrementOperation implements PHPUnit_Extensions_Database_Operation_IDatabaseOperation
    {
    
        /* 
         * @see PHPUnit_Extensions_Database_Operation_IDatabaseOperation::execute()
         */
        public function execute(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, 
                PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
        {
            foreach ($dataSet->getReverseIterator() as $table) {
                $query = "ALTER TABLE {$connection->quoteSchemaObject($table->getTableMetaData()->getTableName())}"
                    . " AUTO_INCREMENT = 1
                ";
    
                try {
                    $connection->getConnection()->query($query);
                } catch (PDOException $e) {
                    throw new PHPUnit_Extensions_Database_Operation_Exception('RESET_AUTOINCREMENT', 
                            $query, array(), $table, $e->getMessage());
                }
            }
        }
    }
    

    now overwrite the getSetUpOperation()and getTearDownOperation() methods as described above.

    class FooTest extends PHPUnit_Extensions_Database_TestCase
    {
        /**
         * @see PHPUnit_Extensions_Database_TestCase::getTearDownOperation()
         */
        protected function getTearDownOperation ()
        {
            // Clean up after ourselves
            return new PHPUnit_Extensions_Database_Operation_Composite(array(
                PHPUnit_Extensions_Database_Operation_Factory::DELETE_ALL(), // 1. delete all records from table
                new ResetAutoincrementOperation() // 2. reset auto increment value
            ));
        }
    
        /**
         * @see PHPUnit_Extensions_Database_TestCase::getSetUpOperation ()
         */
        protected function getSetUpOperation ()
        {
            return new PHPUnit_Extensions_Database_Operation_Composite(array(
                    PHPUnit_Extensions_Database_Operation_Factory::DELETE_ALL(), // 1. delete all records from table
                    new ResetAutoincrementOperation(), // 2. reset auto increment value
                    PHPUnit_Extensions_Database_Operation_Factory::INSERT() // 3. insert new records
            ));
        }
    }
    

    Worked for me with MySQL 5.5.24 and PHPUnit 3.6.10

提交回复
热议问题