14 use Magento\Framework\HTTP\ZendClientFactory;
    21 use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
    23 use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
    27 use Magento\Shipping\Model\Rate\ResultFactory;
    30 use Magento\Shipping\Model\Simplexml\ElementFactory;
    33 use PHPUnit_Framework_MockObject_MockObject as MockObject;
    34 use Psr\Log\LoggerInterface;
    45     private $objectManager;
    50     private $httpResponse;
    65     private $errorFactory;
    80     private $xmlValidator;
    99         $this->request = $this->getMockBuilder(Request::class)
   100             ->disableOriginalConstructor()
   108                     'setValueWithDiscount',
   109                     'setPackageCustomsValue',
   110                     'setFreeMethodWeight',
   112                     'getFreeMethodWeight',
   118         $this->scope = $this->getMockForAbstractClass(ScopeConfigInterface::class);
   120         $xmlElFactory = $this->getXmlFactory();
   121         $rateFactory = $this->getRateFactory();
   122         $rateMethodFactory = $this->getRateMethodFactory();
   123         $httpClientFactory = $this->getHttpClientFactory();
   124         $configReader = $this->getConfigReader();
   125         $readFactory = $this->getReadFactory();
   128         $this->error = $this->getMockBuilder(Error::class)
   129             ->setMethods([
'setCarrier', 
'setCarrierTitle', 
'setErrorMessage'])
   131         $this->errorFactory = $this->getMockBuilder(ErrorFactory::class)
   132             ->disableOriginalConstructor()
   133             ->setMethods([
'create'])
   135         $this->errorFactory->method(
'create')
   136             ->willReturn($this->error);
   138         $carrierHelper = $this->getCarrierHelper();
   140         $this->xmlValidator = $this->getMockBuilder(XmlValidator::class)
   141             ->disableOriginalConstructor()
   144         $this->logger = $this->getMockForAbstractClass(LoggerInterface::class);
   146         $this->model = $this->objectManager->getObject(
   149                 'scopeConfig' => $this->scope,
   151                 'logger' => $this->logger,
   152                 'xmlElFactory' => $xmlElFactory,
   153                 'rateFactory' => $rateFactory,
   154                 'rateErrorFactory' => $this->errorFactory,
   155                 'rateMethodFactory' => $rateMethodFactory,
   156                 'httpClientFactory' => $httpClientFactory,
   157                 'readFactory' => $readFactory,
   159                 'configReader' => $configReader,
   160                 'carrierHelper' => $carrierHelper,
   161                 'data' => [
'id' => 
'dhl', 
'store' => 
'1'],
   162                 'xmlValidator' => $this->xmlValidator,
   176             'carriers/dhl/shipment_days' => 
'Mon,Tue,Wed,Thu,Fri,Sat',
   177             'carriers/dhl/intl_shipment_days' => 
'Mon,Tue,Wed,Thu,Fri,Sat',
   178             'carriers/dhl/allowed_methods' => 
'IE',
   179             'carriers/dhl/international_searvice' => 
'IE',
   180             'carriers/dhl/gateway_url' => 
'https://xmlpi-ea.dhl.com/XMLShippingServlet',
   181             'carriers/dhl/id' => 
'some ID',
   182             'carriers/dhl/password' => 
'some password',
   183             'carriers/dhl/content_type' => 
'N',
   184             'carriers/dhl/nondoc_methods' => 
'1,3,4,8,P,Q,E,F,H,J,M,V,Y',
   185             'carriers/dhl/showmethod' => 1,
   186             'carriers/dhl/title' => 
'dhl Title',
   187             'carriers/dhl/specificerrmsg' => 
'dhl error message',
   188             'carriers/dhl/unit_of_measure' => 
'K',
   189             'carriers/dhl/size' => 
'1',
   190             'carriers/dhl/height' => 
'1.6',
   191             'carriers/dhl/width' => 
'1.6',
   192             'carriers/dhl/depth' => 
'1.6',
   193             'carriers/dhl/debug' => 1,
   194             'shipping/origin/country_id' => 
'GB',
   196         return isset($pathMap[
$path]) ? $pathMap[
$path] : 
null;
   201         $xml = simplexml_load_file(
   202             __DIR__ . 
'/_files/response_shipping_label.xml'   205         $this->assertEquals(1111, 
$result->getTrackingNumber());
   206         $this->assertEquals(base64_decode(
'OutputImageContent'), 
$result->getShippingLabelContent());
   224         $filesPath = 
__DIR__ . DIRECTORY_SEPARATOR . 
'_files' . DIRECTORY_SEPARATOR;
   225         $empty = $billingNumberOnly = $outputImageOnly = simplexml_load_file(
   226             $filesPath . 
'response_shipping_label.xml'   229             $empty->{
'AirwayBillNumber'},
   230             $empty->{
'LabelImage'},
   231             $billingNumberOnly->{
'LabelImage'},
   232             $outputImageOnly->{
'AirwayBillNumber'}
   235         return [[$empty], [$billingNumberOnly], [$outputImageOnly]];
   244         $model = $this->objectManager->getObject(Carrier::class);
   245         $method = new \ReflectionMethod(
$model, 
'_prepareShippingLabelContent');
   250     public function testCollectRates()
   252         $this->scope->method(
'getValue')
   253             ->willReturnCallback([$this, 
'scopeConfigGetValue']);
   255         $this->scope->method(
'isSetFlag')
   258         $this->httpResponse->method(
'getBody')
   262         $request = $this->objectManager->getObject(
   264             require 
__DIR__ . 
'/_files/rates_request_data_dhl.php'   269         $rawPostData->setAccessible(
true);
   271         $this->logger->expects($this->once())
   273             ->with($this->stringContains(
'<SiteID>****</SiteID><Password>****</Password>'));
   275         self::assertNotEmpty($this->model->collectRates(
$request)->getAllRates());
   276         self::assertContains(
'<Weight>18.223</Weight>', $rawPostData->getValue($this->httpClient));
   277         self::assertContains(
'<Height>0.630</Height>', $rawPostData->getValue($this->httpClient));
   278         self::assertContains(
'<Width>0.630</Width>', $rawPostData->getValue($this->httpClient));
   279         self::assertContains(
'<Depth>0.630</Depth>', $rawPostData->getValue($this->httpClient));
   284         $this->scope->method(
'getValue')
   285             ->willReturnCallback([$this, 
'scopeConfigGetValue']);
   287         $this->scope->expects($this->once())->method(
'isSetFlag')->willReturn(
false);
   289         $this->error->expects($this->once())->method(
'setCarrier')->with(
'dhl');
   290         $this->error->expects($this->once())->method(
'setCarrierTitle');
   291         $this->error->expects($this->once())->method(
'setErrorMessage');
   296         $this->assertSame($this->error, $this->model->collectRates(
$request));
   301         $this->scope->expects($this->once())->method(
'isSetFlag')->willReturn(
true);
   306         $this->assertFalse(
false, $this->model->collectRates(
$request));
   314         $this->scope->method(
'getValue')
   315             ->willReturnCallback([$this, 
'scopeConfigGetValue']);
   317         $this->httpResponse->method(
'getBody')
   326                     'dimension_units' => 
'INCH',
   327                     'weight_units' => 
'POUND',
   328                     'weight' => 
'0.454000000001',
   329                     'customs_value' => 
'10.00',
   330                     'container' => Carrier::DHL_CONTENT_TYPE_NON_DOC,
   334                         'name' => 
'item_name',
   340         $order = $this->getMockBuilder(Order::class)
   341             ->disableOriginalConstructor()
   343         $order->method(
'getSubtotal')
   344             ->willReturn(
'10.00');
   347             ->disableOriginalConstructor()
   352         $this->request->method(
'getPackages')
   353             ->willReturn($packages);
   354         $this->request->method(
'getOrigCountryId')
   356         $this->request->method(
'setPackages')
   358         $this->request->method(
'setPackageWeight')
   360         $this->request->method(
'setPackageValue')
   362         $this->request->method(
'setValueWithDiscount')
   364         $this->request->method(
'setPackageCustomsValue')
   366         $this->request->method(
'setFreeMethodWeight')
   368         $this->request->method(
'getPackageWeight')
   369             ->willReturn(
'0.454000000001');
   370         $this->request->method(
'getFreeMethodWeight')
   371             ->willReturn(
'0.454000000001');
   372         $this->request->method(
'getOrderShipment')
   375         $this->logger->method(
'debug')
   376             ->with($this->stringContains(
'<SiteID>****</SiteID><Password>****</Password>'));
   378         $result = $this->model->requestToShipment($this->request);
   382         $rawPostData->setAccessible(
true);
   385         $this->assertContains(
'<Weight>0.454</Weight>', $rawPostData->getValue($this->httpClient));
   412         $this->scope->method(
'getValue')
   415                     [
'shipping/origin/country_id', ScopeInterface::SCOPE_STORE, 
null, 
'SG'],
   416                     [
'carriers/dhl/gateway_url', ScopeInterface::SCOPE_STORE, 
null, 
'https://xmlpi-ea.dhl.com'],
   420         $this->httpResponse->method(
'getBody')
   429                     'dimension_units' => 
'INCH',
   430                     'weight_units' => 
'POUND',
   432                     'customs_value' => 
'10.00',
   433                     'container' => Carrier::DHL_CONTENT_TYPE_NON_DOC,
   437                         'name' => 
'item_name',
   443         $this->request->method(
'getPackages')->willReturn($packages);
   444         $this->request->method(
'getOrigCountryId')->willReturn(
'SG');
   445         $this->request->method(
'setPackages')->willReturnSelf();
   446         $this->request->method(
'setPackageWeight')->willReturnSelf();
   447         $this->request->method(
'setPackageValue')->willReturnSelf();
   448         $this->request->method(
'setValueWithDiscount')->willReturnSelf();
   449         $this->request->method(
'setPackageCustomsValue')->willReturnSelf();
   451         $result = $this->model->requestToShipment($this->request);
   455         $rawPostData->setAccessible(
true);
   458         $requestXml = $rawPostData->getValue($this->httpClient);
   460         $this->assertNotContains(
   463             'NewShipper is restricted field for AP region'   465         $this->assertNotContains(
   468             'Division is restricted field for AP region'   470         $this->assertNotContains(
   473             'RegisteredAccount is restricted field for AP region'   485         $this->assertEquals(
$products, $this->model->getDhlProducts($docType));
   495                 'docType' => Carrier::DHL_CONTENT_TYPE_DOC,
   500                     '7' => 
'Express easy',
   502                     'B' => 
'Break bulk express',
   503                     'C' => 
'Medical express',
   504                     'D' => 
'Express worldwide',
   505                     'U' => 
'Express worldwide',
   506                     'K' => 
'Express 9:00',
   507                     'L' => 
'Express 10:30',
   508                     'G' => 
'Domestic economy select',
   509                     'W' => 
'Economy select',
   510                     'I' => 
'Domestic express 9:00',
   511                     'N' => 
'Domestic express',
   513                     'R' => 
'Globalmail business',
   515                     'T' => 
'Express 12:00',
   516                     'X' => 
'Express envelope',
   520                 'docType' => Carrier::DHL_CONTENT_TYPE_NON_DOC,
   522                     '1' => 
'Domestic express 12:00',
   525                     '8' => 
'Express easy',
   526                     'P' => 
'Express worldwide',
   527                     'Q' => 
'Medical express',
   528                     'E' => 
'Express 9:00',
   529                     'F' => 
'Freight worldwide',
   530                     'H' => 
'Economy select',
   532                     'M' => 
'Express 10:30',
   534                     'Y' => 
'Express 12:00',
   545     private function getXmlFactory(): MockObject
   547         $xmlElFactory = $this->getMockBuilder(ElementFactory::class)
   548             ->disableOriginalConstructor()
   549             ->setMethods([
'create'])
   551         $xmlElFactory->method(
'create')
   552             ->willReturnCallback(
   558                         [
'data' => 
$data[
'data']]
   563         return $xmlElFactory;
   571     private function getRateFactory(): MockObject
   573         $rateFactory = $this->getMockBuilder(ResultFactory::class)
   574             ->disableOriginalConstructor()
   575             ->setMethods([
'create'])
   577         $rateResult = $this->getMockBuilder(Result::class)
   578             ->disableOriginalConstructor()
   581         $rateFactory->method(
'create')
   582             ->willReturn($rateResult);
   592     private function getRateMethodFactory(): MockObject
   594         $rateMethodFactory = $this->getMockBuilder(MethodFactory::class)
   595             ->disableOriginalConstructor()
   596             ->setMethods([
'create'])
   598         $rateMethod = $this->getMockBuilder(Method::class)
   599             ->disableOriginalConstructor()
   600             ->setMethods([
'setPrice'])
   602         $rateMethod->method(
'setPrice')
   604         $rateMethodFactory->method(
'create')
   605             ->willReturn($rateMethod);
   607         return $rateMethodFactory;
   613     private function getConfigReader(): MockObject
   615         $configReader = $this->getMockBuilder(Reader::class)
   616             ->disableOriginalConstructor()
   618         $configReader->method(
'getModuleDir')
   619             ->willReturn(
'/etc/path');
   621         return $configReader;
   627     private function getReadFactory(): MockObject
   629         $modulesDirectory = $this->getMockBuilder(Read::class)
   630             ->disableOriginalConstructor()
   631             ->setMethods([
'getRelativePath', 
'readFile'])
   633         $modulesDirectory->method(
'readFile')
   635         $readFactory = $this->createMock(ReadFactory::class);
   636         $readFactory->method(
'create')
   637             ->willReturn($modulesDirectory);
   645     private function getStoreManager(): MockObject
   648             ->disableOriginalConstructor()
   649             ->setMethods([
'getWebsite'])
   651         $website = $this->getMockBuilder(Website::class)
   652             ->disableOriginalConstructor()
   653             ->setMethods([
'getBaseCurrencyCode', 
'__wakeup'])
   655         $website->method(
'getBaseCurrencyCode')
   666     private function getCarrierHelper(): CarrierHelper
   668         $localeResolver = $this->getMockForAbstractClass(ResolverInterface::class);
   669         $localeResolver->method(
'getLocale')
   670             ->willReturn(
'fr_FR');
   671         $carrierHelper = $this->objectManager->getObject(
   672             CarrierHelper::class,
   674                 'localeResolver' => $localeResolver,
   678         return $carrierHelper;
   684     private function getHttpClientFactory(): MockObject
   686         $this->httpResponse = $this->getMockBuilder(\Zend_Http_Response::class)
   687             ->disableOriginalConstructor()
   689         $this->httpClient = $this->getMockBuilder(ZendClient::class)
   690             ->disableOriginalConstructor()
   691             ->setMethods([
'request'])
   693         $this->httpClient->method(
'request')
   694             ->willReturn($this->httpResponse);
   695         $httpClientFactory = $this->getMockBuilder(ZendClientFactory::class)
   696             ->disableOriginalConstructor()
   698         $httpClientFactory->method(
'create')
   699             ->willReturn($this->httpClient);
   701         return $httpClientFactory;
 
testPrepareShippingLabelContentException(\SimpleXMLElement $xml)
 
defined('TESTS_BP')||define('TESTS_BP' __DIR__
 
scopeConfigGetValue($path)
 
testShippingLabelRequestForAsiaPacificRegion()
 
testPrepareShippingLabelContent()
 
testGetDhlProducts(string $docType, array $products)
 
dhlProductsDataProvider()
 
testCollectRatesErrorMessage()
 
prepareShippingLabelContentExceptionDataProvider()
 
foreach($order->getItems() as $orderItem) $shipment
 
requestToShipmentDataProvider()
 
_invokePrepareShippingLabelContent(\SimpleXMLElement $xml)