You've already forked php-flasher
mirror of
https://github.com/php-flasher/php-flasher.git
synced 2026-04-05 12:32:55 +01:00
add more tests
This commit is contained in:
@@ -83,21 +83,21 @@ final class FilterBuilder
|
||||
if (null !== $orderings) {
|
||||
usort($envelopes, static function (Envelope $a, Envelope $b) use ($orderings) {
|
||||
foreach ($orderings as $field => $ordering) {
|
||||
if (FilterBuilder::ASC === $ordering) {
|
||||
if (FilterBuilder::ASC !== $ordering) {
|
||||
list($a, $b) = array($b, $a);
|
||||
}
|
||||
|
||||
$stampA = $a->get($field);
|
||||
$stampB = $b->get($field);
|
||||
|
||||
if (!$stampA instanceof OrderableStampInterface) {
|
||||
return 0;
|
||||
if (!$stampA instanceof OrderableStampInterface || !$stampB instanceof OrderableStampInterface) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return $stampA->compare($stampB);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return -1;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -34,14 +34,14 @@ final class CreatedAtStamp implements StampInterface, OrderableStampInterface
|
||||
/**
|
||||
* @param mixed $orderable
|
||||
*
|
||||
* @return bool
|
||||
* @return int
|
||||
*/
|
||||
public function compare($orderable)
|
||||
{
|
||||
if (!$orderable instanceof CreatedAtStamp) {
|
||||
return false;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return $this->createdAt > $orderable->createdAt;
|
||||
return $this->createdAt->getTimestamp() - $orderable->createdAt->getTimestamp();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ interface OrderableStampInterface
|
||||
/**
|
||||
* @param mixed $orderable
|
||||
*
|
||||
* @return bool
|
||||
* @return int
|
||||
*/
|
||||
public function compare($orderable);
|
||||
}
|
||||
|
||||
@@ -28,14 +28,14 @@ final class PriorityStamp implements StampInterface, OrderableStampInterface
|
||||
/**
|
||||
* @param mixed $orderable
|
||||
*
|
||||
* @return bool
|
||||
* @return int
|
||||
*/
|
||||
public function compare($orderable)
|
||||
{
|
||||
if (!$orderable instanceof PriorityStamp) {
|
||||
return false;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return $this->priority > $orderable->priority;
|
||||
return $this->priority - $orderable->priority;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,61 +2,218 @@
|
||||
|
||||
namespace Flasher\Prime\Tests\EventDispatcher;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\StoppableEventInterface;
|
||||
use Flasher\Prime\EventDispatcher\EventDispatcher;
|
||||
use Flasher\Prime\EventDispatcher\EventListener\EventSubscriberInterface;
|
||||
use Flasher\Prime\Tests\TestCase;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
|
||||
class EventDispatcherTest extends TestCase
|
||||
{
|
||||
/* Some pseudo events */
|
||||
const preFoo = 'pre.foo';
|
||||
const postFoo = 'post.foo';
|
||||
const preBar = 'pre.bar';
|
||||
const postBar = 'post.bar';
|
||||
|
||||
/**
|
||||
* @var EventDispatcher
|
||||
*/
|
||||
private $dispatcher;
|
||||
|
||||
/**
|
||||
* @var TestEventListener
|
||||
*/
|
||||
private $listener;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->dispatcher = new EventDispatcher();
|
||||
$this->listener = new TestEventListener();
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->dispatcher = null;
|
||||
$this->listener = null;
|
||||
}
|
||||
|
||||
public function testInitialState()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$this->assertSame(array(), $dispatcher->getListeners('fake_event'));
|
||||
$this->assertSame(array(), $this->dispatcher->getListeners('fake_event'));
|
||||
}
|
||||
|
||||
public function testAddListener()
|
||||
{
|
||||
$listener = $this->getMockBuilder('Flasher\Prime\EventDispatcher\EventListener\EventSubscriberInterface')->getMock();
|
||||
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->addListener('mocked_event', array($listener, '__invoke'));
|
||||
|
||||
$this->assertSame(array(array($listener, '__invoke')), $dispatcher->getListeners('mocked_event'));
|
||||
$this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo'));
|
||||
$this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'));
|
||||
$this->assertCount(1, $this->dispatcher->getListeners(self::preFoo));
|
||||
$this->assertCount(1, $this->dispatcher->getListeners(self::postFoo));
|
||||
}
|
||||
|
||||
public function testGetListenersSortsByPriority()
|
||||
{
|
||||
$listener1 = $this->getMockBuilder('Flasher\Prime\EventDispatcher\EventListener\EventSubscriberInterface')->getMock();
|
||||
$listener2 = $this->getMockBuilder('Flasher\Prime\EventDispatcher\EventListener\EventSubscriberInterface')->getMock();
|
||||
$listener3 = $this->getMockBuilder('Flasher\Prime\EventDispatcher\EventListener\EventSubscriberInterface')->getMock();
|
||||
$listener1 = new TestEventListener();
|
||||
$listener2 = new TestEventListener();
|
||||
$listener3 = new TestEventListener();
|
||||
$listener1->name = '1';
|
||||
$listener2->name = '2';
|
||||
$listener3->name = '3';
|
||||
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->addListener('mocked_event', array($listener1, '__invoke'), -10);
|
||||
$dispatcher->addListener('mocked_event', array($listener2, '__invoke'), 10);
|
||||
$dispatcher->addListener('mocked_event', array($listener3, '__invoke'));
|
||||
$this->dispatcher->addListener('pre.foo', array($listener1, 'preFoo'), -10);
|
||||
$this->dispatcher->addListener('pre.foo', array($listener2, 'preFoo'), 10);
|
||||
$this->dispatcher->addListener('pre.foo', array($listener3, 'preFoo'));
|
||||
|
||||
$expected = array(
|
||||
array($listener2, '__invoke'),
|
||||
array($listener3, '__invoke'),
|
||||
array($listener1, '__invoke'),
|
||||
array($listener2, 'preFoo'),
|
||||
array($listener3, 'preFoo'),
|
||||
array($listener1, 'preFoo'),
|
||||
);
|
||||
|
||||
$this->assertSame($expected, $dispatcher->getListeners('mocked_event'));
|
||||
$this->assertSame($expected, $this->dispatcher->getListeners('pre.foo'));
|
||||
}
|
||||
|
||||
public function testDispatch()
|
||||
{
|
||||
return;
|
||||
$this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo'));
|
||||
$this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'));
|
||||
$this->dispatcher->dispatch(self::preFoo);
|
||||
$event = new Event();
|
||||
$this->dispatcher->addListener('Flasher\Prime\Tests\EventDispatcher\Event', array($this->listener, 'preFoo'));
|
||||
$this->dispatcher->addListener('NotFoundEvent', array($this->listener, 'postFoo'));
|
||||
|
||||
$return = $this->dispatcher->dispatch($event);
|
||||
|
||||
$this->assertTrue($this->listener->preFooInvoked);
|
||||
$this->assertFalse($this->listener->postFooInvoked);
|
||||
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch('noevent'));
|
||||
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(self::preFoo));
|
||||
$event = new Event();
|
||||
$return = $this->dispatcher->dispatch(self::preFoo, $event);
|
||||
$this->assertEquals('pre.foo', $event->getName());
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\Tests\EventDispatcher\Event', $return);
|
||||
$this->assertSame($event, $return);
|
||||
}
|
||||
|
||||
public function testDispatchForClosure()
|
||||
{
|
||||
$invoked = 0;
|
||||
$listener = function () use (&$invoked) {
|
||||
++$invoked;
|
||||
};
|
||||
|
||||
$event = new Event();
|
||||
$this->dispatcher->addListener('Flasher\Prime\Tests\EventDispatcher\Event', $listener);
|
||||
$this->dispatcher->addListener('AnotherEvent', $listener);
|
||||
$this->dispatcher->dispatch($event);
|
||||
$this->assertEquals(1, $invoked);
|
||||
}
|
||||
|
||||
public function testStopEventPropagation()
|
||||
{
|
||||
$otherListener = new TestEventListener();
|
||||
|
||||
$event = new Event();
|
||||
// postFoo() stops the propagation, so only one listener should
|
||||
// be executed
|
||||
// Manually set priority to enforce $this->listener to be called first
|
||||
$this->dispatcher->addListener('Flasher\Prime\Tests\EventDispatcher\Event', array($this->listener, 'postFoo'), 10);
|
||||
$this->dispatcher->addListener('Flasher\Prime\Tests\EventDispatcher\Event', array($otherListener, 'preFoo'));
|
||||
$this->dispatcher->dispatch($event);
|
||||
$this->assertTrue($this->listener->postFooInvoked);
|
||||
$this->assertFalse($otherListener->postFooInvoked);
|
||||
}
|
||||
|
||||
public function testDispatchByPriority()
|
||||
{
|
||||
$event = new Event();
|
||||
|
||||
$invoked = array();
|
||||
$listener1 = function () use (&$invoked) {
|
||||
$invoked[] = '1';
|
||||
};
|
||||
$listener2 = function () use (&$invoked) {
|
||||
$invoked[] = '2';
|
||||
};
|
||||
$listener3 = function () use (&$invoked) {
|
||||
$invoked[] = '3';
|
||||
};
|
||||
$this->dispatcher->addListener('Flasher\Prime\Tests\EventDispatcher\Event', $listener1, -10);
|
||||
$this->dispatcher->addListener('Flasher\Prime\Tests\EventDispatcher\Event', $listener2);
|
||||
$this->dispatcher->addListener('Flasher\Prime\Tests\EventDispatcher\Event', $listener3, 10);
|
||||
$this->dispatcher->dispatch($event);
|
||||
$this->assertEquals(array('3', '2', '1'), $invoked);
|
||||
}
|
||||
}
|
||||
|
||||
class Event implements StoppableEventInterface
|
||||
{
|
||||
private $propagationStopped = false;
|
||||
|
||||
private $data;
|
||||
|
||||
public function __construct($data = null)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function isPropagationStopped()
|
||||
{
|
||||
return $this->propagationStopped;
|
||||
}
|
||||
|
||||
public function stopPropagation()
|
||||
{
|
||||
$this->propagationStopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
class CallableClass
|
||||
{
|
||||
public function __invoke()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventListener
|
||||
{
|
||||
public $preFooInvoked = false;
|
||||
|
||||
public $postFooInvoked = false;
|
||||
|
||||
public function preFoo(Event $e)
|
||||
{
|
||||
$this->preFooInvoked = true;
|
||||
}
|
||||
|
||||
public function postFoo(Event $e)
|
||||
{
|
||||
$this->postFooInvoked = true;
|
||||
|
||||
$e->stopPropagation();
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array('pre.foo' => 'preFoo', 'post.foo' => 'postFoo');
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventSubscriberWithPriorities implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
'pre.foo' => array('preFoo', 10),
|
||||
'post.foo' => array('postFoo'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
'pre.foo' => array(
|
||||
array('preFoo1'),
|
||||
array('preFoo2', 10),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace Flasher\Prime\Tests\Filter;
|
||||
|
||||
use Flasher\Prime\Envelope;
|
||||
use Flasher\Prime\Filter\FilterBuilder;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\CreatedAtStamp;
|
||||
use Flasher\Prime\Stamp\PriorityStamp;
|
||||
use Flasher\Prime\Tests\TestCase;
|
||||
|
||||
final class FilterBuilderTest extends TestCase
|
||||
{
|
||||
public function testEmptyFilter()
|
||||
{
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
|
||||
$builder = new FilterBuilder();
|
||||
|
||||
$filtered = $builder->filter($envelopes);
|
||||
$expected = $envelopes;
|
||||
|
||||
$this->assertSame($expected, $filtered);
|
||||
}
|
||||
|
||||
public function testMaxResults()
|
||||
{
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
|
||||
$builder = new FilterBuilder();
|
||||
$builder->setMaxResults(2);
|
||||
|
||||
$filtered = $builder->filter($envelopes);
|
||||
$expected = array_slice($envelopes, 0, 2);
|
||||
|
||||
$this->assertSame($expected, $filtered);
|
||||
}
|
||||
|
||||
public function testOrderingByPriority()
|
||||
{
|
||||
$envelopes = array(
|
||||
0 => new Envelope(new Notification(), array(
|
||||
new PriorityStamp(5),
|
||||
new CreatedAtStamp(),
|
||||
)),
|
||||
1 => new Envelope(new Notification(), array(
|
||||
new PriorityStamp(4),
|
||||
)),
|
||||
2 => new Envelope(new Notification(), array(
|
||||
new PriorityStamp(-2),
|
||||
)),
|
||||
3 => new Envelope(new Notification(), array(
|
||||
new CreatedAtStamp(),
|
||||
)),
|
||||
4 => new Envelope(new Notification(), array(
|
||||
new PriorityStamp(7),
|
||||
)),
|
||||
5 => new Envelope(new Notification(), array(
|
||||
new PriorityStamp(6),
|
||||
)),
|
||||
6 => new Envelope(new Notification(), array(
|
||||
new PriorityStamp(-1),
|
||||
)),
|
||||
);
|
||||
|
||||
$builder = new FilterBuilder();
|
||||
$builder->orderBy(array(
|
||||
'Flasher\Prime\Stamp\PriorityStamp' => FilterBuilder::ASC,
|
||||
));
|
||||
|
||||
$filtered = $builder->filter($envelopes);
|
||||
$expected = array(
|
||||
$envelopes[3],
|
||||
$envelopes[2],
|
||||
$envelopes[6],
|
||||
$envelopes[1],
|
||||
$envelopes[0],
|
||||
$envelopes[5],
|
||||
$envelopes[4],
|
||||
);
|
||||
|
||||
$this->assertSame($expected, $filtered);
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ final class ArrayStorageTest extends TestCase
|
||||
$envelopes = array();
|
||||
|
||||
foreach (range(0, 4) as $index) {
|
||||
$envelopes[$index] = new Envelope(new Notification('success', 'success message'));
|
||||
$envelopes[$index] = new Envelope(new Notification());
|
||||
$storage->add($envelopes[$index]);
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ final class ArrayStorageTest extends TestCase
|
||||
$envelopes = array();
|
||||
|
||||
foreach (range(0, 4) as $index) {
|
||||
$envelopes[$index] = new Envelope(new Notification('success', 'success message'), new UuidStamp());
|
||||
$envelopes[$index] = new Envelope(new Notification(), new UuidStamp());
|
||||
$storage->add($envelopes[$index]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user