Skip to content

Commit 052f677

Browse files
committed
Added new option to allow EventManager override + improved EventManager tests
1 parent ae04e46 commit 052f677

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

lib/Phpfastcache/Event/EventManagerInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ interface EventManagerInterface
5252
*/
5353
public static function getInstance(): static;
5454

55+
/**
56+
* @param EventManagerInterface $eventManagerInstance
57+
* @return void
58+
*/
59+
public static function setInstance(EventManagerInterface $eventManagerInstance): void;
60+
5561
/**
5662
* @param string $eventName
5763
* @param array ...$args

lib/Phpfastcache/Event/EventReferenceParameter.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919

2020
class EventReferenceParameter
2121
{
22-
protected mixed $parameter;
23-
2422
public function __construct(
25-
mixed &$parameter,
23+
protected mixed &$parameter,
2624
protected bool $allowTypeChange = false
2725
) {
28-
$this->parameter = &$parameter;
2926
}
3027

3128
public function getParameterValue(): mixed

lib/Phpfastcache/EventManager.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public static function getInstance(): static
4545
return (self::$instance ?? self::$instance = new static());
4646
}
4747

48+
/**
49+
* @param EventManagerInterface $eventManagerInstance
50+
* @return void
51+
*/
52+
public static function setInstance(EventManagerInterface $eventManagerInstance): void
53+
{
54+
self::$instance = $eventManagerInstance;
55+
}
56+
57+
4858
/**
4959
* @param string $eventName
5060
* @param array $args
@@ -59,7 +69,7 @@ public function dispatch(string $eventName, ...$args): void
5969
if (isset($this->events[$eventName]) && $eventName !== self::ON_EVERY_EVENT) {
6070
$loopArgs = array_merge($args, [$eventName]);
6171
foreach ($this->events[$eventName] as $event) {
62-
$event(... $loopArgs);
72+
$event(...$loopArgs);
6373
}
6474
}
6575
foreach ($this->events[self::ON_EVERY_EVENT] as $event) {
@@ -107,7 +117,7 @@ public function onEveryEvents(callable $callback, string $callbackName): void
107117
public function on(array $events, callable $callback): void
108118
{
109119
foreach ($events as $event) {
110-
if (!\preg_match('#^([a-zA-z])*$#', $event)) {
120+
if (!\preg_match('#^([a-zA-Z])*$#', $event)) {
111121
throw new PhpfastcacheEventManagerException(\sprintf('Invalid event name "%s"', $event));
112122
}
113123

tests/EventManager.test.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
1818
use Phpfastcache\Event\EventReferenceParameter;
1919
use Phpfastcache\EventManager;
20+
use Phpfastcache\Exceptions\PhpfastcacheInvalidTypeException;
2021
use Phpfastcache\Tests\Helper\TestHelper;
2122

2223
chdir(__DIR__);
@@ -33,18 +34,26 @@
3334
}
3435
});
3536

37+
$eventInstance->onCacheItemSet(static function (ExtendedCacheItemInterface $item, EventReferenceParameter $eventReferenceParameter) use ($testHelper) {
38+
try{
39+
$eventReferenceParameter->setParameterValue(1000);
40+
$testHelper->assertPass('The event reference parameter accepted a value type change');
41+
} catch(PhpfastcacheInvalidTypeException){
42+
$testHelper->assertFail('The event reference parameter denied a value type change');
43+
}
44+
});
3645

3746
$cacheKey = 'testItem';
3847
$cacheKey2 = 'testItem2';
3948

4049
$item = $cacheInstance->getItem($cacheKey);
41-
$item->set(1000)->expiresAfter(60);
50+
$item->set(false)->expiresAfter(60);
4251
$cacheInstance->save($item);
4352

4453
if ($cacheInstance->getItem($cacheKey)->get() === 1337) {
4554
$testHelper->assertPass('The dispatched event executed the custom callback to alter the item');
4655
} else {
47-
$testHelper->assertFail("The dispatched event is not working properly, the expected value '1337', got '" . (int) $cacheInstance->getItem($cacheKey)->get() . "'");
56+
$testHelper->assertFail("The dispatched event is not working properly, the expected value '1337', got '" . $cacheInstance->getItem($cacheKey)->get() . "'");
4857
}
4958
$cacheInstance->clear();
5059
unset($item);
@@ -53,7 +62,14 @@
5362

5463
$eventInstance->onCacheSaveMultipleItems(static function (ExtendedCacheItemPoolInterface $itemPool, EventReferenceParameter $eventReferenceParameter) use ($testHelper) {
5564
$parameterValue = $eventReferenceParameter->getParameterValue();
56-
$eventReferenceParameter->setParameterValue([]);
65+
66+
try{
67+
$eventReferenceParameter->setParameterValue(null);
68+
$testHelper->assertFail('The event reference parameter accepted a value type change');
69+
} catch(PhpfastcacheInvalidTypeException){
70+
$testHelper->assertPass('The event reference parameter denied a value type change');
71+
$eventReferenceParameter->setParameterValue([]);
72+
}
5773

5874
if (is_array($parameterValue) && count($parameterValue) === 2) {
5975
$testHelper->assertPass('The event reference parameter returned an array of 2 cache items');

0 commit comments

Comments
 (0)