public function testDeleteStopPropagation()
 {
     //Test stop propagation (pre-stage)
     $preEventCalled = false;
     $postEventCalled = false;
     $listener = $this->model->getEventManager()->attach('delete.pre', function ($e) use(&$preEventCalled) {
         /** @var $e ModelEvent */
         $preEventCalled = true;
         return 1;
     });
     $mockCriteria = $this->getMock('\\Matryoshka\\Model\\Criteria\\DeletableCriteriaInterface', ['applyDelete']);
     $this->assertEquals(1, $this->model->delete($mockCriteria));
     $this->assertTrue($preEventCalled);
     $this->assertFalse($postEventCalled);
     //Test stop propagation (pre-stage)
     $preEventCalled = false;
     $postEventCalled = false;
     $this->model->getEventManager()->clearListeners('delete.pre');
     $listener = $this->model->getEventManager()->attach('delete.post', function ($e) use(&$postEventCalled) {
         /** @var $e ModelEvent */
         $postEventCalled = true;
         return 2;
     });
     $this->assertEquals(2, $this->model->delete($mockCriteria));
     $this->assertFalse($preEventCalled);
     $this->assertTrue($postEventCalled);
 }
 /**
  * @param ServiceLocatorInterface $serviceLocator
  * @param array $listeners
  * @param ObservableModel $model
  * @throws Exception\ServiceNotCreatedException
  */
 protected function injectListeners(ServiceLocatorInterface $serviceLocator, array $listeners, ObservableModel $model)
 {
     $eventManager = $model->getEventManager();
     if ($serviceLocator->has('Matryoshka\\Model\\Listener\\ListenerManager')) {
         $serviceLocator = $serviceLocator->get('Matryoshka\\Model\\Listener\\ListenerManager');
     }
     foreach ($listeners as $listener) {
         $listenerAggregate = $serviceLocator->get($listener);
         if ($listenerAggregate instanceof ListenerAggregateInterface) {
             $eventManager->attach($listenerAggregate);
         } else {
             throw new Exception\ServiceNotCreatedException(sprintf('Invalid service "%s" specified in "listeners" model configuration; must be an instance of "%s"', $listener, ListenerAggregateInterface::class));
         }
     }
 }
 /**
  * {@inheritdoc}
  * @throws InvalidArgumentException
  * @throws DomainException
  * @throws RuntimeException
  */
 public function delete(DeletableCriteriaInterface $criteria)
 {
     if (!$criteria instanceof ActiveRecordCriteria) {
         throw new InvalidArgumentException(sprintf('Isolated criteria required, "%s" given', get_class($criteria)));
     }
     // Ensure isolation and check allowed states
     $transaction = $this->find($criteria)->current();
     if (!$transaction instanceof TransactionInterface) {
         throw new RuntimeException(sprintf('Transaction "%s" cannot be deleted because it does not exist or is inconsistent', $criteria->getId()));
     }
     switch ($transaction->getState()) {
         case TransactionInterface::STATE_INITIAL:
         case TransactionInterface::STATE_ABORTED:
             // Only not started transaction can be deleted
             break;
         default:
             throw new DomainException(sprintf('Only transactions with "%s" or "%s" states can be deleted: transaction "%s" has "%s" state', TransactionInterface::STATE_INITIAL, TransactionInterface::STATE_ABORTED, $transaction->getId(), $transaction->getState()));
     }
     $criteria->setMongoOptions($this->getMongoOptions());
     return parent::delete($criteria);
 }