Skip to content

Commit

Permalink
changes to bring inline with pr review
Browse files Browse the repository at this point in the history
#88
- add ramsey/composer-install
- fix docblocks
- add import for count function
- updated unit test preferring 'self':: over 'this->' for assertSame/assertCount
  • Loading branch information
ericsizemore committed May 20, 2024
1 parent 99d2b7b commit 4bd2aeb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 72 deletions.
22 changes: 4 additions & 18 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,17 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP, extensions and composer with shivammathur/setup-php
- name: Setup PHP and extensions with shivammathur/setup-php
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring, ctype, iconv, bcmath, filter, json
coverage: xdebug, pcov
tools: composer:v2

- name: Get composer cache directory
id: composer-cache
run: |
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v4
- name: Setup Composer, install dependencies
uses: ramsey/composer-install@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: composer-${{ runner.os }}-${{ matrix.php }}-${{ hashFiles('**/composer.json') }}
restore-keys: |
composer-${{ runner.os }}-${{ matrix.php }}
composer-${{ runner.os }}-
composer-
- name: Install Composer dependencies
run: composer install --no-progress --no-interaction --optimize-autoloader --ansi
composer-options: "--optimize-autoloader"

- name: Test
run: |
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
composer.lock
vendor
.phpunit.result.cache
.phpunit.cache
.phpunit.cache
4 changes: 4 additions & 0 deletions src/EventEmitterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ public function removeAllListeners(?string $event = null): void;
/**
* Allows you to inspect the listeners attached to an event. Particularly useful
* to check if there are any listeners at all.
*
* @return array<string, array<int, (callable)>>|list<(callable)>
*/
public function listeners(?string $event = null): array;

/**
* Emit an event, which will call all listeners.
*
* @param array<mixed> $arguments
*/
public function emit(string $event, array $arguments = []): void;
}
12 changes: 4 additions & 8 deletions src/EventEmitterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use function array_search;
use function array_unique;
use function array_values;
use function count;

trait EventEmitterTrait
{
Expand Down Expand Up @@ -73,7 +74,7 @@ public function removeListener(string $event, callable $listener): void
if (false !== $index) {
unset($this->listeners[$event][$index]);

if (\count($this->listeners[$event]) === 0) {
if (count($this->listeners[$event]) === 0) {
unset($this->listeners[$event]);
}
}
Expand All @@ -85,7 +86,7 @@ public function removeListener(string $event, callable $listener): void
if (false !== $index) {
unset($this->onceListeners[$event][$index]);

if (\count($this->onceListeners[$event]) === 0) {
if (count($this->onceListeners[$event]) === 0) {
unset($this->onceListeners[$event]);
}
}
Expand All @@ -102,9 +103,6 @@ public function removeAllListeners(?string $event = null): void
}
}

/**
* @return array<string, array<int, (callable)>>|list<(callable)>
*/
public function listeners(?string $event = null): array
{
if ($event === null) {
Expand All @@ -122,6 +120,7 @@ public function listeners(?string $event = null): array
$this->onceListeners[$eventName] ?? []
);
}

return $events;
}

Expand All @@ -131,9 +130,6 @@ public function listeners(?string $event = null): array
);
}

/**
* @param array<mixed> $arguments
*/
public function emit(string $event, array $arguments = []): void
{
if ($event === '') {
Expand Down
81 changes: 36 additions & 45 deletions tests/EventEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@
use PHPUnit\Framework\TestCase;

#[CoversClass(EventEmitter::class)]
#[BackupGlobals(false)]
#[BackupStaticProperties(false)]
class EventEmitterTest extends TestCase
{
/**
* @var EventEmitter
*/
private EventEmitter $emitter;

#[Before]
Expand All @@ -40,22 +35,22 @@ public function testAddListenerWithLambda(): void
{
$this->emitter->on('foo', static function (): void {});

$this->assertCount(1, $this->emitter->listeners('foo'));
self::assertCount(1, $this->emitter->listeners('foo'));
}

public function testAddListenerWithMethod(): void
{
$listener = new Listener();
$this->emitter->on('foo', [$listener, 'onFoo']);

$this->assertCount(1, $this->emitter->listeners('foo'));
self::assertCount(1, $this->emitter->listeners('foo'));
}

public function testAddListenerWithStaticMethod(): void
{
$this->emitter->on('bar', ['Evenement\Tests\Listener', 'onBar']);

$this->assertCount(1, $this->emitter->listeners('bar'));
self::assertCount(1, $this->emitter->listeners('bar'));
}

public function testAddListenerWithInvalidListener(): void
Expand All @@ -72,15 +67,15 @@ public function testOnce(): void
$listenerCalled++;
});

$this->assertSame(0, $listenerCalled);
self::assertSame(0, $listenerCalled);

$this->emitter->emit('foo');

$this->assertSame(1, $listenerCalled);
self::assertSame(1, $listenerCalled);

$this->emitter->emit('foo');

$this->assertSame(1, $listenerCalled);
self::assertSame(1, $listenerCalled);
}

public function testOnceWithArguments(): void
Expand All @@ -93,7 +88,7 @@ public function testOnceWithArguments(): void

$this->emitter->emit('foo', ['a', 'b']);

