Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Dec 27, 2021
1 parent a04d207 commit 05fac01
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
2 changes: 1 addition & 1 deletion modules/Events/src/EventBus.php
Expand Up @@ -61,7 +61,7 @@ public function unsubscribe(string $id): void
$this->subscriptionSubscriberMapping->remove($id);
}

public function publish(Event $event): void
public function publish(Contract\Event $event): void
{
$key = $event->getName();
if (!$this->eventSubscriptionsMapping->has($key)) {
Expand Down
54 changes: 53 additions & 1 deletion modules/Events/test/EventBusTest.php
Expand Up @@ -9,7 +9,8 @@
* @covers \Elephox\Events\EventBus
* @covers \Elephox\Collection\ArrayList
* @covers \Elephox\Collection\ArrayMap
* @uses \Elephox\Events\ClassNameAsEventName
* @covers \Elephox\Events\NamedEvent
* @uses \Elephox\Events\ClassNameAsEventName
*/
class EventBusTest extends TestCase
{
Expand All @@ -36,6 +37,49 @@ public function testPubSub(): void

self::assertFalse($triggered);
}

public function testPubSubNamed(): void
{
$bus = new EventBus();
$triggeredA1 = false;
$triggeredB = false;

$bus->subscribe('testA', function (TestNamedEvent $event) use (&$triggeredA1) {
$triggeredA1 = true;

self::assertEquals(5, $event->data);
});

$idA2 = $bus->subscribe('testA', function () use (&$triggeredA2) {
$triggeredA2 = true;
});

$bus->subscribe('testB', function (TestNamedEvent $event) use (&$triggeredB) {
$triggeredB = true;

self::assertEquals(6, $event->data);
});

$bus->publish(new TestNamedEvent('testA', 5));

self::assertTrue($triggeredA1);
self::assertTrue($triggeredA2);
self::assertFalse($triggeredB);

$bus->publish(new TestNamedEvent('testB', 6));

self::assertTrue($triggeredB);

$bus->unsubscribe($idA2);
$bus->unsubscribe($idA2);
}

public function testEventNameFromClass(): void
{
$testEvent = new TestEvent(5);

self::assertEquals(TestEvent::class, $testEvent->getName());
}
}

class TestEvent extends Event
Expand All @@ -44,3 +88,11 @@ public function __construct(public readonly int $data)
{
}
}

class TestNamedEvent extends NamedEvent
{
public function __construct(string $name, public readonly int $data)
{
parent::__construct($name);
}
}
43 changes: 43 additions & 0 deletions modules/Stream/test/LazyStreamTest.php
Expand Up @@ -42,4 +42,47 @@ public function testGetStreamOnlyExecutesOnce()
self::assertSame($streamMock, $stream->getStream());
self::assertTrue($stream->isReadable());
}

public function methodNameProvider(): array
{
return [
['__toString', [], "test"],
['detach', [], null],
['close', [], null],
['getSize', [], 0],
['getSize', [], null],
['tell', [], 0],
['eof', [], false],
['seek', [1, SEEK_SET], null],
['seek', [1, SEEK_CUR], null],
['seek', [1, SEEK_END], null],
['rewind', [], null],
['write', ["test"], 4],
['read', [1], ""],
['getContents', [], "test"],
['getMetadata', [null], null],
['getMetadata', [null], ['test' => true]],
['getMetadata', ['test'], true],
];
}

/**
* @dataProvider methodNameProvider
*/
public function testMockMethod(string $method, array $args, mixed $result): void
{
$streamMock = M::mock(Stream::class);

$streamMock
->expects($method)
->withArgs($args)
->andReturn($result)
;

$stream = new LazyStream(fn() => $streamMock);

$actual = $stream->{$method}(...$args);

self::assertEquals($result, $actual);
}
}

0 comments on commit 05fac01

Please sign in to comment.