From 9dc7ed42c226ff53f941d1d2139c697515912e0e Mon Sep 17 00:00:00 2001
From: Andrew <33055211+andrew-nuwber@users.noreply.github.com>
Date: Mon, 27 Sep 2021 18:11:18 +0200
Subject: [PATCH] allow to assert that event published without payload, update
testing doc (#76)
---
readme.md | 57 ++++++++++++++++---
src/Event/Testing/PublishableEventTesting.php | 4 +-
tests/Event/PublishableTest.php | 9 +++
3 files changed, 59 insertions(+), 11 deletions(-)
diff --git a/readme.md b/readme.md
index e0986ab..5043787 100644
--- a/readme.md
+++ b/readme.md
@@ -458,27 +458,66 @@ The package also supports your application logger. To use it set config value `r
# Testing
-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
+payload;
+ }
+}
+```
+
+`Test.php`
```php
'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
diff --git a/src/Event/Testing/PublishableEventTesting.php b/src/Event/Testing/PublishableEventTesting.php
index 4d26593..7093ac0 100644
--- a/src/Event/Testing/PublishableEventTesting.php
+++ b/src/Event/Testing/PublishableEventTesting.php
@@ -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();
}
diff --git a/tests/Event/PublishableTest.php b/tests/Event/PublishableTest.php
index cf4a142..f73ea3f 100644
--- a/tests/Event/PublishableTest.php
+++ b/tests/Event/PublishableTest.php
@@ -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);