Inital commit for unit test configuration

This commit is contained in:
Manish Verma
2018-09-05 12:36:46 +05:30
committed by Manish Verma
parent 2f0796e954
commit e0436b7757
252 changed files with 3219 additions and 1631 deletions

View File

@@ -32,11 +32,6 @@ abstract class AbstractRedisSessionHandlerTestCase extends TestCase
*/
protected $redisClient;
/**
* @var \Redis
*/
protected $validator;
/**
* @return \Redis|\RedisArray|\RedisCluster|\Predis\Client
*/
@@ -52,9 +47,6 @@ abstract class AbstractRedisSessionHandlerTestCase extends TestCase
$host = getenv('REDIS_HOST') ?: 'localhost';
$this->validator = new \Redis();
$this->validator->connect($host);
$this->redisClient = $this->createRedisClient($host);
$this->storage = new RedisSessionHandler(
$this->redisClient,
@@ -82,8 +74,8 @@ abstract class AbstractRedisSessionHandlerTestCase extends TestCase
public function testReadSession()
{
$this->setFixture(self::PREFIX.'id1', null);
$this->setFixture(self::PREFIX.'id2', 'abc123');
$this->redisClient->set(self::PREFIX.'id1', null);
$this->redisClient->set(self::PREFIX.'id2', 'abc123');
$this->assertEquals('', $this->storage->read('id1'));
$this->assertEquals('abc123', $this->storage->read('id2'));
@@ -93,14 +85,14 @@ abstract class AbstractRedisSessionHandlerTestCase extends TestCase
{
$this->assertTrue($this->storage->write('id', 'data'));
$this->assertTrue($this->hasFixture(self::PREFIX.'id'));
$this->assertEquals('data', $this->getFixture(self::PREFIX.'id'));
$this->assertTrue((bool) $this->redisClient->exists(self::PREFIX.'id'));
$this->assertEquals('data', $this->redisClient->get(self::PREFIX.'id'));
}
public function testUseSessionGcMaxLifetimeAsTimeToLive()
{
$this->storage->write('id', 'data');
$ttl = $this->fixtureTtl(self::PREFIX.'id');
$ttl = $this->redisClient->ttl(self::PREFIX.'id');
$this->assertLessThanOrEqual(ini_get('session.gc_maxlifetime'), $ttl);
$this->assertGreaterThanOrEqual(0, $ttl);
@@ -108,11 +100,11 @@ abstract class AbstractRedisSessionHandlerTestCase extends TestCase
public function testDestroySession()
{
$this->setFixture(self::PREFIX.'id', 'foo');
$this->redisClient->set(self::PREFIX.'id', 'foo');
$this->assertTrue($this->hasFixture(self::PREFIX.'id'));
$this->assertTrue((bool) $this->redisClient->exists(self::PREFIX.'id'));
$this->assertTrue($this->storage->destroy('id'));
$this->assertFalse($this->hasFixture(self::PREFIX.'id'));
$this->assertFalse((bool) $this->redisClient->exists(self::PREFIX.'id'));
}
public function testGcSession()
@@ -122,12 +114,12 @@ abstract class AbstractRedisSessionHandlerTestCase extends TestCase
public function testUpdateTimestamp()
{
$lowTTL = 10;
$lowTtl = 10;
$this->setFixture(self::PREFIX.'id', 'foo', $lowTTL);
$this->redisClient->setex(self::PREFIX.'id', $lowTtl, 'foo');
$this->storage->updateTimestamp('id', array());
$this->assertGreaterThan($lowTTL, $this->fixtureTtl(self::PREFIX.'id'));
$this->assertGreaterThan($lowTtl, $this->redisClient->ttl(self::PREFIX.'id'));
}
/**
@@ -150,28 +142,4 @@ abstract class AbstractRedisSessionHandlerTestCase extends TestCase
array(array('prefix' => 'sfs', 'foo' => 'bar'), false),
);
}
protected function setFixture($key, $value, $ttl = null)
{
if (null !== $ttl) {
$this->validator->setex($key, $ttl, $value);
} else {
$this->validator->set($key, $value);
}
}
protected function getFixture($key)
{
return $this->validator->get($key);
}
protected function hasFixture($key): bool
{
return $this->validator->exists($key);
}
protected function fixtureTtl($key): int
{
return $this->validator->ttl($key);
}
}

View File

@@ -17,6 +17,6 @@ class PredisClusterSessionHandlerTest extends AbstractRedisSessionHandlerTestCas
{
protected function createRedisClient(string $host): Client
{
return new Client(array(array('host' => $host)));
return new Client(array(array('host' => $host)));
}
}

View File

@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
class RedisClusterSessionHandlerTest extends AbstractRedisSessionHandlerTestCase
{
public static function setupBeforeClass()
{
if (!class_exists('RedisCluster')) {
self::markTestSkipped('The RedisCluster class is required.');
}
if (!$hosts = getenv('REDIS_CLUSTER_HOSTS')) {
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
}
}
protected function createRedisClient(string $host): \RedisCluster
{
return new \RedisCluster(null, explode(' ', getenv('REDIS_CLUSTER_HOSTS')));
}
}