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

3
namespace yiiunit\extensions\redis;
4

5
use Yii;
6
use yii\redis\Connection;
7 8
use yiiunit\TestCase;

Qiang Xue committed
9
Yii::setAlias('@yii/redis', __DIR__ . '/../../../../extensions/yii/redis');
10

11 12 13
/**
 * RedisTestCase is the base class for all redis related test cases
 */
14
abstract class RedisTestCase extends TestCase
15
{
16
	protected function setUp()
17
	{
Carsten Brandt committed
18 19
		$databases = $this->getParam('databases');
		$params = isset($databases['redis']) ? $databases['redis'] : null;
20
		if ($params === null) {
21 22
			$this->markTestSkipped('No redis server connection configured.');
		}
23 24 25
		$connection = new Connection($params);
		if(!@stream_socket_client($connection->hostname . ':' . $connection->port, $errorNumber, $errorDescription, 0.5)) {
			$this->markTestSkipped('No redis server running at ' . $connection->hostname . ':' . $connection->port . ' : ' . $errorNumber . ' - ' . $errorDescription);
26 27
		}

28 29
		$this->mockApplication(['components' => ['redis' => $connection]]);

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

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