/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function setUp() { $this->helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->scope = $this->getMockBuilder('\\Magento\\Framework\\App\\Config\\ScopeConfigInterface')->disableOriginalConstructor()->getMock(); $this->scope->expects($this->any())->method('getValue')->will($this->returnCallback([$this, 'scopeConfiggetValue'])); // xml element factory $xmlElFactory = $this->getMockBuilder('\\Magento\\Shipping\\Model\\Simplexml\\ElementFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $xmlElFactory->expects($this->any())->method('create')->will($this->returnCallback(function ($data) { $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); return $helper->getObject('\\Magento\\Shipping\\Model\\Simplexml\\Element', ['data' => $data['data']]); })); // rate factory $rateFactory = $this->getMockBuilder('\\Magento\\Shipping\\Model\\Rate\\ResultFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $rateResult = $this->getMockBuilder('\\Magento\\Shipping\\Model\\Rate\\Result')->disableOriginalConstructor()->setMethods(null)->getMock(); $rateFactory->expects($this->any())->method('create')->will($this->returnValue($rateResult)); // rate method factory $rateMethodFactory = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\MethodFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $rateMethod = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Address\\RateResult\\Method')->disableOriginalConstructor()->setMethods(['setPrice'])->getMock(); $rateMethod->expects($this->any())->method('setPrice')->will($this->returnSelf()); $rateMethodFactory->expects($this->any())->method('create')->will($this->returnValue($rateMethod)); // http client $this->httpResponse = $this->getMockBuilder('\\Zend_Http_Response')->disableOriginalConstructor()->setMethods(['getBody'])->getMock(); $httpClient = $this->getMockBuilder('\\Magento\\Framework\\HTTP\\ZendClient')->disableOriginalConstructor()->setMethods(['request'])->getMock(); $httpClient->expects($this->any())->method('request')->will($this->returnValue($this->httpResponse)); $httpClientFactory = $this->getMockBuilder('\\Magento\\Framework\\HTTP\\ZendClientFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $httpClientFactory->expects($this->any())->method('create')->will($this->returnValue($httpClient)); $data = ['id' => 'usps', 'store' => '1']; $this->error = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\Error')->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])->getMock(); $this->errorFactory = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Address\\RateResult\\ErrorFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->errorFactory->expects($this->any())->method('create')->willReturn($this->error); $arguments = ['scopeConfig' => $this->scope, 'xmlSecurity' => new \Magento\Framework\Xml\Security(), 'xmlElFactory' => $xmlElFactory, 'rateFactory' => $rateFactory, 'rateMethodFactory' => $rateMethodFactory, 'httpClientFactory' => $httpClientFactory, 'data' => $data, 'rateErrorFactory' => $this->errorFactory]; $this->carrier = $this->helper->getObject('Magento\\Usps\\Model\\Carrier', $arguments); }
/** * @param \Magento\Framework\DataObject $request * @return $this|bool|false|\Magento\Framework\Model\AbstractModel * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function checkAvailableShipCountries(\Magento\Framework\DataObject $request) { $speCountriesAllow = $this->getConfigData('sallowspecific'); /* * for specific countries, the flag will be 1 */ if ($speCountriesAllow && $speCountriesAllow == 1) { $showMethod = $this->getConfigData('showmethod'); $availableCountries = []; if ($this->getConfigData('specificcountry')) { $availableCountries = explode(',', $this->getConfigData('specificcountry')); } if ($availableCountries && in_array($request->getDestCountryId(), $availableCountries)) { return $this; } elseif ($showMethod && (!$availableCountries || $availableCountries && !in_array($request->getDestCountryId(), $availableCountries))) { /** @var Error $error */ $error = $this->_rateErrorFactory->create(); $error->setCarrier($this->_code); $error->setCarrierTitle($this->getConfigData('title')); $errorMsg = $this->getConfigData('specificerrmsg'); $error->setErrorMessage($errorMsg ? $errorMsg : __('Sorry, but we can\'t deliver to the destination country with this shipping module.')); return $error; } else { /* * The admin set not to show the shipping module if the delivery country is not within specific countries */ return false; } } return $this; }
protected function setUp() { $this->helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->scope = $this->getMockBuilder('\\Magento\\Framework\\App\\Config\\ScopeConfigInterface')->disableOriginalConstructor()->getMock(); $this->scope->expects($this->any())->method('getValue')->will($this->returnCallback([$this, 'scopeConfiggetValue'])); $this->error = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\Error')->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])->getMock(); $this->errorFactory = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Address\\RateResult\\ErrorFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->errorFactory->expects($this->any())->method('create')->willReturn($this->error); $this->rate = $this->getMock('Magento\\Shipping\\Model\\Rate\\Result', ['getError'], [], '', false); $rateFactory = $this->getMock('Magento\\Shipping\\Model\\Rate\\ResultFactory', ['create'], [], '', false); $rateFactory->expects($this->any())->method('create')->willReturn($this->rate); $this->country = $this->getMockBuilder('\\Magento\\Directory\\Model\\Country')->disableOriginalConstructor()->setMethods(['load'])->getMock(); $this->abstractModel = $this->getMockBuilder('Magento\\Framework\\Model\\AbstractModel')->disableOriginalConstructor()->setMethods(['getData'])->getMock(); $this->country->expects($this->any())->method('load')->willReturn($this->abstractModel); $this->countryFactory = $this->getMockBuilder('\\Magento\\Directory\\Model\\CountryFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->countryFactory->expects($this->any())->method('create')->willReturn($this->country); $this->model = $this->helper->getObject('Magento\\Ups\\Model\\Carrier', ['scopeConfig' => $this->scope, 'rateErrorFactory' => $this->errorFactory, 'countryFactory' => $this->countryFactory, 'rateFactory' => $rateFactory]); }
/** * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function setUp() { $this->_helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->scope = $this->getMockBuilder('\\Magento\\Framework\\App\\Config\\ScopeConfigInterface')->disableOriginalConstructor()->getMock(); $this->scope->expects($this->any())->method('getValue')->will($this->returnCallback([$this, 'scopeConfiggetValue'])); // xml element factory $xmlElFactory = $this->getMockBuilder('\\Magento\\Shipping\\Model\\Simplexml\\ElementFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $xmlElFactory->expects($this->any())->method('create')->will($this->returnCallback(function ($data) { $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); return $helper->getObject('\\Magento\\Shipping\\Model\\Simplexml\\Element', ['data' => $data['data']]); })); // rate factory $rateFactory = $this->getMockBuilder('\\Magento\\Shipping\\Model\\Rate\\ResultFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $rateResult = $this->getMockBuilder('\\Magento\\Shipping\\Model\\Rate\\Result')->disableOriginalConstructor()->setMethods(null)->getMock(); $rateFactory->expects($this->any())->method('create')->will($this->returnValue($rateResult)); // rate method factory $rateMethodFactory = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\MethodFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $rateMethod = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Address\\RateResult\\Method')->disableOriginalConstructor()->setMethods(['setPrice'])->getMock(); $rateMethod->expects($this->any())->method('setPrice')->will($this->returnSelf()); $rateMethodFactory->expects($this->any())->method('create')->will($this->returnValue($rateMethod)); // http client $this->_httpResponse = $this->getMockBuilder('\\Zend_Http_Response')->disableOriginalConstructor()->setMethods(['getBody'])->getMock(); $httpClient = $this->getMockBuilder('\\Magento\\Framework\\HTTP\\ZendClient')->disableOriginalConstructor()->setMethods(['request'])->getMock(); $httpClient->expects($this->any())->method('request')->will($this->returnValue($this->_httpResponse)); $httpClientFactory = $this->getMockBuilder('\\Magento\\Framework\\HTTP\\ZendClientFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $httpClientFactory->expects($this->any())->method('create')->will($this->returnValue($httpClient)); $modulesDirectory = $this->getMockBuilder('\\Magento\\Framework\\Filesystem\\Directory\\Read')->disableOriginalConstructor()->setMethods(['getRelativePath', 'readFile'])->getMock(); $modulesDirectory->expects($this->any())->method('readFile')->will($this->returnValue(file_get_contents(__DIR__ . '/_files/countries.xml'))); $readFactory = $this->getMock('Magento\\Framework\\Filesystem\\Directory\\ReadFactory', [], [], '', false); $readFactory->expects($this->any())->method('create')->willReturn($modulesDirectory); $storeManager = $this->getMockBuilder('\\Magento\\Store\\Model\\StoreManager')->disableOriginalConstructor()->setMethods(['getWebsite'])->getMock(); $website = $this->getMockBuilder('\\Magento\\Store\\Model\\Website')->disableOriginalConstructor()->setMethods(['getBaseCurrencyCode', '__wakeup'])->getMock(); $website->expects($this->any())->method('getBaseCurrencyCode')->will($this->returnValue('USD')); $storeManager->expects($this->any())->method('getWebsite')->will($this->returnValue($website)); $this->error = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\Error')->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])->getMock(); $this->errorFactory = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Address\\RateResult\\ErrorFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->errorFactory->expects($this->any())->method('create')->willReturn($this->error); $this->_model = $this->_helper->getObject('Magento\\Dhl\\Model\\Carrier', ['scopeConfig' => $this->scope, 'xmlSecurity' => new Security(), 'xmlElFactory' => $xmlElFactory, 'rateFactory' => $rateFactory, 'rateErrorFactory' => $this->errorFactory, 'rateMethodFactory' => $rateMethodFactory, 'httpClientFactory' => $httpClientFactory, 'readFactory' => $readFactory, 'storeManager' => $storeManager, 'data' => ['id' => 'dhl', 'store' => '1']]); }
/** * @return void */ public function setUp() { $this->scope = $this->getMockBuilder('\\Magento\\Framework\\App\\Config\\ScopeConfigInterface')->disableOriginalConstructor()->getMock(); $this->scope->expects($this->any())->method('getValue')->will($this->returnCallback([$this, 'scopeConfiggetValue'])); $country = $this->getMock('Magento\\Directory\\Model\\Country', ['load', 'getData', '__wakeup'], [], '', false); $country->expects($this->any())->method('load')->will($this->returnSelf()); $countryFactory = $this->getMock('Magento\\Directory\\Model\\CountryFactory', ['create'], [], '', false); $countryFactory->expects($this->any())->method('create')->will($this->returnValue($country)); $rate = $this->getMock('Magento\\Shipping\\Model\\Rate\\Result', ['getError'], [], '', false); $rateFactory = $this->getMock('Magento\\Shipping\\Model\\Rate\\ResultFactory', ['create'], [], '', false); $rateFactory->expects($this->any())->method('create')->will($this->returnValue($rate)); $this->error = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote\\Address\\RateResult\\Error')->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])->getMock(); $this->errorFactory = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Address\\RateResult\\ErrorFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->errorFactory->expects($this->any())->method('create')->willReturn($this->error); $store = $this->getMock('Magento\\Store\\Model\\Store', ['getBaseCurrencyCode', '__wakeup'], [], '', false); $storeManager = $this->getMockForAbstractClass('Magento\\Store\\Model\\StoreManagerInterface'); $storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store)); $priceCurrency = $this->getMockBuilder('Magento\\Framework\\Pricing\\PriceCurrencyInterface')->getMock(); $rateMethod = $this->getMock('Magento\\Quote\\Model\\Quote\\Address\\RateResult\\Method', null, ['priceCurrency' => $priceCurrency]); $rateMethodFactory = $this->getMock('Magento\\Quote\\Model\\Quote\\Address\\RateResult\\MethodFactory', ['create'], [], '', false); $rateMethodFactory->expects($this->any())->method('create')->will($this->returnValue($rateMethod)); $this->_model = $this->getMock('Magento\\Fedex\\Model\\Carrier', ['_getCachedQuotes', '_debug'], ['scopeConfig' => $this->scope, 'rateErrorFactory' => $this->errorFactory, 'logger' => $this->getMock('Psr\\Log\\LoggerInterface'), 'xmlSecurity' => new Security(), 'xmlElFactory' => $this->getMock('Magento\\Shipping\\Model\\Simplexml\\ElementFactory', [], [], '', false), 'rateFactory' => $rateFactory, 'rateMethodFactory' => $rateMethodFactory, 'trackFactory' => $this->getMock('Magento\\Shipping\\Model\\Tracking\\ResultFactory', [], [], '', false), 'trackErrorFactory' => $this->getMock('Magento\\Shipping\\Model\\Tracking\\Result\\ErrorFactory', [], [], '', false), 'trackStatusFactory' => $this->getMock('Magento\\Shipping\\Model\\Tracking\\Result\\StatusFactory', [], [], '', false), 'regionFactory' => $this->getMock('Magento\\Directory\\Model\\RegionFactory', [], [], '', false), 'countryFactory' => $countryFactory, 'currencyFactory' => $this->getMock('Magento\\Directory\\Model\\CurrencyFactory', [], [], '', false), 'directoryData' => $this->getMock('Magento\\Directory\\Helper\\Data', [], [], '', false), 'stockRegistry' => $this->getMock('Magento\\CatalogInventory\\Model\\StockRegistry', [], [], '', false), 'storeManager' => $storeManager, 'configReader' => $this->getMock('Magento\\Framework\\Module\\Dir\\Reader', [], [], '', false), 'productCollectionFactory' => $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory', [], [], '', false), 'data' => []]); }