diff --git a/CHANGELOG.md b/CHANGELOG.md index dd1e653..d25aecb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,10 @@ -# Test Support Change Log - +# Yii Test Support Change Log ## 1.0.1 under development -- no changes in this release. +- Enh #23: Add `SimpleEventDispatcher::clearEvents()` that clear all events in event dispatcher (vjik) ## 1.0.0 December 24, 2020 - - Initial release. diff --git a/composer.json b/composer.json index b66d9c4..edeac72 100644 --- a/composer.json +++ b/composer.json @@ -23,9 +23,9 @@ }, "require-dev": { "phpunit/phpunit": "^9.5", - "roave/infection-static-analysis-plugin": "^1.5", + "roave/infection-static-analysis-plugin": "^1.7", "spatie/phpunit-watcher": "^1.23", - "vimeo/psalm": "^4.3" + "vimeo/psalm": "^4.6" }, "autoload": { "psr-4": { diff --git a/src/EventDispatcher/SimpleEventDispatcher.php b/src/EventDispatcher/SimpleEventDispatcher.php index 6d18d45..efa9f04 100644 --- a/src/EventDispatcher/SimpleEventDispatcher.php +++ b/src/EventDispatcher/SimpleEventDispatcher.php @@ -7,12 +7,14 @@ use Closure; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\EventDispatcher\StoppableEventInterface; + use function get_class; final class SimpleEventDispatcher implements EventDispatcherInterface { - /** @var array */ + /** @var array */ private array $listeners; + /** @var object[] */ private array $events = []; @@ -41,6 +43,11 @@ public function getEvents(): array return $this->events; } + public function clearEvents(): void + { + $this->events = []; + } + public function isObjectTriggered(object $event, int $times = null): bool { return $this->processBoolResult(static fn (object $e): bool => $e === $event, $times); diff --git a/src/SimpleCache/MemorySimpleCache.php b/src/SimpleCache/MemorySimpleCache.php index 4e482ec..00cfc93 100644 --- a/src/SimpleCache/MemorySimpleCache.php +++ b/src/SimpleCache/MemorySimpleCache.php @@ -77,6 +77,9 @@ public function clear(): bool } /** + * @param iterable $keys + * @param mixed $default + * * @return mixed[] */ public function getMultiple($keys, $default = null): iterable @@ -92,6 +95,10 @@ public function getMultiple($keys, $default = null): iterable return $result; } + /** + * @param iterable $values + * @param DateInterval|int|null $ttl + */ public function setMultiple($values, $ttl = null): bool { $values = $this->iterableToArray($values); diff --git a/tests/EventDispatcher/SimpleEventDispatcherTest.php b/tests/EventDispatcher/SimpleEventDispatcherTest.php index af7e9fb..cd230f1 100644 --- a/tests/EventDispatcher/SimpleEventDispatcherTest.php +++ b/tests/EventDispatcher/SimpleEventDispatcherTest.php @@ -232,6 +232,15 @@ public function testGetEmptyEvents(): void $this->assertSame([], $dispatcher->getEvents()); } + public function testClear(): void + { + $dispatcher = $this->prepareDispatcher(); + $dispatcher->dispatch(new DateTimeImmutable()); + $dispatcher->clearEvents(); + + self::assertEmpty($dispatcher->getEvents()); + } + protected function prepareDispatcher(Closure ...$dispatcher): SimpleEventDispatcher { return new SimpleEventDispatcher(...$dispatcher);