示例#1
0
 /**
  * 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);
 }
 /**
  * Run test validate
  *
  * @param $commentDataMap
  * @param $commentData
  * @param $expectedWarnings
  * @dataProvider providerCommentData
  */
 public function testValidate($commentDataMap, $commentData, $expectedWarnings)
 {
     $this->commentModelMock->expects($this->any())->method('hasData')->will($this->returnValueMap($commentDataMap));
     $this->commentModelMock->expects($this->once())->method('getData')->will($this->returnValue($commentData));
     $actualWarnings = $this->validator->validate($this->commentModelMock);
     $this->assertEquals($expectedWarnings, $actualWarnings);
 }
示例#3
0
 /**
  * Performs validation before save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  * @throws \Magento\Framework\Model\Exception
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     parent::_beforeSave($object);
     $errors = $this->validator->validate($object);
     if (!empty($errors)) {
         throw new \Magento\Framework\Model\Exception(__("Cannot save comment") . ":\n" . implode("\n", $errors));
     }
     return $this;
 }
示例#4
0
 /**
  * Performs validation before save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order\Invoice\Comment $object */
     if (!$object->getParentId() && $object->getInvoice()) {
         $object->setParentId($object->getInvoice()->getId());
     }
     parent::_beforeSave($object);
     $errors = $this->validator->validate($object);
     if (!empty($errors)) {
         throw new \Magento\Framework\Exception\LocalizedException(__("Cannot save comment:\n%1", implode("\n", $errors)));
     }
     return $this;
 }