8000 Merge branch '4.1' · symfony/symfony@ff1727e · GitHub
[go: up one dir, main page]

Skip to content

Commit ff1727e

Browse files
Merge branch '4.1'
* 4.1: [HttpFoundation] cleanup test case [HttpFoundation] Allow RedisCluster class for RedisSessionHandler
2 parents 9ef362e + 620dfde commit ff1727e

File tree

5 files changed

+53
-48
lines changed

5 files changed

+53
-48
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ public function registerAliasForArgument(string $id, string $type, string $name
13501350
$name = lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $name ?? $id))));
13511351

13521352
if (!preg_match('/^[a-zA-Z_\x7f-\xff]/', $name)) {
1353-
throw new \InvalidArgumentException(sprintf('Invalid argument name "%s" for service "%s": the first character must be a letter.', $name, $id));
1353+
throw new InvalidArgumentException(sprintf('Invalid argument name "%s" for service "%s": the first character must be a letter.', $name, $id));
13541354
}
13551355

13561356
return $this->setAlias($type.' $'.$name, $id);

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,20 @@ class RedisSessionHandler extends AbstractSessionHandler
3333
* List of available options:
3434
* * prefix: The prefix to use for the keys in order to avoid collision on the Redis server.
3535
*
36-
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redis
37-
* @param array $options An associative array of options
36+
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client|RedisProxy $redis
37+
* @param array $options An associative array of options
3838
*
3939
* @throws \InvalidArgumentException When unsupported client or options are passed
4040
*/
4141
public function __construct($redis, array $options = array())
4242
{
43-
if (!$redis instanceof \Redis && !$redis instanceof \RedisArray && !$redis instanceof \Predis\Client && !$redis instanceof RedisProxy) {
43+
if (
44+
!$redis instanceof \Redis &&
45+
!$redis instanceof \RedisArray &&
46+
!$redis instanceof \RedisCluster &&
47+
!$redis instanceof \Predis\Client &&
48+
!$redis instanceof RedisProxy
49+
) {
4450
throw new \InvalidArgumentException(sprintf('%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given', __METHOD__, \is_object($redis) ? \get_class($redis) : \gettype($redis)));
4551
}
4652

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/AbstractRedisSessionHandlerTestCase.php

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ abstract class AbstractRedisSessionHandlerTestCase extends TestCase
3232
*/
3333
protected $redisClient;
3434

35-
/**
36-
* @var \Redis
37-
*/
38-
protected $validator;
39-
4035
/**
4136
* @return \Redis|\RedisArray|\RedisCluster|\Predis\Client
4237
*/
@@ -52,9 +47,6 @@ protected function setUp()
5247

5348
$host = getenv('REDIS_HOST') ?: 'localhost';
5449

55-
$this->validator = new \Redis();
56-
$this->validator->connect($host);
57-
5850
$this->redisClient = $this->createRedisClient($host);
5951
$this->storage = new RedisSessionHandler(
6052
$this->redisClient,
@@ -82,8 +74,8 @@ public function testCloseSession()
8274

8375
public function testReadSession()
8476
{
85-
$this->setFixture(self::PREFIX.'id1', null);
86-
$this->setFixture(self::PREFIX.'id2', 'abc123');
77+
$this->redisClient->set(self::PREFIX.'id1', null);
78+
$this->redisClient->set(self::PREFIX.'id2', 'abc123');
8779

8880
$this->assertEquals('', $this->storage->read('id1'));
8981
$this->assertEquals('abc123', $this->storage->read('id2'));
@@ -93,26 +85,26 @@ public function testWriteSession()
9385
{
9486
$this->assertTrue($this->storage->write('id', 'data'));
9587

96-
$this->assertTrue($this->hasFixture(self::PREFIX.'id'));
97-
$this->assertEquals('data', $this->getFixture(self::PREFIX.'id'));
88+
$this->assertTrue((bool) $this->redisClient->exists(self::PREFIX.'id'));
89+
$this->assertEquals('data', $this->redisClient->get(self::PREFIX.'id'));
9890
}
9991

10092
public function testUseSessionGcMaxLifetimeAsTimeToLive()
10193
{
10294
$this->storage->write('id', 'data');
103-
$ttl = $this->fixtureTtl(self::PREFIX.'id');
95+
$ttl = $this->redisClient->ttl(self::PREFIX.'id');
10496

10597
$this->assertLessThanOrEqual(ini_get('session.gc_maxlifetime'), $ttl);
10698
$this->assertGreaterThanOrEqual(0, $ttl);
10799
}
108100

109101
public function testDestroySession()
110102
{
111-
$this->setFixture(self::PREFIX.'id', 'foo');
103+
$this->redisClient->set(self::PREFIX.'id', 'foo');
112104

113-
$this->assertTrue($this->hasFixture(self::PREFIX.'id'));
105+
$this->assertTrue((bool) $this->redisClient->exists(self::PREFIX.'id'));
114106
$this->assertTrue($this->storage->destroy('id'));
115-
$this->assertFalse($this->hasFixture(self::PREFIX.'id'));
107+
$this->assertFalse((bool) $this->redisClient->exists(self::PREFIX.'id'));
116108
}
117109

118110
public function testGcSession()
@@ -122,12 +114,12 @@ public function testGcSession()
122114

123115
public function testUpdateTimestamp()
124116
{
125-
$lowTTL = 10;
117+
$lowTtl = 10;
126118

127-
$this->setFixture(self::PREFIX.'id', 'foo', $lowTTL);
119+
$this->redisClient->setex(self::PREFIX.'id', $lowTtl, 'foo');
128120
$this->storage->updateTimestamp('id', array());
129121

130-
$this->assertGreaterThan($lowTTL, $this->fixtureTtl(self::PREFIX.'id'));
122+
$this->assertGreaterThan($lowTtl, $this->redisClient->ttl(self::PREFIX.'id'));
131123
}
132124

133125
/**
@@ -150,28 +142,4 @@ public function getOptionFixtures(): array
150142
array(array('prefix' => 'sfs', 'foo' => 'bar'), false),
151143
);
152144
}
153-
154-
protected function setFixture($key, $value, $ttl = null)
155-
{
156-
if (null !== $ttl) {
157-
$this->validator->setex($key, $ttl, $value);
158-
} else {
159-
$this->validator->set($key, $value);
160-
}
161-
}
162-
163-
protected function getFixture($key)
164-
{
165-
return $this->validator->get($key);
166-
}
167-
168-
protected function hasFixture($key): bool
169-
{
170-
return $this->validator->exists($key);
171-
}
172-
173-
protected function fixtureTtl($key): int
174-
{
175-
return $this->validator->ttl($key);
176-
}
177145
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisClusterSessionHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ class PredisClusterSessionHandlerTest extends AbstractRedisSessionHandlerTestCas
1717
{
1818
protected function createRedisClient(string $host): Client
1919
{
20-
return new Client(array(array('host' => $host)));
20+
return new Client(array(array('host' => $host)));
2121
}
2222
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
13+
14+
class RedisClusterSessionHandlerTest extends AbstractRedisSessionHandlerTestCase
15+
{
16+
public static function setupBeforeClass()
17+
{
18+
if (!class_exists('RedisCluster')) {
19+
self::markTestSkipped('The RedisCluster class is required.');
20+
}
21+
22+
if (!$hosts = getenv('REDIS_CLUSTER_HOSTS')) {
23+
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
24+
}
25+
}
26+
27+
protected function createRedisClient(string $host): \RedisCluster
28+
{
29+
return new \RedisCluster(null, explode(' ', getenv('REDIS_CLUSTER_HOSTS')));
30+
}
31+
}

0 commit comments

Comments
 (0)
0