public function testExecute() { $entityId = 1; $linkId = 2; $oldStore = 1; $newStore = 2; $linkField = 'link_id'; $adapter = $this->getMockBuilder('Magento\\Framework\\DB\\Adapter\\AdapterInterface')->getMockForAbstractClass(); $whereForDelete = [$linkField . ' = ?' => $linkId, 'store_id IN (?)' => [$oldStore]]; $adapter->expects($this->once())->method('delete')->with('cms_block_store', $whereForDelete)->willReturnSelf(); $whereForInsert = [$linkField => $linkId, 'store_id' => $newStore]; $adapter->expects($this->once())->method('insertMultiple')->with('cms_block_store', [$whereForInsert])->willReturnSelf(); $entityMetadata = $this->getMockBuilder('Magento\\Framework\\Model\\Entity\\EntityMetadata')->disableOriginalConstructor()->getMock(); $entityMetadata->expects($this->once())->method('getEntityConnection')->willReturn($adapter); $entityMetadata->expects($this->once())->method('getLinkField')->willReturn($linkField); $this->metadataPool->expects($this->once())->method('getMetadata')->with('Magento\\Cms\\Model\\Block')->willReturn($entityMetadata); $this->resourceBlock->expects($this->once())->method('lookupStoreIds')->willReturn([$oldStore]); $this->resourceBlock->expects($this->once())->method('getTable')->with('cms_block_store')->willReturn('cms_block_store'); $block = $this->getMockBuilder('Magento\\Cms\\Model\\Block')->disableOriginalConstructor()->setMethods(['getStores', 'getId', 'getData'])->getMock(); $block->expects($this->once())->method('getStores')->willReturn($newStore); $block->expects($this->once())->method('getId')->willReturn($entityId); $block->expects($this->exactly(2))->method('getData')->with($linkField)->willReturn($linkId); $result = $this->model->execute('Magento\\Cms\\Model\\Block', $block); $this->assertInstanceOf('Magento\\Cms\\Model\\Block', $result); }
/** * @param string $entityType * @param object $entity * @return object * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entityType, $entity) { if ($entity->getId()) { $stores = $this->resourceBlock->lookupStoreIds((int) $entity->getId()); $entity->setData('store_id', $stores); $entity->setData('stores', $stores); } return $entity; }
public function testExecute() { $entityId = 1; $storeId = 1; $this->resourceBlock->expects($this->once())->method('lookupStoreIds')->willReturn([$storeId]); $block = $this->getMockBuilder('Magento\\Cms\\Model\\Block')->disableOriginalConstructor()->getMock(); $block->expects($this->exactly(2))->method('getId')->willReturn($entityId); $block->expects($this->exactly(2))->method('setData')->willReturnMap([['store_id', [$storeId], $block], ['stores', [$storeId], $block]]); $result = $this->model->execute('', $block); $this->assertInstanceOf('Magento\\Cms\\Model\\Block', $result); }
/** * @test * * @expectedException \Magento\Framework\Exception\NoSuchEntityException */ public function testGetByIdException() { $blockId = '123'; $this->block->expects($this->once())->method('getId')->willReturn(false); $this->blockResource->expects($this->once())->method('load')->with($this->block, $blockId)->willReturn($this->block); $this->repository->getById($blockId); }
/** * Delete Block * * @param \Magento\Cms\Api\Data\BlockInterface $block * @return bool * @throws CouldNotDeleteException */ public function delete(Data\BlockInterface $block) { try { $this->resource->delete($block); } catch (\Exception $exception) { throw new CouldNotDeleteException(__($exception->getMessage())); } return true; }
/** * @param string $entityType * @param object $entity * @param array $arguments * @return object * @throws \Exception */ public function execute($entityType, $entity, $arguments = []) { $entityMetadata = $this->metadataPool->getMetadata($entityType); $linkField = $entityMetadata->getLinkField(); $connection = $entityMetadata->getEntityConnection(); $oldStores = $this->resourceBlock->lookupStoreIds((int) $entity->getId()); $newStores = (array) $entity->getStores(); $table = $this->resourceBlock->getTable('cms_block_store'); $delete = array_diff($oldStores, $newStores); if ($delete) { $where = [$linkField . ' = ?' => (int) $entity->getData($linkField), 'store_id IN (?)' => $delete]; $connection->delete($table, $where); } $insert = array_diff($newStores, $oldStores); if ($insert) { $data = []; foreach ($insert as $storeId) { $data[] = [$linkField => (int) $entity->getData($linkField), 'store_id' => (int) $storeId]; } $connection->insertMultiple($table, $data); } return $entity; }
/** * @test * * @expectedException \Magento\Framework\Exception\CouldNotSaveException */ public function testSaveException() { $this->locationResource->expects($this->once())->method('save')->with($this->location)->willThrowException(new \Exception()); $this->repository->save($this->location); }