Skip to content

Commit

Permalink
[#13439] - Cache tests and Session refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed May 9, 2019
1 parent 3d4e572 commit 5b61541
Show file tree
Hide file tree
Showing 43 changed files with 2,607 additions and 78 deletions.
89 changes: 43 additions & 46 deletions phalcon/Session/Adapter/Libmemcached.zep
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

namespace Phalcon\Session\Adapter;

use Phalcon\Cache\Backend\Libmemcached as CacheLibmemcached;
use Phalcon\Cache\Frontend\Data as FrontendData;
use Phalcon\Helper\Arr;
use Phalcon\Session\Exception;
use Phalcon\Storage\Adapter\Libmemcached as StorageLibmemcached;
use SessionHandlerInterface;

/**
* Phalcon\Session\Adapter\Noop
* Phalcon\Session\Adapter\Libmemcached
*
* This is an "empty" or null adapter. It can be used for testing or any
* other purpose that no session needs to be invoked
Expand Down Expand Up @@ -49,68 +47,67 @@ use Phalcon\Session\Exception;
* $session->setHandler($adapter);
* </code>
*/
class Libmemcached extends Noop
class Libmemcached extends StorageLibmemcached implements SessionHandlerInterface
{
/**
* Constructor
*/
public function __construct(array! options = []) -> void
{
var client, options, persistentId, prefix, servers, statsKey, ttl;
let options["prefix"] = "sess-memc-";

parent::__construct(options);
}

let options = this->options;

if unlikely !fetch servers, options["servers"] {
throw new Exception("No 'servers' specified in the options");
}

let client = Arr::get(options, "client", []),
ttl = Arr::get(options, "ttl", this->ttl),
statsKey = Arr::get(options, "statsKey", ""),
persistentId = Arr::get(options, "persistent_id", "phalcon-session");


// Memcached has an internal max lifetime of 30 days
let this->ttl = min(ttl, 2592000);

let this->connection = new CacheLibmemcached(
new FrontendData(
[
"lifetime" : this->ttl
]
),
[
"servers" : servers,
"client" : client,
"prefix" : prefix,
"statsKey" : statsKey,
"persistent_id" : persistentId
]
);
/**
* Close
*/
public function close() -> bool
{
return true;
}

/**
* Destroy
*/
public function destroy(var id) -> bool
{
var name = this->getPrefixedName(id);

if (true !== empty(name) && this->connection->exists(name)) {
return (bool) this->connection->delete(name);
if !empty(id) && this->has(id) {
return this->delete(id);
}

return true;
}

/**
* Garbage Collector
*/
public function gc(var maxlifetime) -> bool
{
return true;
}

/**
* Read
*/
public function read(var id) -> string
{
var name = this->getPrefixedName(id),
data = this->connection->get(name, this->ttl);
return this->get(id);
}

return data;
/**
* Open
*/
public function open(var savePath, var sessionName) -> bool
{
return true;
}

/**
* Write
*/
public function write(var id, var data) -> bool
{
var name = this->getPrefixedName(id);

return this->connection->save(name, data, this->ttl);
return this->set(id, data);
}
}
70 changes: 40 additions & 30 deletions phalcon/Session/Adapter/Redis.zep
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

namespace Phalcon\Session\Adapter;

use Phalcon\Cache\Backend\Redis as CacheRedis;
use Phalcon\Cache\Frontend\None as FrontendNone;
use Phalcon\Storage\Adapter\Redis as StorageRedis;
use Phalcon\Helper\Arr;
use Phalcon\Session\Exception;
use SessionHandlerInterface;

/**
* Phalcon\Session\Adapter\Noop
Expand Down Expand Up @@ -40,55 +41,64 @@ use Phalcon\Helper\Arr;
* $session->setHandler($adapter);
* </code>
*/
class Redis extends Noop
class Redis extends StorageRedis implements SessionHandlerInterface
{
public function __construct(array! options = []) -> void
{
var options, params;
let options["prefix"] = "sess-reds-";

parent::__construct(options);
}

let options = this->options,
params = [],
params["host"] = Arr::get(options, "host", "127.0.0.1"),
params["port"] = Arr::get(options, "port", 6379),
params["index"] = Arr::get(options, "index", 0),
params["persistent"] = Arr::get(options, "persistent", false),
this->ttl = Arr::get(options, "ttl", this->ttl);

let this->connection = new CacheRedis(
new FrontendNone(
[
"lifetime" : this->ttl
]
),
params
);
/**
* Close
*/
public function close() -> bool
{
return true;
}

/**
* Destroy
*/
public function destroy(var id) -> bool
{
var name = this->getPrefixedName(id);

if (true !== empty(name) && this->connection->exists(name)) {
return (bool) this->connection->delete(name);
if !empty(id) && this->has(id) {
return this->delete(id);
}

return true;
}

/**
* Garbage Collector
*/
public function gc(var maxlifetime) -> bool
{
return true;
}

/**
* Read
*/
public function read(var id) -> string
{
var name = this->getPrefixedName(id),
data = this->connection->get(name, this->ttl);
return this->get(id);
}

return data;
/**
* Open
*/
public function open(var savePath, var sessionName) -> bool
{
return true;
}

/**
* Write
*/
public function write(var id, var data) -> bool
{
var name = this->getPrefixedName(id);

return this->connection->save(name, data, this->ttl);
return this->set(id, data);
}
}
88 changes: 88 additions & 0 deletions tests/unit/Cache/Adapter/Apcu/ClearCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
declare(strict_types=1);

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Unit\Cache\Adapter\Apcu;

use Phalcon\Cache\Adapter\Apcu;
use Phalcon\Test\Fixtures\Traits\ApcuTrait;
use UnitTester;

/**
* Class ClearCest
*/
class ClearCest
{
use ApcuTrait;

/**
* Tests Phalcon\Cache\Adapter\Apcu :: clear()
*
* @param UnitTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2019-03-31
*/
public function storageAdapterApcuClear(UnitTester $I)
{
$I->wantToTest('Cache\Adapter\Apcu - clear()');
$adapter = new Apcu();

$key1 = uniqid();
$key2 = uniqid();
$adapter->set($key1, 'test');
$actual = $adapter->has($key1);
$I->assertTrue($actual);

$adapter->set($key2, 'test');
$actual = $adapter->has($key2);
$I->assertTrue($actual);

$actual = $adapter->clear();
$I->assertTrue($actual);

$actual = $adapter->has($key1);
$I->assertFalse($actual);

$actual = $adapter->has($key2);
$I->assertFalse($actual);
}

/**
* Tests Phalcon\Cache\Adapter\Apcu :: clear() - twice
*
* @param UnitTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2019-03-31
*/
public function storageAdapterApcuClearTwice(UnitTester $I)
{
$I->wantToTest('Cache\Adapter\Apcu - clear() - twice');
$adapter = new Apcu();

$key1 = uniqid();
$key2 = uniqid();
$adapter->set($key1, 'test');
$actual = $adapter->has($key1);
$I->assertTrue($actual);

$adapter->set($key2, 'test');
$actual = $adapter->has($key2);
$I->assertTrue($actual);

$actual = $adapter->clear();
$I->assertTrue($actual);

$actual = $adapter->clear();
$I->assertTrue($actual);
}
}
46 changes: 46 additions & 0 deletions tests/unit/Cache/Adapter/Apcu/ConstructCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Unit\Cache\Adapter\Apcu;

use Phalcon\Cache\Adapter\Apcu;
use Phalcon\Storage\Adapter\AdapterInterface;
use Phalcon\Test\Fixtures\Traits\ApcuTrait;
use UnitTester;

/**
* Class ConstructCest
*/
class ConstructCest
{
use ApcuTrait;

/**
* Tests Phalcon\Cache\Adapter\Apcu :: __construct()
*
* @param UnitTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2019-04-09
*/
public function storageAdapterApcuConstruct(UnitTester $I)
{
$I->wantToTest('Cache\Adapter\Apcu - __construct()');
$adapter = new Apcu();

$class = Apcu::class;
$I->assertInstanceOf($class, $adapter);

$class = AdapterInterface::class;
$I->assertInstanceOf($class, $adapter);
}
}
Loading

0 comments on commit 5b61541

Please sign in to comment.