/** * @covers \Magento\ImportExport\Model\Resource\CollectionByPagesIterator::iterate */ public function testIterate() { $pageSize = 2; $pageCount = 3; /** @var $callbackMock \PHPUnit_Framework_MockObject_MockObject */ $callbackMock = $this->getMock('stdClass', ['callback']); $fetchStrategy = $this->getMockForAbstractClass('Magento\\Framework\\Data\\Collection\\Db\\FetchStrategyInterface'); $select = $this->getMock('Zend_Db_Select', [], [], '', false); $entityFactory = $this->getMock('Magento\\Framework\\Data\\Collection\\EntityFactory', [], [], '', false); $logger = $this->getMock('Psr\\Log\\LoggerInterface'); /** @var $collectionMock \Magento\Framework\Data\Collection\Db|PHPUnit_Framework_MockObject_MockObject */ $collectionMock = $this->getMock('Magento\\Framework\\Data\\Collection\\Db', ['clear', 'setPageSize', 'setCurPage', 'count', 'getLastPageNumber', 'getSelect'], [$entityFactory, $logger, $fetchStrategy]); $collectionMock->expects($this->any())->method('getSelect')->will($this->returnValue($select)); $collectionMock->expects($this->exactly($pageCount + 1))->method('clear')->will($this->returnSelf()); $collectionMock->expects($this->exactly($pageCount))->method('setPageSize')->will($this->returnSelf()); $collectionMock->expects($this->exactly($pageCount))->method('setCurPage')->will($this->returnSelf()); $collectionMock->expects($this->exactly($pageCount))->method('count')->will($this->returnValue($pageSize)); $collectionMock->expects($this->exactly($pageCount))->method('getLastPageNumber')->will($this->returnValue($pageCount)); for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) { for ($rowNumber = 1; $rowNumber <= $pageSize; $rowNumber++) { $itemId = ($pageNumber - 1) * $pageSize + $rowNumber; $item = new \Magento\Framework\Object(['id' => $itemId]); $collectionMock->addItem($item); $callbackMock->expects($this->at($itemId - 1))->method('callback')->with($item); } } $this->_resourceModel->iterate($collectionMock, $pageSize, [[$callbackMock, 'callback']]); }
/** * Load needed data from customer collection * * @return void */ public function load() { if ($this->_isCollectionLoaded == false) { $collection = clone $this->_customerCollection; $collection->removeAttributeToSelect(); $tableName = $collection->getResource()->getEntityTable(); $collection->getSelect()->from($tableName, ['entity_id', 'website_id', 'email']); $this->_byPagesIterator->iterate($this->_customerCollection, $this->_pageSize, [[$this, 'addCustomer']]); $this->_isCollectionLoaded = true; } }
/** * Iterate through given collection page by page and export items * * @param \Magento\Framework\Data\Collection\AbstractDb $collection * @return void */ protected function _exportCollectionByPages(\Magento\Framework\Data\Collection\AbstractDb $collection) { $this->_byPagesIterator->iterate($collection, $this->_pageSize, [[$this, 'exportItem']]); }
/** * Load exiting custom options data * * @return $this */ protected function _initOldCustomOptions() { if (!$this->_oldCustomOptions) { $oldCustomOptions = array(); $optionTitleTable = $this->_tables['catalog_product_option_title']; $productIds = array_values($this->_productsSkuToId); foreach ($this->_storeCodeToId as $storeId) { $addCustomOptions = function (\Magento\Catalog\Model\Product\Option $customOption) use(&$oldCustomOptions, $storeId) { $productId = $customOption->getProductId(); if (!isset($oldCustomOptions[$productId])) { $oldCustomOptions[$productId] = array(); } if (isset($oldCustomOptions[$productId][$customOption->getId()])) { $oldCustomOptions[$productId][$customOption->getId()]['titles'][$storeId] = $customOption->getTitle(); } else { $oldCustomOptions[$productId][$customOption->getId()] = array('titles' => array($storeId => $customOption->getTitle()), 'type' => $customOption->getType()); } }; /** @var $collection \Magento\Catalog\Model\Resource\Product\Option\Collection */ $this->_optionCollection->reset(); $this->_optionCollection->addProductToFilter($productIds); $this->_optionCollection->getSelect()->join(array('option_title' => $optionTitleTable), 'option_title.option_id = main_table.option_id', array('title' => 'title', 'store_id' => 'store_id'))->where('option_title.store_id = ?', $storeId); $this->_byPagesIterator->iterate($this->_optionCollection, $this->_pageSize, array($addCustomOptions)); } $this->_oldCustomOptions = $oldCustomOptions; } return $this; }
/** * Iterate through given collection page by page and export items * * @param \Magento\Framework\Data\Collection\Db $collection * @return void */ protected function _exportCollectionByPages(\Magento\Framework\Data\Collection\Db $collection) { $this->_byPagesIterator->iterate($collection, $this->_pageSize, array(array($this, 'exportItem'))); }