MysqlTestCase.php 813 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
	{
Carsten Brandt committed
9
		parent::setUp();
Qiang Xue committed
10
		if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
Qiang Xue committed
11 12 13 14 15 16
			$this->markTestSkipped('pdo and pdo_mysql extensions are required.');
		}
	}

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