/** * 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); }
/** * Run test validate * * @param $addressData * @param $email * @param $addressType * @param $expectedWarnings * @dataProvider providerAddressData */ public function testValidate($addressData, $email, $addressType, $expectedWarnings) { $this->addressMock->expects($this->any())->method('hasData')->will($this->returnValueMap($addressData)); $this->addressMock->expects($this->once())->method('getEmail')->will($this->returnValue($email)); $this->addressMock->expects($this->once())->method('getAddressType')->will($this->returnValue($addressType)); $actualWarnings = $this->validator->validate($this->addressMock); $this->assertEquals($expectedWarnings, $actualWarnings); }
/** * 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) { parent::_beforeSave($object); if (!$object->getParentId() && $object->getOrder()) { $object->setParentId($object->getOrder()->getId()); } // Init customer address id if customer address is assigned $customerData = $object->getCustomerAddressData(); if ($customerData) { $object->setCustomerAddressId($customerData->getId()); } $warnings = $this->_validator->validate($object); if (!empty($warnings)) { throw new \Magento\Framework\Exception\LocalizedException(__("Cannot save address:\n%1", implode("\n", $warnings))); } return $this; }
/** * 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); $warnings = $this->_validator->validate($object); if (!empty($warnings)) { throw new \Magento\Framework\Model\Exception(__("Cannot save address") . ":\n" . implode("\n", $warnings)); } return $this; }
/** * Validate order addresses * * @return bool */ protected function validateAddresses() { $order = $this->orderRepository->get($this->checkoutSession->getLastOrderId()); $addresses = $order->getAddresses(); foreach ($addresses as $address) { $result = $this->addressValidator->validateForCustomer($address); if (is_array($result) && !empty($result)) { return false; } } return true; }
/** * test _beforeSaveMethod via save() with failed validation * * @expectedException \Magento\Framework\Model\Exception * @expectedExceptionMessage Cannot save address: */ public function testSaveValidationFailed() { $this->validatorMock->expects($this->once())->method('validate')->with($this->equalTo($this->addressMock))->will($this->returnValue(['warning message'])); $this->addressResource->save($this->addressMock); $this->assertTrue(true); }