Merge pull request #131 from php-flasher/test/event_listener

test: add event listener tests
This commit is contained in:
Younes KHOUBZA
2023-01-31 22:48:00 +01:00
committed by GitHub
8 changed files with 327 additions and 15 deletions
@@ -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<string, mixed>,
* }
* }
* @phpstan-type PresetType array<string, array{
* type: string,
* title: string,
* message: string,
* options: array<string, mixed>,
* }>
*/
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)
{
@@ -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;
@@ -0,0 +1,44 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
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());
}
}
@@ -0,0 +1,86 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
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);
}
}
@@ -0,0 +1,44 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
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());
}
}
@@ -0,0 +1,45 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
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'));
}
}
@@ -0,0 +1,87 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
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());
}
}
@@ -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();
}
}