/**
  * @param \Doctrine\Common\Persistence\ObjectManager $objectManager
  * @param \Zend\Stdlib\Hydrator\HydratorInterface $hydrator
  * @param \Phpro\Apigility\Doctrine\Bulk\Event\BulkEvent $event
  */
 public function it_should_handle_create_events($objectManager, $hydrator, $event)
 {
     $this->mockSaveEntity($objectManager);
     $event->getParams()->willReturn([]);
     $hydrator->hydrate([], Argument::type('stdClass'))->shouldBeCalled();
     $event->stopPropagation(true)->shouldBeCalled();
     $result = $this->create($event);
     $result->shouldBeAnInstanceOf('Phpro\\Apigility\\Doctrine\\Bulk\\Model\\Result');
 }
 /**
  * @param BulkEvent $event
  *
  * @return object|string
  */
 public function create(BulkEvent $event)
 {
     $data = $event->getParams();
     $entity = $this->loadEntity($event);
     $this->hydrator->hydrate((array) $data, $entity);
     $this->saveEntity($entity);
     $event->stopPropagation(true);
     return $this->createResult(self::EVENT_NAME, $entity);
 }
 /**
  * @param \Doctrine\Common\Persistence\ObjectManager $objectManager
  * @param \Zend\Stdlib\Hydrator\HydratorInterface $hydrator
  * @param \Phpro\Apigility\Doctrine\Bulk\Event\BulkEvent $event
  * @param \stdClass $entity
  */
 public function it_should_handle_delete_events($objectManager, $hydrator, $event, $entity)
 {
     $this->stubLoadEntity($objectManager, $entity);
     $objectManager->remove($entity)->shouldBeCalled();
     $objectManager->flush()->shouldBeCalled();
     $event->getParams()->willReturn(['id' => 1]);
     $event->stopPropagation(true)->shouldBeCalled();
     $result = $this->delete($event);
     $result->shouldBeAnInstanceOf('Phpro\\Apigility\\Doctrine\\Bulk\\Model\\Result');
 }
 /**
  * @param \Doctrine\Common\Persistence\ObjectManager $objectManager
  * @param \Zend\Stdlib\Hydrator\HydratorInterface $hydrator
  * @param \Phpro\Apigility\Doctrine\Bulk\Event\BulkEvent $event
  * @param \Zend\Stdlib\Message $entity
  */
 public function it_should_handle_custom_events($objectManager, $hydrator, $event, $entity)
 {
     $this->stubLoadEntity($objectManager, $entity);
     $this->mockSaveEntity($objectManager);
     $event->getName()->willReturn('setContent');
     $event->getParams()->willReturn(['id' => 1, 'value' => 'content']);
     $event->stopPropagation(true)->shouldBeCalled();
     $entity->setContent('content')->shouldBeCalled();
     $result = $this->handle($event);
     $result->shouldBeAnInstanceOf('Phpro\\Apigility\\Doctrine\\Bulk\\Model\\Result');
 }
 /**
  * @param BulkEvent $e
  *
  * @return object
  * @throws
  */
 protected function loadEntity(BulkEvent $e)
 {
     $meta = $this->objectManager->getClassMetadata($this->className);
     $identifier = current($meta->getIdentifierFieldNames());
     $params = $e->getParams();
     $id = isset($params[$identifier]) ? $params[$identifier] : 0;
     $entity = $this->objectManager->find($this->className, $id);
     if (!$entity) {
         throw new \RuntimeException('Entity with id ' . $id . ' was not found');
     }
     return $entity;
 }