$this->assertSame(['a', 'b'], $capturedArgs);
self::assertSame(['a', 'b'], $capturedArgs);
}

public function testEmitWithoutArguments(): void
Expand All @@ -104,44 +99,40 @@ public function testEmitWithoutArguments(): void
$listenerCalled = true;
});

$this->assertSame(false, $listenerCalled);
self::assertFalse($listenerCalled);
$this->emitter->emit('foo');
$this->assertSame(true, $listenerCalled);
self::assertTrue($listenerCalled);
}

public function testEmitWithOneArgument(): void
{
$test = $this;

$listenerCalled = false;

$this->emitter->on('foo', static function (string $value) use (&$listenerCalled, $test): void {
$this->emitter->on('foo', static function (string $value) use (&$listenerCalled): void {
$listenerCalled = true;

$test->assertSame('bar', $value);
self::assertSame('bar', $value);
});

$this->assertSame(false, $listenerCalled);
self::assertFalse($listenerCalled);
$this->emitter->emit('foo', ['bar']);
$this->assertSame(true, $listenerCalled);
self::assertTrue($listenerCalled);
}

public function testEmitWithTwoArguments(): void
{
$test = $this;

$listenerCalled = false;

$this->emitter->on('foo', static function (string $arg1, string $arg2) use (&$listenerCalled, $test): void {
$this->emitter->on('foo', static function (string $arg1, string $arg2) use (&$listenerCalled): void {
$listenerCalled = true;

$test->assertSame('bar', $arg1);
$test->assertSame('baz', $arg2);
self::assertSame('bar', $arg1);
self::assertSame('baz', $arg2);
});

$this->assertSame(false, $listenerCalled);
self::assertFalse($listenerCalled);
$this->emitter->emit('foo', ['bar', 'baz']);
$this->assertSame(true, $listenerCalled);
self::assertTrue($listenerCalled);
}

#[DoesNotPerformAssertions]
Expand All @@ -164,9 +155,9 @@ public function testEmitWithTwoListeners(): void
$listenersCalled++;
});

$this->assertSame(0, $listenersCalled);
self::assertSame(0, $listenersCalled);
$this->emitter->emit('foo');
$this->assertSame(2, $listenersCalled);
self::assertSame(2, $listenersCalled);
}

public function testRemoveListenerMatching(): void
Expand All @@ -180,9 +171,9 @@ public function testRemoveListenerMatching(): void
$this->emitter->on('foo', $listener);
$this->emitter->removeListener('foo', $listener);

$this->assertSame(0, $listenersCalled);
self::assertSame(0, $listenersCalled);
$this->emitter->emit('foo');
$this->assertSame(0, $listenersCalled);
self::assertSame(0, $listenersCalled);
}

public function testRemoveListenerNotMatching(): void
Expand All @@ -196,9 +187,9 @@ public function testRemoveListenerNotMatching(): void
$this->emitter->on('foo', $listener);
$this->emitter->removeListener('bar', $listener);

$this->assertSame(0, $listenersCalled);
self::assertSame(0, $listenersCalled);
$this->emitter->emit('foo');
$this->assertSame(1, $listenersCalled);
self::assertSame(1, $listenersCalled);
}

public function testRemoveAllListenersMatching(): void
Expand All @@ -211,9 +202,9 @@ public function testRemoveAllListenersMatching(): void

$this->emitter->removeAllListeners('foo');

$this->assertSame(0, $listenersCalled);
self::assertSame(0, $listenersCalled);
$this->emitter->emit('foo');
$this->assertSame(0, $listenersCalled);
self::assertSame(0, $listenersCalled);
}

public function testRemoveAllListenersNotMatching(): void
Expand All @@ -226,9 +217,9 @@ public function testRemoveAllListenersNotMatching(): void

$this->emitter->removeAllListeners('bar');

$this->assertSame(0, $listenersCalled);
self::assertSame(0, $listenersCalled);
$this->emitter->emit('foo');
$this->assertSame(1, $listenersCalled);
self::assertSame(1, $listenersCalled);
}

public function testRemoveAllListenersWithoutArguments(): void
Expand All @@ -245,10 +236,10 @@ public function testRemoveAllListenersWithoutArguments(): void

$this->emitter->removeAllListeners();

$this->assertSame(0, $listenersCalled);
self::assertSame(0, $listenersCalled);
$this->emitter->emit('foo');
$this->emitter->emit('bar');
$this->assertSame(0, $listenersCalled);
self::assertSame(0, $listenersCalled);
}

public function testCallablesClosure(): void
Expand Down Expand Up @@ -476,12 +467,12 @@ public function testNestedOn(): void
});

$emitter->emit('event');
$this->assertEquals(1, $first);
$this->assertEquals(0, $second);
$this->assertEquals(0, $third);
self::assertSame(1, $first);
self::assertSame(0, $second);
self::assertSame(0, $third);
$emitter->emit('event');
$this->assertEquals(2, $first);
$this->assertEquals(1, $second);
$this->assertEquals(1, $third);
self::assertSame(2, $first);
self::assertSame(1, $second);
self::assertSame(1, $third);
}
}

0 comments on commit 4bd2aeb

Please sign in to comment.