| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?php
- /*
- * This file is part of Evenement.
- *
- * (c) Igor Wiedler <igor@wiedler.ch>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Evenement\Tests;
- use Evenement\EventEmitter;
- use PHPUnit\Framework\TestCase;
- class EventEmitterTest extends TestCase
- {
- private $emitter;
- public function setUp()
- {
- $this->emitter = new EventEmitter();
- }
- public function testAddListenerWithLambda()
- {
- $this->emitter->on('foo', function () {});
- }
- public function testAddListenerWithMethod()
- {
- $listener = new Listener();
- $this->emitter->on('foo', [$listener, 'onFoo']);
- }
- public function testAddListenerWithStaticMethod()
- {
- $this->emitter->on('bar', ['Evenement\Tests\Listener', 'onBar']);
- }
- public function testAddListenerWithInvalidListener()
- {
- try {
- $this->emitter->on('foo', 'not a callable');
- $this->fail();
- } catch (\Exception $e) {
- } catch (\TypeError $e) {
- }
- }
- public function testOnce()
- {
- $listenerCalled = 0;
- $this->emitter->once('foo', function () use (&$listenerCalled) {
- $listenerCalled++;
- });
- $this->assertSame(0, $listenerCalled);
- $this->emitter->emit('foo');
- $this->assertSame(1, $listenerCalled);
- $this->emitter->emit('foo');
- $this->assertSame(1, $listenerCalled);
- }
- public function testOnceWithArguments()
- {
- $capturedArgs = [];
- $this->emitter->once('foo', function ($a, $b) use (&$capturedArgs) {
- $capturedArgs = array($a, $b);
- });
- $this->emitter->emit('foo', array('a', 'b'));
- $this->assertSame(array('a', 'b'), $capturedArgs);
- }
- public function testEmitWithoutArguments()
- {
- $listenerCalled = false;
- $this->emitter->on('foo', function () use (&$listenerCalled) {
- $listenerCalled = true;
- });
- $this->assertSame(false, $listenerCalled);
- $this->emitter->emit('foo');
- $this->assertSame(true, $listenerCalled);
- }
- public function testEmitWithOneArgument()
- {
- $test = $this;
- $listenerCalled = false;
- $this->emitter->on('foo', function ($value) use (&$listenerCalled, $test) {
- $listenerCalled = true;
- $test->assertSame('bar', $value);
- });
- $this->assertSame(false, $listenerCalled);
- $this->emitter->emit('foo', ['bar']);
- $this->assertSame(true, $listenerCalled);
- }
- public function testEmitWithTwoArguments()
- {
- $test = $this;
- $listenerCalled = false;
- $this->emitter->on('foo', function ($arg1, $arg2) use (&$listenerCalled, $test) {
- $listenerCalled = true;
- $test->assertSame('bar', $arg1);
- $test->assertSame('baz', $arg2);
- });
- $this->assertSame(false, $listenerCalled);
- $this->emitter->emit('foo', ['bar', 'baz']);
- $this->assertSame(true, $listenerCalled);
- }
- public function testEmitWithNoListeners()
- {
- $this->emitter->emit('foo');
- $this->emitter->emit('foo', ['bar']);
- $this->emitter->emit('foo', ['bar', 'baz']);
- }
- public function testEmitWithTwoListeners()
- {
- $listenersCalled = 0;
- $this->emitter->on('foo', function () use (&$listenersCalled) {
- $listenersCalled++;
- });
- $this->emitter->on('foo', function () use (&$listenersCalled) {
- $listenersCalled++;
- });
- $this->assertSame(0, $listenersCalled);
- $this->emitter->emit('foo');
- $this->assertSame(2, $listenersCalled);
- }
- public function testRemoveListenerMatching()
- {
- $listenersCalled = 0;
- $listener = function () use (&$listenersCalled) {
- $listenersCalled++;
- };
- $this->emitter->on('foo', $listener);
- $this->emitter->removeListener('foo', $listener);
- $this->assertSame(0, $listenersCalled);
- $this->emitter->emit('foo');
- $this->assertSame(0, $listenersCalled);
- }
- public function testRemoveListenerNotMatching()
- {
- $listenersCalled = 0;
- $listener = function () use (&$listenersCalled) {
- $listenersCalled++;
- };
- $this->emitter->on('foo', $listener);
- $this->emitter->removeListener('bar', $listener);
- $this->assertSame(0, $listenersCalled);
- $this->emitter->emit('foo');
- $this->assertSame(1, $listenersCalled);
- }
- public function testRemoveAllListenersMatching()
- {
- $listenersCalled = 0;
- $this->emitter->on('foo', function () use (&$listenersCalled) {
- $listenersCalled++;
- });
- $this->emitter->removeAllListeners('foo');
- $this->assertSame(0, $listenersCalled);
- $this->emitter->emit('foo');
- $this->assertSame(0, $listenersCalled);
- }
- public function testRemoveAllListenersNotMatching()
- {
- $listenersCalled = 0;
- $this->emitter->on('foo', function () use (&$listenersCalled) {
- $listenersCalled++;
- });
- $this->emitter->removeAllListeners('bar');
- $this->assertSame(0, $listenersCalled);
- $this->emitter->emit('foo');
- $this->assertSame(1, $listenersCalled);
- }
- public function testRemoveAllListenersWithoutArguments()
- {
- $listenersCalled = 0;
- $this->emitter->on('foo', function () use (&$listenersCalled) {
- $listenersCalled++;
- });
- $this->emitter->on('bar', function () use (&$listenersCalled) {
- $listenersCalled++;
- });
- $this->emitter->removeAllListeners();
- $this->assertSame(0, $listenersCalled);
- $this->emitter->emit('foo');
- $this->emitter->emit('bar');
- $this->assertSame(0, $listenersCalled);
- }
- public function testCallablesClosure()
- {
- $calledWith = null;
- $this->emitter->on('foo', function ($data) use (&$calledWith) {
- $calledWith = $data;
- });
- $this->emitter->emit('foo', ['bar']);
- self::assertSame('bar', $calledWith);
- }
- public function testCallablesClass()
- {
- $listener = new Listener();
- $this->emitter->on('foo', [$listener, 'onFoo']);
- $this->emitter->emit('foo', ['bar']);
- self::assertSame(['bar'], $listener->getData());
- }
- public function testCallablesClassInvoke()
- {
- $listener = new Listener();
- $this->emitter->on('foo', $listener);
- $this->emitter->emit('foo', ['bar']);
- self::assertSame(['bar'], $listener->getMagicData());
- }
- public function testCallablesStaticClass()
- {
- $this->emitter->on('foo', '\Evenement\Tests\Listener::onBar');
- $this->emitter->emit('foo', ['bar']);
- self::assertSame(['bar'], Listener::getStaticData());
- }
- public function testCallablesFunction()
- {
- $this->emitter->on('foo', '\Evenement\Tests\setGlobalTestData');
- $this->emitter->emit('foo', ['bar']);
- self::assertSame('bar', $GLOBALS['evenement-evenement-test-data']);
- unset($GLOBALS['evenement-evenement-test-data']);
- }
- }
|