Created
August 6, 2013 21:31
-
-
Save gsouf/6168877 to your computer and use it in GitHub Desktop.
PHPUnit / DBUnit / Fixture : fixing Foreign key contraints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<phpunit bootstrap="bootstrap.php"> | |
<testsuites> | |
<testsuite name="Core"> | |
<directory>./CoreTest</directory> | |
</testsuite> | |
</testsuites> | |
<php> | |
<var name="DB_DSN" value="mysql:dbname=DBNAME;host=localhost" /> | |
<var name="DB_USER" value="USER" /> | |
<var name="DB_PASSWD" value="SECRET" /> | |
<var name="DB_DBNAME" value="DBNAME" /> | |
</php> | |
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Test; | |
abstract class PHPUnitTestDb extends \PHPUnit_Extensions_Database_TestCase | |
{ | |
// one instance per whole test | |
static private $pdo = null; | |
// one instance per test | |
private $conn = null; | |
final public function getConnection() | |
{ | |
if ($this->conn === null) { | |
if (self::$pdo == null) { | |
self::$pdo = new \PDO( $GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD'] ); | |
} | |
$this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_DBNAME']); | |
} | |
return $this->conn; | |
} | |
protected function getDataSet() | |
{ | |
return $this->createMySQLXMLDataSet('dataset.xml'); | |
} | |
protected function setUp() { | |
$conn=$this->getConnection(); | |
$conn->getConnection()->query("set foreign_key_checks=0"); | |
parent::setUp(); | |
$conn->getConnection()->query("set foreign_key_checks=1"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment