/**
  * @param string $title
  * @param object $object
  *
  * @return Event
  *
  * @throws LogicException
  */
 private function buildEvent($title, $object)
 {
     if (!is_object($object)) {
         throw new LogicException(sprintf('Connector only allows publish objects, "%s" given', gettype($object)));
     }
     $event = new Event();
     $event->setTitle($title)->setNamespace(get_class($object))->setPayload($this->serializer->serialize($object, 'json'));
     return $event;
 }
 /**
  * @param Event $event
  */
 public function listen(Event $event)
 {
     if (!in_array($event->getTitle(), $this->events, true)) {
         return;
     }
     $payload = $this->serializer->deserialize($event->getPayload(), $event->getNamespace(), 'json');
     $ndewezEvent = new NdewezEvent($payload);
     $this->dispatcher->dispatch(sprintf('ndewez_events.%s', $event->getTitle()), $ndewezEvent);
 }
 public function testListen()
 {
     $event = new Event();
     $event->setTitle('title');
     $ndewezEvent = new NdewezEvent($event);
     $dispatcher = $this->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface')->getMock();
     $dispatcher->expects($this->once())->method('dispatch')->with('ndewez_events.myEvent', $this->equalTo($ndewezEvent));
     $listener = new Listener($dispatcher, ['myEvent']);
     $eventArg = new Event();
     $eventArg->setTitle('myEvent')->setNamespace('Ndewez\\EventsBundle\\Model\\Event')->setPayload('{"title":"title"}');
     $listener->listen($eventArg);
 }
 public function testBuildEvent()
 {
     $expected = new Event();
     $expected->setTitle('title')->setNamespace('Tests\\Ndewez\\EventsBundle\\Model\\MyModel')->setPayload('{"field1":"field1","field2":"field2"}');
     $publisher = new Publisher(new MockConnector());
     $class = new \ReflectionClass($publisher);
     $method = $class->getMethod('buildEvent');
     $method->setAccessible(true);
     $object = new MyModel();
     $object->setField1('field1')->setField2('field2');
     $event = $method->invokeArgs($publisher, ['title', $object]);
     $this->assertEquals($expected, $event);
 }