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); }
/** * {@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); }