Created
March 7, 2013 05:40
-
-
Save gnarfle/5105774 to your computer and use it in GitHub Desktop.
Base context for Behat/Mink with DBUnit fixture support
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 | |
use Behat\MinkExtension\Context\MinkContext; | |
require_once "PHPUnit/Extensions/Database/TestCase.php"; | |
class BaseFeatureContext extends MinkContext | |
{ | |
private $databaseTester; | |
public function getConnection() | |
{ | |
$database = 'mydatabase'; | |
$pdo = new PDO('mysql:dbname=mydatabase;host=localhost', 'user', 'password'); | |
return $this->createDefaultDBConnection($pdo, $database); | |
} | |
protected function getDataSet() | |
{ | |
$fixtures = array( | |
'documents' => 'T2_Document', | |
'entity' => 'T2_Entity', | |
'entity_baked' => 'T2_Entity_Baked', | |
'entity_baked_versions' => 'T2_Entity_Baked_Versions', | |
'entity_locations' => 'T2_Entity_Locations', | |
'exam' => 'T2_Exam', | |
'exam_versions' => 'T2_Exam_Versions', | |
'service' => 'T2_Service', | |
'service_customfielddata' => 'T2_Service_CustomFieldData', | |
'service_issues' => 'T2_Service_Issues', | |
'service_versions' => 'T2_Service_Versions' | |
); | |
$dataSet = new PHPUnit_Extensions_Database_DataSet_CsvDataSet(); | |
foreach( $fixtures as $file => $table ) | |
{ | |
$dataSet->addTable($table, dirname(__FILE__)."/fixtures/{$file}.csv"); | |
} | |
return $dataSet; | |
} | |
/** @BeforeScenario */ | |
public function before($event) | |
{ | |
$this->databaseTester = NULL; | |
$this->getDatabaseTester()->setSetUpOperation($this->getSetUpOperation()); | |
$this->getDatabaseTester()->setDataSet($this->getDataSet()); | |
$this->getDatabaseTester()->onSetUp(); | |
} | |
/** @AfterScenario */ | |
public function after($event) | |
{ | |
$this->getDatabaseTester()->setTearDownOperation($this->getTearDownOperation()); | |
$this->getDatabaseTester()->setDataSet($this->getDataSet()); | |
$this->getDatabaseTester()->onTearDown(); | |
/** | |
* Destroy the tester after the test is run to keep DB connections | |
* from piling up. | |
*/ | |
$this->databaseTester = NULL; | |
} | |
protected function getDatabaseTester() | |
{ | |
if (empty($this->databaseTester)) { | |
$this->databaseTester = $this->newDatabaseTester(); | |
} | |
return $this->databaseTester; | |
} | |
protected function newDatabaseTester() | |
{ | |
return new PHPUnit_Extensions_Database_DefaultTester($this->getConnection()); | |
} | |
protected function getSetUpOperation() | |
{ | |
return PHPUnit_Extensions_Database_Operation_Factory::CLEAN_INSERT(); | |
} | |
protected function getTearDownOperation() | |
{ | |
return PHPUnit_Extensions_Database_Operation_Factory::NONE(); | |
} | |
protected function createDefaultDBConnection(PDO $connection, $schema = '') | |
{ | |
return new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection($connection, $schema); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment