public function setUp() { if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) { $this->markTestSkipped('PDO and PostgreSQL extensions are required.'); } $this->db = new CDbConnection('pgsql:host=127.0.0.1;dbname=yii', 'test', 'test'); try { $this->db->active = true; } catch (Exception $e) { $schemaFile = realpath(dirname(__FILE__) . '/../data/postgres.sql'); $this->markTestSkipped("Please read {$schemaFile} for details on setting up the test environment for PostgreSQL test case."); } try { $this->db->createCommand('DROP SCHEMA test CASCADE')->execute(); } catch (Exception $e) { } try { $this->db->createCommand('DROP TABLE yii_types CASCADE')->execute(); } catch (Exception $e) { } $sqls = file_get_contents(dirname(__FILE__) . '/../data/postgres.sql'); foreach (explode(';', $sqls) as $sql) { if (trim($sql) !== '') { $this->db->createCommand($sql)->execute(); } } $this->db->active = false; $config = array('basePath' => dirname(__FILE__), 'components' => array('db' => array('class' => 'system.db.CDbConnection', 'connectionString' => 'pgsql:host=127.0.0.1;dbname=yii', 'username' => 'test', 'password' => 'test'))); $app = new TestApplication($config); $app->db->active = true; CActiveRecord::$db = $this->db = $app->db; }
public function setUp() { $this->getFixtureManager()->resetTable('order'); $this->getFixtureManager()->loadFixture('order'); CActiveRecord::$db = $this->db = Yii::app()->db; parent::setUp(); }
/** * set up environment with yii application and migrate up db */ public function setUp() { $basePath = dirname(__FILE__) . '/tmp'; if (!file_exists($basePath)) { mkdir($basePath, 0777, true); } if (!file_exists($basePath . '/runtime')) { mkdir($basePath . '/runtime', 0777, true); } // create webapp if (\Yii::app() === null) { \Yii::createWebApplication(array('basePath' => $basePath)); } \CActiveRecord::$db = null; if (!isset($_ENV['DB']) || $_ENV['DB'] == 'sqlite') { if (!$this->dbFile) { $this->dbFile = $basePath . '/test.' . uniqid(time()) . '.db'; } \Yii::app()->setComponent('db', new \CDbConnection('sqlite:' . $this->dbFile)); } elseif ($_ENV['DB'] == 'mysql') { \Yii::app()->setComponent('db', new \CDbConnection('mysql:dbname=test;host=localhost', 'root')); } elseif ($_ENV['DB'] == 'pgsql') { \Yii::app()->setComponent('db', new \CDbConnection('pqsql:dbname=test;host=localhost', 'postgres')); } else { throw new \Exception('Unknown db. Only sqlite, mysql and pgsql are valid.'); } // create db $this->migration = new EActiveRecordRelationBehaviorTestMigration(); $this->migration->dbConnection = \Yii::app()->db; $this->migration->up(); }
protected function setUp() { if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) { $this->markTestSkipped('PDO and SQLite extensions are required.'); } $this->_connection = new CDbConnection('sqlite::memory:'); $this->_connection->active = true; $this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__) . '/../fixtures/data/sqlite.sql')); CActiveRecord::$db = $this->_connection; }
public function setUp() { if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) { $this->markTestSkipped('PDO and SQLite extensions are required.'); } $this->db = new CDbConnection('sqlite::memory:'); $this->db->active = true; $this->db->pdoInstance->exec(file_get_contents(dirname(__FILE__) . '/CSortTest.sql')); CActiveRecord::$db = $this->db; }
public function setUp() { if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) { $this->markTestSkipped('PDO and SQLite extensions are required.'); } copy(SRC_DB_FILE, TEST_DB_FILE); $config = array('basePath' => dirname(__FILE__), 'components' => array('db' => array('class' => 'system.db.CDbConnection', 'connectionString' => 'sqlite:' . TEST_DB_FILE))); $app = new TestApplication($config); $app->db->active = true; CActiveRecord::$db = $this->db = $app->db; }
protected function setUp() { if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) { $this->markTestSkipped('PDO and SQLite extensions are required.'); } $this->_connection = new CDbConnection('sqlite::memory:'); $this->_connection->active = true; $columns = array('id' => 'pk', 'name' => 'string'); $this->_connection->createCommand()->createTable($this->_tableName, $columns); CActiveRecord::$db = $this->_connection; $this->declareArModelClass(); }
public function setup() { define('YII_DEBUG', false); $basePath = dirname(dirname(dirname(dirname(__DIR__)))); // go 4 up require $basePath . '/vendor/yiisoft/yii/framework/yii.php'; require_once "{$basePath}/app/classes/model/yii1/Comment.php"; require_once "{$basePath}/app/classes/model/yii1/Post.php"; $dbConfig = DbConfig::get_params(); $db = new CDbConnection("mysql:host={$dbConfig['host']};dbname={$dbConfig['dbname']};charset=utf8", $dbConfig['username'], $dbConfig['password']); CActiveRecord::$db = $db; }
protected function setUp() { // pdo and pdo_sqlite extensions are obligatory if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) { $this->markTestSkipped('PDO and SQLite extensions are required.'); } // open connection and create testing tables $this->_connection = new CDbConnection('sqlite::memory:'); $this->_connection->active = true; $this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__) . '/YexIPBehaviorTest.sql')); CActiveRecord::$db = $this->_connection; }
public function setUp() { if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) { $this->markTestSkipped('PDO and SQLite extensions are required.'); } $this->_connection = new CDbConnection('sqlite::memory:'); $this->_connection->active = true; $this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__) . '/../data/sqlite.sql')); CActiveRecord::$db = $this->_connection; UserWithWrappers::clearCounters(); PostWithWrappers::clearCounters(); CommentWithWrappers::clearCounters(); }
public function setUp() { if (self::DB_DSN_PREFIX == 'sqlsrv' && (!extension_loaded('pdo') || !extension_loaded('sqlsrv') || !extension_loaded('pdo_sqlsrv'))) { $this->markTestSkipped('PDO and SQLSRV extensions are required.'); } else { if (self::DB_DSN_PREFIX != 'sqlsrv' && (!extension_loaded('pdo') || !extension_loaded('pdo_dblib'))) { $this->markTestSkipped('PDO and MSSQL extensions are required.'); } } if (self::DB_DSN_PREFIX == 'sqlsrv') { $dsn = self::DB_DSN_PREFIX . ':Server=' . self::DB_HOST . ';Database=' . self::DB_NAME; } else { $dsn = self::DB_DSN_PREFIX . ':host=' . self::DB_HOST . ';dbname=' . self::DB_NAME; } $this->db = new CDbConnection($dsn, self::DB_USER, self::DB_PASS); if (self::DB_DSN_PREFIX == 'sqlsrv') { $this->db->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_SYSTEM); } try { $this->db->active = true; } catch (Exception $e) { $schemaFile = realpath(dirname(__FILE__) . '/../data/mssql.sql'); $this->markTestSkipped("Please read {$schemaFile} for details on setting up the test environment for MSSQL test case."); } $tables = array('comments', 'post_category', 'posts', 'categories', 'profiles', 'users', 'items', 'orders', 'types'); foreach ($tables as $table) { $sql = <<<EOD IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[{$table}]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1) DROP TABLE [dbo].[{$table}] EOD; $this->db->createCommand($sql)->execute(); } $rawSqls = file_get_contents(dirname(__FILE__) . '/../data/mssql.sql'); // remove comments from SQL $sqls = ''; foreach (array_filter(explode("\n", $rawSqls)) as $line) { if (substr($line, 0, 2) == '--') { continue; } $sqls .= $line . "\n"; } // run SQL foreach (explode('GO', $sqls) as $sql) { if (trim($sql) !== '') { $this->db->createCommand($sql)->execute(); } } CActiveRecord::$db = $this->db; }
/** * set up environment with yii application and migrate up db */ public function setUp() { $basePath = dirname(__FILE__) . '/tmp'; if (!file_exists($basePath)) { mkdir($basePath, 0777, true); } if (!file_exists($basePath . '/runtime')) { mkdir($basePath . '/runtime', 0777, true); } if (!$this->db) { $this->db = $basePath . '/test.' . uniqid(time()) . '.db'; } // create webapp if (\Yii::app() === null) { \Yii::createWebApplication(array('basePath' => $basePath)); } \CActiveRecord::$db = null; \Yii::app()->setComponent('db', new \CDbConnection('sqlite:' . $this->db)); // create db $this->migration = new EActiveRecordRelationBehaviorTestMigration(); $this->migration->dbConnection = \Yii::app()->db; $this->migration->up(); }
public function getDbConnection() { if (self::$db !== null) { return self::$db; } else { self::$db = Yii::app()->getDb(); if (self::$db instanceof CDbConnection) { return self::$db; } else { throw new CDbException(Yii::t('yii', 'Active Record requires a "db" CDbConnection application component.')); } } }