RedisCacheTest.php 2.71 KB
Newer Older
1
<?php
2 3 4 5 6 7 8
namespace yiiunit\extensions\redis;

use Yii;
use yii\redis\Cache;
use yii\redis\Connection;
use yiiunit\framework\caching\CacheTestCase;

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

/**
 * Class for testing redis cache backend
13 14
 * @group redis
 * @group caching
15 16 17 18 19 20
 */
class RedisCacheTest extends CacheTestCase
{
	private $_cacheInstance = null;

	/**
21
	 * @return Cache
22 23 24
	 */
	protected function getCacheInstance()
	{
25 26 27 28
		$databases = $this->getParam('databases');
		$params = isset($databases['redis']) ? $databases['redis'] : null;
		if ($params === null) {
			$this->markTestSkipped('No redis server connection configured.');
29
		}
30 31 32 33 34 35
		$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);
		}

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

37
		if ($this->_cacheInstance === null) {
38
			$this->_cacheInstance = new Cache();
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
		}
		return $this->_cacheInstance;
	}

	public function testExpireMilliseconds()
	{
		$cache = $this->getCacheInstance();

		$this->assertTrue($cache->set('expire_test_ms', 'expire_test_ms', 0.2));
		usleep(100000);
		$this->assertEquals('expire_test_ms', $cache->get('expire_test_ms'));
		usleep(300000);
		$this->assertFalse($cache->get('expire_test_ms'));
	}

54 55 56 57 58 59 60 61 62 63 64
	public function testExpireAddMilliseconds()
	{
		$cache = $this->getCacheInstance();

		$this->assertTrue($cache->add('expire_testa_ms', 'expire_testa_ms', 0.2));
		usleep(100000);
		$this->assertEquals('expire_testa_ms', $cache->get('expire_testa_ms'));
		usleep(300000);
		$this->assertFalse($cache->get('expire_testa_ms'));
	}

65 66 67 68 69 70 71 72
	/**
	 * Store a value that is 2 times buffer size big
	 * https://github.com/yiisoft/yii2/issues/743
	 */
	public function testLargeData()
	{
		$cache = $this->getCacheInstance();

73 74
		$data = str_repeat('XX', 8192); // http://www.php.net/manual/en/function.fread.php
		$key = 'bigdata1';
75 76

		$this->assertFalse($cache->get($key));
77 78
		$cache->set($key, $data);
		$this->assertTrue($cache->get($key) === $data);
79 80

		// try with multibyte string
81 82
		$data = str_repeat('ЖЫ', 8192); // http://www.php.net/manual/en/function.fread.php
		$key = 'bigdata2';
83 84

		$this->assertFalse($cache->get($key));
85 86
		$cache->set($key, $data);
		$this->assertTrue($cache->get($key) === $data);
87 88 89 90 91 92
	}

	public function testMultiByteGetAndSet()
	{
		$cache = $this->getCacheInstance();

93 94
		$data = ['abc' => 'ежик', 2 => 'def'];
		$key = 'data1';
95 96

		$this->assertFalse($cache->get($key));
97 98
		$cache->set($key, $data);
		$this->assertTrue($cache->get($key) === $data);
99 100
	}

Qiang Xue committed
101
}