MysqlTestCase.php 794 Bytes
Newer Older
Qiang Xue committed
1 2 3 4 5 6
<?php

namespace yiiunit;

class MysqlTestCase extends TestCase
{
7
	protected function setUp()
Qiang Xue committed
8
	{
Qiang Xue committed
9
		if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
Qiang Xue committed
10 11 12 13 14 15
			$this->markTestSkipped('pdo and pdo_mysql extensions are required.');
		}
	}

	/**
	 * @param bool $reset whether to clean up the test database
Qiang Xue committed
16
	 * @return \yii\db\Connection
Qiang Xue committed
17
	 */
18
	public function getConnection($reset = true)
Qiang Xue committed
19 20
	{
		$params = $this->getParam('mysql');
Qiang Xue committed
21
		$db = new \yii\db\Connection;
Qiang Xue committed
22 23 24
		$db->dsn = $params['dsn'];
		$db->username = $params['username'];
		$db->password = $params['password'];
Qiang Xue committed
25
		if ($reset) {
26
			$db->open();
Qiang Xue committed
27 28 29 30 31 32 33 34 35
			$lines = explode(';', file_get_contents($params['fixture']));
			foreach ($lines as $line) {
				if (trim($line) !== '') {
					$db->pdo->exec($line);
				}
			}
		}
		return $db;
	}
Zander Baldwin committed
36
}