Skip to content

Commit

Permalink
allow to assert that event published without payload, update testing …
Browse files Browse the repository at this point in the history
…doc (#76)
  • Loading branch information
andrew-nuwber authored Sep 27, 2021
1 parent 7de5a9e commit 9dc7ed4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
57 changes: 48 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,27 +458,66 @@ The package also supports your application logger. To use it set config value `r

# Testing <a name="testing"></a>

We always write tests. Tests in our applications contains many mocks and fakes to test how events published. We've made this process a bit easier for Event classes that implements `ShouldPublish` and uses `Publishable` trait.
We always write tests. Tests in our applications contains many mocks and fakes to test how events published.
We've made this process a bit easier for Event classes that implements `ShouldPublish` and uses `Publishable` trait.

Simply use `PublishableEventTesting` trait that provides assertion methods in class that you want to test.

`Event.php`

```php
<?php

namespace App\BroadcastEvents;

use Nuwber\Events\Event\Publishable;
use Nuwber\Events\Event\ShouldPublish;
use Nuwber\Events\Event\Testing\PublishableEventTesting;

class Event implements ShouldPublish
{
use Publishable;
use PublishableEventTesting;

public function __construct(private array $payload)
{
}

public function publishEventKey(): string
{
return 'something.happened';
}

public function toPublish(): array
{
return $this->payload;
}
}
```

`Test.php`

```php
<?php
use \App\Listeners\Listener;

Listener::fake();
use \App\BroadcastEvents\Event;
use \App\BroadcastEvents\AnotherEvent;

Event::fake();

$payload = [
"key1" => 'value1',
"key2" => 'value2',
'key1' => 'value1',
'key2' => 'value2',
];

Listener::publish($payload);
Event::publish($payload);

Listener::assertPublished('something.happened', $payload);
Event::assertPublished('something.happened', $payload);

AnotherListener::assertNotPublished();
AnotherEvent::assertNotPublished();
```

If assertion not passes `Mockery\Exception\InvalidCountException` will bw thrown.
If assertion not passes `Mockery\Exception\InvalidCountException` will bw thrown.
Don't forget to call `\Mockery::close()` in `tearDown` or similar methods of your tests.

# Non-standard use <a name="#non-standard-use">
Expand Down
4 changes: 2 additions & 2 deletions src/Event/Testing/PublishableEventTesting.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public static function fake(): void
Container::getInstance()->instance(Publisher::class, \Mockery::spy(Publisher::class));
}

public static function assertPublished(string $event, array $payload): void
public static function assertPublished(string $event, array $payload = null): void
{
Container::getInstance()->get(Publisher::class)
->shouldHaveReceived()
->publish(\Mockery::on(function (ShouldPublish $object) use ($event, $payload) {
return $object instanceof static
&& $object->publishEventKey() == $event
&& $object->toPublish() == $payload;
&& (is_null($payload) || $object->toPublish() == $payload);
}))
->once();
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Event/PublishableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ public function testFake()
Listener::assertPublished('something.happened', $payload);
}

public function testFakeAssertWithoutPayload()
{
Listener::fake();

self::assertNull(Listener::publish(['whatever' => 1]));

Listener::assertPublished('something.happened');
}

public function testFakeAssertionFailed()
{
$this->expectException(InvalidCountException::class);
Expand Down

0 comments on commit 9dc7ed4

Please sign in to comment.