From 4fcf78fbda906b5806184777b8c1827475b5931e Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Tue, 31 Jan 2023 22:16:14 +0100 Subject: [PATCH 1/6] test: add AddToStorageListener tests --- .../AddToStorageListenerTest.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/Prime/EventDispatcher/EventListener/AddToStorageListenerTest.php diff --git a/tests/Prime/EventDispatcher/EventListener/AddToStorageListenerTest.php b/tests/Prime/EventDispatcher/EventListener/AddToStorageListenerTest.php new file mode 100644 index 00000000..769e8041 --- /dev/null +++ b/tests/Prime/EventDispatcher/EventListener/AddToStorageListenerTest.php @@ -0,0 +1,44 @@ + + */ + +namespace Flasher\Tests\Prime\EventDispatcher\EventListener; + +use Flasher\Prime\EventDispatcher\Event\PersistEvent; +use Flasher\Prime\EventDispatcher\EventDispatcher; +use Flasher\Prime\EventDispatcher\EventListener\AddToStorageListener; +use Flasher\Prime\Notification\Envelope; +use Flasher\Prime\Notification\Notification; +use Flasher\Prime\Stamp\UnlessStamp; +use Flasher\Prime\Stamp\WhenStamp; +use Flasher\Tests\Prime\TestCase; + +class AddToStorageListenerTest extends TestCase +{ + /** + * @return void + */ + public function testAddToStorageListener() + { + $eventDispatcher = new EventDispatcher(); + $this->setProperty($eventDispatcher, 'listeners', array()); + + $listener = new AddToStorageListener(); + $eventDispatcher->addSubscriber($listener); + + $envelopes = array( + new Envelope(new Notification(), new WhenStamp(false)), + new Envelope(new Notification()), + new Envelope(new Notification(), new UnlessStamp(true)), + new Envelope(new Notification()), + ); + $event = new PersistEvent($envelopes); + + $eventDispatcher->dispatch($event); + + $this->assertEquals(array($envelopes[1], $envelopes[3]), $event->getEnvelopes()); + } +} From 5f51aef458d9e45e3f2c31b4de28e97ab6a44824 Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Tue, 31 Jan 2023 22:16:33 +0100 Subject: [PATCH 2/6] test: add PresetListener tests --- .../EventListener/PresetListener.php | 18 ++-- .../EventListener/PresetListenerTest.php | 86 +++++++++++++++++++ 2 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 tests/Prime/EventDispatcher/EventListener/PresetListenerTest.php diff --git a/src/Prime/EventDispatcher/EventListener/PresetListener.php b/src/Prime/EventDispatcher/EventListener/PresetListener.php index 5ddef7cd..2e76ea3d 100644 --- a/src/Prime/EventDispatcher/EventListener/PresetListener.php +++ b/src/Prime/EventDispatcher/EventListener/PresetListener.php @@ -13,24 +13,22 @@ use Flasher\Prime\Notification\Envelope; use Flasher\Prime\Stamp\PresetStamp; /** - * @phpstan-type PresetType array{ - * string: array{ - * type: string, - * title: string, - * message: string, - * options: array, - * } - * } + * @phpstan-type PresetType array, + * }> */ final class PresetListener implements EventSubscriberInterface { /** - * @phpstan-var PresetType[] + * @phpstan-var PresetType */ private $presets = array(); /** - * @phpstan-param PresetType[] $presets + * @phpstan-param PresetType $presets */ public function __construct(array $presets) { diff --git a/tests/Prime/EventDispatcher/EventListener/PresetListenerTest.php b/tests/Prime/EventDispatcher/EventListener/PresetListenerTest.php new file mode 100644 index 00000000..2c33f8d3 --- /dev/null +++ b/tests/Prime/EventDispatcher/EventListener/PresetListenerTest.php @@ -0,0 +1,86 @@ + + */ + +namespace Flasher\Tests\Prime\EventDispatcher\EventListener; + +use Flasher\Prime\EventDispatcher\Event\PersistEvent; +use Flasher\Prime\EventDispatcher\EventDispatcher; +use Flasher\Prime\EventDispatcher\EventListener\PresetListener; +use Flasher\Prime\Notification\Envelope; +use Flasher\Prime\Notification\Notification; +use Flasher\Prime\Stamp\PresetStamp; +use Flasher\Tests\Prime\TestCase; + +class PresetListenerTest extends TestCase +{ + /** + * @return void + */ + public function testPresetListener() + { + $eventDispatcher = new EventDispatcher(); + $this->setProperty($eventDispatcher, 'listeners', array()); + + $listener = new PresetListener(array( + 'entity_saved' => array( + 'type' => 'success', + 'title' => 'PHPFlasher', + 'message' => 'success message', + 'options' => array('timeout' => 2500), + ), + )); + $eventDispatcher->addSubscriber($listener); + + $envelopes = array( + new Envelope(new Notification(), new PresetStamp('entity_saved')), + new Envelope(new Notification()), + ); + $event = new PersistEvent($envelopes); + + $eventDispatcher->dispatch($event); + + $envelopes = $event->getEnvelopes(); + + $this->assertCount(2, $envelopes); + $this->assertEquals('success', $envelopes[0]->getType()); + $this->assertEquals('PHPFlasher', $envelopes[0]->getTitle()); + $this->assertEquals('success message', $envelopes[0]->getMessage()); + $this->assertEquals(array('timeout' => 2500), $envelopes[0]->getOptions()); + } + + /** + * @return void + */ + public function testThrowExceptionIfPresetNotFound() + { + $this->setExpectedException( + 'Flasher\Prime\Exception\PresetNotFoundException', + 'Preset "entity_deleted" not found, did you forget to register it? Available presets: entity_saved' + ); + + $eventDispatcher = new EventDispatcher(); + $this->setProperty($eventDispatcher, 'listeners', array()); + + $listener = new PresetListener(array( + 'entity_saved' => array( + 'type' => 'success', + 'title' => 'PHPFlasher', + 'message' => 'success message', + 'options' => array('timeout' => 2500), + ), + )); + $eventDispatcher->addSubscriber($listener); + + $envelopes = array( + new Envelope(new Notification(), new PresetStamp('entity_deleted')), + new Envelope(new Notification()), + ); + $event = new PersistEvent($envelopes); + + $eventDispatcher->dispatch($event); + } +} From 1007c8536c9f898a87189c90765772f7c10b828d Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Tue, 31 Jan 2023 22:16:47 +0100 Subject: [PATCH 3/6] test: add RemoveListener tests --- .../EventListener/RemoveListener.php | 6 +-- .../EventListener/RemoveListenerTest.php | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 tests/Prime/EventDispatcher/EventListener/RemoveListenerTest.php diff --git a/src/Prime/EventDispatcher/EventListener/RemoveListener.php b/src/Prime/EventDispatcher/EventListener/RemoveListener.php index e73a0291..6851b76e 100644 --- a/src/Prime/EventDispatcher/EventListener/RemoveListener.php +++ b/src/Prime/EventDispatcher/EventListener/RemoveListener.php @@ -22,11 +22,7 @@ final class RemoveListener implements EventSubscriberInterface foreach ($event->getEnvelopesToRemove() as $envelope) { $hopsStamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp'); - if (!$hopsStamp instanceof HopsStamp) { - continue; - } - - if (1 === $hopsStamp->getAmount()) { + if (!$hopsStamp instanceof HopsStamp || 1 === $hopsStamp->getAmount()) { $envelopesToRemove[] = $envelope; continue; diff --git a/tests/Prime/EventDispatcher/EventListener/RemoveListenerTest.php b/tests/Prime/EventDispatcher/EventListener/RemoveListenerTest.php new file mode 100644 index 00000000..d1b363ce --- /dev/null +++ b/tests/Prime/EventDispatcher/EventListener/RemoveListenerTest.php @@ -0,0 +1,44 @@ + + */ + +namespace Flasher\Tests\Prime\EventDispatcher\EventListener; + +use Flasher\Prime\EventDispatcher\Event\RemoveEvent; +use Flasher\Prime\EventDispatcher\EventDispatcher; +use Flasher\Prime\EventDispatcher\EventListener\RemoveListener; +use Flasher\Prime\Notification\Envelope; +use Flasher\Prime\Notification\Notification; +use Flasher\Prime\Stamp\HopsStamp; +use Flasher\Tests\Prime\TestCase; + +class RemoveListenerTest extends TestCase +{ + /** + * @return void + */ + public function testRemoveListener() + { + $eventDispatcher = new EventDispatcher(); + $this->setProperty($eventDispatcher, 'listeners', array()); + + $listener = new RemoveListener(); + $eventDispatcher->addSubscriber($listener); + + $envelopes = array( + new Envelope(new Notification()), + new Envelope(new Notification(), new HopsStamp(2)), + new Envelope(new Notification(), new HopsStamp(1)), + new Envelope(new Notification(), new HopsStamp(3)), + ); + $event = new RemoveEvent($envelopes); + + $eventDispatcher->dispatch($event); + + $this->assertEquals(array($envelopes[0], $envelopes[2]), $event->getEnvelopesToRemove()); + $this->assertEquals(array($envelopes[1], $envelopes[3]), $event->getEnvelopesToKeep()); + } +} From 426bc17001b417cab13df35ea7d34fb12371c0c6 Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Tue, 31 Jan 2023 22:21:20 +0100 Subject: [PATCH 4/6] test: add StampsListener tests --- .../EventListener/StampsListenerTest.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/Prime/EventDispatcher/EventListener/StampsListenerTest.php diff --git a/tests/Prime/EventDispatcher/EventListener/StampsListenerTest.php b/tests/Prime/EventDispatcher/EventListener/StampsListenerTest.php new file mode 100644 index 00000000..11ea8a74 --- /dev/null +++ b/tests/Prime/EventDispatcher/EventListener/StampsListenerTest.php @@ -0,0 +1,45 @@ + + */ + +namespace Flasher\Tests\Prime\EventDispatcher\EventListener; + +use Flasher\Prime\EventDispatcher\Event\PersistEvent; +use Flasher\Prime\EventDispatcher\EventDispatcher; +use Flasher\Prime\EventDispatcher\EventListener\StampsListener; +use Flasher\Prime\Notification\Envelope; +use Flasher\Prime\Notification\Notification; +use Flasher\Tests\Prime\TestCase; + +class StampsListenerTest extends TestCase +{ + /** + * @return void + */ + public function testStampsListener() + { + $eventDispatcher = new EventDispatcher(); + $this->setProperty($eventDispatcher, 'listeners', array()); + + $listener = new StampsListener(); + $eventDispatcher->addSubscriber($listener); + + $envelopes = array( + new Envelope(new Notification()), + ); + $event = new PersistEvent($envelopes); + + $eventDispatcher->dispatch($event); + + $envelopes = $event->getEnvelopes(); + + $this->assertInstanceOf('Flasher\Prime\Stamp\CreatedAtStamp', $envelopes[0]->get('Flasher\Prime\Stamp\CreatedAtStamp')); + $this->assertInstanceOf('Flasher\Prime\Stamp\UuidStamp', $envelopes[0]->get('Flasher\Prime\Stamp\UuidStamp')); + $this->assertInstanceOf('Flasher\Prime\Stamp\DelayStamp', $envelopes[0]->get('Flasher\Prime\Stamp\DelayStamp')); + $this->assertInstanceOf('Flasher\Prime\Stamp\HopsStamp', $envelopes[0]->get('Flasher\Prime\Stamp\HopsStamp')); + $this->assertInstanceOf('Flasher\Prime\Stamp\PriorityStamp', $envelopes[0]->get('Flasher\Prime\Stamp\PriorityStamp')); + } +} From 7097a59cad59325376bda7f6a9485f8eb983cff3 Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Tue, 31 Jan 2023 22:41:39 +0100 Subject: [PATCH 5/6] test: add TranslationListener tests --- .../EventListener/TranslationListenerTest.php | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 tests/Prime/EventDispatcher/EventListener/TranslationListenerTest.php diff --git a/tests/Prime/EventDispatcher/EventListener/TranslationListenerTest.php b/tests/Prime/EventDispatcher/EventListener/TranslationListenerTest.php new file mode 100644 index 00000000..d40600bd --- /dev/null +++ b/tests/Prime/EventDispatcher/EventListener/TranslationListenerTest.php @@ -0,0 +1,87 @@ + + */ + +namespace Flasher\Tests\Prime\EventDispatcher\EventListener; + +use Flasher\Prime\EventDispatcher\Event\PresentationEvent; +use Flasher\Prime\EventDispatcher\EventDispatcher; +use Flasher\Prime\EventDispatcher\EventListener\TranslationListener; +use Flasher\Prime\Notification\Envelope; +use Flasher\Prime\Notification\Notification; +use Flasher\Prime\Stamp\PresetStamp; +use Flasher\Prime\Stamp\TranslationStamp; +use Flasher\Prime\Translation\EchoTranslator; +use Flasher\Tests\Prime\TestCase; + +class TranslationListenerTest extends TestCase +{ + /** + * @return void + */ + public function testTranslationListenerWithAutoTranslateEnabled() + { + $eventDispatcher = new EventDispatcher(); + $this->setProperty($eventDispatcher, 'listeners', array()); + + $listener = new TranslationListener(new EchoTranslator(), true); + $eventDispatcher->addSubscriber($listener); + + $notification = new Notification(); + $notification->setTitle('PHPFlasher'); + $notification->setMessage('success message'); + + $envelopes = array( + new Envelope($notification), + new Envelope(new Notification()), + new Envelope(new Notification()), + ); + + $envelopes[0]->withStamp(new TranslationStamp(array('resource' => 'resource'), 'ar')); + $envelopes[0]->withStamp(new PresetStamp('entity_saved', array('resource' => 'resource'))); + + $envelopes[1]->withStamp(new TranslationStamp(array('resource' => 'resource'), 'ar')); + $envelopes[1]->withStamp(new PresetStamp('entity_saved', array('resource' => 'resource'))); + + $event = new PresentationEvent($envelopes, array()); + $eventDispatcher->dispatch($event); + + $this->assertEquals($envelopes, $event->getEnvelopes()); + } + + /** + * @return void + */ + public function testTranslationListenerWithAutoTranslateDisabled() + { + $eventDispatcher = new EventDispatcher(); + $this->setProperty($eventDispatcher, 'listeners', array()); + + $listener = new TranslationListener(new EchoTranslator(), false); + $eventDispatcher->addSubscriber($listener); + + $notification = new Notification(); + $notification->setTitle('PHPFlasher'); + $notification->setMessage('success message'); + + $envelopes = array( + new Envelope($notification), + new Envelope(new Notification()), + new Envelope(new Notification()), + ); + + $envelopes[0]->withStamp(new TranslationStamp(array('resource' => 'resource'), 'ar')); + $envelopes[0]->withStamp(new PresetStamp('entity_saved', array('resource' => 'resource'))); + + $envelopes[1]->withStamp(new TranslationStamp(array('resource' => 'resource'), 'ar')); + $envelopes[1]->withStamp(new PresetStamp('entity_saved', array('resource' => 'resource'))); + + $event = new PresentationEvent($envelopes, array()); + $eventDispatcher->dispatch($event); + + $this->assertEquals($envelopes, $event->getEnvelopes()); + } +} From 238eb063a4e683608c6b57be3e3ea42a1d84a4cb Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Tue, 31 Jan 2023 22:46:21 +0100 Subject: [PATCH 6/6] test: add testDynamicCallToNotificationBuilder test --- tests/Prime/Factory/NotificationFactoryTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Prime/Factory/NotificationFactoryTest.php b/tests/Prime/Factory/NotificationFactoryTest.php index c5c594e8..6d169821 100644 --- a/tests/Prime/Factory/NotificationFactoryTest.php +++ b/tests/Prime/Factory/NotificationFactoryTest.php @@ -33,4 +33,16 @@ class NotificationFactoryTest extends TestCase $this->assertInstanceOf('Flasher\Prime\Storage\StorageManagerInterface', $manager); } + + /** + * @return void + */ + public function testDynamicCallToNotificationBuilder() + { + $storageManager = $this->getMockBuilder('Flasher\Prime\Storage\StorageManagerInterface')->getMock(); + $storageManager->expects($this->once())->method('add'); + + $factory = new NotificationFactory($storageManager); + $factory->addCreated(); + } }