Yii2 framework backup
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.

124 lines
3.5 KiB

<?php
namespace yiiunit\framework\db;
use yii\db\Connection;
use yiiunit\TestCase as TestCase;
abstract class DatabaseTestCase extends TestCase
{
protected $database;
/**
* @var string the driver name of this test class. Must be set by a subclass.
*/
protected $driverName;
/**
* @var Connection
*/
private $_db;
protected function setUp()
{
if ($this->driverName === null) {
throw new \Exception('driverName is not set for a DatabaseTestCase.');
}
parent::setUp();
$databases = self::getParam('databases');
$this->database = $databases[$this->driverName];
$pdo_database = 'pdo_'.$this->driverName;
if ($this->driverName === 'oci') {
$pdo_database = 'oci8';
}
if (!extension_loaded('pdo') || !extension_loaded($pdo_database)) {
$this->markTestSkipped('pdo and '.$pdo_database.' extension are required.');
}
$this->mockApplication();
}
protected function tearDown()
{
if ($this->_db) {
$this->_db->close();
}
$this->destroyApplication();
}
/**
* @param bool $reset whether to clean up the test database
* @param bool $open whether to open and populate test database
* @return \yii\db\Connection
*/
public function getConnection($reset = true, $open = true)
{
if (!$reset && $this->_db) {
return $this->_db;
}
10 years ago
$config = $this->database;
if (isset($config['fixture'])) {
$fixture = $config['fixture'];
unset($config['fixture']);
} else {
$fixture = null;
}
try {
$this->_db = $this->prepareDatabase($config, $fixture, $open);
} catch (\Exception $e) {
$this->markTestSkipped("Something wrong when preparing database: " . $e->getMessage());
}
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';
}
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) {
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));
}
foreach ($lines as $line) {
if (trim($line) !== '') {
$db->pdo->exec($line);
}
}
}
return $db;
}
/**
* adjust dbms specific escaping
* @param $sql
* @return mixed
*/
protected function replaceQuotes($sql)
{
switch ($this->driverName) {
case 'mysql':
case 'sqlite':
return str_replace(['[[', ']]'], '`', $sql);
case 'cubrid':
case 'pgsql':
case 'oci':
return str_replace(['[[', ']]'], '"', $sql);
case 'sqlsrv':
return str_replace(['[[', ']]'], ['[', ']'], $sql);
default:
return $sql;
}
}
}