You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.7 KiB
93 lines
2.7 KiB
12 years ago
|
<?php
|
||
12 years ago
|
namespace yiiunit\framework\db;
|
||
12 years ago
|
|
||
11 years ago
|
use yii\db\Connection;
|
||
12 years ago
|
use yiiunit\TestCase as TestCase;
|
||
12 years ago
|
|
||
12 years ago
|
abstract class DatabaseTestCase extends TestCase
|
||
12 years ago
|
{
|
||
11 years ago
|
protected $database;
|
||
|
protected $driverName = 'mysql';
|
||
|
/**
|
||
|
* @var Connection
|
||
|
*/
|
||
10 years ago
|
private $_db;
|
||
12 years ago
|
|
||
11 years ago
|
protected function setUp()
|
||
|
{
|
||
|
parent::setUp();
|
||
11 years ago
|
$databases = self::getParam('databases');
|
||
11 years ago
|
$this->database = $databases[$this->driverName];
|
||
|
$pdo_database = 'pdo_'.$this->driverName;
|
||
10 years ago
|
if ($this->driverName === 'oci') {
|
||
|
$pdo_database = 'oci8';
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
if (!extension_loaded('pdo') || !extension_loaded($pdo_database)) {
|
||
|
$this->markTestSkipped('pdo and '.$pdo_database.' extension are required.');
|
||
|
}
|
||
|
$this->mockApplication();
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
protected function tearDown()
|
||
|
{
|
||
10 years ago
|
if ($this->_db) {
|
||
|
$this->_db->close();
|
||
11 years ago
|
}
|
||
|
$this->destroyApplication();
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
10 years ago
|
* @param boolean $reset whether to clean up the test database
|
||
|
* @param boolean $open whether to open and populate test database
|
||
11 years ago
|
* @return \yii\db\Connection
|
||
|
*/
|
||
|
public function getConnection($reset = true, $open = true)
|
||
|
{
|
||
10 years ago
|
if (!$reset && $this->_db) {
|
||
|
return $this->_db;
|
||
11 years ago
|
}
|
||
10 years ago
|
$config = $this->database;
|
||
|
if (isset($config['fixture'])) {
|
||
|
$fixture = $config['fixture'];
|
||
|
unset($config['fixture']);
|
||
|
} else {
|
||
|
$fixture = null;
|
||
11 years ago
|
}
|
||
10 years ago
|
try {
|
||
10 years ago
|
$this->_db = $this->prepareDatabase($config, $fixture, $open);
|
||
10 years ago
|
} catch (\Exception $e) {
|
||
|
$this->markTestSkipped("Something wrong when preparing database: " . $e->getMessage());
|
||
|
}
|
||
10 years ago
|
return $this->_db;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public function prepareDatabase($config, $fixture, $open = true)
|
||
10 years ago
|
{
|
||
|
if (!isset($config['class'])) {
|
||
|
$config['class'] = 'yii\db\Connection';
|
||
11 years ago
|
}
|
||
10 years ago
|
/* @var $db \yii\db\Connection */
|
||
|
$db = \Yii::createObject($config);
|
||
10 years ago
|
if (!$open) {
|
||
|
return $db;
|
||
|
}
|
||
10 years ago
|
$db->open();
|
||
|
if ($fixture !== null) {
|
||
10 years ago
|
if ($this->driverName === 'oci') {
|
||
|
list($drops, $creates) = explode('/* STATEMENTS */', file_get_contents($fixture), 2);
|
||
|
list($statements, $triggers, $data) = explode('/* TRIGGERS */', $creates, 3);
|
||
|
$lines = array_merge(explode('--', $drops), explode(';', $statements), explode('/', $triggers), explode(';', $data));
|
||
|
} else {
|
||
|
$lines = explode(';', file_get_contents($fixture));
|
||
|
}
|
||
11 years ago
|
foreach ($lines as $line) {
|
||
|
if (trim($line) !== '') {
|
||
|
$db->pdo->exec($line);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return $db;
|
||
|
}
|
||
12 years ago
|
}
|