Skip to content

Commit

Permalink
Rename EventManager to Events, exception too, add psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Mar 11, 2021
1 parent 6131dea commit c0dc2bc
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ jobs:

- name: Run test suite
run: vendor/bin/phpunit tests

- name: Run static analysis
run: vendor/bin/psalm
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.idea
composer.lock
vendor
coverage
coverage
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Parable Event

## 0.4.0

_Changes_
- Add static analysis using psalm.
- `EventManager` has been renamed to `Events`.
- `Exception` has been renamed to `EventsException` for clarity.

## 0.3.0

_Changes_
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ dependencies:
--no-plugins \
--no-scripts

psalm:
vendor/bin/psalm --clear-cache
vendor/bin/psalm

tests: dependencies
vendor/bin/phpunit --verbose tests

coverage: dependencies
rm -rf ./coverage
vendor/bin/phpunit --coverage-html ./coverage tests
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests

tests-clean:
vendor/bin/phpunit --verbose tests

coverage-clean:
rm -rf ./coverage
vendor/bin/phpunit --coverage-html ./coverage tests
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ $ composer require parable-php/event
## Usage

Events are very simple. You add listeners to events (`string` values) and then trigger an update with those events. You
can pass payloads into the `trigger` calls, which will get passed to all relevant listeners.
can pass payloads into the `trigger` calls, which will get passed to all relevant listeners.

```php
use \Parable\Event\EventManager;
use \Parable\Event\Events;

$eventManager = new EventManager();
$eventManager = new Events();

$eventManager->listen('event_number_one', function (string $event, string &$payload) {
$payload .= '-updated!';
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"php": ">=8.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
"phpunit/phpunit": "^8.0",
"vimeo/psalm": "^4.6"
},
"autoload": {
"psr-4": {
Expand Down
19 changes: 19 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<UnresolvableInclude errorLevel="suppress" />
</issueHandlers>
</psalm>
10 changes: 4 additions & 6 deletions src/EventManager.php → src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace Parable\Event;

class EventManager
class Events
{
protected const GLOBAL_EVENT = '*';

/**
* @var callable[][]
*/
/** @var callable[][] */
protected array $listeners = [];

public function listen(string $event, callable $listener): void
Expand All @@ -33,7 +31,7 @@ public function listenAll(callable $listener): void
public function trigger(string $event, &$payload = null)
{
if ($event === self::GLOBAL_EVENT) {
throw new Exception('Cannot specifically trigger global event.');
throw new EventsException('Cannot trigger global event, only listen to it.');
}

foreach ($this->getListeners($event) as $listener) {
Expand All @@ -44,7 +42,7 @@ public function trigger(string $event, &$payload = null)
/**
* Return the callables associated with the event.
*
* @return callable[][]
* @return callable[]
*/
protected function getListeners(string $event): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exception.php → src/EventsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Parable\Event;

class Exception extends \Exception
class EventsException extends \Exception
{
}
84 changes: 0 additions & 84 deletions tests/EventManagerTest.php

This file was deleted.

84 changes: 84 additions & 0 deletions tests/EventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php declare(strict_types=1);

namespace Parable\Event\Tests;

use Parable\Event\Events;
use Parable\Event\EventsException;
use PHPUnit\Framework\TestCase;

class EventsTest extends TestCase
{
protected Events $events;

public function setUp(): void
{
$this->events = new Events();
}

public function testListenToEventAndUpdate(): void
{
$this->events->listen('test_event', static function ($event, string &$payload) {
$payload .= "-suffixed";
});

$payload = 'payload';

$this->events->trigger('test_event', $payload);

self::assertSame('payload-suffixed', $payload);
}

public function testMultipleEvents(): void
{
$this->events->listen('test_event', static function ($event, string &$payload) {
$payload .= "-suffixed";
});
$this->events->listen('test_event', static function ($event, string &$payload) {
$payload .= "-twice!";
});

$payload = 'payload';

$this->events->trigger('test_event', $payload);

self::assertSame('payload-suffixed-twice!', $payload);
}

public function testSameEventMultipleTimesGetsCalledOnce(): void
{
$closure = static function ($event, string &$payload) {
$payload .= "-suffixed";
};

$this->events->listen('test_event', $closure);
$this->events->listen('test_event', $closure);

$payload = 'payload';

$this->events->trigger('test_event', $payload);

self::assertSame('payload-suffixed', $payload);
}

public function testGlobalListeners(): void
{
$this->events->listenAll(static function ($event, string &$payload) {
$payload .= "-suffixed";
});

$payload = 'payload';

$this->events->trigger('once update', $payload);
$this->events->trigger('twice update', $payload);

self::assertSame('payload-suffixed-suffixed', $payload);
}

public function testCannotTriggerGlobalEvent(): void
{
$this->expectException(EventsException::class);
$this->expectExceptionMessage('Cannot trigger global event, only listen to it.');

$this->events->trigger('*');
}
}

0 comments on commit c0dc2bc

Please sign in to comment.