RedisTestCase.php 1.26 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace yiiunit\framework\db\redis;

use yii\db\redis\Connection;
use yiiunit\TestCase;

8 9 10
/**
 * RedisTestCase is the base class for all redis related test cases
 */
11 12
class RedisTestCase extends TestCase
{
13
	protected function setUp()
14
	{
15 16 17 18 19 20 21 22 23 24 25 26 27 28
		$params = $this->getParam('redis');
		if ($params === null || !isset($params['dsn'])) {
			$this->markTestSkipped('No redis server connection configured.');
		}
		$dsn = explode('/', $params['dsn']);
		$host = $dsn[2];
		if (strpos($host, ':')===false) {
			$host .= ':6379';
		}
		if(!@stream_socket_client($host, $errorNumber, $errorDescription, 0.5)) {
			$this->markTestSkipped('No redis server running at ' . $params['dsn'] . ' : ' . $errorNumber . ' - ' . $errorDescription);
		}

		parent::setUp();
29 30 31 32 33 34
	}

	/**
	 * @param bool $reset whether to clean up the test database
	 * @return Connection
	 */
35
	public function getConnection($reset = true)
36 37 38 39 40 41
	{
		$params = $this->getParam('redis');
		$db = new \yii\db\redis\Connection;
		$db->dsn = $params['dsn'];
		$db->password = $params['password'];
		if ($reset) {
42 43 44
			$db->open();
			$db->flushall();
/*			$lines = explode(';', file_get_contents($params['fixture']));
45 46 47 48 49 50 51 52 53
			foreach ($lines as $line) {
				if (trim($line) !== '') {
					$db->pdo->exec($line);
				}
			}*/
		}
		return $db;
	}
}