RedisTestCase.php 1.25 KB
Newer Older
1 2
<?php

3
namespace yiiunit\framework\redis;
4

5
use yii\redis\Connection;
6 7
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
	{
Carsten Brandt committed
15 16 17 18
		$this->mockApplication();

		$databases = $this->getParam('databases');
		$params = isset($databases['redis']) ? $databases['redis'] : null;
19 20 21 22 23 24 25 26 27 28 29 30 31
		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();
32 33 34 35 36 37
	}

	/**
	 * @param bool $reset whether to clean up the test database
	 * @return Connection
	 */
38
	public function getConnection($reset = true)
39
	{
Carsten Brandt committed
40 41
		$databases = $this->getParam('databases');
		$params = isset($databases['redis']) ? $databases['redis'] : array();
42
		$db = new Connection;
43 44 45
		$db->dsn = $params['dsn'];
		$db->password = $params['password'];
		if ($reset) {
46 47
			$db->open();
			$db->flushall();
48 49 50 51
		}
		return $db;
	}
}