/** * Get model mock object * * @param bool $mockBeforeSave * @return \Magento\Sitemap\Model\Sitemap|PHPUnit_Framework_MockObject_MockObject */ protected function _getModelMock($mockBeforeSave = false) { $methods = ['_construct', '_getResource', '_getBaseDir', '_getFileObject', '_afterSave', '_getStoreBaseUrl', '_getCurrentDateTime', '_getCategoryItemsCollection', '_getProductItemsCollection', '_getPageItemsCollection', '_getDocumentRoot']; if ($mockBeforeSave) { $methods[] = 'beforeSave'; } $this->_sitemapCategoryMock->expects($this->any())->method('getCollection')->will($this->returnValue([new \Magento\Framework\DataObject(['url' => 'category.html', 'updated_at' => '2012-12-21 00:00:00']), new \Magento\Framework\DataObject(['url' => '/category/sub-category.html', 'updated_at' => '2012-12-21 00:00:00'])])); $this->_sitemapProductMock->expects($this->any())->method('getCollection')->will($this->returnValue([new \Magento\Framework\DataObject(['url' => 'product.html', 'updated_at' => '2012-12-21 00:00:00']), new \Magento\Framework\DataObject(['url' => 'product2.html', 'updated_at' => '2012-12-21 00:00:00', 'images' => new \Magento\Framework\DataObject(['collection' => [new \Magento\Framework\DataObject(['url' => 'image1.png', 'caption' => 'caption & > title < "']), new \Magento\Framework\DataObject(['url' => 'image_no_caption.png', 'caption' => null])], 'thumbnail' => 'thumbnail.jpg', 'title' => 'Product & > title < "'])])])); $this->_sitemapCmsPageMock->expects($this->any())->method('getCollection')->will($this->returnValue([])); /** @var $model \Magento\Sitemap\Model\Sitemap */ $model = $this->getMockBuilder('Magento\\Sitemap\\Model\\Sitemap')->setMethods($methods)->setConstructorArgs($this->_getModelConstructorArgs())->getMock(); $model->expects($this->any())->method('_getResource')->will($this->returnValue($this->_resourceMock)); $model->expects($this->any())->method('_getStoreBaseUrl')->will($this->returnValue('http://store.com/')); $model->expects($this->any())->method('_getCurrentDateTime')->will($this->returnValue('2012-12-21T00:00:00-08:00')); $model->expects($this->any())->method('_getDocumentRoot')->will($this->returnValue('/project')); $model->setSitemapFilename('sitemap.xml'); $model->setStoreId(1); $model->setSitemapPath('/'); return $model; }
protected function execute(InputInterface $input, OutputInterface $output) { if ($input->getOption('add-all')) { $input->setOption('add-categories', true); $input->setOption('add-products', true); $input->setOption('add-cmspages', true); } $this->detectMagento($output, true); if ($this->initMagento()) { $stores = explode(',', $input->getArgument('stores')); $urls = array(); foreach ($stores as $storeId) { try { $currentStore = $this->storeManager->getStore($storeId); } catch (\Exception $e) { throw new \RuntimeException("Store with id {$storeId} doesn´t exist"); } // base url $urls[] = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); $linkBaseUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK); if ($input->getOption('add-categories')) { $collection = $this->sitemapCategoryCollection->getCollection($storeId); if ($collection) { foreach ($collection as $item) { /* @var $item \Magento\Framework\Object */ $urls[] = $linkBaseUrl . $item->getUrl(); } unset($collection); } } if ($input->getOption('add-products')) { $collection = $this->sitemapProductCollection->getCollection($storeId); if ($collection) { foreach ($collection as $item) { /* @var $item \Magento\Framework\Object */ $urls[] = $linkBaseUrl . $item->getUrl(); } unset($collection); } } if ($input->getOption('add-cmspages')) { $collection = $this->sitemapPageCollection->getCollection($storeId); if ($collection) { foreach ($collection as $item) { /* @var $item \Magento\Framework\Object */ $urls[] = $linkBaseUrl . $item->getUrl(); } unset($collection); } } } // foreach ($stores as $storeId) if (count($urls) > 0) { foreach ($urls as $url) { // pre-process $line = $input->getArgument('linetemplate'); $line = str_replace('{url}', $url, $line); $parts = parse_url($url); foreach ($parts as $key => $value) { $line = str_replace('{' . $key . '}', $value, $line); } // ... and output $output->writeln($line); } } } }