Skip to content

[HttpFoundation] Add native handlers for Memcache and Redis #48588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add some tests to the handlers
  • Loading branch information
mevdschee committed Dec 11, 2022
commit 43c1f64d0cae57d5ae7742cd3ba43863816a32ca
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeMemcachedSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;

/**
* Test class for NativeMemcachedSessionHandler.
*
* @author Maurits van der Schee <maurits@vdschee.nl>
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class NativeMemcachedSessionHandlerTest extends TestCase
{
/**
* @dataProvider savePathDataProvider
*/
public function testConstruct(string $savePath, array $sessionOptions, string $expectedSavePath, string $expectedSessionName)
{
ini_set('session.save_path', '/var/lib/php/sessions');
ini_set('session.name', 'PHPSESSID');

new NativeSessionStorage($sessionOptions, new NativeMemcachedSessionHandler($savePath, $sessionOptions));

$this->assertEquals($expectedSessionName, ini_get('session.name'));
$this->assertEquals($expectedSavePath, ini_get('session.save_path'));
$this->assertTrue((bool) ini_get('memcached.sess_locking'));
}

public function savePathDataProvider()
{
return [
['localhost:11211', ['name' => 'TESTING'], 'localhost:11211', 'TESTING'],
['', ['name' => 'TESTING'], '', 'TESTING'],
['', [], '', 'PHPSESSID'],
];
}

public function testConstructDefault()
{
ini_set('session.save_path', 'localhost:11211');
ini_set('session.name', 'TESTING');

new NativeSessionStorage([], new NativeMemcachedSessionHandler());

$this->assertEquals('localhost:11211', ini_get('session.save_path'));
$this->assertEquals('TESTING', ini_get('session.name'));
$this->assertTrue((bool) ini_get('memcached.sess_locking'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeRedisSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;

/**
* Test class for NativeRedisSessionHandler.
*
* @author Maurits van der Schee <maurits@vdschee.nl>
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class NativeRedisSessionHandlerTest extends TestCase
{
/**
* @dataProvider savePathDataProvider
*/
public function testConstruct(string $savePath, array $sessionOptions, string $expectedSavePath, string $expectedSessionName)
{
ini_set('session.save_path', '/var/lib/php/sessions');
ini_set('session.name', 'PHPSESSID');

new NativeSessionStorage($sessionOptions, new NativeRedisSessionHandler($savePath, $sessionOptions));

$this->assertEquals($expectedSessionName, ini_get('session.name'));
$this->assertEquals($expectedSavePath, ini_get('session.save_path'));
$this->assertTrue((bool) ini_get('redis.session.locking_enabled'));
}

public function savePathDataProvider()
{
return [
['tcp://localhost:6379', [], 'tcp://localhost:6379?prefix=PHPREDIS_SESSION.PHPSESSID.', 'PHPSESSID'],
['tcp://localhost:6379', ['name' => 'TESTING'], 'tcp://localhost:6379?prefix=PHPREDIS_SESSION.TESTING.', 'TESTING'],
['tcp://localhost:6379?prefix=CUSTOM.', ['name' => 'TESTING'], 'tcp://localhost:6379?prefix=CUSTOM.', 'TESTING'],
['tcp://localhost:6379?prefix=CUSTOM.&prefix=CUSTOM2.', ['name' => 'TESTING'], 'tcp://localhost:6379?prefix=CUSTOM2.', 'TESTING'],
];
}

public function testConstructDefault()
{
ini_set('session.save_path', 'tcp://localhost:6379');
ini_set('session.name', 'TESTING');

new NativeSessionStorage([], new NativeRedisSessionHandler());

$this->assertEquals('tcp://localhost:6379?prefix=PHPREDIS_SESSION.TESTING.', ini_get('session.save_path'));
$this->assertEquals('TESTING', ini_get('session.name'));
$this->assertTrue((bool) ini_get('redis.session.locking_enabled'));
}
}