public function testSaveStopPropagation() { $data = ['foo' => 'bar']; //Test stop propagation (pre-stage) $preEventCalled = false; $postEventCalled = false; $listener = $this->model->getEventManager()->attach('save.pre', function ($e) use(&$preEventCalled) { /** @var $e ModelEvent */ $preEventCalled = true; return 10; }); $mockCriteria = $this->getMock('\\Matryoshka\\Model\\Criteria\\WritableCriteriaInterface', ['applyWrite']); $this->assertEquals(10, $this->model->save($mockCriteria, $data)); $this->assertTrue($preEventCalled); $this->assertFalse($postEventCalled); //Test stop propagation (pre-stage) $preEventCalled = false; $postEventCalled = false; $this->model->getEventManager()->clearListeners('save.pre'); $listener = $this->model->getEventManager()->attach('save.post', function ($e) use(&$postEventCalled) { /** @var $e ModelEvent */ $postEventCalled = true; return 20; }); $mockCriteria = $this->getMock('\\Matryoshka\\Model\\Criteria\\WritableCriteriaInterface', ['applyWrite']); $this->assertEquals(20, $this->model->save($mockCriteria, $data)); $this->assertFalse($preEventCalled); $this->assertTrue($postEventCalled); }
/** * @param WritableCriteriaInterface $criteria * @param TransactionInterface $transaction * @throws InvalidArgumentException * @throws RuntimeException * @return number */ protected function isolatedSave(WritableCriteriaInterface $criteria, TransactionInterface $transaction) { if (!$criteria instanceof ActiveRecordCriteria) { throw new InvalidArgumentException(sprintf('Isolated criteria required, "%s" given', get_class($criteria))); } $criteria->setMongoOptions($this->getMongoOptions()); $result = parent::save($criteria, $transaction); if ($result != 1) { throw new RuntimeException(sprintf('Unexpected write result: expected just one, got "%s"', is_int($result) ? $result : gettype($result))); } return 1; }