Skip to content

Commit

Permalink
Fix #23: Add SimpleEventDispatcher::clearEvents() that clear all ev…
Browse files Browse the repository at this point in the history
…ents in event dispatcher
  • Loading branch information
vjik committed Feb 22, 2021
1 parent 41a852d commit f336e5f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
6 changes: 2 additions & 4 deletions 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.

4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -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": {
Expand Down
9 changes: 8 additions & 1 deletion src/EventDispatcher/SimpleEventDispatcher.php
Expand Up @@ -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<int, Closure> */
/** @var array<int, Closure> */
private array $listeners;

/** @var object[] */
private array $events = [];

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/SimpleCache/MemorySimpleCache.php
Expand Up @@ -77,6 +77,9 @@ public function clear(): bool
}

/**
* @param iterable $keys
* @param mixed $default
*
* @return mixed[]
*/
public function getMultiple($keys, $default = null): iterable
Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions tests/EventDispatcher/SimpleEventDispatcherTest.php
Expand Up @@ -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);
Expand Down

0 comments on commit f336e5f

Please sign in to comment.