-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename EventManager to Events, exception too, add psalm
- Loading branch information
Robin de Graaf
committed
Mar 11, 2021
1 parent
6131dea
commit c0dc2bc
Showing
11 changed files
with
131 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,6 @@ jobs: | |
|
||
- name: Run test suite | ||
run: vendor/bin/phpunit tests | ||
|
||
- name: Run static analysis | ||
run: vendor/bin/psalm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.DS_Store | ||
.idea | ||
composer.lock | ||
vendor | ||
coverage | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
|
||
namespace Parable\Event; | ||
|
||
class Exception extends \Exception | ||
class EventsException extends \Exception | ||
{ | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('*'); | ||
} | ||
} |