/** * test _beforeSaveMethod via save() with failed validation * * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage We can't save the address: */ public function testSaveValidationFailed() { $this->entitySnapshotMock->expects($this->once())->method('isModified')->with($this->addressMock)->willReturn(true); $this->addressMock->expects($this->any())->method('hasDataChanges')->will($this->returnValue(true)); $this->validatorMock->expects($this->once())->method('validate')->with($this->equalTo($this->addressMock))->will($this->returnValue(['warning message'])); $this->addressResource->save($this->addressMock); }
/** * Test _beforeSaveMethod via save() with failed validation * * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Cannot save comment: */ public function testSaveValidationFailed() { $this->entitySnapshotMock->expects($this->once())->method('isModified')->with($this->commentModelMock)->willReturn(true); $this->validatorMock->expects($this->once())->method('validate')->with($this->equalTo($this->commentModelMock))->will($this->returnValue(['warning message'])); $this->commentResource->save($this->commentModelMock); $this->assertTrue(true); }
/** * test _beforeSaveMethod via save() * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Cannot save comment: */ public function testValidate() { $historyMock = $this->getMock('Magento\\Sales\\Model\\Order\\Status\\History', [], [], '', false); $this->entitySnapshotMock->expects($this->once())->method('isModified')->with($historyMock)->willReturn(true); $historyMock->expects($this->any())->method('isSaveAllowed')->will($this->returnValue(true)); $this->validatorMock->expects($this->once())->method('validate')->with($historyMock)->will($this->returnValue(['Some warnings'])); $this->assertEquals($this->historyResource, $this->historyResource->save($historyMock)); }
public function testIsModified() { $entityId = 1; $data = ['id' => $entityId, 'name' => 'test', 'description' => '', 'custom_not_present_attribute' => '']; $fields = ['id' => [], 'name' => [], 'description' => []]; $modifiedData = array_merge($data, ['name' => 'newName']); $this->model->expects($this->exactly(4))->method('getData')->willReturnOnConsecutiveCalls($data, $modifiedData, $modifiedData, $modifiedData); $this->model->expects($this->any())->method('getId')->willReturn($entityId); $this->entityMetadata->expects($this->exactly(4))->method('getFields')->with($this->model)->willReturn($fields); $this->entitySnapshot->registerSnapshot($this->model); $this->assertTrue($this->entitySnapshot->isModified($this->model)); $this->entitySnapshot->registerSnapshot($this->model); $this->assertFalse($this->entitySnapshot->isModified($this->model)); }
public function testSave() { $this->orderMock->expects($this->once())->method('validateBeforeSave')->willReturnSelf(); $this->orderMock->expects($this->once())->method('beforeSave')->willReturnSelf(); $this->orderMock->expects($this->once())->method('isSaveAllowed')->willReturn(true); $this->orderMock->expects($this->once())->method('getEntityType')->willReturn('order'); $this->orderMock->expects($this->once())->method('getStore')->willReturn($this->storeMock); $this->storeMock->expects($this->once())->method('getGroup')->willReturn($this->storeGroupMock); $this->storeGroupMock->expects($this->once())->method('getDefaultStoreId')->willReturn(1); $this->salesSequenceManagerMock->expects($this->once())->method('getSequence')->with('order', 1)->willReturn($this->salesSequenceMock); $this->salesSequenceMock->expects($this->once())->method('getNextValue')->willReturn('10000001'); $this->orderMock->expects($this->once())->method('setIncrementId')->with('10000001')->willReturnSelf(); $this->orderMock->expects($this->once())->method('getIncrementId')->willReturn(null); $this->orderMock->expects($this->once())->method('getData')->willReturn(['increment_id' => '10000001']); $this->objectRelationProcessorMock->expects($this->once())->method('validateDataIntegrity')->with(null, ['increment_id' => '10000001']); $this->relationCompositeMock->expects($this->once())->method('processRelations')->with($this->orderMock); $this->resourceMock->expects($this->any())->method('getConnection')->willReturn($this->adapterMock); $this->adapterMock->expects($this->any())->method('quoteInto'); $this->adapterMock->expects($this->any())->method('describeTable')->will($this->returnValue([])); $this->adapterMock->expects($this->any())->method('update'); $this->adapterMock->expects($this->any())->method('lastInsertId'); $this->orderMock->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->entitySnapshotMock->expects($this->once())->method('isModified')->with($this->orderMock)->will($this->returnValue(true)); $this->resource->save($this->orderMock); }
/** * Save entity * * @param \Magento\Framework\Model\AbstractModel $object * @return $this * @throws \Exception */ public function save(\Magento\Framework\Model\AbstractModel $object) { if ($object->isDeleted()) { return $this->delete($object); } if (!$this->entitySnapshot->isModified($object)) { $this->entityRelationComposite->processRelations($object); return $this; } $this->beginTransaction(); try { $object->validateBeforeSave(); $object->beforeSave(); if ($object->isSaveAllowed()) { $this->_serializeFields($object); $this->_beforeSave($object); $this->_checkUnique($object); $this->objectRelationProcessor->validateDataIntegrity($this->getMainTable(), $object->getData()); if ($object->getId() !== null && (!$this->_useIsObjectNew || !$object->isObjectNew())) { $condition = $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . '=?', $object->getId()); $data = $this->_prepareDataForSave($object); unset($data[$this->getIdFieldName()]); $this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition); } else { $bind = $this->_prepareDataForSave($object); unset($bind[$this->getIdFieldName()]); $this->_getWriteAdapter()->insert($this->getMainTable(), $bind); $object->setId($this->_getWriteAdapter()->lastInsertId($this->getMainTable())); if ($this->_useIsObjectNew) { $object->isObjectNew(false); } } $this->unserializeFields($object); $this->_afterSave($object); $this->entitySnapshot->registerSnapshot($object); $object->afterSave(); $this->entityRelationComposite->processRelations($object); } $this->addCommitCallback([$object, 'afterCommitCallback'])->commit(); $object->setHasDataChanges(false); } catch (\Exception $e) { $this->rollBack(); $object->setHasDataChanges(true); throw $e; } return $this; }
/** * Load data with filter in place * All returned rows marked as non changed to prevent unnecessary persistence operations * * @param bool $printQuery * @param bool $logQuery * @return $this */ public function loadWithFilter($printQuery = false, $logQuery = false) { $this->_beforeLoad(); $this->_renderFilters()->_renderOrders()->_renderLimit(); $this->printLogQuery($printQuery, $logQuery); $data = $this->getData(); $this->resetData(); if (is_array($data)) { foreach ($data as $row) { $item = $this->getNewEmptyItem(); if ($this->getIdFieldName()) { $item->setIdFieldName($this->getIdFieldName()); } $item->setData($row); $this->entitySnapshot->registerSnapshot($item); $this->addItem($item); } } $this->_setIsLoaded(); $this->_afterLoad(); return $this; }
public function testSave() { $this->resourceMock->expects($this->any())->method('getConnection')->willReturn($this->adapterMock); $this->adapterMock->expects($this->any())->method('quoteInto'); $this->adapterMock->expects($this->any())->method('describeTable')->will($this->returnValue([])); $this->adapterMock->expects($this->any())->method('update'); $this->adapterMock->expects($this->any())->method('lastInsertId'); $this->orderMock->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->entitySnapshotMock->expects($this->once())->method('isModified')->with($this->orderMock)->will($this->returnValue(true)); $this->resource->save($this->orderMock); }