/** * @param mixed $schedule * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function scheduledUpdateCurrencyRates($schedule) { $importWarnings = []; if (!$this->_scopeConfig->getValue(self::IMPORT_ENABLE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE) || !$this->_scopeConfig->getValue(self::CRON_STRING_PATH, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) { return; } $errors = []; $rates = []; $service = $this->_scopeConfig->getValue(self::IMPORT_SERVICE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if ($service) { try { $importModel = $this->_importFactory->create($service); $rates = $importModel->fetchRates(); $errors = $importModel->getMessages(); } catch (\Exception $e) { $importWarnings[] = __('FATAL ERROR:') . ' ' . __('We can\'t initialize the import model.'); } } else { $importWarnings[] = __('FATAL ERROR:') . ' ' . __('Please specify the correct Import Service.'); } if (sizeof($errors) > 0) { foreach ($errors as $error) { $importWarnings[] = __('WARNING:') . ' ' . $error; } } if (sizeof($importWarnings) == 0) { $this->_currencyFactory->create()->saveRates($rates); } else { $this->inlineTranslation->suspend(); $this->_transportBuilder->setTemplateIdentifier($this->_scopeConfig->getValue(self::XML_PATH_ERROR_TEMPLATE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE))->setTemplateOptions(['area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID])->setTemplateVars(['warnings' => join("\n", $importWarnings)])->setFrom($this->_scopeConfig->getValue(self::XML_PATH_ERROR_IDENTITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE))->addTo($this->_scopeConfig->getValue(self::XML_PATH_ERROR_RECIPIENT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)); $transport = $this->_transportBuilder->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); } }
public function testScheduledUpdateCurrencyRates() { $this->scopeConfig->expects($this->at(0))->method('getValue')->with(Observer::IMPORT_ENABLE, ScopeInterface::SCOPE_STORE)->will($this->returnValue(1)); $this->scopeConfig->expects($this->at(1))->method('getValue')->with(Observer::CRON_STRING_PATH, ScopeInterface::SCOPE_STORE)->will($this->returnValue('cron-path')); $this->scopeConfig->expects($this->at(2))->method('getValue')->with(Observer::IMPORT_SERVICE, ScopeInterface::SCOPE_STORE)->will($this->returnValue('import-service')); $importInterfaceMock = $this->getMockBuilder('Magento\\Directory\\Model\\Currency\\Import\\Webservicex')->disableOriginalConstructor()->setMethods(['fetchRates', 'getMessages'])->getMock(); $importInterfaceMock->expects($this->once())->method('fetchRates')->will($this->returnValue([])); $importInterfaceMock->expects($this->once())->method('getMessages')->will($this->returnValue([])); $this->importFactory->expects($this->once())->method('create')->with('import-service')->will($this->returnValue($importInterfaceMock)); $currencyMock = $this->getMockBuilder('Magento\\Directory\\Model\\Currency')->disableOriginalConstructor()->setMethods(['saveRates', '__wakeup', '__sleep'])->getMock(); $currencyMock->expects($this->once())->method('saveRates')->will($this->returnValue(null)); $this->currencyFactory->expects($this->once())->method('create')->will($this->returnValue($currencyMock)); $this->observer->scheduledUpdateCurrencyRates(null); }
/** * @expectedException \UnexpectedValueException * @expectedExceptionMessage Class 'stdClass' has to implement * \Magento\Directory\Model\Currency\Import\ImportInterface */ public function testCreateIrrelevantServiceClass() { $this->_importConfig->expects($this->once())->method('getServiceClass')->with('test')->will($this->returnValue('stdClass')); $this->_objectManager->expects($this->once())->method('create')->with('stdClass')->will($this->returnValue(new \stdClass())); $this->_model->create('test'); }