From 3c2d147f23144a5d697e25988469c6118c889a0b Mon Sep 17 00:00:00 2001 From: "n[oO]ne" Date: Fri, 7 Jun 2024 15:37:18 +0700 Subject: [PATCH 1/3] [#16606] - feat: added events and events manager to storage and cache classes. --- .gitignore | 7 + CHANGELOG-5.0.md | 14 + composer.json | 21 +- phalcon/Cache/AbstractCache.zep | 90 ++- phalcon/Cache/Adapter/Apcu.zep | 1 + phalcon/Cache/Adapter/Libmemcached.zep | 1 + phalcon/Cache/Adapter/Memory.zep | 1 + phalcon/Cache/Adapter/Redis.zep | 1 + phalcon/Cache/Adapter/Stream.zep | 1 + phalcon/Cache/Adapter/Weak.zep | 1 + phalcon/Storage/Adapter/AbstractAdapter.zep | 57 +- phalcon/Storage/Adapter/Apcu.zep | 44 +- phalcon/Storage/Adapter/Libmemcached.zep | 51 +- phalcon/Storage/Adapter/Memory.zep | 34 +- phalcon/Storage/Adapter/Redis.zep | 49 +- phalcon/Storage/Adapter/Stream.zep | 65 +- phalcon/Storage/Adapter/Weak.zep | 42 +- psalm.xml | 2 + tests/cli/Cli/Console/HandleCest.php | 3 +- .../Cache/Adapter/Apcu/EventsCest.php | 108 ++++ .../Cache/Adapter/Libmemcached/EventsCest.php | 108 ++++ .../Cache/Adapter/Memory/EventsCest.php | 108 ++++ .../Cache/Adapter/Redis/EventsCest.php | 108 ++++ .../Cache/Adapter/Stream/EventsCest.php | 123 ++++ .../Cache/Adapter/Weak/EventsCest.php | 108 ++++ .../Cache/Cache/GetEventsManagerCest.php | 118 ++++ .../Storage/Adapter/EventsCest.php | 594 ++++++++++++++++++ 27 files changed, 1820 insertions(+), 40 deletions(-) create mode 100644 tests/integration/Cache/Adapter/Apcu/EventsCest.php create mode 100644 tests/integration/Cache/Adapter/Libmemcached/EventsCest.php create mode 100644 tests/integration/Cache/Adapter/Memory/EventsCest.php create mode 100644 tests/integration/Cache/Adapter/Redis/EventsCest.php create mode 100644 tests/integration/Cache/Adapter/Stream/EventsCest.php create mode 100644 tests/integration/Cache/Adapter/Weak/EventsCest.php create mode 100644 tests/integration/Cache/Cache/GetEventsManagerCest.php create mode 100644 tests/integration/Storage/Adapter/EventsCest.php diff --git a/.gitignore b/.gitignore index 102236d6c5f..70283d979ea 100644 --- a/.gitignore +++ b/.gitignore @@ -62,6 +62,10 @@ autom4te.cache/ .zephir/ .temp/ +# JetBrains IDE +*.iml +.idea/ + # Zephir compiler zephir.phar zephir-*.phar @@ -75,3 +79,6 @@ tests/_output/* .php_cs.cache .env zephir + +# Docker +docker-compose.override.yml diff --git a/CHANGELOG-5.0.md b/CHANGELOG-5.0.md index 87b5840d2b0..999ce3298b3 100644 --- a/CHANGELOG-5.0.md +++ b/CHANGELOG-5.0.md @@ -8,6 +8,20 @@ - Changed the `Phalcon\Mvc\Router\Group::getHostname()` to return `null` also. [#16601](https://github.com/phalcon/cphalcon/issues/16601) ### Added + +- Added events and `Phalcon\Events\Manager` for `Phalcon\Storage\Adapter\Apcu`, + `Phalcon\Storage\Adapter\Redis`, + `Phalcon\Storage\Adapter\Memory`, + `Phalcon\Storage\Adapter\Libmemcached`, + `Phalcon\Storage\Adapter\Stream`, + `Phalcon\Storage\Adapter\Weak`, + `Phalcon\Cache\Adapter\Apcu`, + `Phalcon\Cache\Adapter\Redis`, + `Phalcon\Cache\Adapter\Memory`, + `Phalcon\Cache\Adapter\Libmemcached`, + `Phalcon\Cache\Adapter\Stream`, + `Phalcon\Cache\Adapter\Weak` + `Phalcon\Cache\AbstractCache`. [#16606](https://github.com/phalcon/cphalcon/issues/16606) ### Fixed diff --git a/composer.json b/composer.json index 48aad511f2f..156d61d11a9 100644 --- a/composer.json +++ b/composer.json @@ -100,8 +100,23 @@ "test-db-mysql": "php -d extension=ext/modules/phalcon.so ./vendor/bin/codecept run --ext DotReporter database --env mysql -g mysql", "test-db-pgsql": "php -d extension=ext/modules/phalcon.so ./vendor/bin/codecept run --ext DotReporter database --env pgsql -g pgsql", "test-db-sqlite": "php -d extension=ext/modules/phalcon.so ./vendor/bin/codecept run --ext DotReporter database --env sqlite -g sqlite", - "test-db": "composer test-db-common && composer test-db-mysql && composer test-db-sqlite", - "test-all": "composer test-unit && composer test-cli && composer test-integration && composer test-db", - "analyze": "php -d extension=ext/modules/phalcon.so ./vendor/bin/psalm" + "test-db": [ + "@test-db-common", + "@test-db-mysql", + "@test-db-sqlite" + ], + "test-all": [ + "@test-unit", + "@test-cli", + "@test-integration", + "@test-db" + ], + "analyze": "php -d extension=ext/modules/phalcon.so ./vendor/bin/psalm --no-cache", + "clean-build-stubs": [ + "./vendor/bin/zephir fullclean", + "Composer\\Config::disableProcessTimeout", + "./vendor/bin/zephir build", + "./vendor/bin/zephir stubs" + ] } } diff --git a/phalcon/Cache/AbstractCache.zep b/phalcon/Cache/AbstractCache.zep index 06cd30a7111..268a77c0e4a 100644 --- a/phalcon/Cache/AbstractCache.zep +++ b/phalcon/Cache/AbstractCache.zep @@ -13,6 +13,7 @@ namespace Phalcon\Cache; use DateInterval; use Phalcon\Cache\Adapter\AdapterInterface; use Phalcon\Cache\Exception\InvalidArgumentException; +use Phalcon\Events\ManagerInterface; use Traversable; /** @@ -27,6 +28,13 @@ abstract class AbstractCache implements CacheInterface */ protected adapter; + /** + * Event Manager + * + * @var ManagerInterface|null + */ + protected eventsManager = null; + /** * Constructor. * @@ -47,6 +55,22 @@ abstract class AbstractCache implements CacheInterface return this->adapter; } + /** + * Sets the event manager + */ + public function setEventsManager( eventsManager) -> void + { + let this->eventsManager = eventsManager; + } + + /** + * Get the event manager + */ + public function getEventsManager() -> | null + { + return this->eventsManager; + } + /** * Checks the key. If it contains invalid characters an exception is thrown * @@ -108,9 +132,17 @@ abstract class AbstractCache implements CacheInterface */ protected function doDelete(string key) -> bool { + var result; + + this->fire("cache:beforeDelete"); + this->checkKey(key); - return this->adapter->delete(key); + let result = this->adapter->delete(key); + + this->fire("cache:afterDelete"); + + return result; } /** @@ -122,6 +154,8 @@ abstract class AbstractCache implements CacheInterface this->checkKeys(keys); + this->fire("cache:beforeDeleteMultiple"); + let result = true; for key in keys { if (true !== this->adapter->delete(key)) { @@ -129,6 +163,8 @@ abstract class AbstractCache implements CacheInterface } } + this->fire("cache:afterDeleteMultiple"); + return result; } @@ -146,9 +182,17 @@ abstract class AbstractCache implements CacheInterface */ protected function doGet(string key, var defaultValue = null) -> var { + var result; + this->checkKey(key); - return this->adapter->get(key, defaultValue); + this->fire("cache:beforeGet"); + + let result = this->adapter->get(key, defaultValue); + + this->fire("cache:afterGet"); + + return result; } /** @@ -160,11 +204,15 @@ abstract class AbstractCache implements CacheInterface this->checkKeys(keys); + this->fire("cache:beforeGetMultiple"); + let results = []; for element in keys { let results[element] = this->get(element, defaultValue); } + this->fire("cache:afterGetMultiple"); + return results; } @@ -180,9 +228,17 @@ abstract class AbstractCache implements CacheInterface */ protected function doHas(string key) -> bool { + var result; + this->checkKey(key); - return this->adapter->has(key); + this->fire("cache:beforeHas"); + + let result = this->adapter->has(key); + + this->fire("cache:afterHas"); + + return result; } /** @@ -205,9 +261,17 @@ abstract class AbstractCache implements CacheInterface */ protected function doSet(string key, var value, var ttl = null) -> bool { + var result; + this->checkKey(key); - return this->adapter->set(key, value, ttl); + this->fire("cache:beforeSet"); + + let result = this->adapter->set(key, value, ttl); + + this->fire("cache:afterSet"); + + return result; } /** @@ -219,6 +283,8 @@ abstract class AbstractCache implements CacheInterface this->checkKeys(values); + this->fire("cache:beforeSetMultiple"); + let result = true; for key, value in values { if (true !== this->set(key, value, ttl)) { @@ -226,9 +292,25 @@ abstract class AbstractCache implements CacheInterface } } + this->fire("cache:afterSetMultiple"); + return result; } + /** + * Trigger an event for the eventsManager. + * + * @var string $eventName + */ + protected function fire(string eventName) -> void + { + if (this->eventsManager === null) { + return; + } + + this->eventsManager->fire(eventName, this); + } + /** * Returns the exception class that will be used for exceptions thrown * diff --git a/phalcon/Cache/Adapter/Apcu.zep b/phalcon/Cache/Adapter/Apcu.zep index 1ed18a445b3..def73329f8e 100644 --- a/phalcon/Cache/Adapter/Apcu.zep +++ b/phalcon/Cache/Adapter/Apcu.zep @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Apcu as StorageApcu; */ class Apcu extends StorageApcu implements CacheAdapterInterface { + protected eventType = "cache"; } diff --git a/phalcon/Cache/Adapter/Libmemcached.zep b/phalcon/Cache/Adapter/Libmemcached.zep index 71fc6085d26..cff1f415875 100644 --- a/phalcon/Cache/Adapter/Libmemcached.zep +++ b/phalcon/Cache/Adapter/Libmemcached.zep @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Libmemcached as StorageLibmemcached; */ class Libmemcached extends StorageLibmemcached implements CacheAdapterInterface { + protected eventType = "cache"; } diff --git a/phalcon/Cache/Adapter/Memory.zep b/phalcon/Cache/Adapter/Memory.zep index ea7407ec93f..30966f0fc0b 100644 --- a/phalcon/Cache/Adapter/Memory.zep +++ b/phalcon/Cache/Adapter/Memory.zep @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Memory as StorageMemory; */ class Memory extends StorageMemory implements CacheAdapterInterface { + protected eventType = "cache"; } diff --git a/phalcon/Cache/Adapter/Redis.zep b/phalcon/Cache/Adapter/Redis.zep index 738f7a681a1..89cec52f31e 100644 --- a/phalcon/Cache/Adapter/Redis.zep +++ b/phalcon/Cache/Adapter/Redis.zep @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Redis as StorageRedis; */ class Redis extends StorageRedis implements CacheAdapterInterface { + protected eventType = "cache"; } diff --git a/phalcon/Cache/Adapter/Stream.zep b/phalcon/Cache/Adapter/Stream.zep index 60bf7c19bdf..6c6ba6a0865 100644 --- a/phalcon/Cache/Adapter/Stream.zep +++ b/phalcon/Cache/Adapter/Stream.zep @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Stream as StorageStream; */ class Stream extends StorageStream implements CacheAdapterInterface { + protected eventType = "cache"; } diff --git a/phalcon/Cache/Adapter/Weak.zep b/phalcon/Cache/Adapter/Weak.zep index 0999926c89e..afd07c09e5d 100644 --- a/phalcon/Cache/Adapter/Weak.zep +++ b/phalcon/Cache/Adapter/Weak.zep @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Weak as StorageWeak; */ class Weak extends StorageWeak implements CacheAdapterInterface { + protected eventType = "cache"; } diff --git a/phalcon/Storage/Adapter/AbstractAdapter.zep b/phalcon/Storage/Adapter/AbstractAdapter.zep index f8a6cc523a7..8ef2aa07865 100644 --- a/phalcon/Storage/Adapter/AbstractAdapter.zep +++ b/phalcon/Storage/Adapter/AbstractAdapter.zep @@ -13,6 +13,7 @@ namespace Phalcon\Storage\Adapter; use DateInterval; use DateTime; use Exception; +use Phalcon\Events\ManagerInterface; use Phalcon\Storage\Serializer\SerializerInterface; use Phalcon\Storage\SerializerFactory; use Phalcon\Support\Exception as SupportException; @@ -75,6 +76,20 @@ abstract class AbstractAdapter implements AdapterInterface */ protected serializerFactory; + /** + * Event Manager + * + * @var ManagerInterface|null + */ + protected eventsManager = null; + + /** + * EventType prefix. + * + * @var string + */ + protected eventType = "storage"; + /** * AbstractAdapter constructor. * @@ -143,15 +158,23 @@ abstract class AbstractAdapter implements AdapterInterface */ public function get(string key, defaultValue = null) -> var { - var content; + var content, result; + + this->fire(this->eventType . ":beforeGet"); if (true !== this->has(key)) { + this->fire(this->eventType . ":afterGet"); + return defaultValue; } let content = this->doGet(key); - return this->getUnserializedData(content, defaultValue); + let result = this->getUnserializedData(content, defaultValue); + + this->fire(this->eventType . ":afterGet"); + + return result; } /** @@ -384,4 +407,34 @@ abstract class AbstractAdapter implements AdapterInterface return value; } + + /** + * Sets the event manager + */ + public function setEventsManager( eventsManager) -> void + { + let this->eventsManager = eventsManager; + } + + /** + * Get the event manager + */ + public function getEventsManager() -> | null + { + return this->eventsManager; + } + + /** + * Trigger an event for the eventsManager. + * + * @var string $eventName + */ + protected function fire(string eventName) -> void + { + if (this->eventsManager === null) { + return; + } + + this->eventsManager->fire(eventName, this); + } } diff --git a/phalcon/Storage/Adapter/Apcu.zep b/phalcon/Storage/Adapter/Apcu.zep index 6f3b8c3fd7f..59346eeb985 100644 --- a/phalcon/Storage/Adapter/Apcu.zep +++ b/phalcon/Storage/Adapter/Apcu.zep @@ -78,7 +78,15 @@ class Apcu extends AbstractAdapter */ public function decrement(string! key, int value = 1) -> int | bool { - return this->phpApcuDec(this->getPrefixedKey(key), value); + var result; + + this->fire(this->eventType . ":beforeDecrement"); + + let result = this->phpApcuDec(this->getPrefixedKey(key), value); + + this->fire(this->eventType . ":afterDecrement"); + + return result; } /** @@ -90,7 +98,15 @@ class Apcu extends AbstractAdapter */ public function delete(string! key) -> bool { - return (bool) this->phpApcuDelete(this->getPrefixedKey(key)); + var result; + + this->fire(this->eventType . ":beforeDelete"); + + let result = (bool) this->phpApcuDelete(this->getPrefixedKey(key)); + + this->fire(this->eventType . ":afterDelete"); + + return result; } /** @@ -131,8 +147,12 @@ class Apcu extends AbstractAdapter { var result; + this->fire(this->eventType . ":beforeHas"); + let result = this->phpApcuExists(this->getPrefixedKey(key)); + this->fire(this->eventType . ":afterHas"); + return typeof result === "bool" ? result : false; } @@ -146,7 +166,15 @@ class Apcu extends AbstractAdapter */ public function increment(string! key, int value = 1) -> int | bool { - return this->phpApcuInc(this->getPrefixedKey(key), value); + var result; + + this->fire(this->eventType . ":beforeIncrement"); + + let result = this->phpApcuInc(this->getPrefixedKey(key), value); + + this->fire(this->eventType . ":afterIncrement"); + + return result; } /** @@ -167,8 +195,14 @@ class Apcu extends AbstractAdapter { var result; + this->fire(this->eventType . ":beforeSet"); + if (typeof ttl === "integer" && ttl < 1) { - return this->delete(key); + let result = this->delete(key); + + this->fire(this->eventType . ":afterSet"); + + return result; } let result = this->phpApcuStore( @@ -177,6 +211,8 @@ class Apcu extends AbstractAdapter this->getTtl(ttl) ); + this->fire(this->eventType . ":afterSet"); + return typeof result === "bool" ? result : false; } diff --git a/phalcon/Storage/Adapter/Libmemcached.zep b/phalcon/Storage/Adapter/Libmemcached.zep index b533bce2c80..6e0a7e8e8c9 100644 --- a/phalcon/Storage/Adapter/Libmemcached.zep +++ b/phalcon/Storage/Adapter/Libmemcached.zep @@ -71,7 +71,15 @@ class Libmemcached extends AbstractAdapter */ public function decrement(string! key, int value = 1) -> int | bool { - return this->getAdapter()->decrement(key, value); + var result; + + this->fire(this->eventType . ":beforeDecrement"); + + let result = this->getAdapter()->decrement(key, value); + + this->fire(this->eventType . ":afterDecrement"); + + return result; } /** @@ -84,7 +92,15 @@ class Libmemcached extends AbstractAdapter */ public function delete(string! key) -> bool { - return this->getAdapter()->delete(key, 0); + var result; + + this->fire(this->eventType . ":beforeDelete"); + + let result = this->getAdapter()->delete(key, 0); + + this->fire(this->eventType . ":afterDelete"); + + return result; } /** @@ -163,12 +179,17 @@ class Libmemcached extends AbstractAdapter */ public function has(string! key) -> bool { - var connection, result; + var connection, result, code; + + this->fire(this->eventType . ":beforeHas"); let connection = this->getAdapter(), - result = connection->get(key); + result = connection->get(key), + code = connection->getResultCode(); - return \Memcached::RES_NOTFOUND !== connection->getResultCode(); + this->fire(this->eventType . ":afterHas"); + + return \Memcached::RES_NOTFOUND !== code; } /** @@ -182,7 +203,15 @@ class Libmemcached extends AbstractAdapter */ public function increment(string! key, int value = 1) -> int | bool { - return this->getAdapter()->increment(key, value); + var result; + + this->fire(this->eventType . ":beforeIncrement"); + + let result = this->getAdapter()->increment(key, value); + + this->fire(this->eventType . ":afterIncrement"); + + return result; } /** @@ -204,8 +233,14 @@ class Libmemcached extends AbstractAdapter { var result; + this->fire(this->eventType . ":beforeSet"); + if (typeof ttl === "integer" && ttl < 1) { - return this->delete(key); + let result = this->delete(key); + + this->fire(this->eventType . ":afterSet"); + + return result; } let result = this->getAdapter() @@ -216,6 +251,8 @@ class Libmemcached extends AbstractAdapter ) ; + this->fire(this->eventType . ":afterSet"); + return typeof result === "bool" ? result : false; } diff --git a/phalcon/Storage/Adapter/Memory.zep b/phalcon/Storage/Adapter/Memory.zep index 605275c2ef9..3c3db78b42b 100644 --- a/phalcon/Storage/Adapter/Memory.zep +++ b/phalcon/Storage/Adapter/Memory.zep @@ -65,6 +65,8 @@ class Memory extends AbstractAdapter { var current, newValue, prefixedKey, result; + this->fire(this->eventType . ":beforeDecrement"); + let prefixedKey = this->getPrefixedKey(key), result = array_key_exists(prefixedKey, this->data); @@ -76,6 +78,8 @@ class Memory extends AbstractAdapter let this->data[prefixedKey] = newValue; } + this->fire(this->eventType . ":afterDecrement"); + return result; } @@ -90,11 +94,15 @@ class Memory extends AbstractAdapter { var exists, prefixedKey; + this->fire(this->eventType . ":beforeDelete"); + let prefixedKey = this->getPrefixedKey(key), exists = array_key_exists(prefixedKey, this->data); unset(this->data[prefixedKey]); + this->fire(this->eventType . ":afterDelete"); + return exists; } @@ -119,11 +127,17 @@ class Memory extends AbstractAdapter */ public function has(string! key) -> bool { - var prefixedKey; + var prefixedKey, result; let prefixedKey = this->getPrefixedKey(key); - return array_key_exists(prefixedKey, this->data); + this->fire(this->eventType . ":beforeHas"); + + let result = array_key_exists(prefixedKey, this->data); + + this->fire(this->eventType . ":afterHas"); + + return result; } /** @@ -138,6 +152,8 @@ class Memory extends AbstractAdapter { var current, newValue, prefixedKey, result; + this->fire(this->eventType . ":beforeIncrement"); + let prefixedKey = this->getPrefixedKey(key), result = array_key_exists(prefixedKey, this->data); @@ -149,6 +165,8 @@ class Memory extends AbstractAdapter let this->data[prefixedKey] = newValue; } + this->fire(this->eventType . ":afterIncrement"); + return result; } @@ -168,10 +186,16 @@ class Memory extends AbstractAdapter */ public function set(string! key, var value, var ttl = null) -> bool { - var content, prefixedKey; + var content, prefixedKey, result; + + this->fire(this->eventType . ":beforeSet"); if (typeof ttl === "integer" && ttl < 1) { - return this->delete(key); + let result = this->delete(key); + + this->fire(this->eventType . ":afterSet"); + + return result; } let content = this->getSerializedData(value), @@ -179,6 +203,8 @@ class Memory extends AbstractAdapter let this->data[prefixedKey] = content; + this->fire(this->eventType . ":afterSet"); + return true; } diff --git a/phalcon/Storage/Adapter/Redis.zep b/phalcon/Storage/Adapter/Redis.zep index ad1bb7caa99..daf918eb13a 100644 --- a/phalcon/Storage/Adapter/Redis.zep +++ b/phalcon/Storage/Adapter/Redis.zep @@ -90,7 +90,15 @@ class Redis extends AbstractAdapter */ public function decrement(string! key, int value = 1) -> int | bool { - return this->getAdapter()->decrBy(key, value); + var result; + + this->fire(this->eventType . ":beforeDecrement"); + + let result = this->getAdapter()->decrBy(key, value); + + this->fire(this->eventType . ":afterDecrement"); + + return result; } /** @@ -103,7 +111,15 @@ class Redis extends AbstractAdapter */ public function delete(string! key) -> bool { - return (bool) this->getAdapter()->del(key); + var result; + + this->fire(this->eventType . ":beforeDelete"); + + let result = (bool) this->getAdapter()->del(key); + + this->fire(this->eventType . ":afterDelete"); + + return result; } /** @@ -161,7 +177,15 @@ class Redis extends AbstractAdapter */ public function has(string! key) -> bool { - return (bool) this->getAdapter()->exists(key); + var result; + + this->fire(this->eventType . ":beforeHas"); + + let result = (bool) this->getAdapter()->exists(key); + + this->fire(this->eventType . ":afterHas"); + + return result; } /** @@ -175,7 +199,15 @@ class Redis extends AbstractAdapter */ public function increment(string! key, int value = 1) -> int | bool { - return this->getAdapter()->incrBy(key, value); + var result; + + this->fire(this->eventType . ":beforeIncrement"); + + let result = this->getAdapter()->incrBy(key, value); + + this->fire(this->eventType . ":afterIncrement"); + + return result; } /** @@ -196,8 +228,13 @@ class Redis extends AbstractAdapter { var result; + this->fire(this->eventType . ":beforeSet"); + if (typeof ttl === "integer" && ttl < 1) { - return this->delete(key); + let result = this->delete(key); + this->fire(this->eventType . ":afterSet"); + + return result; } let result = this->getAdapter() @@ -208,6 +245,8 @@ class Redis extends AbstractAdapter ) ; + this->fire(this->eventType . ":afterSet"); + return typeof result === "bool" ? result : false; } diff --git a/phalcon/Storage/Adapter/Stream.zep b/phalcon/Storage/Adapter/Stream.zep index a3e50b76aff..6c32157a465 100644 --- a/phalcon/Storage/Adapter/Stream.zep +++ b/phalcon/Storage/Adapter/Stream.zep @@ -103,7 +103,11 @@ class Stream extends AbstractAdapter { var data, result; + this->fire(this->eventType . ":beforeDecrement"); + if unlikely true !== this->has(key) { + this->fire(this->eventType . ":afterDecrement"); + return false; } @@ -115,6 +119,8 @@ class Stream extends AbstractAdapter let result = data; } + this->fire(this->eventType . ":afterDecrement"); + return result; } @@ -127,15 +133,23 @@ class Stream extends AbstractAdapter */ public function delete(string! key) -> bool { - var filepath; + var filepath, result; + + this->fire(this->eventType . ":beforeDelete"); if true !== this->has(key) { + this->fire(this->eventType . ":afterDelete"); + return false; } let filepath = this->getFilepath(key); - return unlink(filepath); + let result = unlink(filepath); + + this->fire(this->eventType . ":afterDelete"); + + return result; } /** @@ -148,11 +162,15 @@ class Stream extends AbstractAdapter */ public function get(string! key, var defaultValue = null) -> var { - var content, filepath, payload; + var content, filepath, payload, result; + + this->fire(this->eventType . ":beforeGet"); let filepath = this->getFilepath(key); if (true !== file_exists(filepath)) { + this->fire(this->eventType . ":afterGet"); + return defaultValue; } @@ -164,7 +182,11 @@ class Stream extends AbstractAdapter let content = this->getArrVal(payload, "content"); - return this->getUnserializedData(content, defaultValue); + let result = this->getUnserializedData(content, defaultValue); + + this->fire(this->eventType . ":afterGet"); + + return result; } /** @@ -206,21 +228,31 @@ class Stream extends AbstractAdapter */ public function has(string! key) -> bool { - var payload, filepath; + var payload, filepath, result; + + this->fire(this->eventType . ":beforeHas"); let filepath = this->getFilepath(key); if unlikely true !== this->phpFileExists(filepath) { + this->fire(this->eventType . ":afterHas"); + return false; } let payload = this->getPayload(filepath); if unlikely empty payload { + this->fire(this->eventType . ":afterHas"); + return false; } - return !this->isExpired(payload); + let result = !this->isExpired(payload); + + this->fire(this->eventType . ":afterHas"); + + return result; } /** @@ -235,7 +267,11 @@ class Stream extends AbstractAdapter { var data, result; + this->fire(this->eventType . ":beforeIncrement"); + if unlikely true !== this->has(key) { + this->fire(this->eventType . ":afterIncrement"); + return false; } @@ -247,6 +283,8 @@ class Stream extends AbstractAdapter let result = data; } + this->fire(this->eventType . ":afterIncrement"); + return result; } @@ -266,9 +304,16 @@ class Stream extends AbstractAdapter public function set(string! key, var value, var ttl = null) -> bool { array payload; + var result; + + this->fire(this->eventType . ":beforeSet"); if (typeof ttl === "integer" && ttl < 1) { - return this->delete(key); + let result = this->delete(key); + + this->fire(this->eventType . ":afterSet"); + + return result; } let payload = [ @@ -277,7 +322,11 @@ class Stream extends AbstractAdapter "content" : this->getSerializedData(value) ]; - return this->storePayload(payload, key); + let result = this->storePayload(payload, key); + + this->fire(this->eventType . ":afterSet"); + + return result; } /** diff --git a/phalcon/Storage/Adapter/Weak.zep b/phalcon/Storage/Adapter/Weak.zep index d4049704658..e09633c5c85 100644 --- a/phalcon/Storage/Adapter/Weak.zep +++ b/phalcon/Storage/Adapter/Weak.zep @@ -73,6 +73,9 @@ class Weak extends AbstractAdapter */ public function decrement(string! key, int value = 1) -> int | bool { + this->fire(this->eventType . ":beforeDecrement"); + this->fire(this->eventType . ":afterDecrement"); + return false; } @@ -86,11 +89,19 @@ class Weak extends AbstractAdapter public function delete(string! key) -> bool { var exists; + + this->fire(this->eventType . ":beforeDelete"); + if key === this->fetching { + this->fire(this->eventType . ":afterDelete"); + return false; } let exists = isset this->weakList[key]; unset(this->weakList[key]); + + this->fire(this->eventType . ":afterDelete"); + return exists; } @@ -128,6 +139,9 @@ class Weak extends AbstractAdapter */ public function increment(string! key, int value = 1) -> int | bool { + this->fire(this->eventType . ":beforeIncrement"); + this->fire(this->eventType . ":afterIncrement"); + return false; } @@ -142,6 +156,9 @@ class Weak extends AbstractAdapter public function get(string! key, var defaultValue = null) -> var { var value, wr; + + this->fire(this->eventType . ":beforeGet"); + /** * while getting a key, garbage collection might be triggered, * this will stop unsetting the key, will not stop however the model gets destroid by GC, @@ -151,6 +168,9 @@ class Weak extends AbstractAdapter let this->fetching = key; if false === isset this->weakList[key] { let this->fetching = null; + + this->fire(this->eventType . ":afterGet"); + return defaultValue; } let wr = this->weakList[key]; @@ -162,6 +182,9 @@ class Weak extends AbstractAdapter if null === value { this->delete(key); } + + this->fire(this->eventType . ":afterGet"); + return value; } @@ -174,7 +197,15 @@ class Weak extends AbstractAdapter */ public function has(string! key) -> bool { - return isset this->weakList[key]; + var result; + + this->fire(this->eventType . ":beforeHas"); + + let result = isset this->weakList[key]; + + this->fire(this->eventType . ":afterHas"); + + return result; } /** @@ -193,12 +224,19 @@ class Weak extends AbstractAdapter */ public function set(string! key, var value, var ttl = null) -> bool { + this->fire(this->eventType . ":beforeSet"); + if typeof value !== "object" { + this->fire(this->eventType . ":afterSet"); + return false; } if false === isset this->weakList[key] { let this->weakList[key] = \WeakReference::create(value); } + + this->fire(this->eventType . ":afterSet"); + return true; } @@ -214,7 +252,7 @@ class Weak extends AbstractAdapter { return this->set(key, value); } - + /** * will never set a serializer, WeakReference cannot be serialized * @param string $serializer diff --git a/psalm.xml b/psalm.xml index f4d7a7699be..544cd6070ee 100644 --- a/psalm.xml +++ b/psalm.xml @@ -2,6 +2,8 @@ + * 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 Codeception\Example; +use IntegrationTester; +use Phalcon\Cache\Adapter\Apcu; +use Phalcon\Events\Manager; +use Phalcon\Storage\SerializerFactory; + +class EventsCest +{ + /** + * Tests Cache\Adapter\Apcu :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterMemoryGetEventsManagerNotSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Apcu - getEventsManager() - not set'); + + $serializer = new SerializerFactory(); + $adapter = new Apcu($serializer); + + $I->assertNull($adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Apcu :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterMemoryGetEventsManagerSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Apcu - getEventsManager() - set'); + + $serializer = new SerializerFactory(); + $adapter = new Apcu($serializer); + + $adapter->setEventsManager(new Manager()); + + $I->assertInstanceOf(Manager::class, $adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Apcu :: trigger cache events + * + * @dataProvider getEvents + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): void + { + $I->wantToTest('Cache\Adapter\Apcu - triggered ' . $example->offsetGet(0)); + + $serializer = new SerializerFactory(); + $adapter = new Apcu($serializer); + + $counter = 0; + $manager = new Manager(); + $adapter->setEventsManager(new Manager()); + + $manager->attach( + 'cache:' . $example->offsetGet(0), + static function () use (&$counter): void { + $counter++; + } + ); + + $adapter->setEventsManager($manager); + + $I->assertInstanceOf($manager::class, $adapter->getEventsManager()); + + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + $I->assertEquals(2, $counter); + } + + public function getEvents(): array + { + // Event, Method, Data + return [ + ['beforeDelete', 'delete', ['test']], + ['afterDelete', 'delete', ['test']], + ['beforeIncrement', 'increment', ['test']], + ['afterIncrement', 'increment', ['test']], + ['beforeGet', 'get', ['test']], + ['afterGet', 'get', ['test']], + ['beforeDecrement', 'decrement', ['test']], + ['afterDecrement', 'decrement', ['test']], + ['beforeHas', 'has', ['test']], + ['afterHas', 'has', ['test']], + ['beforeSet', 'set', ['test', 'test']], + ['afterSet', 'set', ['test', 'test']], + ]; + } +} diff --git a/tests/integration/Cache/Adapter/Libmemcached/EventsCest.php b/tests/integration/Cache/Adapter/Libmemcached/EventsCest.php new file mode 100644 index 00000000000..300cb146fd3 --- /dev/null +++ b/tests/integration/Cache/Adapter/Libmemcached/EventsCest.php @@ -0,0 +1,108 @@ + + * 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 Codeception\Example; +use IntegrationTester; +use Phalcon\Cache\Adapter\Libmemcached; +use Phalcon\Events\Manager; +use Phalcon\Storage\SerializerFactory; + +class EventsCest +{ + /** + * Tests Cache\Adapter\Libmemcached :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterMemoryGetEventsManagerNotSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Libmemcached - getEventsManager() - not set'); + + $serializer = new SerializerFactory(); + $adapter = new Libmemcached($serializer, getOptionsLibmemcached()); + + $I->assertNull($adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Libmemcached :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterMemoryGetEventsManagerSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Libmemcached - getEventsManager() - set'); + + $serializer = new SerializerFactory(); + $adapter = new Libmemcached($serializer, getOptionsLibmemcached()); + + $adapter->setEventsManager(new Manager()); + + $I->assertInstanceOf(Manager::class, $adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Libmemcached :: trigger cache events + * + * @dataProvider getEvents + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): void + { + $I->wantToTest('Cache\Adapter\Libmemcached - triggered ' . $example->offsetGet(0)); + + $serializer = new SerializerFactory(); + $adapter = new Libmemcached($serializer, getOptionsLibmemcached()); + + $counter = 0; + $manager = new Manager(); + $adapter->setEventsManager(new Manager()); + + $manager->attach( + 'cache:' . $example->offsetGet(0), + static function () use (&$counter): void { + $counter++; + } + ); + + $adapter->setEventsManager($manager); + + $I->assertInstanceOf($manager::class, $adapter->getEventsManager()); + + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + $I->assertEquals(2, $counter); + } + + public function getEvents(): array + { + // Event, Method, Data + return [ + ['beforeDelete', 'delete', ['test']], + ['afterDelete', 'delete', ['test']], + ['beforeIncrement', 'increment', ['test']], + ['afterIncrement', 'increment', ['test']], + ['beforeGet', 'get', ['test']], + ['afterGet', 'get', ['test']], + ['beforeDecrement', 'decrement', ['test']], + ['afterDecrement', 'decrement', ['test']], + ['beforeHas', 'has', ['test']], + ['afterHas', 'has', ['test']], + ['beforeSet', 'set', ['test', 'test']], + ['afterSet', 'set', ['test', 'test']], + ]; + } +} diff --git a/tests/integration/Cache/Adapter/Memory/EventsCest.php b/tests/integration/Cache/Adapter/Memory/EventsCest.php new file mode 100644 index 00000000000..e8acc0829fd --- /dev/null +++ b/tests/integration/Cache/Adapter/Memory/EventsCest.php @@ -0,0 +1,108 @@ + + * 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 Codeception\Example; +use IntegrationTester; +use Phalcon\Cache\Adapter\Memory; +use Phalcon\Events\Manager; +use Phalcon\Storage\SerializerFactory; + +class EventsCest +{ + /** + * Tests Cache\Adapter\Memory :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterMemoryGetEventsManagerNotSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Memory - getEventsManager() - not set'); + + $serializer = new SerializerFactory(); + $adapter = new Memory($serializer); + + $I->assertNull($adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Memory :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterMemoryGetEventsManagerSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Memory - getEventsManager() - set'); + + $serializer = new SerializerFactory(); + $adapter = new Memory($serializer); + + $adapter->setEventsManager(new Manager()); + + $I->assertInstanceOf(Manager::class, $adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Memory :: trigger cache events + * + * @dataProvider getEvents + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): void + { + $I->wantToTest('Cache\Adapter\Memory - triggered ' . $example->offsetGet(0)); + + $serializer = new SerializerFactory(); + $adapter = new Memory($serializer); + + $counter = 0; + $manager = new Manager(); + $adapter->setEventsManager(new Manager()); + + $manager->attach( + 'cache:' . $example->offsetGet(0), + static function () use (&$counter): void { + $counter++; + } + ); + + $adapter->setEventsManager($manager); + + $I->assertInstanceOf($manager::class, $adapter->getEventsManager()); + + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + $I->assertEquals(2, $counter); + } + + public function getEvents(): array + { + // Event, Method, Data + return [ + ['beforeDelete', 'delete', ['test']], + ['afterDelete', 'delete', ['test']], + ['beforeIncrement', 'increment', ['test']], + ['afterIncrement', 'increment', ['test']], + ['beforeGet', 'get', ['test']], + ['afterGet', 'get', ['test']], + ['beforeDecrement', 'decrement', ['test']], + ['afterDecrement', 'decrement', ['test']], + ['beforeHas', 'has', ['test']], + ['afterHas', 'has', ['test']], + ['beforeSet', 'set', ['test', 'test']], + ['afterSet', 'set', ['test', 'test']], + ]; + } +} diff --git a/tests/integration/Cache/Adapter/Redis/EventsCest.php b/tests/integration/Cache/Adapter/Redis/EventsCest.php new file mode 100644 index 00000000000..e267cf5975c --- /dev/null +++ b/tests/integration/Cache/Adapter/Redis/EventsCest.php @@ -0,0 +1,108 @@ + + * 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\Redis; + +use Codeception\Example; +use IntegrationTester; +use Phalcon\Cache\Adapter\Redis; +use Phalcon\Events\Manager; +use Phalcon\Storage\SerializerFactory; + +class EventsCest +{ + /** + * Tests Cache\Adapter\Redis :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterRedisGetEventsManagerNotSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Redis - getEventsManager() - not set'); + + $serializer = new SerializerFactory(); + $adapter = new Redis($serializer, getOptionsRedis()); + + $I->assertNull($adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Redis :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterRedisGetEventsManagerSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Redis - getEventsManager() - set'); + + $serializer = new SerializerFactory(); + $adapter = new Redis($serializer, getOptionsRedis()); + + $adapter->setEventsManager(new Manager()); + + $I->assertInstanceOf(Manager::class, $adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Redis :: trigger cache events + * + * @dataProvider getEvents + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): void + { + $I->wantToTest('Cache\Adapter\Redis - triggered ' . $example->offsetGet(0)); + + $serializer = new SerializerFactory(); + $adapter = new Redis($serializer, getOptionsRedis()); + + $counter = 0; + $manager = new Manager(); + $adapter->setEventsManager(new Manager()); + + $manager->attach( + 'cache:' . $example->offsetGet(0), + static function () use (&$counter): void { + $counter++; + } + ); + + $adapter->setEventsManager($manager); + + $I->assertInstanceOf($manager::class, $adapter->getEventsManager()); + + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + $I->assertEquals(2, $counter); + } + + public function getEvents(): array + { + // Event, Method, Data + return [ + ['beforeDelete', 'delete', ['test']], + ['afterDelete', 'delete', ['test']], + ['beforeIncrement', 'increment', ['test']], + ['afterIncrement', 'increment', ['test']], + ['beforeGet', 'get', ['test']], + ['afterGet', 'get', ['test']], + ['beforeDecrement', 'decrement', ['test']], + ['afterDecrement', 'decrement', ['test']], + ['beforeHas', 'has', ['test']], + ['afterHas', 'has', ['test']], + ['beforeSet', 'set', ['test', 'test']], + ['afterSet', 'set', ['test', 'test']], + ]; + } +} diff --git a/tests/integration/Cache/Adapter/Stream/EventsCest.php b/tests/integration/Cache/Adapter/Stream/EventsCest.php new file mode 100644 index 00000000000..f591cb4820c --- /dev/null +++ b/tests/integration/Cache/Adapter/Stream/EventsCest.php @@ -0,0 +1,123 @@ + + * 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 Codeception\Example; +use IntegrationTester; +use Phalcon\Cache\Adapter\Stream; +use Phalcon\Events\Manager; +use Phalcon\Storage\SerializerFactory; + +class EventsCest +{ + /** + * Tests Cache\Adapter\Stream :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterStreamGetEventsManagerNotSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Stream - getEventsManager() - not set'); + + $serializer = new SerializerFactory(); + $adapter = new Stream( + $serializer, + [ + 'storageDir' => outputDir(), + ] + ); + + $I->assertNull($adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Stream :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterStreamGetEventsManagerSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Stream - getEventsManager() - set'); + + $serializer = new SerializerFactory(); + $adapter = new Stream( + $serializer, + [ + 'storageDir' => outputDir(), + ] + ); + + $adapter->setEventsManager(new Manager()); + + $I->assertInstanceOf(Manager::class, $adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Stream :: trigger cache events + * + * @dataProvider getEvents + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): void + { + $I->wantToTest('Cache\Adapter\Stream - triggered ' . $example->offsetGet(0)); + + $serializer = new SerializerFactory(); + $adapter = new Stream( + $serializer, + [ + 'storageDir' => outputDir(), + ] + ); + + $counter = 0; + $manager = new Manager(); + $adapter->setEventsManager(new Manager()); + + $manager->attach( + 'cache:' . $example->offsetGet(0), + static function () use (&$counter): void { + $counter++; + } + ); + + $adapter->setEventsManager($manager); + + $I->assertInstanceOf($manager::class, $adapter->getEventsManager()); + + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + $I->assertEquals(2, $counter); + } + + public function getEvents(): array + { + // Event, Method, Data + return [ + ['beforeDelete', 'delete', ['test']], + ['afterDelete', 'delete', ['test']], + ['beforeIncrement', 'increment', ['test']], + ['afterIncrement', 'increment', ['test']], + ['beforeGet', 'get', ['test']], + ['afterGet', 'get', ['test']], + ['beforeDecrement', 'decrement', ['test']], + ['afterDecrement', 'decrement', ['test']], + ['beforeHas', 'has', ['test']], + ['afterHas', 'has', ['test']], + ['beforeSet', 'set', ['test', 'test']], + ['afterSet', 'set', ['test', 'test']], + ]; + } +} diff --git a/tests/integration/Cache/Adapter/Weak/EventsCest.php b/tests/integration/Cache/Adapter/Weak/EventsCest.php new file mode 100644 index 00000000000..a133df10d62 --- /dev/null +++ b/tests/integration/Cache/Adapter/Weak/EventsCest.php @@ -0,0 +1,108 @@ + + * 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\Weak; + +use Codeception\Example; +use IntegrationTester; +use Phalcon\Cache\Adapter\Weak; +use Phalcon\Events\Manager; +use Phalcon\Storage\SerializerFactory; + +class EventsCest +{ + /** + * Tests Cache\Adapter\Weak() :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterWeakGetEventsManagerNotSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Weak() - getEventsManager() - not set'); + + $serializer = new SerializerFactory(); + $adapter = new Weak($serializer); + + $I->assertNull($adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Weak() :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheAdapterWeakGetEventsManagerSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Adapter\Weak() - getEventsManager() - set'); + + $serializer = new SerializerFactory(); + $adapter = new Weak($serializer); + + $adapter->setEventsManager(new Manager()); + + $I->assertInstanceOf(Manager::class, $adapter->getEventsManager()); + } + + /** + * Tests Cache\Adapter\Weak() :: trigger cache events + * + * @dataProvider getEvents + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): void + { + $I->wantToTest('Cache\Adapter\Weak() - triggered ' . $example->offsetGet(0)); + + $serializer = new SerializerFactory(); + $adapter = new Weak($serializer); + + $counter = 0; + $manager = new Manager(); + $adapter->setEventsManager(new Manager()); + + $manager->attach( + 'cache:' . $example->offsetGet(0), + static function () use (&$counter): void { + $counter++; + } + ); + + $adapter->setEventsManager($manager); + + $I->assertInstanceOf($manager::class, $adapter->getEventsManager()); + + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + $I->assertEquals(2, $counter); + } + + public function getEvents(): array + { + // Event, Method, Data + return [ + ['beforeDelete', 'delete', ['test']], + ['afterDelete', 'delete', ['test']], + ['beforeIncrement', 'increment', ['test']], + ['afterIncrement', 'increment', ['test']], + ['beforeGet', 'get', ['test']], + ['afterGet', 'get', ['test']], + ['beforeDecrement', 'decrement', ['test']], + ['afterDecrement', 'decrement', ['test']], + ['beforeHas', 'has', ['test']], + ['afterHas', 'has', ['test']], + ['beforeSet', 'set', ['test', 'test']], + ['afterSet', 'set', ['test', 'test']], + ]; + } +} diff --git a/tests/integration/Cache/Cache/GetEventsManagerCest.php b/tests/integration/Cache/Cache/GetEventsManagerCest.php new file mode 100644 index 00000000000..7c750862dc4 --- /dev/null +++ b/tests/integration/Cache/Cache/GetEventsManagerCest.php @@ -0,0 +1,118 @@ + + * 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\Cache; + +use Codeception\Example; +use IntegrationTester; +use Phalcon\Cache\AdapterFactory; +use Phalcon\Cache\Cache; +use Phalcon\Events\Manager; +use Phalcon\Storage\SerializerFactory; + +class GetEventsManagerCest +{ + /** + * Tests Phalcon\Cache :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheCacheGetEventsManagerNotSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Cache - getEventsManager() - not set'); + + $serializer = new SerializerFactory(); + $factory = new AdapterFactory($serializer); + $instance = $factory->newInstance('memory'); + + $adapter = new Cache($instance); + + $I->assertNull($adapter->getEventsManager()); + } + + /** + * Tests Phalcon\Cache :: getEventsManager() + * + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheCacheGetEventsManagerSet(IntegrationTester $I): void + { + $I->wantToTest('Cache\Cache - getEventsManager() - set'); + + $serializer = new SerializerFactory(); + $factory = new AdapterFactory($serializer); + $instance = $factory->newInstance('memory'); + + $adapter = new Cache($instance); + + $adapter->setEventsManager(new Manager()); + + $I->assertInstanceOf(Manager::class, $adapter->getEventsManager()); + } + + /** + * Tests Phalcon\Cache :: trigger cache events + * + * @dataProvider getEvents + * @author n[oO]ne + * @since 2024-06-07 + */ + public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): void + { + $I->wantToTest('Cache\Cache - triggered ' . $example->offsetGet(0)); + + $serializer = new SerializerFactory(); + $factory = new AdapterFactory($serializer); + $instance = $factory->newInstance('memory'); + + $counter = 0; + $adapter = new Cache($instance); + $manager = new Manager(); + + $manager->attach( + 'cache:' . $example->offsetGet(0), + static function () use (&$counter) { + $counter++; + } + ); + + $adapter->setEventsManager($manager); + + $I->assertInstanceOf($manager::class, $adapter->getEventsManager()); + + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + call_user_func_array([$adapter, $example->offsetGet(1)], $example->offsetGet(2)); + $I->assertEquals(2, $counter); + } + + public function getEvents(): array + { + // Event, Method, Data + return [ + ['beforeDelete', 'delete', ['test']], + ['afterDelete', 'delete', ['test']], + ['beforeDeleteMultiple', 'deleteMultiple', [['test', 'test2']]], + ['afterDeleteMultiple', 'deleteMultiple', [['test', 'test2']]], + ['beforeGet', 'get', ['test']], + ['afterGet', 'get', ['test']], + ['beforeGetMultiple', 'getMultiple', [['test', 'test2']]], + ['afterGetMultiple', 'getMultiple', [['test', 'test2']]], + ['beforeHas', 'has', ['test']], + ['afterHas', 'has', ['test']], + ['beforeSet', 'set', ['test', 'test']], + ['afterSet', 'set', ['test', 'test']], + ['beforeSetMultiple', 'setMultiple', [['test' => 'test', 'test2' => 'test2']]], + ['afterSetMultiple', 'setMultiple', [['test', 'test', 'test2' => 'test2']]], + ]; + } +} diff --git a/tests/integration/Storage/Adapter/EventsCest.php b/tests/integration/Storage/Adapter/EventsCest.php new file mode 100644 index 00000000000..5c61525d47b --- /dev/null +++ b/tests/integration/Storage/Adapter/EventsCest.php @@ -0,0 +1,594 @@ + + * 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\Events\Manager; +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\Adapter\Weak; +use Phalcon\Storage\SerializerFactory; + +use function getOptionsLibmemcached; +use function getOptionsRedis; +use function outputDir; +use function sprintf; + +class EventsCest +{ + /** + * Tests Phalcon\Storage\Adapter\* :: events - beforeGet + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsBeforeGet(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'beforeGet' + ) + ); + + $manager->attach('storage:beforeGet', function () use (&$counter): void { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'get'], ['test']); + call_user_func_array([$adapter, 'get'], ['test']); + + $I->assertEquals(2, $counter); + } + + /** + * Tests Phalcon\Storage\Adapter\* :: events - afterGet + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsAfterGet(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'afterGet' + ) + ); + + $manager->attach('storage:afterGet', function () use (&$counter): void { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'get'], ['test']); + call_user_func_array([$adapter, 'get'], ['test']); + + $I->assertEquals(2, $counter); + } + + /** + * Tests Phalcon\Storage\Adapter\* :: events - beforeHas + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsBeforeHas(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'beforeHas' + ) + ); + + $manager->attach('storage:beforeHas', function () use (&$counter): void { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'has'], ['test']); + call_user_func_array([$adapter, 'has'], ['test']); + + $I->assertEquals(2, $counter); + } + + /** + * Tests Phalcon\Storage\Adapter\* :: events - afterHas + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsAfterHas(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'afterHas' + ) + ); + + $manager->attach('storage:afterHas', function () use (&$counter): void { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'has'], ['test']); + call_user_func_array([$adapter, 'has'], ['test']); + + $I->assertEquals(2, $counter); + } + + /** + * Tests Phalcon\Storage\Adapter\* :: events - beforeDelete + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsBeforeDelete(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'beforeDelete' + ) + ); + + $manager->attach('storage:beforeDelete', function () use (&$counter): void { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'delete'], ['test']); + call_user_func_array([$adapter, 'delete'], ['test']); + + $I->assertEquals(2, $counter); + } + + /** + * Tests Phalcon\Storage\Adapter\* :: events - afterDelete + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsAfterDelete(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'afterDelete' + ) + ); + + $manager->attach('storage:afterDelete', function () use (&$counter): void { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'delete'], ['test']); + call_user_func_array([$adapter, 'delete'], ['test']); + + $I->assertEquals(2, $counter); + } + + /** + * Tests Phalcon\Storage\Adapter\* :: events - beforeIncrement + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsBeforeIncrement(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'beforeIncrement' + ) + ); + + $manager->attach('storage:beforeIncrement', function () use (&$counter) { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'increment'], ['test']); + call_user_func_array([$adapter, 'increment'], ['test']); + + $I->assertEquals(2, $counter); + } + + /** + * Tests Phalcon\Storage\Adapter\* :: events - afterIncrement + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsAfterIncrement(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'afterIncrement' + ) + ); + + $manager->attach('storage:afterIncrement', function () use (&$counter): void { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'increment'], ['test']); + call_user_func_array([$adapter, 'increment'], ['test']); + + $I->assertEquals(2, $counter); + } + + /** + * Tests Phalcon\Storage\Adapter\* :: events - beforeDecrement + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsBeforeDecrement(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'beforeDecrement' + ) + ); + + $manager->attach('storage:beforeDecrement', function () use (&$counter): void { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'decrement'], ['test']); + call_user_func_array([$adapter, 'decrement'], ['test']); + + $I->assertEquals(2, $counter); + } + + /** + * Tests Phalcon\Storage\Adapter\* :: events - afterDecrement + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsAfterDecrement(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'afterDecrement' + ) + ); + + $manager->attach('storage:afterDecrement', function () use (&$counter): void { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'decrement'], ['test']); + call_user_func_array([$adapter, 'decrement'], ['test']); + + $I->assertEquals(2, $counter); + } + + /** + * Tests Phalcon\Storage\Adapter\* :: events - beforeSet + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsBeforeSet(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'beforeSet' + ) + ); + + $manager->attach('storage:beforeSet', function () use (&$counter): void { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'set'], ['test', 'test']); + call_user_func_array([$adapter, 'set'], ['test', 'test']); + + $I->assertEquals(2, $counter); + } + + /** + * Tests Phalcon\Storage\Adapter\* :: events - afterSet + * + * @dataProvider getExamples + * @author n[oO]ne + * @since 2024-06-07 + */ + public function storageAdapterEventsAfterSet(IntegrationTester $I, Example $example): void + { + $className = $example[0]; + $extension = $example[1]; + $class = $example[2]; + $options = $example[3]; + + if (!empty($extension)) { + $I->checkExtensionIsLoaded($extension); + } + + $counter = 0; + $serializer = new SerializerFactory(); + $adapter = new $class($serializer, $options); + $manager = new Manager(); + + $I->wantToTest( + sprintf( + 'Storage\Adapter\%s - triggered event - %s', + $className, + 'afterSet' + ) + ); + + $manager->attach('storage:afterSet', function () use (&$counter): void { + $counter++; + }); + + $adapter->setEventsManager($manager); + + call_user_func_array([$adapter, 'set'], ['test', 'test']); + call_user_func_array([$adapter, 'set'], ['test', 'test']); + + $I->assertEquals(2, $counter); + } + + /** + * @return array[] + */ + private function getExamples(): array + { + return [ + [ + 'Apcu', + 'apcu', + Apcu::class, + [], + ], + [ + 'Libmemcached', + 'memcached', + Libmemcached::class, + getOptionsLibmemcached(), + ], + [ + 'Memory', + '', + Memory::class, + [], + ], + [ + 'Redis', + 'redis', + Redis::class, + getOptionsRedis(), + ], + [ + 'Stream', + '', + Stream::class, + [ + 'storageDir' => outputDir(), + ], + ], + [ + 'Weak', + '', + Weak::class, + [], + ], + ]; + } +} From 566c600938a90da1f7919343726d8de21520ac11 Mon Sep 17 00:00:00 2001 From: "n[oO]ne" Date: Fri, 7 Jun 2024 16:22:06 +0700 Subject: [PATCH 2/3] [#16606] - fix: added missing EventsAwareInterface --- phalcon/Cache/AbstractCache.zep | 3 ++- phalcon/Storage/Adapter/AbstractAdapter.zep | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/phalcon/Cache/AbstractCache.zep b/phalcon/Cache/AbstractCache.zep index 268a77c0e4a..d2b0fd48a6f 100644 --- a/phalcon/Cache/AbstractCache.zep +++ b/phalcon/Cache/AbstractCache.zep @@ -13,13 +13,14 @@ namespace Phalcon\Cache; use DateInterval; use Phalcon\Cache\Adapter\AdapterInterface; use Phalcon\Cache\Exception\InvalidArgumentException; +use Phalcon\Events\EventsAwareInterface; use Phalcon\Events\ManagerInterface; use Traversable; /** * This component offers caching capabilities for your application. */ -abstract class AbstractCache implements CacheInterface +abstract class AbstractCache implements CacheInterface, EventsAwareInterface { /** * The adapter diff --git a/phalcon/Storage/Adapter/AbstractAdapter.zep b/phalcon/Storage/Adapter/AbstractAdapter.zep index 8ef2aa07865..f6931852047 100644 --- a/phalcon/Storage/Adapter/AbstractAdapter.zep +++ b/phalcon/Storage/Adapter/AbstractAdapter.zep @@ -13,6 +13,7 @@ namespace Phalcon\Storage\Adapter; use DateInterval; use DateTime; use Exception; +use Phalcon\Events\EventsAwareInterface; use Phalcon\Events\ManagerInterface; use Phalcon\Storage\Serializer\SerializerInterface; use Phalcon\Storage\SerializerFactory; @@ -31,7 +32,7 @@ use Phalcon\Support\Exception as SupportException; * @property SerializerInterface $serializer * @property SerializerFactory $serializerFactory */ -abstract class AbstractAdapter implements AdapterInterface +abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface { /** * @var mixed From 09a6657f2af37e78271e0da02bc7d19085b4a456 Mon Sep 17 00:00:00 2001 From: noone-silent Date: Sat, 8 Jun 2024 07:55:34 +0700 Subject: [PATCH 3/3] [#16606] - refactor: changed fire method to request key/keys. --- phalcon/Cache/AbstractCache.zep | 33 ++--- phalcon/Storage/Adapter/AbstractAdapter.zep | 11 +- phalcon/Storage/Adapter/Apcu.zep | 22 +-- phalcon/Storage/Adapter/Libmemcached.zep | 22 +-- phalcon/Storage/Adapter/Memory.zep | 22 +-- phalcon/Storage/Adapter/Redis.zep | 22 +-- phalcon/Storage/Adapter/Stream.zep | 38 ++--- phalcon/Storage/Adapter/Weak.zep | 30 ++-- .../Cache/Adapter/Apcu/EventsCest.php | 5 +- .../Cache/Adapter/Libmemcached/EventsCest.php | 11 +- .../Cache/Adapter/Memory/EventsCest.php | 5 +- .../Cache/Adapter/Redis/EventsCest.php | 5 +- .../Cache/Adapter/Stream/EventsCest.php | 5 +- .../Cache/Adapter/Weak/EventsCest.php | 5 +- .../Storage/Adapter/EventsCest.php | 133 +++++++++++++----- 15 files changed, 225 insertions(+), 144 deletions(-) diff --git a/phalcon/Cache/AbstractCache.zep b/phalcon/Cache/AbstractCache.zep index d2b0fd48a6f..0711701d139 100644 --- a/phalcon/Cache/AbstractCache.zep +++ b/phalcon/Cache/AbstractCache.zep @@ -135,13 +135,13 @@ abstract class AbstractCache implements CacheInterface, EventsAwareInterface { var result; - this->fire("cache:beforeDelete"); + this->fire("cache:beforeDelete", key); this->checkKey(key); let result = this->adapter->delete(key); - this->fire("cache:afterDelete"); + this->fire("cache:afterDelete", key); return result; } @@ -155,7 +155,7 @@ abstract class AbstractCache implements CacheInterface, EventsAwareInterface this->checkKeys(keys); - this->fire("cache:beforeDeleteMultiple"); + this->fire("cache:beforeDeleteMultiple", keys); let result = true; for key in keys { @@ -164,7 +164,7 @@ abstract class AbstractCache implements CacheInterface, EventsAwareInterface } } - this->fire("cache:afterDeleteMultiple"); + this->fire("cache:afterDeleteMultiple", keys); return result; } @@ -187,11 +187,11 @@ abstract class AbstractCache implements CacheInterface, EventsAwareInterface this->checkKey(key); - this->fire("cache:beforeGet"); + this->fire("cache:beforeGet", key); let result = this->adapter->get(key, defaultValue); - this->fire("cache:afterGet"); + this->fire("cache:afterGet", key); return result; } @@ -205,14 +205,14 @@ abstract class AbstractCache implements CacheInterface, EventsAwareInterface this->checkKeys(keys); - this->fire("cache:beforeGetMultiple"); + this->fire("cache:beforeGetMultiple", keys); let results = []; for element in keys { let results[element] = this->get(element, defaultValue); } - this->fire("cache:afterGetMultiple"); + this->fire("cache:afterGetMultiple", keys); return results; } @@ -233,11 +233,11 @@ abstract class AbstractCache implements CacheInterface, EventsAwareInterface this->checkKey(key); - this->fire("cache:beforeHas"); + this->fire("cache:beforeHas", key); let result = this->adapter->has(key); - this->fire("cache:afterHas"); + this->fire("cache:afterHas", key); return result; } @@ -266,11 +266,11 @@ abstract class AbstractCache implements CacheInterface, EventsAwareInterface this->checkKey(key); - this->fire("cache:beforeSet"); + this->fire("cache:beforeSet", key); let result = this->adapter->set(key, value, ttl); - this->fire("cache:afterSet"); + this->fire("cache:afterSet", key); return result; } @@ -284,7 +284,7 @@ abstract class AbstractCache implements CacheInterface, EventsAwareInterface this->checkKeys(values); - this->fire("cache:beforeSetMultiple"); + this->fire("cache:beforeSetMultiple", array_keys(values)); let result = true; for key, value in values { @@ -293,7 +293,7 @@ abstract class AbstractCache implements CacheInterface, EventsAwareInterface } } - this->fire("cache:afterSetMultiple"); + this->fire("cache:afterSetMultiple", array_keys(values)); return result; } @@ -302,14 +302,15 @@ abstract class AbstractCache implements CacheInterface, EventsAwareInterface * Trigger an event for the eventsManager. * * @var string $eventName + * @var mixed $keys */ - protected function fire(string eventName) -> void + protected function fire(string eventName, var keys) -> void { if (this->eventsManager === null) { return; } - this->eventsManager->fire(eventName, this); + this->eventsManager->fire(eventName, this, keys, false); } /** diff --git a/phalcon/Storage/Adapter/AbstractAdapter.zep b/phalcon/Storage/Adapter/AbstractAdapter.zep index f6931852047..ed872143c51 100644 --- a/phalcon/Storage/Adapter/AbstractAdapter.zep +++ b/phalcon/Storage/Adapter/AbstractAdapter.zep @@ -161,10 +161,10 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface { var content, result; - this->fire(this->eventType . ":beforeGet"); + this->fire(this->eventType . ":beforeGet", key); if (true !== this->has(key)) { - this->fire(this->eventType . ":afterGet"); + this->fire(this->eventType . ":afterGet", key); return defaultValue; } @@ -173,7 +173,7 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface let result = this->getUnserializedData(content, defaultValue); - this->fire(this->eventType . ":afterGet"); + this->fire(this->eventType . ":afterGet", key); return result; } @@ -429,13 +429,14 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface * Trigger an event for the eventsManager. * * @var string $eventName + * @var mixed $keys */ - protected function fire(string eventName) -> void + protected function fire(string eventName, var keys) -> void { if (this->eventsManager === null) { return; } - this->eventsManager->fire(eventName, this); + this->eventsManager->fire(eventName, this, keys, false); } } diff --git a/phalcon/Storage/Adapter/Apcu.zep b/phalcon/Storage/Adapter/Apcu.zep index 59346eeb985..28377892550 100644 --- a/phalcon/Storage/Adapter/Apcu.zep +++ b/phalcon/Storage/Adapter/Apcu.zep @@ -80,11 +80,11 @@ class Apcu extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeDecrement"); + this->fire(this->eventType . ":beforeDecrement", key); let result = this->phpApcuDec(this->getPrefixedKey(key), value); - this->fire(this->eventType . ":afterDecrement"); + this->fire(this->eventType . ":afterDecrement", key); return result; } @@ -100,11 +100,11 @@ class Apcu extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeDelete"); + this->fire(this->eventType . ":beforeDelete", key); let result = (bool) this->phpApcuDelete(this->getPrefixedKey(key)); - this->fire(this->eventType . ":afterDelete"); + this->fire(this->eventType . ":afterDelete", key); return result; } @@ -147,11 +147,11 @@ class Apcu extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeHas"); + this->fire(this->eventType . ":beforeHas", key); let result = this->phpApcuExists(this->getPrefixedKey(key)); - this->fire(this->eventType . ":afterHas"); + this->fire(this->eventType . ":afterHas", key); return typeof result === "bool" ? result : false; } @@ -168,11 +168,11 @@ class Apcu extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeIncrement"); + this->fire(this->eventType . ":beforeIncrement", key); let result = this->phpApcuInc(this->getPrefixedKey(key), value); - this->fire(this->eventType . ":afterIncrement"); + this->fire(this->eventType . ":afterIncrement", key); return result; } @@ -195,12 +195,12 @@ class Apcu extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeSet"); + this->fire(this->eventType . ":beforeSet", key); if (typeof ttl === "integer" && ttl < 1) { let result = this->delete(key); - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return result; } @@ -211,7 +211,7 @@ class Apcu extends AbstractAdapter this->getTtl(ttl) ); - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return typeof result === "bool" ? result : false; } diff --git a/phalcon/Storage/Adapter/Libmemcached.zep b/phalcon/Storage/Adapter/Libmemcached.zep index 6e0a7e8e8c9..37d513060b6 100644 --- a/phalcon/Storage/Adapter/Libmemcached.zep +++ b/phalcon/Storage/Adapter/Libmemcached.zep @@ -73,11 +73,11 @@ class Libmemcached extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeDecrement"); + this->fire(this->eventType . ":beforeDecrement", key); let result = this->getAdapter()->decrement(key, value); - this->fire(this->eventType . ":afterDecrement"); + this->fire(this->eventType . ":afterDecrement", key); return result; } @@ -94,11 +94,11 @@ class Libmemcached extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeDelete"); + this->fire(this->eventType . ":beforeDelete", key); let result = this->getAdapter()->delete(key, 0); - this->fire(this->eventType . ":afterDelete"); + this->fire(this->eventType . ":afterDelete", key); return result; } @@ -181,13 +181,13 @@ class Libmemcached extends AbstractAdapter { var connection, result, code; - this->fire(this->eventType . ":beforeHas"); + this->fire(this->eventType . ":beforeHas", key); let connection = this->getAdapter(), result = connection->get(key), code = connection->getResultCode(); - this->fire(this->eventType . ":afterHas"); + this->fire(this->eventType . ":afterHas", key); return \Memcached::RES_NOTFOUND !== code; } @@ -205,11 +205,11 @@ class Libmemcached extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeIncrement"); + this->fire(this->eventType . ":beforeIncrement", key); let result = this->getAdapter()->increment(key, value); - this->fire(this->eventType . ":afterIncrement"); + this->fire(this->eventType . ":afterIncrement", key); return result; } @@ -233,12 +233,12 @@ class Libmemcached extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeSet"); + this->fire(this->eventType . ":beforeSet", key); if (typeof ttl === "integer" && ttl < 1) { let result = this->delete(key); - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return result; } @@ -251,7 +251,7 @@ class Libmemcached extends AbstractAdapter ) ; - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return typeof result === "bool" ? result : false; } diff --git a/phalcon/Storage/Adapter/Memory.zep b/phalcon/Storage/Adapter/Memory.zep index 3c3db78b42b..ec178627be9 100644 --- a/phalcon/Storage/Adapter/Memory.zep +++ b/phalcon/Storage/Adapter/Memory.zep @@ -65,7 +65,7 @@ class Memory extends AbstractAdapter { var current, newValue, prefixedKey, result; - this->fire(this->eventType . ":beforeDecrement"); + this->fire(this->eventType . ":beforeDecrement", key); let prefixedKey = this->getPrefixedKey(key), result = array_key_exists(prefixedKey, this->data); @@ -78,7 +78,7 @@ class Memory extends AbstractAdapter let this->data[prefixedKey] = newValue; } - this->fire(this->eventType . ":afterDecrement"); + this->fire(this->eventType . ":afterDecrement", key); return result; } @@ -94,14 +94,14 @@ class Memory extends AbstractAdapter { var exists, prefixedKey; - this->fire(this->eventType . ":beforeDelete"); + this->fire(this->eventType . ":beforeDelete", key); let prefixedKey = this->getPrefixedKey(key), exists = array_key_exists(prefixedKey, this->data); unset(this->data[prefixedKey]); - this->fire(this->eventType . ":afterDelete"); + this->fire(this->eventType . ":afterDelete", key); return exists; } @@ -131,11 +131,11 @@ class Memory extends AbstractAdapter let prefixedKey = this->getPrefixedKey(key); - this->fire(this->eventType . ":beforeHas"); + this->fire(this->eventType . ":beforeHas", key); let result = array_key_exists(prefixedKey, this->data); - this->fire(this->eventType . ":afterHas"); + this->fire(this->eventType . ":afterHas", key); return result; } @@ -152,7 +152,7 @@ class Memory extends AbstractAdapter { var current, newValue, prefixedKey, result; - this->fire(this->eventType . ":beforeIncrement"); + this->fire(this->eventType . ":beforeIncrement", key); let prefixedKey = this->getPrefixedKey(key), result = array_key_exists(prefixedKey, this->data); @@ -165,7 +165,7 @@ class Memory extends AbstractAdapter let this->data[prefixedKey] = newValue; } - this->fire(this->eventType . ":afterIncrement"); + this->fire(this->eventType . ":afterIncrement", key); return result; } @@ -188,12 +188,12 @@ class Memory extends AbstractAdapter { var content, prefixedKey, result; - this->fire(this->eventType . ":beforeSet"); + this->fire(this->eventType . ":beforeSet", key); if (typeof ttl === "integer" && ttl < 1) { let result = this->delete(key); - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return result; } @@ -203,7 +203,7 @@ class Memory extends AbstractAdapter let this->data[prefixedKey] = content; - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return true; } diff --git a/phalcon/Storage/Adapter/Redis.zep b/phalcon/Storage/Adapter/Redis.zep index daf918eb13a..9c80eff517d 100644 --- a/phalcon/Storage/Adapter/Redis.zep +++ b/phalcon/Storage/Adapter/Redis.zep @@ -92,11 +92,11 @@ class Redis extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeDecrement"); + this->fire(this->eventType . ":beforeDecrement", key); let result = this->getAdapter()->decrBy(key, value); - this->fire(this->eventType . ":afterDecrement"); + this->fire(this->eventType . ":afterDecrement", key); return result; } @@ -113,11 +113,11 @@ class Redis extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeDelete"); + this->fire(this->eventType . ":beforeDelete", key); let result = (bool) this->getAdapter()->del(key); - this->fire(this->eventType . ":afterDelete"); + this->fire(this->eventType . ":afterDelete", key); return result; } @@ -179,11 +179,11 @@ class Redis extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeHas"); + this->fire(this->eventType . ":beforeHas", key); let result = (bool) this->getAdapter()->exists(key); - this->fire(this->eventType . ":afterHas"); + this->fire(this->eventType . ":afterHas", key); return result; } @@ -201,11 +201,11 @@ class Redis extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeIncrement"); + this->fire(this->eventType . ":beforeIncrement", key); let result = this->getAdapter()->incrBy(key, value); - this->fire(this->eventType . ":afterIncrement"); + this->fire(this->eventType . ":afterIncrement", key); return result; } @@ -228,11 +228,11 @@ class Redis extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeSet"); + this->fire(this->eventType . ":beforeSet", key); if (typeof ttl === "integer" && ttl < 1) { let result = this->delete(key); - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return result; } @@ -245,7 +245,7 @@ class Redis extends AbstractAdapter ) ; - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return typeof result === "bool" ? result : false; } diff --git a/phalcon/Storage/Adapter/Stream.zep b/phalcon/Storage/Adapter/Stream.zep index 6c32157a465..3a902e98de1 100644 --- a/phalcon/Storage/Adapter/Stream.zep +++ b/phalcon/Storage/Adapter/Stream.zep @@ -103,10 +103,10 @@ class Stream extends AbstractAdapter { var data, result; - this->fire(this->eventType . ":beforeDecrement"); + this->fire(this->eventType . ":beforeDecrement", key); if unlikely true !== this->has(key) { - this->fire(this->eventType . ":afterDecrement"); + this->fire(this->eventType . ":afterDecrement", key); return false; } @@ -119,7 +119,7 @@ class Stream extends AbstractAdapter let result = data; } - this->fire(this->eventType . ":afterDecrement"); + this->fire(this->eventType . ":afterDecrement", key); return result; } @@ -135,10 +135,10 @@ class Stream extends AbstractAdapter { var filepath, result; - this->fire(this->eventType . ":beforeDelete"); + this->fire(this->eventType . ":beforeDelete", key); if true !== this->has(key) { - this->fire(this->eventType . ":afterDelete"); + this->fire(this->eventType . ":afterDelete", key); return false; } @@ -147,7 +147,7 @@ class Stream extends AbstractAdapter let result = unlink(filepath); - this->fire(this->eventType . ":afterDelete"); + this->fire(this->eventType . ":afterDelete", key); return result; } @@ -164,12 +164,12 @@ class Stream extends AbstractAdapter { var content, filepath, payload, result; - this->fire(this->eventType . ":beforeGet"); + this->fire(this->eventType . ":beforeGet", key); let filepath = this->getFilepath(key); if (true !== file_exists(filepath)) { - this->fire(this->eventType . ":afterGet"); + this->fire(this->eventType . ":afterGet", key); return defaultValue; } @@ -184,7 +184,7 @@ class Stream extends AbstractAdapter let result = this->getUnserializedData(content, defaultValue); - this->fire(this->eventType . ":afterGet"); + this->fire(this->eventType . ":afterGet", key); return result; } @@ -230,12 +230,12 @@ class Stream extends AbstractAdapter { var payload, filepath, result; - this->fire(this->eventType . ":beforeHas"); + this->fire(this->eventType . ":beforeHas", key); let filepath = this->getFilepath(key); if unlikely true !== this->phpFileExists(filepath) { - this->fire(this->eventType . ":afterHas"); + this->fire(this->eventType . ":afterHas", key); return false; } @@ -243,14 +243,14 @@ class Stream extends AbstractAdapter let payload = this->getPayload(filepath); if unlikely empty payload { - this->fire(this->eventType . ":afterHas"); + this->fire(this->eventType . ":afterHas", key); return false; } let result = !this->isExpired(payload); - this->fire(this->eventType . ":afterHas"); + this->fire(this->eventType . ":afterHas", key); return result; } @@ -267,10 +267,10 @@ class Stream extends AbstractAdapter { var data, result; - this->fire(this->eventType . ":beforeIncrement"); + this->fire(this->eventType . ":beforeIncrement", key); if unlikely true !== this->has(key) { - this->fire(this->eventType . ":afterIncrement"); + this->fire(this->eventType . ":afterIncrement", key); return false; } @@ -283,7 +283,7 @@ class Stream extends AbstractAdapter let result = data; } - this->fire(this->eventType . ":afterIncrement"); + this->fire(this->eventType . ":afterIncrement", key); return result; } @@ -306,12 +306,12 @@ class Stream extends AbstractAdapter array payload; var result; - this->fire(this->eventType . ":beforeSet"); + this->fire(this->eventType . ":beforeSet", key); if (typeof ttl === "integer" && ttl < 1) { let result = this->delete(key); - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return result; } @@ -324,7 +324,7 @@ class Stream extends AbstractAdapter let result = this->storePayload(payload, key); - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return result; } diff --git a/phalcon/Storage/Adapter/Weak.zep b/phalcon/Storage/Adapter/Weak.zep index e09633c5c85..8e1475b87ca 100644 --- a/phalcon/Storage/Adapter/Weak.zep +++ b/phalcon/Storage/Adapter/Weak.zep @@ -73,8 +73,8 @@ class Weak extends AbstractAdapter */ public function decrement(string! key, int value = 1) -> int | bool { - this->fire(this->eventType . ":beforeDecrement"); - this->fire(this->eventType . ":afterDecrement"); + this->fire(this->eventType . ":beforeDecrement", key); + this->fire(this->eventType . ":afterDecrement", key); return false; } @@ -90,17 +90,17 @@ class Weak extends AbstractAdapter { var exists; - this->fire(this->eventType . ":beforeDelete"); + this->fire(this->eventType . ":beforeDelete", key); if key === this->fetching { - this->fire(this->eventType . ":afterDelete"); + this->fire(this->eventType . ":afterDelete", key); return false; } let exists = isset this->weakList[key]; unset(this->weakList[key]); - this->fire(this->eventType . ":afterDelete"); + this->fire(this->eventType . ":afterDelete", key); return exists; } @@ -139,8 +139,8 @@ class Weak extends AbstractAdapter */ public function increment(string! key, int value = 1) -> int | bool { - this->fire(this->eventType . ":beforeIncrement"); - this->fire(this->eventType . ":afterIncrement"); + this->fire(this->eventType . ":beforeIncrement", key); + this->fire(this->eventType . ":afterIncrement", key); return false; } @@ -157,7 +157,7 @@ class Weak extends AbstractAdapter { var value, wr; - this->fire(this->eventType . ":beforeGet"); + this->fire(this->eventType . ":beforeGet", key); /** * while getting a key, garbage collection might be triggered, @@ -169,7 +169,7 @@ class Weak extends AbstractAdapter if false === isset this->weakList[key] { let this->fetching = null; - this->fire(this->eventType . ":afterGet"); + this->fire(this->eventType . ":afterGet", key); return defaultValue; } @@ -183,7 +183,7 @@ class Weak extends AbstractAdapter this->delete(key); } - this->fire(this->eventType . ":afterGet"); + this->fire(this->eventType . ":afterGet", key); return value; } @@ -199,11 +199,11 @@ class Weak extends AbstractAdapter { var result; - this->fire(this->eventType . ":beforeHas"); + this->fire(this->eventType . ":beforeHas", key); let result = isset this->weakList[key]; - this->fire(this->eventType . ":afterHas"); + this->fire(this->eventType . ":afterHas", key); return result; } @@ -224,10 +224,10 @@ class Weak extends AbstractAdapter */ public function set(string! key, var value, var ttl = null) -> bool { - this->fire(this->eventType . ":beforeSet"); + this->fire(this->eventType . ":beforeSet", key); if typeof value !== "object" { - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return false; } @@ -235,7 +235,7 @@ class Weak extends AbstractAdapter let this->weakList[key] = \WeakReference::create(value); } - this->fire(this->eventType . ":afterSet"); + this->fire(this->eventType . ":afterSet", key); return true; } diff --git a/tests/integration/Cache/Adapter/Apcu/EventsCest.php b/tests/integration/Cache/Adapter/Apcu/EventsCest.php index 82624dce2c4..f680fb93ae9 100644 --- a/tests/integration/Cache/Adapter/Apcu/EventsCest.php +++ b/tests/integration/Cache/Adapter/Apcu/EventsCest.php @@ -14,6 +14,7 @@ use Codeception\Example; use IntegrationTester; use Phalcon\Cache\Adapter\Apcu; +use Phalcon\Events\Event; use Phalcon\Events\Manager; use Phalcon\Storage\SerializerFactory; @@ -73,8 +74,10 @@ public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): $manager->attach( 'cache:' . $example->offsetGet(0), - static function () use (&$counter): void { + static function (Event $event) use (&$counter, $example): void { $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); } ); diff --git a/tests/integration/Cache/Adapter/Libmemcached/EventsCest.php b/tests/integration/Cache/Adapter/Libmemcached/EventsCest.php index 300cb146fd3..6fc5ea408a2 100644 --- a/tests/integration/Cache/Adapter/Libmemcached/EventsCest.php +++ b/tests/integration/Cache/Adapter/Libmemcached/EventsCest.php @@ -14,6 +14,7 @@ use Codeception\Example; use IntegrationTester; use Phalcon\Cache\Adapter\Libmemcached; +use Phalcon\Events\Event; use Phalcon\Events\Manager; use Phalcon\Storage\SerializerFactory; @@ -30,7 +31,7 @@ public function cacheAdapterMemoryGetEventsManagerNotSet(IntegrationTester $I): $I->wantToTest('Cache\Adapter\Libmemcached - getEventsManager() - not set'); $serializer = new SerializerFactory(); - $adapter = new Libmemcached($serializer, getOptionsLibmemcached()); + $adapter = new Libmemcached($serializer, getOptionsLibmemcached()); $I->assertNull($adapter->getEventsManager()); } @@ -46,7 +47,7 @@ public function cacheAdapterMemoryGetEventsManagerSet(IntegrationTester $I): voi $I->wantToTest('Cache\Adapter\Libmemcached - getEventsManager() - set'); $serializer = new SerializerFactory(); - $adapter = new Libmemcached($serializer, getOptionsLibmemcached()); + $adapter = new Libmemcached($serializer, getOptionsLibmemcached()); $adapter->setEventsManager(new Manager()); @@ -65,7 +66,7 @@ public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): $I->wantToTest('Cache\Adapter\Libmemcached - triggered ' . $example->offsetGet(0)); $serializer = new SerializerFactory(); - $adapter = new Libmemcached($serializer, getOptionsLibmemcached()); + $adapter = new Libmemcached($serializer, getOptionsLibmemcached()); $counter = 0; $manager = new Manager(); @@ -73,8 +74,10 @@ public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): $manager->attach( 'cache:' . $example->offsetGet(0), - static function () use (&$counter): void { + static function (Event $event) use (&$counter, $example): void { $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); } ); diff --git a/tests/integration/Cache/Adapter/Memory/EventsCest.php b/tests/integration/Cache/Adapter/Memory/EventsCest.php index e8acc0829fd..e7adec2f364 100644 --- a/tests/integration/Cache/Adapter/Memory/EventsCest.php +++ b/tests/integration/Cache/Adapter/Memory/EventsCest.php @@ -14,6 +14,7 @@ use Codeception\Example; use IntegrationTester; use Phalcon\Cache\Adapter\Memory; +use Phalcon\Events\Event; use Phalcon\Events\Manager; use Phalcon\Storage\SerializerFactory; @@ -73,8 +74,10 @@ public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): $manager->attach( 'cache:' . $example->offsetGet(0), - static function () use (&$counter): void { + static function (Event $event) use (&$counter, $example): void { $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); } ); diff --git a/tests/integration/Cache/Adapter/Redis/EventsCest.php b/tests/integration/Cache/Adapter/Redis/EventsCest.php index e267cf5975c..f6372b508a5 100644 --- a/tests/integration/Cache/Adapter/Redis/EventsCest.php +++ b/tests/integration/Cache/Adapter/Redis/EventsCest.php @@ -14,6 +14,7 @@ use Codeception\Example; use IntegrationTester; use Phalcon\Cache\Adapter\Redis; +use Phalcon\Events\Event; use Phalcon\Events\Manager; use Phalcon\Storage\SerializerFactory; @@ -73,8 +74,10 @@ public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): $manager->attach( 'cache:' . $example->offsetGet(0), - static function () use (&$counter): void { + static function (Event $event) use (&$counter, $example): void { $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); } ); diff --git a/tests/integration/Cache/Adapter/Stream/EventsCest.php b/tests/integration/Cache/Adapter/Stream/EventsCest.php index f591cb4820c..ea4f341846d 100644 --- a/tests/integration/Cache/Adapter/Stream/EventsCest.php +++ b/tests/integration/Cache/Adapter/Stream/EventsCest.php @@ -14,6 +14,7 @@ use Codeception\Example; use IntegrationTester; use Phalcon\Cache\Adapter\Stream; +use Phalcon\Events\Event; use Phalcon\Events\Manager; use Phalcon\Storage\SerializerFactory; @@ -88,8 +89,10 @@ public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): $manager->attach( 'cache:' . $example->offsetGet(0), - static function () use (&$counter): void { + static function (Event $event) use (&$counter, $example): void { $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); } ); diff --git a/tests/integration/Cache/Adapter/Weak/EventsCest.php b/tests/integration/Cache/Adapter/Weak/EventsCest.php index a133df10d62..7e2e1af2696 100644 --- a/tests/integration/Cache/Adapter/Weak/EventsCest.php +++ b/tests/integration/Cache/Adapter/Weak/EventsCest.php @@ -14,6 +14,7 @@ use Codeception\Example; use IntegrationTester; use Phalcon\Cache\Adapter\Weak; +use Phalcon\Events\Event; use Phalcon\Events\Manager; use Phalcon\Storage\SerializerFactory; @@ -73,8 +74,10 @@ public function cacheCacheEventTriggers(IntegrationTester $I, Example $example): $manager->attach( 'cache:' . $example->offsetGet(0), - static function () use (&$counter): void { + static function (Event $event) use (&$counter, $example): void { $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); } ); diff --git a/tests/integration/Storage/Adapter/EventsCest.php b/tests/integration/Storage/Adapter/EventsCest.php index 5c61525d47b..8481b3adc67 100644 --- a/tests/integration/Storage/Adapter/EventsCest.php +++ b/tests/integration/Storage/Adapter/EventsCest.php @@ -13,6 +13,7 @@ use Codeception\Example; use IntegrationTester; +use Phalcon\Events\Event; use Phalcon\Events\Manager; use Phalcon\Storage\Adapter\Apcu; use Phalcon\Storage\Adapter\Libmemcached; @@ -60,9 +61,14 @@ public function storageAdapterEventsBeforeGet(IntegrationTester $I, Example $exa ) ); - $manager->attach('storage:beforeGet', function () use (&$counter): void { - $counter++; - }); + $manager->attach( + 'storage:beforeGet', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager); @@ -103,9 +109,14 @@ public function storageAdapterEventsAfterGet(IntegrationTester $I, Example $exam ) ); - $manager->attach('storage:afterGet', function () use (&$counter): void { - $counter++; - }); + $manager->attach( + 'storage:afterGet', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager); @@ -146,9 +157,14 @@ public function storageAdapterEventsBeforeHas(IntegrationTester $I, Example $exa ) ); - $manager->attach('storage:beforeHas', function () use (&$counter): void { - $counter++; - }); + $manager->attach( + 'storage:beforeHas', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager); @@ -189,9 +205,14 @@ public function storageAdapterEventsAfterHas(IntegrationTester $I, Example $exam ) ); - $manager->attach('storage:afterHas', function () use (&$counter): void { - $counter++; - }); + $manager->attach( + 'storage:afterHas', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager); @@ -232,9 +253,14 @@ public function storageAdapterEventsBeforeDelete(IntegrationTester $I, Example $ ) ); - $manager->attach('storage:beforeDelete', function () use (&$counter): void { - $counter++; - }); + $manager->attach( + 'storage:beforeDelete', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager); @@ -275,9 +301,14 @@ public function storageAdapterEventsAfterDelete(IntegrationTester $I, Example $e ) ); - $manager->attach('storage:afterDelete', function () use (&$counter): void { - $counter++; - }); + $manager->attach( + 'storage:afterDelete', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager); @@ -318,9 +349,14 @@ public function storageAdapterEventsBeforeIncrement(IntegrationTester $I, Exampl ) ); - $manager->attach('storage:beforeIncrement', function () use (&$counter) { - $counter++; - }); + $manager->attach( + 'storage:beforeIncrement', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager); @@ -361,9 +397,14 @@ public function storageAdapterEventsAfterIncrement(IntegrationTester $I, Example ) ); - $manager->attach('storage:afterIncrement', function () use (&$counter): void { - $counter++; - }); + $manager->attach( + 'storage:afterIncrement', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager); @@ -404,9 +445,14 @@ public function storageAdapterEventsBeforeDecrement(IntegrationTester $I, Exampl ) ); - $manager->attach('storage:beforeDecrement', function () use (&$counter): void { - $counter++; - }); + $manager->attach( + 'storage:beforeDecrement', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager); @@ -447,9 +493,14 @@ public function storageAdapterEventsAfterDecrement(IntegrationTester $I, Example ) ); - $manager->attach('storage:afterDecrement', function () use (&$counter): void { - $counter++; - }); + $manager->attach( + 'storage:afterDecrement', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager); @@ -490,9 +541,14 @@ public function storageAdapterEventsBeforeSet(IntegrationTester $I, Example $exa ) ); - $manager->attach('storage:beforeSet', function () use (&$counter): void { - $counter++; - }); + $manager->attach( + 'storage:beforeSet', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager); @@ -533,9 +589,14 @@ public function storageAdapterEventsAfterSet(IntegrationTester $I, Example $exam ) ); - $manager->attach('storage:afterSet', function () use (&$counter): void { - $counter++; - }); + $manager->attach( + 'storage:afterSet', + static function (Event $event) use (&$counter, $example): void { + $counter++; + $data = $event->getData(); + $data === 'test' ?: throw new \RuntimeException('wrong key'); + } + ); $adapter->setEventsManager($manager);