PHPUnit and MySQL truncation error

后端 未结 3 2153
臣服心动
臣服心动 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:53

    I found the answer it seems. I ended up overriding some methods by extending a class.

    getConnection()->query("SET foreign_key_checks = 0");
            parent::execute($connection, $dataSet);
            $connection->getConnection()->query("SET foreign_key_checks = 1");
        }
    }
    

    Then example usage:

    class FooTest extends \PHPUnit_Extensions_Database_TestCase
    {
        public function getSetUpOperation()
        {
            $cascadeTruncates = true; // If you want cascading truncates, false otherwise. If unsure choose false.
    
            return new \PHPUnit_Extensions_Database_Operation_Composite(array(
                new TruncateOperation($cascadeTruncates),
                \PHPUnit_Extensions_Database_Operation_Factory::INSERT()
            ));
        }
    }
    

    So I'm effectively disabling foreign key checks and setting them back if they were ever set. Obviously you should make a base class that has this functionality and you extend it rather than PHPUnit's TestCase.

提交回复
热议问题