diff --git a/CHANGELOG-5.0.md b/CHANGELOG-5.0.md index 005da9b5c40..4b3d3ad5333 100644 --- a/CHANGELOG-5.0.md +++ b/CHANGELOG-5.0.md @@ -19,17 +19,6 @@ - Changes to `Phalcon\Crypt` - Moved `Phalcon\Crypt\Exception` to `Phalcon\Crypt\Exception\Exception` - Moved `Phalcon\Crypt\Mismatch` to `Phalcon\Crypt\Exception\Mismatch` - - Added `Phalcon\Crypt\Padding\PadInteface` and padding adapters - - `Phalcon\Crypt\Padding\Ansi` - - `Phalcon\Crypt\Padding\Iso10126` - - `Phalcon\Crypt\Padding\IsoIek` - - `Phalcon\Crypt\Padding\Noop` - - `Phalcon\Crypt\Padding\PadInterface` - - `Phalcon\Crypt\Padding\Pkcs7` - - `Phalcon\Crypt\Padding\Space` - - `Phalcon\Crypt\Padding\Zero` - - Added `Phalcon\Crypt\PadFactory` to easily create padding adapters - - Added more tests increasing coverage - Changed the ccm/gcm modes to store the `authTag` with the encryption string and process it with the decryption string [#15717](https://github.com/phalcon/cphalcon/issues/15717) - Created new namespace `Phalcon\Encryption` - Moved `Phalcon\Crypt` to `Phalcon\Encryption\Crypt` @@ -86,6 +75,19 @@ - Added more tests in the suite for additional code coverage [#15691](https://github.com/phalcon/cphalcon/issues/15691) - Added `Phalcon\Events\AbstractEventsAware` class to handle the Events Manager when necessary [#15691](https://github.com/phalcon/cphalcon/issues/15691) - Added `Phalcon\Acl\Adapter\AdapterInterface::getInheritedRoles()` and `Phalcon\Acl\Adapter\Memory::getInheritedRoles()` that returns the inherited roles based on a passed role name (or all if no parameter supplied) [#15154](https://github.com/phalcon/cphalcon/issues/15154) +- Changes to `Phalcon\Crypt` + - Added `Phalcon\Crypt\Padding\PadInteface` and padding adapters + - `Phalcon\Crypt\Padding\Ansi` + - `Phalcon\Crypt\Padding\Iso10126` + - `Phalcon\Crypt\Padding\IsoIek` + - `Phalcon\Crypt\Padding\Noop` + - `Phalcon\Crypt\Padding\PadInterface` + - `Phalcon\Crypt\Padding\Pkcs7` + - `Phalcon\Crypt\Padding\Space` + - `Phalcon\Crypt\Padding\Zero` + - Added `Phalcon\Crypt\PadFactory` to easily create padding adapters + - Added more tests increasing coverage [#15717](https://github.com/phalcon/cphalcon/issues/15717) +- Added `Phalcon\Cache\Adapter\*::setForever()` and `Phalcon\Storage\Adapter\*::setForever()` to allow storing a key forever [#15485](https://github.com/phalcon/cphalcon/issues/15485) ## Fixed - Fixed `Query::getExpression()` return type [#15553](https://github.com/phalcon/cphalcon/issues/15553) @@ -97,6 +99,7 @@ - Fixed globals (Zephir change) to correctly display string values for global settings in `phpinfo()` [#15269](https://github.com/phalcon/cphalcon/issues/15269) - Fixed `Phalcon\Storage\Adapter\Redis::getAdapter()` and `Phalcon\Cache\Adapter\Redis::getAdapter()` to accept the connection timeout in the constructor `options` [#15744](https://github.com/phalcon/cphalcon/issues/15744) - Fixed `Phalcon\Db\Adapter\AbstractAdapter::getSQLVariables()` to return an empty array when initialized [#15637](https://github.com/phalcon/cphalcon/issues/15637) +- Fixed `Phalcon\Cache\Adapter\*` and `Phalcon\Storage\Adapter\*` to delete a key when `set()` is called with a zero or negative TTL [#15485](https://github.com/phalcon/cphalcon/issues/15485) # [5.0.0alpha6](https://github.com/phalcon/cphalcon/releases/tag/v5.0.0alpha6) (2021-09-16) diff --git a/phalcon/Storage/Adapter/AdapterInterface.zep b/phalcon/Storage/Adapter/AdapterInterface.zep index 95086857162..1bda93a2013 100644 --- a/phalcon/Storage/Adapter/AdapterInterface.zep +++ b/phalcon/Storage/Adapter/AdapterInterface.zep @@ -69,11 +69,28 @@ interface AdapterInterface public function increment(string! key, int value = 1) -> int | bool; /** - * Stores data in the adapter + * Stores data in the adapter. If the TTL is `null` (default) or not defined + * then the default TTL will be used, as set in this adapter. If the TTL + * is `0` or a negative number, a `delete()` will be issued, since this + * item has expired. If you need to set this key forever, you should use + * the `setForever()` method. * - * @param string key - * @param mixed value - * @param \DateInterval|int|null ttl + * @param string $key + * @param mixed $value + * @param \DateInterval|int|null $ttl + * + * @return bool */ public function set(string! key, var value, var ttl = null) -> bool; + + /** + * Stores data in the adapter forever. The key needs to manually deleted + * from the adapter. + * + * @param string $key + * @param mixed $value + * + * @return bool + */ + public function setForever(string key, value) -> bool; } diff --git a/phalcon/Storage/Adapter/Apcu.zep b/phalcon/Storage/Adapter/Apcu.zep index 4aeecee5193..0eea3249156 100644 --- a/phalcon/Storage/Adapter/Apcu.zep +++ b/phalcon/Storage/Adapter/Apcu.zep @@ -169,9 +169,9 @@ class Apcu extends AbstractAdapter /** * Stores data in the adapter * - * @param string $key - * @param mixed $value - * @param DateInterval|int|null $ttl + * @param string $key + * @param mixed $value + * @param \DateInterval|int|null $ttl * * @return bool * @throws Exception @@ -180,6 +180,10 @@ class Apcu extends AbstractAdapter { var result; + if (typeof ttl === "integer" && ttl < 1) { + return this->delete(key); + } + let result = apcu_store( this->getPrefixedKey(key), this->getSerializedData(value), @@ -188,4 +192,25 @@ class Apcu extends AbstractAdapter return typeof result === "bool" ? result : false; } + + /** + * Stores data in the adapter forever. The key needs to manually deleted + * from the adapter. + * + * @param string $key + * @param mixed $value + * + * @return bool + */ + public function setForever(string! key, var value) -> bool + { + var result; + + let result = apcu_store( + this->getPrefixedKey(key), + this->getSerializedData(value) + ); + + return typeof result === "bool" ? result : false; + } } diff --git a/phalcon/Storage/Adapter/Libmemcached.zep b/phalcon/Storage/Adapter/Libmemcached.zep index af7c966afd7..23b6c16c4d2 100644 --- a/phalcon/Storage/Adapter/Libmemcached.zep +++ b/phalcon/Storage/Adapter/Libmemcached.zep @@ -205,9 +205,9 @@ class Libmemcached extends AbstractAdapter /** * Stores data in the adapter * - * @param string $key - * @param mixed $value - * @param DateInterval|int|null $ttl + * @param string $key + * @param mixed $value + * @param \DateInterval|int|null $ttl * * @return bool * @throws BaseException @@ -215,13 +215,40 @@ class Libmemcached extends AbstractAdapter */ public function set(string key, var value, var ttl = null) -> bool { - return this->getAdapter() - ->set( - key, - this->getSerializedData(value), - this->getTtl(ttl) - ) + var result; + + if (typeof ttl === "integer" && ttl < 1) { + return this->delete(key); + } + + let result = this->getAdapter() + ->set( + key, + this->getSerializedData(value), + this->getTtl(ttl) + ) ; + + return typeof result === "bool" ? result : false; + } + + /** + * Stores data in the adapter forever. The key needs to manually deleted + * from the adapter. + * + * @param string $key + * @param mixed $value + * + * @return bool + */ + public function setForever(string key, var value) -> bool + { + var result; + + let result = this->getAdapter() + ->set(key, this->getSerializedData(value), 0); + + return typeof result === "bool" ? result : false; } /** diff --git a/phalcon/Storage/Adapter/Memory.zep b/phalcon/Storage/Adapter/Memory.zep index cc935b524b9..6c279c9025b 100644 --- a/phalcon/Storage/Adapter/Memory.zep +++ b/phalcon/Storage/Adapter/Memory.zep @@ -169,23 +169,40 @@ class Memory extends AbstractAdapter /** * Stores data in the adapter * - * @param string $key - * @param mixed $value - * @param DateInterval|int|null $ttl + * @param string $key + * @param mixed $value + * @param \DateInterval|int|null $ttl * * @return bool * @throws BaseException */ public function set(string! key, var value, var ttl = null) -> bool { - var content, lifetime, prefixedKey; + var content, prefixedKey; + + if (typeof ttl === "integer" && ttl < 1) { + return this->delete(key); + } let content = this->getSerializedData(value), - lifetime = this->getTtl(ttl), prefixedKey = this->getPrefixedKey(key); let this->data[prefixedKey] = content; return true; } + + /** + * Stores data in the adapter forever. The key needs to manually deleted + * from the adapter. + * + * @param string $key + * @param mixed $value + * + * @return bool + */ + public function setForever(string! key, var value) -> bool + { + return this->set(key, value); + } } diff --git a/phalcon/Storage/Adapter/Redis.zep b/phalcon/Storage/Adapter/Redis.zep index 87327f06655..f6bfdb93a78 100644 --- a/phalcon/Storage/Adapter/Redis.zep +++ b/phalcon/Storage/Adapter/Redis.zep @@ -186,22 +186,49 @@ class Redis extends AbstractAdapter /** * Stores data in the adapter * - * @param string $key - * @param mixed $value - * @param DateInterval|int|null $ttl + * @param string $key + * @param mixed $value + * @param \DateInterval|int|null $ttl * * @return bool * @throws BaseException */ public function set(string! key, var value, var ttl = null) -> bool { - return this->getAdapter() - ->set( - key, - this->getSerializedData(value), - this->getTtl(ttl) - ) + var result; + + if (typeof ttl === "integer" && ttl < 1) { + return this->delete(key); + } + + let result = this->getAdapter() + ->set( + key, + this->getSerializedData(value), + this->getTtl(ttl) + ) ; + + return typeof result === "bool" ? result : false; + } + + /** + * Stores data in the adapter forever. The key needs to manually deleted + * from the adapter. + * + * @param string $key + * @param mixed $value + * + * @return bool + */ + public function setForever(string! key, var value) -> bool + { + var result; + + let result = this->getAdapter() + ->set(key, this->getSerializedData(value)); + + return typeof result === "bool" ? result : false; } /** diff --git a/phalcon/Storage/Adapter/Stream.zep b/phalcon/Storage/Adapter/Stream.zep index 4fa514a629e..f2bd81e55c5 100644 --- a/phalcon/Storage/Adapter/Stream.zep +++ b/phalcon/Storage/Adapter/Stream.zep @@ -246,30 +246,49 @@ class Stream extends AbstractAdapter /** * Stores data in the adapter * - * @param string $key - * @param mixed $value - * @param DateInterval|int|null $ttl + * @param string $key + * @param mixed $value + * @param \DateInterval|int|null $ttl * * @return bool */ public function set(string! key, var value, var ttl = null) -> bool { - var directory; array payload; + if (typeof ttl === "integer" && ttl < 1) { + return this->delete(key); + } + let payload = [ - "created" : time(), - "ttl" : this->getTtl(ttl), - "content" : this->getSerializedData(value) - ], - payload = serialize(payload), - directory = this->getDir(key); + "created" : time(), + "ttl" : this->getTtl(ttl), + "content" : this->getSerializedData(value) + ]; - if !is_dir(directory) { - mkdir(directory, 0777, true); - } + return this->storePayload(payload, key); + } - return false !== file_put_contents(directory . key, payload, LOCK_EX); + /** + * Stores data in the adapter forever. The key needs to manually deleted + * from the adapter. + * + * @param string $key + * @param mixed $value + * + * @return bool + */ + public function setForever(string! key, var value) -> bool + { + array payload; + + let payload = [ + "created" : time(), + "ttl" : "forever", + "content" : this->getSerializedData(value) + ]; + + return this->storePayload(payload, key); } /** @@ -400,9 +419,36 @@ class Stream extends AbstractAdapter let created = this->getArrVal(payload, "created", time()), ttl = this->getArrVal(payload, "ttl", 3600); + if ("forever" === ttl) { + return false; + } + return (created + ttl) < time(); } + + /** + * Stores an array payload on the file system + * + * @param array $payload + * @param string $key + * + * @return bool + */ + private function storePayload(array payload, string key) -> bool + { + var directory, payload; + + let payload = serialize(payload), + directory = this->getDir(key); + + if !is_dir(directory) { + mkdir(directory, 0777, true); + } + + return false !== file_put_contents(directory . key, payload, LOCK_EX); + } + /** * @todo Remove this when we get traits */ diff --git a/tests/integration/Cache/Adapter/Apcu/DecrementCest.php b/tests/integration/Cache/Adapter/Apcu/DecrementCest.php deleted file mode 100644 index f8b335489ed..00000000000 --- a/tests/integration/Cache/Adapter/Apcu/DecrementCest.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Cache\Adapter\Apcu; - -use IntegrationTester; -use Phalcon\Cache\Adapter\Apcu; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as ExceptionAlias; -use Phalcon\Tests\Fixtures\Traits\ApcuTrait; - -class DecrementCest -{ - use ApcuTrait; - - /** - * Tests Phalcon\Cache\Adapter\Apcu :: decrement() - * - * @param IntegrationTester $I - * - * @throws ExceptionAlias - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterApcuDecrement(IntegrationTester $I) - { - $I->wantToTest('Cache\Adapter\Apcu - decrement()'); - - $serializer = new SerializerFactory(); - $adapter = new Apcu($serializer); - - $key = uniqid(); - $result = $adapter->set($key, 100); - $I->assertTrue($result); - - $expected = 99; - $actual = $adapter->decrement($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 90; - $actual = $adapter->decrement($key, 9); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - } -} diff --git a/tests/integration/Cache/Adapter/Apcu/GetSetCest.php b/tests/integration/Cache/Adapter/Apcu/GetSetCest.php index d1807163e88..a33793c901c 100644 --- a/tests/integration/Cache/Adapter/Apcu/GetSetCest.php +++ b/tests/integration/Cache/Adapter/Apcu/GetSetCest.php @@ -26,7 +26,7 @@ class GetSetCest use ApcuTrait; /** - * Tests Phalcon\Cache\Adapter\Apcu :: get() + * Tests Phalcon\Cache\Adapter\Apcu :: get()/set() * * @dataProvider getExamples * @@ -55,6 +55,9 @@ public function storageAdapterApcuGetSet(IntegrationTester $I, Example $example) $I->assertEquals($expected, $actual); } + /** + * @return array + */ private function getExamples(): array { return [ diff --git a/tests/integration/Cache/Adapter/Apcu/IncrementCest.php b/tests/integration/Cache/Adapter/Apcu/IncrementCest.php deleted file mode 100644 index aad96e78e38..00000000000 --- a/tests/integration/Cache/Adapter/Apcu/IncrementCest.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Cache\Adapter\Apcu; - -use IntegrationTester; -use Phalcon\Cache\Adapter\Apcu; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception; -use Phalcon\Tests\Fixtures\Traits\ApcuTrait; - -class IncrementCest -{ - use ApcuTrait; - - /** - * Tests Phalcon\Cache\Adapter\Apcu :: increment() - * - * @param IntegrationTester $I - * - * @throws Exception - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterApcuIncrement(IntegrationTester $I) - { - $I->wantToTest('Cache\Adapter\Apcu - increment()'); - - $serializer = new SerializerFactory(); - $adapter = new Apcu($serializer); - - $key = uniqid(); - $result = $adapter->set($key, 1); - $I->assertTrue($result); - - $expected = 2; - $actual = $adapter->increment($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 10; - $actual = $adapter->increment($key, 8); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - } -} diff --git a/tests/integration/Cache/Adapter/ClearCest.php b/tests/integration/Cache/Adapter/ClearCest.php index d12f0754fce..de6223c392f 100644 --- a/tests/integration/Cache/Adapter/ClearCest.php +++ b/tests/integration/Cache/Adapter/ClearCest.php @@ -37,10 +37,13 @@ class ClearCest * @author Phalcon Team * @since 2020-09-09 */ - public function cacheAdapterClear(IntegrationTester $I, Example $example) + public function storageAdapterClear(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Cache\Adapter\'' . $example['className'] . ' - getPrefix()' + sprintf( + 'Cache\Adapter\%s - getPrefix()', + $example['className'] + ) ); $extension = $example['extension']; diff --git a/tests/integration/Cache/Adapter/ConstructCest.php b/tests/integration/Cache/Adapter/ConstructCest.php index 15effbc01f6..153b4832479 100644 --- a/tests/integration/Cache/Adapter/ConstructCest.php +++ b/tests/integration/Cache/Adapter/ConstructCest.php @@ -14,6 +14,7 @@ namespace Phalcon\Tests\Integration\Cache\Adapter; use Codeception\Example; +use DateInterval; use IntegrationTester; use Phalcon\Cache\Adapter\AdapterInterface; use Phalcon\Cache\Adapter\Apcu; @@ -21,14 +22,107 @@ use Phalcon\Cache\Adapter\Memory; use Phalcon\Cache\Adapter\Redis; use Phalcon\Cache\Adapter\Stream; +use Phalcon\Storage\Exception as CacheException; use Phalcon\Storage\SerializerFactory; +use Phalcon\Support\Exception as SupportException; +use Phalcon\Tests\Fixtures\Cache\Adapter\Libmemcached as LibmemcachedFixture; use function getOptionsLibmemcached; use function getOptionsRedis; use function outputDir; +use function sprintf; class ConstructCest { + /** + * Tests Phalcon\Cache\Adapter\Stream :: __construct() - exception + * + * @param IntegrationTester $I + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterStreamConstructException(IntegrationTester $I) + { + $I->wantToTest('Cache\Adapter\Stream - __construct() - exception'); + + $I->expectThrowable( + new CacheException("The 'storageDir' must be specified in the options"), + function () { + $serializer = new SerializerFactory(); + (new Stream($serializer)); + } + ); + } + + /** + * Tests Phalcon\Cache\Adapter\Libmemcached :: __construct() - empty + * options + * + * @param IntegrationTester $I + * + * @author Phalcon Team + * @since 2020-09-09 + * + * @throws SupportException + */ + public function storageAdapterLibmemcachedConstructEmptyOptions(IntegrationTester $I) + { + $I->wantToTest('Cache\Adapter\Libmemcached - __construct() - empty options'); + + $I->checkExtensionIsLoaded('memcached'); + $serializer = new SerializerFactory(); + $adapter = new LibmemcachedFixture($serializer); + + $expected = [ + 'servers' => [ + 0 => [ + 'host' => '127.0.0.1', + 'port' => 11211, + 'weight' => 1, + ], + ], + ]; + $actual = $adapter->getOptions(); + $I->assertEquals($expected, $actual); + } + + /** + * Tests Phalcon\Cache\Adapter\Libmemcached :: __construct() - getTtl + * options + * + * @param IntegrationTester $I + * + * @throws SupportException + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterLibmemcachedConstructGetTtl(IntegrationTester $I) + { + $I->wantToTest('Cache\Adapter\Libmemcached - __construct() - getTtl'); + + $I->checkExtensionIsLoaded('memcached'); + $serializer = new SerializerFactory(); + $adapter = new LibmemcachedFixture( + $serializer, + getOptionsLibmemcached() + ); + + $expected = 3600; + $actual = $adapter->getTtl(null); + $I->assertEquals($expected, $actual); + + $expected = 20; + $actual = $adapter->getTtl(20); + $I->assertEquals($expected, $actual); + + $time = new DateInterval('PT5S'); + $expected = 5; + $actual = $adapter->getTtl($time); + $I->assertEquals($expected, $actual); + } + /** * Tests Phalcon\Cache\Adapter\* :: __construct() * @@ -37,10 +131,13 @@ class ConstructCest * @author Phalcon Team * @since 2020-09-09 */ - public function cacheAdapterConstruct(IntegrationTester $I, Example $example) + public function storageAdapterConstruct(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Cache\Adapter\'' . $example['className'] . ' - __construct()' + sprintf( + 'Cache\Adapter\%s - __construct()', + $example['className'] + ) ); $extension = $example['extension']; diff --git a/tests/integration/Cache/Adapter/DecrementCest.php b/tests/integration/Cache/Adapter/DecrementCest.php new file mode 100644 index 00000000000..8d551d3d4d7 --- /dev/null +++ b/tests/integration/Cache/Adapter/DecrementCest.php @@ -0,0 +1,136 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Phalcon\Tests\Integration\Cache\Adapter; + +use Codeception\Example; +use IntegrationTester; +use Phalcon\Cache\Adapter\Apcu; +use Phalcon\Cache\Adapter\Libmemcached; +use Phalcon\Cache\Adapter\Memory; +use Phalcon\Cache\Adapter\Redis; +use Phalcon\Cache\Adapter\Stream; +use Phalcon\Storage\SerializerFactory; + +use function getOptionsLibmemcached; +use function getOptionsRedis; +use function outputDir; +use function uniqid; + +class DecrementCest +{ + /** + * Tests Phalcon\Cache\Adapter\* :: decrement() + * + * @dataProvider getExamples + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterClear(IntegrationTester $I, Example $example) + { + $I->wantToTest( + sprintf( + 'Cache\Adapter\%s - decrement()', + $example['className'] + ) + ); + + $extension = $example['extension']; + $class = $example['class']; + $options = $example['options']; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + + $key = uniqid(); + $result = $adapter->set($key, 100); + $I->assertTrue($result); + + $expected = 99; + $actual = $adapter->decrement($key); + $I->assertEquals($expected, $actual); + + $actual = $adapter->get($key); + $I->assertEquals($expected, $actual); + + $expected = 90; + $actual = $adapter->decrement($key, 9); + $I->assertEquals($expected, $actual); + + $actual = $adapter->get($key); + $I->assertEquals($expected, $actual); + + /** + * unknown key + */ + $key = uniqid(); + $expected = $example['unknown']; + $actual = $adapter->decrement($key); + $I->assertEquals($expected, $actual); + + if ('Stream' === $example['className']) { + $I->safeDeleteDirectory(outputDir('ph-strm')); + } + } + + /** + * @return array[] + */ + private function getExamples(): array + { + return [ + [ + 'className' => 'Apcu', + 'class' => Apcu::class, + 'options' => [], + 'extension' => 'apcu', + 'unknown' => -1, + ], + [ + 'className' => 'Libmemcached', + 'class' => Libmemcached::class, + 'options' => getOptionsLibmemcached(), + 'extension' => 'memcached', + 'unknown' => false, + ], + [ + 'className' => 'Memory', + 'class' => Memory::class, + 'options' => [], + 'extension' => '', + 'unknown' => false, + ], +// [ +// 'className' => 'Redis', +// 'class' => Redis::class, +// 'options' => getOptionsRedis(), +// 'extension' => 'redis', +// 'unknown' => 1, +// ], + [ + 'className' => 'Stream', + 'class' => Stream::class, + 'options' => [ + 'storageDir' => outputDir(), + ], + 'extension' => '', + 'unknown' => false, + ], + ]; + } +} diff --git a/tests/integration/Cache/Adapter/DeleteCest.php b/tests/integration/Cache/Adapter/DeleteCest.php index a66f746c328..255c0b24555 100644 --- a/tests/integration/Cache/Adapter/DeleteCest.php +++ b/tests/integration/Cache/Adapter/DeleteCest.php @@ -25,6 +25,7 @@ use function getOptionsLibmemcached; use function getOptionsRedis; use function outputDir; +use function sprintf; use function uniqid; class DeleteCest @@ -37,10 +38,13 @@ class DeleteCest * @author Phalcon Team * @since 2020-09-09 */ - public function cacheAdapterDelete(IntegrationTester $I, Example $example) + public function storageAdapterDelete(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Cache\Adapter\'' . $example['className'] . ' - delete()' + sprintf( + 'Cache\Adapter\%s - delete()', + $example['className'] + ) ); $extension = $example['extension']; @@ -70,6 +74,13 @@ public function cacheAdapterDelete(IntegrationTester $I, Example $example) */ $actual = $adapter->delete($key); $I->assertFalse($actual); + + /** + * Delete unknown + */ + $key = uniqid(); + $actual = $adapter->delete($key); + $I->assertFalse($actual); } /** diff --git a/tests/integration/Cache/Adapter/GetAdapterCest.php b/tests/integration/Cache/Adapter/GetAdapterCest.php index a51aabce68f..356a0906238 100644 --- a/tests/integration/Cache/Adapter/GetAdapterCest.php +++ b/tests/integration/Cache/Adapter/GetAdapterCest.php @@ -27,6 +27,7 @@ use function getOptionsLibmemcached; use function getOptionsRedis; use function outputDir; +use function sprintf; class GetAdapterCest { @@ -38,10 +39,13 @@ class GetAdapterCest * @author Phalcon Team * @since 2020-09-09 */ - public function cacheAdapterGetAdapter(IntegrationTester $I, Example $example) + public function storageAdapterGetAdapter(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Cache\Adapter\'' . $example['className'] . ' - getAdapter()' + sprintf( + 'Cache\Adapter\%s - getAdapter()', + $example['className'] + ) ); $extension = $example['extension']; diff --git a/tests/integration/Cache/Adapter/GetPrefixCest.php b/tests/integration/Cache/Adapter/GetPrefixCest.php index 8d049ce0741..d77ec873335 100644 --- a/tests/integration/Cache/Adapter/GetPrefixCest.php +++ b/tests/integration/Cache/Adapter/GetPrefixCest.php @@ -25,6 +25,7 @@ use function array_merge; use function getOptionsRedis; use function outputDir; +use function sprintf; class GetPrefixCest { @@ -36,10 +37,14 @@ class GetPrefixCest * @author Phalcon Team * @since 2020-09-09 */ - public function cacheAdapterGetSetPrefix(IntegrationTester $I, Example $example) + public function storageAdapterGetSetPrefix(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Cache\Adapter\'' . $example['className'] . ' - getPrefix() - ' . $example['label'] + sprintf( + 'Cache\Adapter\%s - getPrefix() - %s', + $example['className'], + $example['label'] + ) ); $extension = $example['extension']; diff --git a/tests/integration/Cache/Adapter/GetSetCest.php b/tests/integration/Cache/Adapter/GetSetCest.php new file mode 100644 index 00000000000..7b317733b4f --- /dev/null +++ b/tests/integration/Cache/Adapter/GetSetCest.php @@ -0,0 +1,129 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Phalcon\Tests\Integration\Cache\Adapter; + +use Codeception\Example; +use IntegrationTester; +use Memcached as NativeMemcached; +use Phalcon\Cache\Adapter\Apcu; +use Phalcon\Cache\Adapter\Libmemcached; +use Phalcon\Cache\Adapter\Memory; +use Phalcon\Cache\Adapter\Redis; +use Phalcon\Cache\Adapter\Stream; +use Phalcon\Storage\SerializerFactory; +use Redis as NativeRedis; + +use function getOptionsLibmemcached; +use function getOptionsRedis; +use function outputDir; +use function sprintf; +use function uniqid; + +class GetSetCest +{ + /** + * Tests Phalcon\Cache\Adapter\* :: get()/set() + * + * @dataProvider getExamples + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterGetSetWithZeroTtl(IntegrationTester $I, Example $example) + { + $I->wantToTest( + sprintf( + 'Cache\Adapter\%s - get()/set()', + $example['className'] + ) + ); + + $extension = $example['extension']; + $class = $example['class']; + $options = $example['options']; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + + $key = uniqid(); + + $result = $adapter->set($key, "test"); + $I->assertTrue($result); + + $result = $adapter->has($key); + $I->assertTrue($result); + + /** + * This will issue delete + */ + $result = $adapter->set($key, "test", 0); + $I->assertTrue($result); + + $result = $adapter->has($key); + $I->assertFalse($result); + } + + /** + * @return array[] + */ + private function getExamples(): array + { + return [ + [ + 'className' => 'Apcu', + 'class' => Apcu::class, + 'options' => [], + 'expected' => null, + 'extension' => 'apcu', + ], + [ + 'className' => 'Libmemcached', + 'class' => Libmemcached::class, + 'options' => getOptionsLibmemcached(), + 'expected' => NativeMemcached::class, + 'extension' => 'memcached', + ], + [ + 'className' => 'Memory', + 'label' => 'default', + 'class' => Memory::class, + 'options' => [], + 'expected' => null, + 'extension' => '', + ], + [ + 'className' => 'Redis', + 'label' => 'default', + 'class' => Redis::class, + 'options' => getOptionsRedis(), + 'expected' => NativeRedis::class, + 'extension' => 'redis', + ], + [ + 'className' => 'Stream', + 'label' => 'default', + 'class' => Stream::class, + 'options' => [ + 'storageDir' => outputDir(), + ], + 'expected' => null, + 'extension' => '', + ], + ]; + } +} diff --git a/tests/integration/Cache/Adapter/GetSetDefaultSerializerCest.php b/tests/integration/Cache/Adapter/GetSetDefaultSerializerCest.php index e9d8a69b11f..39d543b7612 100644 --- a/tests/integration/Cache/Adapter/GetSetDefaultSerializerCest.php +++ b/tests/integration/Cache/Adapter/GetSetDefaultSerializerCest.php @@ -25,7 +25,6 @@ use function getOptionsLibmemcached; use function getOptionsRedis; use function outputDir; -use function sprintf; class GetSetDefaultSerializerCest { @@ -38,7 +37,7 @@ class GetSetDefaultSerializerCest * @author Phalcon Team * @since 2020-09-09 */ - public function cacheAdapterGetSetDefaultSerializer(IntegrationTester $I, Example $example) + public function storageAdapterGetSetDefaultSerializer(IntegrationTester $I, Example $example) { $I->wantToTest( sprintf( diff --git a/tests/integration/Cache/Adapter/GetSetForeverCest.php b/tests/integration/Cache/Adapter/GetSetForeverCest.php new file mode 100644 index 00000000000..a3a3c6a16d4 --- /dev/null +++ b/tests/integration/Cache/Adapter/GetSetForeverCest.php @@ -0,0 +1,128 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Phalcon\Tests\Integration\Cache\Adapter; + +use Codeception\Example; +use IntegrationTester; +use Memcached as NativeMemcached; +use Phalcon\Cache\Adapter\Apcu; +use Phalcon\Cache\Adapter\Libmemcached; +use Phalcon\Cache\Adapter\Memory; +use Phalcon\Cache\Adapter\Redis; +use Phalcon\Cache\Adapter\Stream; +use Phalcon\Storage\SerializerFactory; +use Redis as NativeRedis; + +use function getOptionsLibmemcached; +use function getOptionsRedis; +use function outputDir; +use function sprintf; +use function uniqid; +use function usleep; + +class GetSetForeverCest +{ + /** + * Tests Phalcon\Cache\Adapter\* :: get()/setForever() + * + * @dataProvider getExamples + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterGetSetForever(IntegrationTester $I, Example $example) + { + $I->wantToTest( + sprintf( + 'Cache\Adapter\%s - get()/setForever()', + $example['className'] + ) + ); + + $extension = $example['extension']; + $class = $example['class']; + $options = $example['options']; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + + $key = uniqid(); + + $result = $adapter->setForever($key, "test"); + $I->assertTrue($result); + + sleep(2); + $result = $adapter->has($key); + $I->assertTrue($result); + + /** + * Delete it + */ + $result = $adapter->delete($key); + $I->assertTrue($result); + } + + /** + * @return array[] + */ + private function getExamples(): array + { + return [ + [ + 'className' => 'Apcu', + 'class' => Apcu::class, + 'options' => [], + 'expected' => null, + 'extension' => 'apcu', + ], + [ + 'className' => 'Libmemcached', + 'class' => Libmemcached::class, + 'options' => getOptionsLibmemcached(), + 'expected' => NativeMemcached::class, + 'extension' => 'memcached', + ], + [ + 'className' => 'Memory', + 'label' => 'default', + 'class' => Memory::class, + 'options' => [], + 'expected' => null, + 'extension' => '', + ], + [ + 'className' => 'Redis', + 'label' => 'default', + 'class' => Redis::class, + 'options' => getOptionsRedis(), + 'expected' => NativeRedis::class, + 'extension' => 'redis', + ], + [ + 'className' => 'Stream', + 'label' => 'default', + 'class' => Stream::class, + 'options' => [ + 'storageDir' => outputDir(), + ], + 'expected' => null, + 'extension' => '', + ], + ]; + } +} diff --git a/tests/integration/Cache/Adapter/HasCest.php b/tests/integration/Cache/Adapter/HasCest.php index b24ce09ea8e..77bca552893 100644 --- a/tests/integration/Cache/Adapter/HasCest.php +++ b/tests/integration/Cache/Adapter/HasCest.php @@ -25,6 +25,7 @@ use function getOptionsLibmemcached; use function getOptionsRedis; use function outputDir; +use function sprintf; use function uniqid; class HasCest @@ -37,10 +38,13 @@ class HasCest * @author Phalcon Team * @since 2020-09-09 */ - public function cacheAdapterHas(IntegrationTester $I, Example $example) + public function storageAdapterHas(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Cache\Adapter\'' . $example['className'] . ' - has()' + sprintf( + 'Cache\Adapter\%s - has()', + $example['className'] + ) ); $extension = $example['extension']; diff --git a/tests/integration/Cache/Adapter/IncrementCest.php b/tests/integration/Cache/Adapter/IncrementCest.php new file mode 100644 index 00000000000..7d2ced89d16 --- /dev/null +++ b/tests/integration/Cache/Adapter/IncrementCest.php @@ -0,0 +1,132 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Phalcon\Tests\Integration\Cache\Adapter; + +use Codeception\Example; +use IntegrationTester; +use Phalcon\Cache\Adapter\Apcu; +use Phalcon\Cache\Adapter\Libmemcached; +use Phalcon\Cache\Adapter\Memory; +use Phalcon\Cache\Adapter\Redis; +use Phalcon\Cache\Adapter\Stream; +use Phalcon\Storage\SerializerFactory; + +use function getOptionsLibmemcached; +use function getOptionsRedis; +use function outputDir; +use function uniqid; + +class IncrementCest +{ + /** + * Tests Phalcon\Cache\Adapter\* :: increment() + * + * @dataProvider getExamples + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterClear(IntegrationTester $I, Example $example) + { + $I->wantToTest( + sprintf( + 'Cache\Adapter\%s - increment()', + $example['className'] + ) + ); + + $extension = $example['extension']; + $class = $example['class']; + $options = $example['options']; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + + $key = uniqid(); + $result = $adapter->set($key, 1); + $I->assertTrue($result); + + $expected = 2; + $actual = $adapter->increment($key); + $I->assertEquals($expected, $actual); + + $actual = $adapter->get($key); + $I->assertEquals($expected, $actual); + + $expected = 10; + $actual = $adapter->increment($key, 8); + $I->assertEquals($expected, $actual); + + $actual = $adapter->get($key); + $I->assertEquals($expected, $actual); + + /** + * unknown key + */ + $key = uniqid(); + $expected = $example['unknown']; + $actual = $adapter->increment($key); + $I->assertEquals($expected, $actual); + } + + /** + * @return array[] + */ + private function getExamples(): array + { + return [ + [ + 'className' => 'Apcu', + 'class' => Apcu::class, + 'options' => [], + 'extension' => 'apcu', + 'unknown' => 1, + ], + [ + 'className' => 'Libmemcached', + 'class' => Libmemcached::class, + 'options' => getOptionsLibmemcached(), + 'extension' => 'memcached', + 'unknown' => false, + ], + [ + 'className' => 'Memory', + 'class' => Memory::class, + 'options' => [], + 'extension' => '', + 'unknown' => false, + ], +// [ +// 'className' => 'Redis', +// 'class' => Redis::class, +// 'options' => getOptionsRedis(), +// 'extension' => 'redis', +// 'unknown' => 1, +// ], + [ + 'className' => 'Stream', + 'class' => Stream::class, + 'options' => [ + 'storageDir' => outputDir(), + ], + 'extension' => '', + 'unknown' => false, + ], + ]; + } +} diff --git a/tests/integration/Cache/Adapter/Libmemcached/DecrementCest.php b/tests/integration/Cache/Adapter/Libmemcached/DecrementCest.php deleted file mode 100644 index c98ec5679ae..00000000000 --- a/tests/integration/Cache/Adapter/Libmemcached/DecrementCest.php +++ /dev/null @@ -1,74 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Cache\Adapter\Libmemcached; - -use IntegrationTester; -use Phalcon\Cache\Adapter\Libmemcached; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; -use Phalcon\Tests\Fixtures\Traits\LibmemcachedTrait; - -use function getOptionsLibmemcached; - -class DecrementCest -{ - use LibmemcachedTrait; - - /** - * Tests Phalcon\Cache\Adapter\Libmemcached :: decrement() - */ - /** - * @param IntegrationTester $I - * - * @throws Exception - * @throws HelperException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterLibmemcachedDecrement(IntegrationTester $I) - { - $I->wantToTest('Cache\Adapter\Libmemcached - decrement()'); - - $serializer = new SerializerFactory(); - $adapter = new Libmemcached( - $serializer, - getOptionsLibmemcached() - ); - - $key = uniqid(); - $actual = $adapter->set($key, 100); - $I->assertTrue($actual); - - $expected = 99; - $actual = $adapter->decrement($key); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 90; - $actual = $adapter->decrement($key, 9); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - /** - * unknown key - */ - $key = uniqid(); - $actual = $adapter->decrement($key); - $I->assertFalse($actual); - } -} diff --git a/tests/integration/Cache/Adapter/Libmemcached/GetKeysCest.php b/tests/integration/Cache/Adapter/Libmemcached/GetKeysCest.php index 01f3aa9c22a..56b3364e174 100644 --- a/tests/integration/Cache/Adapter/Libmemcached/GetKeysCest.php +++ b/tests/integration/Cache/Adapter/Libmemcached/GetKeysCest.php @@ -15,7 +15,7 @@ use IntegrationTester; use Phalcon\Cache\Adapter\Libmemcached; -use Phalcon\Storage\Exception as CacheException; +use Phalcon\Cache\Exception as CacheException; use Phalcon\Storage\SerializerFactory; use Phalcon\Support\Exception as HelperException; use Phalcon\Tests\Fixtures\Traits\LibmemcachedTrait; diff --git a/tests/integration/Cache/Adapter/Libmemcached/GetSetCest.php b/tests/integration/Cache/Adapter/Libmemcached/GetSetCest.php index e0b59cd1c0a..df5d52163f7 100644 --- a/tests/integration/Cache/Adapter/Libmemcached/GetSetCest.php +++ b/tests/integration/Cache/Adapter/Libmemcached/GetSetCest.php @@ -16,7 +16,7 @@ use Codeception\Example; use IntegrationTester; use Phalcon\Cache\Adapter\Libmemcached; -use Phalcon\Storage\Exception as CacheException; +use Phalcon\Cache\Exception as CacheException; use Phalcon\Storage\SerializerFactory; use Phalcon\Support\Exception as HelperException; use Phalcon\Tests\Fixtures\Traits\LibmemcachedTrait; diff --git a/tests/integration/Cache/Adapter/Libmemcached/IncrementCest.php b/tests/integration/Cache/Adapter/Libmemcached/IncrementCest.php deleted file mode 100644 index 856cf3fc825..00000000000 --- a/tests/integration/Cache/Adapter/Libmemcached/IncrementCest.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Cache\Adapter\Libmemcached; - -use IntegrationTester; -use Phalcon\Cache\Adapter\Libmemcached; -use Phalcon\Storage\Exception as CacheException; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; -use Phalcon\Tests\Fixtures\Traits\LibmemcachedTrait; - -use function getOptionsLibmemcached; - -class IncrementCest -{ - use LibmemcachedTrait; - - /** - * Tests Phalcon\Cache\Adapter\Libmemcached :: increment() - * - * @param IntegrationTester $I - * - * @throws HelperException - * @throws CacheException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterLibmemcachedIncrement(IntegrationTester $I) - { - $I->wantToTest('Cache\Adapter\Libmemcached - increment()'); - - $serializer = new SerializerFactory(); - $adapter = new Libmemcached( - $serializer, - getOptionsLibmemcached() - ); - - $key = uniqid(); - $result = $adapter->set($key, 1); - $I->assertTrue($result); - - $expected = 2; - $actual = $adapter->increment($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 10; - $actual = $adapter->increment($key, 8); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - /** - * unknown key - */ - $key = uniqid(); - $result = $adapter->increment($key); - $I->assertFalse($result); - } -} diff --git a/tests/integration/Cache/Adapter/Memory/DecrementCest.php b/tests/integration/Cache/Adapter/Memory/DecrementCest.php deleted file mode 100644 index 2803b4cae16..00000000000 --- a/tests/integration/Cache/Adapter/Memory/DecrementCest.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Cache\Adapter\Memory; - -use IntegrationTester; -use Phalcon\Cache\Adapter\Memory; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; - -class DecrementCest -{ - /** - * Tests Phalcon\Cache\Adapter\Memory :: decrement() - * - * @param IntegrationTester $I - * - * @throws HelperException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterMemoryDecrement(IntegrationTester $I) - { - $I->wantToTest('Cache\Adapter\Memory - decrement()'); - - $serializer = new SerializerFactory(); - $adapter = new Memory($serializer); - - $key = uniqid(); - $result = $adapter->set($key, 100); - $I->assertTrue($result); - - $expected = 99; - $actual = $adapter->decrement($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 90; - $actual = $adapter->decrement($key, 9); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - } -} diff --git a/tests/integration/Cache/Adapter/Memory/IncrementCest.php b/tests/integration/Cache/Adapter/Memory/IncrementCest.php deleted file mode 100644 index 822367374a0..00000000000 --- a/tests/integration/Cache/Adapter/Memory/IncrementCest.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Cache\Adapter\Memory; - -use IntegrationTester; -use Phalcon\Cache\Adapter\Memory; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; - -class IncrementCest -{ - /** - * Tests Phalcon\Cache\Adapter\Memory :: increment() - * - * @param IntegrationTester $I - * - * @throws HelperException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterMemoryIncrement(IntegrationTester $I) - { - $I->wantToTest('Cache\Adapter\Memory - increment()'); - - $serializer = new SerializerFactory(); - $adapter = new Memory($serializer); - - $key = uniqid(); - $result = $adapter->set($key, 1); - $I->assertTrue($result); - - $expected = 2; - $actual = $adapter->increment($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 10; - $actual = $adapter->increment($key, 8); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - } -} diff --git a/tests/integration/Cache/Adapter/Redis/DecrementCest.php b/tests/integration/Cache/Adapter/Redis/DecrementCest.php index 74ad5b9cb02..b60f4a0f76c 100644 --- a/tests/integration/Cache/Adapter/Redis/DecrementCest.php +++ b/tests/integration/Cache/Adapter/Redis/DecrementCest.php @@ -15,7 +15,7 @@ use IntegrationTester; use Phalcon\Cache\Adapter\Redis; -use Phalcon\Storage\Exception as CacheException; +use Phalcon\Cache\Exception as CacheException; use Phalcon\Storage\SerializerFactory; use Phalcon\Support\Exception as HelperException; use Phalcon\Tests\Fixtures\Traits\RedisTrait; diff --git a/tests/integration/Cache/Adapter/Redis/GetKeysCest.php b/tests/integration/Cache/Adapter/Redis/GetKeysCest.php index 1f99ad9c20b..e6e48f5b3bb 100644 --- a/tests/integration/Cache/Adapter/Redis/GetKeysCest.php +++ b/tests/integration/Cache/Adapter/Redis/GetKeysCest.php @@ -15,7 +15,7 @@ use IntegrationTester; use Phalcon\Cache\Adapter\Redis; -use Phalcon\Storage\Exception as CacheException; +use Phalcon\Cache\Exception as CacheException; use Phalcon\Storage\SerializerFactory; use Phalcon\Support\Exception as HelperException; use Phalcon\Tests\Fixtures\Traits\RedisTrait; diff --git a/tests/integration/Cache/Adapter/Redis/GetSetCest.php b/tests/integration/Cache/Adapter/Redis/GetSetCest.php index 0143cb79dad..eb9512ff4e2 100644 --- a/tests/integration/Cache/Adapter/Redis/GetSetCest.php +++ b/tests/integration/Cache/Adapter/Redis/GetSetCest.php @@ -16,7 +16,7 @@ use Codeception\Example; use IntegrationTester; use Phalcon\Cache\Adapter\Redis; -use Phalcon\Storage\Exception as CacheException; +use Phalcon\Storage\Exception as StorageException; use Phalcon\Storage\SerializerFactory; use Phalcon\Support\Exception as HelperException; use Phalcon\Tests\Fixtures\Traits\RedisTrait; @@ -39,7 +39,7 @@ class GetSetCest * @param Example $example * * @throws HelperException - * @throws CacheException + * @throws StorageException * * @author Phalcon Team * @since 2020-09-09 @@ -66,7 +66,7 @@ public function storageAdapterRedisGetSet(IntegrationTester $I, Example $example * @param IntegrationTester $I * * @throws HelperException - * @throws CacheException + * @throws StorageException * * @author Phalcon Team * @since 2020-09-09 @@ -108,7 +108,7 @@ public function storageAdapterRedisGetSetWrongIndex(IntegrationTester $I) $I->wantToTest('Cache\Adapter\Redis - get()/set() - wrong index'); $I->expectThrowable( - new CacheException('Redis server selected database failed'), + new StorageException('Redis server selected database failed'), function () { $serializer = new SerializerFactory(); $adapter = new Redis( @@ -139,7 +139,7 @@ public function storageAdapterRedisGetSetFailedAuth(IntegrationTester $I) $I->wantToTest('Cache\Adapter\Redis - get()/set() - failed auth'); $I->expectThrowable( - new CacheException('Failed to authenticate with the Redis server'), + new StorageException('Failed to authenticate with the Redis server'), function () { $serializer = new SerializerFactory(); $adapter = new Redis( @@ -163,7 +163,7 @@ function () { * @param IntegrationTester $I * * @throws HelperException - * @throws CacheException + * @throws StorageException * * @author Phalcon Team * @since 2020-09-09 diff --git a/tests/integration/Cache/Adapter/Redis/IncrementCest.php b/tests/integration/Cache/Adapter/Redis/IncrementCest.php index ff1ebaa4a4f..54e14f27a55 100644 --- a/tests/integration/Cache/Adapter/Redis/IncrementCest.php +++ b/tests/integration/Cache/Adapter/Redis/IncrementCest.php @@ -15,7 +15,7 @@ use IntegrationTester; use Phalcon\Cache\Adapter\Redis; -use Phalcon\Storage\Exception as CacheException; +use Phalcon\Cache\Exception as CacheException; use Phalcon\Storage\SerializerFactory; use Phalcon\Support\Exception as HelperException; use Phalcon\Tests\Fixtures\Traits\RedisTrait; diff --git a/tests/integration/Cache/Adapter/Stream/DecrementCest.php b/tests/integration/Cache/Adapter/Stream/DecrementCest.php deleted file mode 100644 index 4d41dcc04a1..00000000000 --- a/tests/integration/Cache/Adapter/Stream/DecrementCest.php +++ /dev/null @@ -1,76 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Cache\Adapter\Stream; - -use IntegrationTester; -use Phalcon\Cache\Adapter\Stream; -use Phalcon\Storage\Exception as CacheException; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; - -use function outputDir; - -class DecrementCest -{ - /** - * Tests Phalcon\Cache\Adapter\Stream :: decrement() - * - * @param IntegrationTester $I - * - * @throws HelperException - * @throws CacheException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterStreamDecrement(IntegrationTester $I) - { - $I->wantToTest('Cache\Adapter\Stream - decrement()'); - - $serializer = new SerializerFactory(); - $adapter = new Stream( - $serializer, - [ - 'storageDir' => outputDir(), - ] - ); - - $key = uniqid(); - $actual = $adapter->set($key, 100); - $I->assertTrue($actual); - - $expected = 99; - $actual = $adapter->decrement($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 90; - $actual = $adapter->decrement($key, 9); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - /** - * unknown key - */ - $key = uniqid(); - $actual = $adapter->decrement($key); - $I->assertFalse($actual); - - $I->safeDeleteDirectory(outputDir('ph-strm')); - } -} diff --git a/tests/integration/Cache/Adapter/Stream/GetKeysCest.php b/tests/integration/Cache/Adapter/Stream/GetKeysCest.php index 202b71ffb97..cd80359719e 100644 --- a/tests/integration/Cache/Adapter/Stream/GetKeysCest.php +++ b/tests/integration/Cache/Adapter/Stream/GetKeysCest.php @@ -15,7 +15,7 @@ use IntegrationTester; use Phalcon\Cache\Adapter\Stream; -use Phalcon\Storage\Exception as CacheException; +use Phalcon\Cache\Exception as CacheException; use Phalcon\Storage\SerializerFactory; use Phalcon\Support\Exception as HelperException; diff --git a/tests/integration/Cache/Adapter/Stream/GetSetCest.php b/tests/integration/Cache/Adapter/Stream/GetSetCest.php index 09d06a8aec8..65adbbd5956 100644 --- a/tests/integration/Cache/Adapter/Stream/GetSetCest.php +++ b/tests/integration/Cache/Adapter/Stream/GetSetCest.php @@ -15,7 +15,7 @@ use IntegrationTester; use Phalcon\Cache\Adapter\Stream; -use Phalcon\Storage\Exception as CacheException; +use Phalcon\Cache\Exception as CacheException; use Phalcon\Storage\SerializerFactory; use Phalcon\Support\Exception as HelperException; use stdClass; diff --git a/tests/integration/Cache/Adapter/Stream/IncrementCest.php b/tests/integration/Cache/Adapter/Stream/IncrementCest.php deleted file mode 100644 index c6b6a5ccb66..00000000000 --- a/tests/integration/Cache/Adapter/Stream/IncrementCest.php +++ /dev/null @@ -1,72 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Cache\Adapter\Stream; - -use IntegrationTester; -use Phalcon\Cache\Adapter\Stream; -use Phalcon\Storage\Exception as CacheException; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; - -use function outputDir; - -class IncrementCest -{ - /** - * Tests Phalcon\Cache\Adapter\Stream :: increment() - * - * @param IntegrationTester $I - * - * @throws HelperException - * @throws CacheException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterStreamIncrement(IntegrationTester $I) - { - $I->wantToTest('Cache\Adapter\Stream - increment()'); - - $serializer = new SerializerFactory(); - $adapter = new Stream( - $serializer, - [ - 'storageDir' => outputDir(), - ] - ); - - $key = uniqid(); - $actual = $adapter->set($key, 1); - $I->assertTrue($actual); - - $expected = 2; - $actual = $adapter->increment($key); - $I->assertEquals($expected, $actual); - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 10; - $actual = $adapter->increment($key, 8); - $I->assertEquals($expected, $actual); - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - /** - * unknown key - */ - $key = uniqid(); - $actual = $adapter->increment($key); - $I->assertFalse($actual); - } -} diff --git a/tests/integration/Cache/CacheFactory/NewInstanceCest.php b/tests/integration/Cache/CacheFactory/NewInstanceCest.php index 622e6bbfa9d..f9304e3bea9 100644 --- a/tests/integration/Cache/CacheFactory/NewInstanceCest.php +++ b/tests/integration/Cache/CacheFactory/NewInstanceCest.php @@ -17,9 +17,12 @@ use Phalcon\Cache\AdapterFactory; use Phalcon\Cache\Cache; use Phalcon\Cache\CacheFactory; +use Phalcon\Cache\Exception\Exception; use Phalcon\Storage\SerializerFactory; use Psr\SimpleCache\CacheInterface; +use function uniqid; + class NewInstanceCest { /** @@ -40,23 +43,4 @@ public function cacheCacheFactoryNewInstance(IntegrationTester $I) $I->assertInstanceOf(Cache::class, $adapter); $I->assertInstanceOf(CacheInterface::class, $adapter); } - - /** - * Tests Phalcon\Cache\CacheFactory :: newInstance() - exception - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function cacheCacheFactoryNewInstanceException(IntegrationTester $I) - { - $I->wantToTest('Cache\CacheFactory - newInstance() - exception'); - - $serializer = new SerializerFactory(); - $adapterFactory = new AdapterFactory($serializer); - $cacheFactory = new CacheFactory($adapterFactory); - $adapter = $cacheFactory->newInstance('apcu'); - - $I->assertInstanceOf(Cache::class, $adapter); - $I->assertInstanceOf(CacheInterface::class, $adapter); - } } diff --git a/tests/integration/Storage/Adapter/Apcu/DecrementCest.php b/tests/integration/Storage/Adapter/Apcu/DecrementCest.php deleted file mode 100644 index 8754d9f7b45..00000000000 --- a/tests/integration/Storage/Adapter/Apcu/DecrementCest.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Storage\Adapter\Apcu; - -use IntegrationTester; -use Phalcon\Storage\Adapter\Apcu; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as ExceptionAlias; -use Phalcon\Tests\Fixtures\Traits\ApcuTrait; - -class DecrementCest -{ - use ApcuTrait; - - /** - * Tests Phalcon\Storage\Adapter\Apcu :: decrement() - * - * @param IntegrationTester $I - * - * @throws ExceptionAlias - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterApcuDecrement(IntegrationTester $I) - { - $I->wantToTest('Storage\Adapter\Apcu - decrement()'); - - $serializer = new SerializerFactory(); - $adapter = new Apcu($serializer); - - $key = uniqid(); - $result = $adapter->set($key, 100); - $I->assertTrue($result); - - $expected = 99; - $actual = $adapter->decrement($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 90; - $actual = $adapter->decrement($key, 9); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - } -} diff --git a/tests/integration/Storage/Adapter/Apcu/GetKeysCest.php b/tests/integration/Storage/Adapter/Apcu/GetKeysCest.php index 06b31a334ba..5db0325bb68 100644 --- a/tests/integration/Storage/Adapter/Apcu/GetKeysCest.php +++ b/tests/integration/Storage/Adapter/Apcu/GetKeysCest.php @@ -13,6 +13,7 @@ namespace Phalcon\Tests\Integration\Storage\Adapter\Apcu; +use Codeception\Stub; use IntegrationTester; use Phalcon\Storage\Adapter\Apcu; use Phalcon\Storage\SerializerFactory; diff --git a/tests/integration/Storage/Adapter/Apcu/GetSetCest.php b/tests/integration/Storage/Adapter/Apcu/GetSetCest.php index 36ab1dc651e..08e8c2667b3 100644 --- a/tests/integration/Storage/Adapter/Apcu/GetSetCest.php +++ b/tests/integration/Storage/Adapter/Apcu/GetSetCest.php @@ -26,7 +26,7 @@ class GetSetCest use ApcuTrait; /** - * Tests Phalcon\Storage\Adapter\Apcu :: get() + * Tests Phalcon\Storage\Adapter\Apcu :: get()/set() * * @dataProvider getExamples * @@ -55,6 +55,9 @@ public function storageAdapterApcuGetSet(IntegrationTester $I, Example $example) $I->assertEquals($expected, $actual); } + /** + * @return array + */ private function getExamples(): array { return [ diff --git a/tests/integration/Storage/Adapter/Apcu/IncrementCest.php b/tests/integration/Storage/Adapter/Apcu/IncrementCest.php deleted file mode 100644 index 095c99fd6ac..00000000000 --- a/tests/integration/Storage/Adapter/Apcu/IncrementCest.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Storage\Adapter\Apcu; - -use IntegrationTester; -use Phalcon\Storage\Adapter\Apcu; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception; -use Phalcon\Tests\Fixtures\Traits\ApcuTrait; - -class IncrementCest -{ - use ApcuTrait; - - /** - * Tests Phalcon\Storage\Adapter\Apcu :: increment() - * - * @param IntegrationTester $I - * - * @throws Exception - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterApcuIncrement(IntegrationTester $I) - { - $I->wantToTest('Storage\Adapter\Apcu - increment()'); - - $serializer = new SerializerFactory(); - $adapter = new Apcu($serializer); - - $key = uniqid(); - $result = $adapter->set($key, 1); - $I->assertTrue($result); - - $expected = 2; - $actual = $adapter->increment($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 10; - $actual = $adapter->increment($key, 8); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - } -} diff --git a/tests/integration/Storage/Adapter/ClearCest.php b/tests/integration/Storage/Adapter/ClearCest.php index daf08f04b34..dabb61be159 100644 --- a/tests/integration/Storage/Adapter/ClearCest.php +++ b/tests/integration/Storage/Adapter/ClearCest.php @@ -40,7 +40,10 @@ class ClearCest public function storageAdapterClear(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Storage\Adapter\'' . $example['className'] . ' - getPrefix()' + sprintf( + 'Storage\Adapter\%s - getPrefix()', + $example['className'] + ) ); $extension = $example['extension']; diff --git a/tests/integration/Storage/Adapter/ConstructCest.php b/tests/integration/Storage/Adapter/ConstructCest.php index 110d59628db..e8c7961d67f 100644 --- a/tests/integration/Storage/Adapter/ConstructCest.php +++ b/tests/integration/Storage/Adapter/ConstructCest.php @@ -14,6 +14,7 @@ namespace Phalcon\Tests\Integration\Storage\Adapter; use Codeception\Example; +use DateInterval; use IntegrationTester; use Phalcon\Storage\Adapter\AdapterInterface; use Phalcon\Storage\Adapter\Apcu; @@ -21,14 +22,107 @@ use Phalcon\Storage\Adapter\Memory; use Phalcon\Storage\Adapter\Redis; use Phalcon\Storage\Adapter\Stream; +use Phalcon\Storage\Exception as StorageException; use Phalcon\Storage\SerializerFactory; +use Phalcon\Support\Exception as SupportException; +use Phalcon\Tests\Fixtures\Storage\Adapter\Libmemcached as LibmemcachedFixture; use function getOptionsLibmemcached; use function getOptionsRedis; use function outputDir; +use function sprintf; class ConstructCest { + /** + * Tests Phalcon\Storage\Adapter\Stream :: __construct() - exception + * + * @param IntegrationTester $I + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterStreamConstructException(IntegrationTester $I) + { + $I->wantToTest('Storage\Adapter\Stream - __construct() - exception'); + + $I->expectThrowable( + new StorageException("The 'storageDir' must be specified in the options"), + function () { + $serializer = new SerializerFactory(); + (new Stream($serializer)); + } + ); + } + + /** + * Tests Phalcon\Storage\Adapter\Libmemcached :: __construct() - empty + * options + * + * @param IntegrationTester $I + * + * @author Phalcon Team + * @since 2020-09-09 + * + * @throws SupportException + */ + public function storageAdapterLibmemcachedConstructEmptyOptions(IntegrationTester $I) + { + $I->wantToTest('Storage\Adapter\Libmemcached - __construct() - empty options'); + + $I->checkExtensionIsLoaded('memcached'); + $serializer = new SerializerFactory(); + $adapter = new LibmemcachedFixture($serializer); + + $expected = [ + 'servers' => [ + 0 => [ + 'host' => '127.0.0.1', + 'port' => 11211, + 'weight' => 1, + ], + ], + ]; + $actual = $adapter->getOptions(); + $I->assertEquals($expected, $actual); + } + + /** + * Tests Phalcon\Storage\Adapter\Libmemcached :: __construct() - getTtl + * options + * + * @param IntegrationTester $I + * + * @throws SupportException + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterLibmemcachedConstructGetTtl(IntegrationTester $I) + { + $I->wantToTest('Storage\Adapter\Libmemcached - __construct() - getTtl'); + + $I->checkExtensionIsLoaded('memcached'); + $serializer = new SerializerFactory(); + $adapter = new LibmemcachedFixture( + $serializer, + getOptionsLibmemcached() + ); + + $expected = 3600; + $actual = $adapter->getTtl(null); + $I->assertEquals($expected, $actual); + + $expected = 20; + $actual = $adapter->getTtl(20); + $I->assertEquals($expected, $actual); + + $time = new DateInterval('PT5S'); + $expected = 5; + $actual = $adapter->getTtl($time); + $I->assertEquals($expected, $actual); + } + /** * Tests Phalcon\Storage\Adapter\* :: __construct() * @@ -40,7 +134,10 @@ class ConstructCest public function storageAdapterConstruct(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Storage\Adapter\'' . $example['className'] . ' - __construct()' + sprintf( + 'Storage\Adapter\%s - __construct()', + $example['className'] + ) ); $extension = $example['extension']; diff --git a/tests/integration/Storage/Adapter/DecrementCest.php b/tests/integration/Storage/Adapter/DecrementCest.php new file mode 100644 index 00000000000..bc697d63634 --- /dev/null +++ b/tests/integration/Storage/Adapter/DecrementCest.php @@ -0,0 +1,136 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Phalcon\Tests\Integration\Storage\Adapter; + +use Codeception\Example; +use IntegrationTester; +use Phalcon\Storage\Adapter\Apcu; +use Phalcon\Storage\Adapter\Libmemcached; +use Phalcon\Storage\Adapter\Memory; +use Phalcon\Storage\Adapter\Redis; +use Phalcon\Storage\Adapter\Stream; +use Phalcon\Storage\SerializerFactory; + +use function getOptionsLibmemcached; +use function getOptionsRedis; +use function outputDir; +use function uniqid; + +class DecrementCest +{ + /** + * Tests Phalcon\Storage\Adapter\* :: decrement() + * + * @dataProvider getExamples + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterClear(IntegrationTester $I, Example $example) + { + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - decrement()', + $example['className'] + ) + ); + + $extension = $example['extension']; + $class = $example['class']; + $options = $example['options']; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + + $key = uniqid(); + $result = $adapter->set($key, 100); + $I->assertTrue($result); + + $expected = 99; + $actual = $adapter->decrement($key); + $I->assertEquals($expected, $actual); + + $actual = $adapter->get($key); + $I->assertEquals($expected, $actual); + + $expected = 90; + $actual = $adapter->decrement($key, 9); + $I->assertEquals($expected, $actual); + + $actual = $adapter->get($key); + $I->assertEquals($expected, $actual); + + /** + * unknown key + */ + $key = uniqid(); + $expected = $example['unknown']; + $actual = $adapter->decrement($key); + $I->assertEquals($expected, $actual); + + if ('Stream' === $example['className']) { + $I->safeDeleteDirectory(outputDir('ph-strm')); + } + } + + /** + * @return array[] + */ + private function getExamples(): array + { + return [ + [ + 'className' => 'Apcu', + 'class' => Apcu::class, + 'options' => [], + 'extension' => 'apcu', + 'unknown' => -1, + ], + [ + 'className' => 'Libmemcached', + 'class' => Libmemcached::class, + 'options' => getOptionsLibmemcached(), + 'extension' => 'memcached', + 'unknown' => false, + ], + [ + 'className' => 'Memory', + 'class' => Memory::class, + 'options' => [], + 'extension' => '', + 'unknown' => false, + ], +// [ +// 'className' => 'Redis', +// 'class' => Redis::class, +// 'options' => getOptionsRedis(), +// 'extension' => 'redis', +// 'unknown' => 1, +// ], + [ + 'className' => 'Stream', + 'class' => Stream::class, + 'options' => [ + 'storageDir' => outputDir(), + ], + 'extension' => '', + 'unknown' => false, + ], + ]; + } +} diff --git a/tests/integration/Storage/Adapter/DeleteCest.php b/tests/integration/Storage/Adapter/DeleteCest.php index 311018e8373..7d567b7abfa 100644 --- a/tests/integration/Storage/Adapter/DeleteCest.php +++ b/tests/integration/Storage/Adapter/DeleteCest.php @@ -25,6 +25,7 @@ use function getOptionsLibmemcached; use function getOptionsRedis; use function outputDir; +use function sprintf; use function uniqid; class DeleteCest @@ -40,7 +41,10 @@ class DeleteCest public function storageAdapterDelete(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Storage\Adapter\'' . $example['className'] . ' - delete()' + sprintf( + 'Storage\Adapter\%s - delete()', + $example['className'] + ) ); $extension = $example['extension']; @@ -70,6 +74,13 @@ public function storageAdapterDelete(IntegrationTester $I, Example $example) */ $actual = $adapter->delete($key); $I->assertFalse($actual); + + /** + * Delete unknown + */ + $key = uniqid(); + $actual = $adapter->delete($key); + $I->assertFalse($actual); } /** diff --git a/tests/integration/Storage/Adapter/GetAdapterCest.php b/tests/integration/Storage/Adapter/GetAdapterCest.php index 862702ae8e9..22e31cfaa79 100644 --- a/tests/integration/Storage/Adapter/GetAdapterCest.php +++ b/tests/integration/Storage/Adapter/GetAdapterCest.php @@ -27,6 +27,7 @@ use function getOptionsLibmemcached; use function getOptionsRedis; use function outputDir; +use function sprintf; class GetAdapterCest { @@ -41,7 +42,10 @@ class GetAdapterCest public function storageAdapterGetAdapter(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Storage\Adapter\'' . $example['className'] . ' - getAdapter()' + sprintf( + 'Storage\Adapter\%s - getAdapter()', + $example['className'] + ) ); $extension = $example['extension']; diff --git a/tests/integration/Storage/Adapter/GetPrefixCest.php b/tests/integration/Storage/Adapter/GetPrefixCest.php index 379a09de7f1..da98fbeeb56 100644 --- a/tests/integration/Storage/Adapter/GetPrefixCest.php +++ b/tests/integration/Storage/Adapter/GetPrefixCest.php @@ -25,6 +25,7 @@ use function array_merge; use function getOptionsRedis; use function outputDir; +use function sprintf; class GetPrefixCest { @@ -39,7 +40,11 @@ class GetPrefixCest public function storageAdapterGetSetPrefix(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Storage\Adapter\'' . $example['className'] . ' - getPrefix() - ' . $example['label'] + sprintf( + 'Storage\Adapter\%s - getPrefix() - %s', + $example['className'], + $example['label'] + ) ); $extension = $example['extension']; diff --git a/tests/integration/Storage/Adapter/GetSetCest.php b/tests/integration/Storage/Adapter/GetSetCest.php new file mode 100644 index 00000000000..913630d3002 --- /dev/null +++ b/tests/integration/Storage/Adapter/GetSetCest.php @@ -0,0 +1,129 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Phalcon\Tests\Integration\Storage\Adapter; + +use Codeception\Example; +use IntegrationTester; +use Memcached as NativeMemcached; +use Phalcon\Storage\Adapter\Apcu; +use Phalcon\Storage\Adapter\Libmemcached; +use Phalcon\Storage\Adapter\Memory; +use Phalcon\Storage\Adapter\Redis; +use Phalcon\Storage\Adapter\Stream; +use Phalcon\Storage\SerializerFactory; +use Redis as NativeRedis; + +use function getOptionsLibmemcached; +use function getOptionsRedis; +use function outputDir; +use function sprintf; +use function uniqid; + +class GetSetCest +{ + /** + * Tests Phalcon\Storage\Adapter\* :: get()/set() + * + * @dataProvider getExamples + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterGetSetWithZeroTtl(IntegrationTester $I, Example $example) + { + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - get()/set()', + $example['className'] + ) + ); + + $extension = $example['extension']; + $class = $example['class']; + $options = $example['options']; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + + $key = uniqid(); + + $result = $adapter->set($key, "test"); + $I->assertTrue($result); + + $result = $adapter->has($key); + $I->assertTrue($result); + + /** + * This will issue delete + */ + $result = $adapter->set($key, "test", 0); + $I->assertTrue($result); + + $result = $adapter->has($key); + $I->assertFalse($result); + } + + /** + * @return array[] + */ + private function getExamples(): array + { + return [ + [ + 'className' => 'Apcu', + 'class' => Apcu::class, + 'options' => [], + 'expected' => null, + 'extension' => 'apcu', + ], + [ + 'className' => 'Libmemcached', + 'class' => Libmemcached::class, + 'options' => getOptionsLibmemcached(), + 'expected' => NativeMemcached::class, + 'extension' => 'memcached', + ], + [ + 'className' => 'Memory', + 'label' => 'default', + 'class' => Memory::class, + 'options' => [], + 'expected' => null, + 'extension' => '', + ], + [ + 'className' => 'Redis', + 'label' => 'default', + 'class' => Redis::class, + 'options' => getOptionsRedis(), + 'expected' => NativeRedis::class, + 'extension' => 'redis', + ], + [ + 'className' => 'Stream', + 'label' => 'default', + 'class' => Stream::class, + 'options' => [ + 'storageDir' => outputDir(), + ], + 'expected' => null, + 'extension' => '', + ], + ]; + } +} diff --git a/tests/integration/Storage/Adapter/GetSetForeverCest.php b/tests/integration/Storage/Adapter/GetSetForeverCest.php new file mode 100644 index 00000000000..cbd844108c4 --- /dev/null +++ b/tests/integration/Storage/Adapter/GetSetForeverCest.php @@ -0,0 +1,128 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Phalcon\Tests\Integration\Storage\Adapter; + +use Codeception\Example; +use IntegrationTester; +use Memcached as NativeMemcached; +use Phalcon\Storage\Adapter\Apcu; +use Phalcon\Storage\Adapter\Libmemcached; +use Phalcon\Storage\Adapter\Memory; +use Phalcon\Storage\Adapter\Redis; +use Phalcon\Storage\Adapter\Stream; +use Phalcon\Storage\SerializerFactory; +use Redis as NativeRedis; + +use function getOptionsLibmemcached; +use function getOptionsRedis; +use function outputDir; +use function sprintf; +use function uniqid; +use function usleep; + +class GetSetForeverCest +{ + /** + * Tests Phalcon\Storage\Adapter\* :: get()/setForever() + * + * @dataProvider getExamples + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterGetSetForever(IntegrationTester $I, Example $example) + { + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - get()/setForever()', + $example['className'] + ) + ); + + $extension = $example['extension']; + $class = $example['class']; + $options = $example['options']; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + + $key = uniqid(); + + $result = $adapter->setForever($key, "test"); + $I->assertTrue($result); + + sleep(2); + $result = $adapter->has($key); + $I->assertTrue($result); + + /** + * Delete it + */ + $result = $adapter->delete($key); + $I->assertTrue($result); + } + + /** + * @return array[] + */ + private function getExamples(): array + { + return [ + [ + 'className' => 'Apcu', + 'class' => Apcu::class, + 'options' => [], + 'expected' => null, + 'extension' => 'apcu', + ], + [ + 'className' => 'Libmemcached', + 'class' => Libmemcached::class, + 'options' => getOptionsLibmemcached(), + 'expected' => NativeMemcached::class, + 'extension' => 'memcached', + ], + [ + 'className' => 'Memory', + 'label' => 'default', + 'class' => Memory::class, + 'options' => [], + 'expected' => null, + 'extension' => '', + ], + [ + 'className' => 'Redis', + 'label' => 'default', + 'class' => Redis::class, + 'options' => getOptionsRedis(), + 'expected' => NativeRedis::class, + 'extension' => 'redis', + ], + [ + 'className' => 'Stream', + 'label' => 'default', + 'class' => Stream::class, + 'options' => [ + 'storageDir' => outputDir(), + ], + 'expected' => null, + 'extension' => '', + ], + ]; + } +} diff --git a/tests/integration/Storage/Adapter/HasCest.php b/tests/integration/Storage/Adapter/HasCest.php index 9fc8a0ec075..ce4ba6349d3 100644 --- a/tests/integration/Storage/Adapter/HasCest.php +++ b/tests/integration/Storage/Adapter/HasCest.php @@ -25,6 +25,7 @@ use function getOptionsLibmemcached; use function getOptionsRedis; use function outputDir; +use function sprintf; use function uniqid; class HasCest @@ -40,7 +41,10 @@ class HasCest public function storageAdapterHas(IntegrationTester $I, Example $example) { $I->wantToTest( - 'Storage\Adapter\'' . $example['className'] . ' - has()' + sprintf( + 'Storage\Adapter\%s - has()', + $example['className'] + ) ); $extension = $example['extension']; diff --git a/tests/integration/Storage/Adapter/IncrementCest.php b/tests/integration/Storage/Adapter/IncrementCest.php new file mode 100644 index 00000000000..43331a3c17c --- /dev/null +++ b/tests/integration/Storage/Adapter/IncrementCest.php @@ -0,0 +1,132 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Phalcon\Tests\Integration\Storage\Adapter; + +use Codeception\Example; +use IntegrationTester; +use Phalcon\Storage\Adapter\Apcu; +use Phalcon\Storage\Adapter\Libmemcached; +use Phalcon\Storage\Adapter\Memory; +use Phalcon\Storage\Adapter\Redis; +use Phalcon\Storage\Adapter\Stream; +use Phalcon\Storage\SerializerFactory; + +use function getOptionsLibmemcached; +use function getOptionsRedis; +use function outputDir; +use function uniqid; + +class IncrementCest +{ + /** + * Tests Phalcon\Storage\Adapter\* :: increment() + * + * @dataProvider getExamples + * + * @author Phalcon Team + * @since 2020-09-09 + */ + public function storageAdapterClear(IntegrationTester $I, Example $example) + { + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - increment()', + $example['className'] + ) + ); + + $extension = $example['extension']; + $class = $example['class']; + $options = $example['options']; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + + $key = uniqid(); + $result = $adapter->set($key, 1); + $I->assertTrue($result); + + $expected = 2; + $actual = $adapter->increment($key); + $I->assertEquals($expected, $actual); + + $actual = $adapter->get($key); + $I->assertEquals($expected, $actual); + + $expected = 10; + $actual = $adapter->increment($key, 8); + $I->assertEquals($expected, $actual); + + $actual = $adapter->get($key); + $I->assertEquals($expected, $actual); + + /** + * unknown key + */ + $key = uniqid(); + $expected = $example['unknown']; + $actual = $adapter->increment($key); + $I->assertEquals($expected, $actual); + } + + /** + * @return array[] + */ + private function getExamples(): array + { + return [ + [ + 'className' => 'Apcu', + 'class' => Apcu::class, + 'options' => [], + 'extension' => 'apcu', + 'unknown' => 1, + ], + [ + 'className' => 'Libmemcached', + 'class' => Libmemcached::class, + 'options' => getOptionsLibmemcached(), + 'extension' => 'memcached', + 'unknown' => false, + ], + [ + 'className' => 'Memory', + 'class' => Memory::class, + 'options' => [], + 'extension' => '', + 'unknown' => false, + ], +// [ +// 'className' => 'Redis', +// 'class' => Redis::class, +// 'options' => getOptionsRedis(), +// 'extension' => 'redis', +// 'unknown' => 1, +// ], + [ + 'className' => 'Stream', + 'class' => Stream::class, + 'options' => [ + 'storageDir' => outputDir(), + ], + 'extension' => '', + 'unknown' => false, + ], + ]; + } +} diff --git a/tests/integration/Storage/Adapter/Libmemcached/DecrementCest.php b/tests/integration/Storage/Adapter/Libmemcached/DecrementCest.php deleted file mode 100644 index 91ba1ec2b78..00000000000 --- a/tests/integration/Storage/Adapter/Libmemcached/DecrementCest.php +++ /dev/null @@ -1,76 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Storage\Adapter\Libmemcached; - -use IntegrationTester; -use Phalcon\Storage\Adapter\Libmemcached; -use Phalcon\Storage\Exception; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; -use Phalcon\Tests\Fixtures\Traits\LibmemcachedTrait; - -use function getOptionsLibmemcached; - -class DecrementCest -{ - use LibmemcachedTrait; - - /** - * Tests Phalcon\Storage\Adapter\Libmemcached :: decrement() - */ - /** - * @param IntegrationTester $I - * - * @throws Exception - * @throws HelperException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterLibmemcachedDecrement(IntegrationTester $I) - { - $I->wantToTest('Storage\Adapter\Libmemcached - decrement()'); - - $serializer = new SerializerFactory(); - $adapter = new Libmemcached( - $serializer, - getOptionsLibmemcached() - ); - - $key = uniqid(); - $actual = $adapter->set($key, 100); - $I->assertTrue($actual); - - $expected = 99; - $actual = $adapter->decrement($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 90; - $actual = $adapter->decrement($key, 9); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - /** - * unknown key - */ - $key = uniqid(); - $actual = $adapter->decrement($key); - $I->assertFalse($actual); - } -} diff --git a/tests/integration/Storage/Adapter/Libmemcached/IncrementCest.php b/tests/integration/Storage/Adapter/Libmemcached/IncrementCest.php deleted file mode 100644 index d976a4a020e..00000000000 --- a/tests/integration/Storage/Adapter/Libmemcached/IncrementCest.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Storage\Adapter\Libmemcached; - -use IntegrationTester; -use Phalcon\Storage\Adapter\Libmemcached; -use Phalcon\Storage\Exception as StorageException; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; -use Phalcon\Tests\Fixtures\Traits\LibmemcachedTrait; - -use function getOptionsLibmemcached; - -class IncrementCest -{ - use LibmemcachedTrait; - - /** - * Tests Phalcon\Storage\Adapter\Libmemcached :: increment() - * - * @param IntegrationTester $I - * - * @throws HelperException - * @throws StorageException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterLibmemcachedIncrement(IntegrationTester $I) - { - $I->wantToTest('Storage\Adapter\Libmemcached - increment()'); - - $serializer = new SerializerFactory(); - $adapter = new Libmemcached( - $serializer, - getOptionsLibmemcached() - ); - - $key = uniqid(); - $result = $adapter->set($key, 1); - $I->assertTrue($result); - - $expected = 2; - $actual = $adapter->increment($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 10; - $actual = $adapter->increment($key, 8); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - /** - * unknown key - */ - $key = uniqid(); - $result = $adapter->increment($key); - $I->assertFalse($result); - } -} diff --git a/tests/integration/Storage/Adapter/Memory/DecrementCest.php b/tests/integration/Storage/Adapter/Memory/DecrementCest.php deleted file mode 100644 index 93ed32dae34..00000000000 --- a/tests/integration/Storage/Adapter/Memory/DecrementCest.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Storage\Adapter\Memory; - -use IntegrationTester; -use Phalcon\Storage\Adapter\Memory; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; - -class DecrementCest -{ - /** - * Tests Phalcon\Storage\Adapter\Memory :: decrement() - * - * @param IntegrationTester $I - * - * @throws HelperException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterMemoryDecrement(IntegrationTester $I) - { - $I->wantToTest('Storage\Adapter\Memory - decrement()'); - - $serializer = new SerializerFactory(); - $adapter = new Memory($serializer); - - $key = uniqid(); - $result = $adapter->set($key, 100); - $I->assertTrue($result); - - $expected = 99; - $actual = $adapter->decrement($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 90; - $actual = $adapter->decrement($key, 9); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - } -} diff --git a/tests/integration/Storage/Adapter/Memory/IncrementCest.php b/tests/integration/Storage/Adapter/Memory/IncrementCest.php deleted file mode 100644 index e6801b8bab6..00000000000 --- a/tests/integration/Storage/Adapter/Memory/IncrementCest.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Storage\Adapter\Memory; - -use IntegrationTester; -use Phalcon\Storage\Adapter\Memory; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; - -class IncrementCest -{ - /** - * Tests Phalcon\Storage\Adapter\Memory :: increment() - * - * @param IntegrationTester $I - * - * @throws HelperException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterMemoryIncrement(IntegrationTester $I) - { - $I->wantToTest('Storage\Adapter\Memory - increment()'); - - $serializer = new SerializerFactory(); - $adapter = new Memory($serializer); - - $key = uniqid(); - $result = $adapter->set($key, 1); - $I->assertTrue($result); - - $expected = 2; - $actual = $adapter->increment($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 10; - $actual = $adapter->increment($key, 8); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - } -} diff --git a/tests/integration/Storage/Adapter/Stream/DecrementCest.php b/tests/integration/Storage/Adapter/Stream/DecrementCest.php deleted file mode 100644 index 2749e3bd29e..00000000000 --- a/tests/integration/Storage/Adapter/Stream/DecrementCest.php +++ /dev/null @@ -1,76 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Storage\Adapter\Stream; - -use IntegrationTester; -use Phalcon\Storage\Adapter\Stream; -use Phalcon\Storage\Exception as StorageException; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; - -use function outputDir; - -class DecrementCest -{ - /** - * Tests Phalcon\Storage\Adapter\Stream :: decrement() - * - * @param IntegrationTester $I - * - * @throws HelperException - * @throws StorageException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterStreamDecrement(IntegrationTester $I) - { - $I->wantToTest('Storage\Adapter\Stream - decrement()'); - - $serializer = new SerializerFactory(); - $adapter = new Stream( - $serializer, - [ - 'storageDir' => outputDir(), - ] - ); - - $key = uniqid(); - $actual = $adapter->set($key, 100); - $I->assertTrue($actual); - - $expected = 99; - $actual = $adapter->decrement($key); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 90; - $actual = $adapter->decrement($key, 9); - $I->assertEquals($expected, $actual); - - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - /** - * unknown key - */ - $key = uniqid(); - $actual = $adapter->decrement($key); - $I->assertFalse($actual); - - $I->safeDeleteDirectory(outputDir('ph-strm')); - } -} diff --git a/tests/integration/Storage/Adapter/Stream/IncrementCest.php b/tests/integration/Storage/Adapter/Stream/IncrementCest.php deleted file mode 100644 index 70f6e88b521..00000000000 --- a/tests/integration/Storage/Adapter/Stream/IncrementCest.php +++ /dev/null @@ -1,72 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Phalcon\Tests\Integration\Storage\Adapter\Stream; - -use IntegrationTester; -use Phalcon\Storage\Adapter\Stream; -use Phalcon\Storage\Exception as StorageException; -use Phalcon\Storage\SerializerFactory; -use Phalcon\Support\Exception as HelperException; - -use function outputDir; - -class IncrementCest -{ - /** - * Tests Phalcon\Storage\Adapter\Stream :: increment() - * - * @param IntegrationTester $I - * - * @throws HelperException - * @throws StorageException - * - * @author Phalcon Team - * @since 2020-09-09 - */ - public function storageAdapterStreamIncrement(IntegrationTester $I) - { - $I->wantToTest('Storage\Adapter\Stream - increment()'); - - $serializer = new SerializerFactory(); - $adapter = new Stream( - $serializer, - [ - 'storageDir' => outputDir(), - ] - ); - - $key = uniqid(); - $actual = $adapter->set($key, 1); - $I->assertTrue($actual); - - $expected = 2; - $actual = $adapter->increment($key); - $I->assertEquals($expected, $actual); - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - $expected = 10; - $actual = $adapter->increment($key, 8); - $I->assertEquals($expected, $actual); - $actual = $adapter->get($key); - $I->assertEquals($expected, $actual); - - /** - * unknown key - */ - $key = uniqid(); - $actual = $adapter->increment($key); - $I->assertFalse($actual); - } -}