/**
  * Tests that an instance removed from the registry will cause the registry to load the model again.
  *
  * @return void
  */
 public function testRemove()
 {
     $groupId = 1;
     $group = $this->getMockBuilder('Magento\\Customer\\Model\\Group')->disableOriginalConstructor()->setMethods(['load', 'getId', '__wakeup'])->getMock();
     $group->expects($this->exactly(2))->method('load')->with($groupId)->will($this->returnValue($group));
     $group->expects($this->exactly(4))->method('getId')->will($this->returnValue($groupId));
     $this->groupFactory->expects($this->exactly(2))->method('create')->will($this->returnValue($group));
     $actual = $this->unit->retrieve($groupId);
     $this->assertSame($group, $actual);
     $this->unit->remove($groupId);
     $actual = $this->unit->retrieve($groupId);
     $this->assertSame($group, $actual);
 }
 /**
  * @magentoDataFixture Magento/Customer/_files/customer_group.php
  * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  */
 public function testRemove()
 {
     $groupId = $this->_findGroupIdWithCode(self::GROUP_CODE);
     $group = $this->_model->retrieve($groupId);
     $this->assertInstanceOf('\\Magento\\Customer\\Model\\Group', $group);
     $group->delete();
     $this->_model->remove($groupId);
     $this->_model->retrieve($groupId);
 }
Beispiel #3
0
 /**
  * Delete customer group by ID.
  *
  * @param int $id
  * @return bool true on success
  * @throws \Magento\Framework\Exception\NoSuchEntityException
  * @throws \Magento\Framework\Exception\StateException If customer group cannot be deleted
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function deleteById($id)
 {
     $groupModel = $this->groupRegistry->retrieve($id);
     if ($id <= 0 || $groupModel->usesAsDefault()) {
         throw new \Magento\Framework\Exception\StateException(__('Cannot delete group.'));
     }
     $groupModel->delete();
     $this->groupRegistry->remove($id);
     return true;
 }
 /**
  * {@inheritdoc}
  */
 public function saveGroup(Data\CustomerGroup $group)
 {
     if (!$group->getCode()) {
         throw InputException::invalidFieldValue('code', $group->getCode());
     }
     $customerGroup = null;
     if ($group->getId()) {
         try {
             $customerGroup = $this->_groupRegistry->retrieve($group->getId());
         } catch (NoSuchEntityException $e) {
             throw NoSuchEntityException::singleField('id', $group->getId());
         }
     }
     if (!$customerGroup) {
         $customerGroup = $this->_groupFactory->create();
     }
     $customerGroup->setCode($group->getCode());
     $taxClassId = $group->getTaxClassId();
     if (!$taxClassId) {
         $taxClassId = self::DEFAULT_TAX_CLASS_ID;
     }
     $this->_verifyTaxClassModel($taxClassId, $group);
     $customerGroup->setTaxClassId($taxClassId);
     try {
         $customerGroup->save();
     } catch (\Magento\Framework\Model\Exception $e) {
         /**
          * Would like a better way to determine this error condition but
          *  difficult to do without imposing more database calls
          */
         if ($e->getMessage() === __('Customer Group already exists.')) {
             throw new InvalidTransitionException('Customer Group already exists.');
         }
         throw $e;
     }
     return $customerGroup->getId();
 }