CarrierTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Dhl\Test\Unit\Model;
  7. use Magento\Dhl\Model\Carrier;
  8. use Magento\Dhl\Model\Validator\XmlValidator;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\App\ProductMetadataInterface;
  11. use Magento\Framework\Filesystem\Directory\Read;
  12. use Magento\Framework\Filesystem\Directory\ReadFactory;
  13. use Magento\Framework\HTTP\ZendClient;
  14. use Magento\Framework\HTTP\ZendClientFactory;
  15. use Magento\Framework\Locale\ResolverInterface;
  16. use Magento\Framework\Module\Dir\Reader;
  17. use Magento\Framework\Stdlib\DateTime\DateTime;
  18. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  19. use Magento\Framework\Xml\Security;
  20. use Magento\Quote\Model\Quote\Address\RateRequest;
  21. use Magento\Quote\Model\Quote\Address\RateResult\Error;
  22. use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
  23. use Magento\Quote\Model\Quote\Address\RateResult\Method;
  24. use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
  25. use Magento\Sales\Model\Order;
  26. use Magento\Shipping\Helper\Carrier as CarrierHelper;
  27. use Magento\Shipping\Model\Rate\Result;
  28. use Magento\Shipping\Model\Rate\ResultFactory;
  29. use Magento\Shipping\Model\Shipment\Request;
  30. use Magento\Shipping\Model\Simplexml\Element;
  31. use Magento\Shipping\Model\Simplexml\ElementFactory;
  32. use Magento\Store\Model\StoreManager;
  33. use Magento\Store\Model\Website;
  34. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  35. use Psr\Log\LoggerInterface;
  36. use Magento\Store\Model\ScopeInterface;
  37. /**
  38. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  39. */
  40. class CarrierTest extends \PHPUnit\Framework\TestCase
  41. {
  42. /**
  43. * @var ObjectManager
  44. */
  45. private $objectManager;
  46. /**
  47. * @var \Zend_Http_Response|MockObject
  48. */
  49. private $httpResponse;
  50. /**
  51. * @var Carrier
  52. */
  53. private $model;
  54. /**
  55. * @var Error|MockObject
  56. */
  57. private $error;
  58. /**
  59. * @var ErrorFactory|MockObject
  60. */
  61. private $errorFactory;
  62. /**
  63. * @var ScopeConfigInterface|MockObject
  64. */
  65. private $scope;
  66. /**
  67. * @var ZendClient|MockObject
  68. */
  69. private $httpClient;
  70. /**
  71. * @var XmlValidator|MockObject
  72. */
  73. private $xmlValidator;
  74. /**
  75. * @var Request|MockObject
  76. */
  77. private $request;
  78. /**
  79. * @var LoggerInterface|MockObject
  80. */
  81. private $logger;
  82. /**
  83. * @var DateTime|MockObject
  84. */
  85. private $coreDateMock;
  86. /**
  87. * @var ProductMetadataInterface
  88. */
  89. private $productMetadataMock;
  90. /**
  91. * @inheritdoc
  92. */
  93. protected function setUp()
  94. {
  95. $this->objectManager = new ObjectManager($this);
  96. $this->request = $this->getMockBuilder(Request::class)
  97. ->disableOriginalConstructor()
  98. ->setMethods(
  99. [
  100. 'getPackages',
  101. 'getOrigCountryId',
  102. 'setPackages',
  103. 'setPackageWeight',
  104. 'setPackageValue',
  105. 'setValueWithDiscount',
  106. 'setPackageCustomsValue',
  107. 'setFreeMethodWeight',
  108. 'getPackageWeight',
  109. 'getFreeMethodWeight',
  110. 'getOrderShipment',
  111. ]
  112. )
  113. ->getMock();
  114. $this->scope = $this->getMockForAbstractClass(ScopeConfigInterface::class);
  115. $this->error = $this->getMockBuilder(Error::class)
  116. ->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])
  117. ->getMock();
  118. $this->errorFactory = $this->getMockBuilder(ErrorFactory::class)
  119. ->disableOriginalConstructor()
  120. ->setMethods(['create'])
  121. ->getMock();
  122. $this->errorFactory->method('create')
  123. ->willReturn($this->error);
  124. $this->xmlValidator = $this->getMockBuilder(XmlValidator::class)
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $this->logger = $this->getMockForAbstractClass(LoggerInterface::class);
  128. $this->coreDateMock = $this->getMockBuilder(DateTime::class)
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $this->coreDateMock->method('date')
  132. ->willReturn('currentTime');
  133. $this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class)
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $this->productMetadataMock->method('getName')
  137. ->willReturn('Software_Product_Name_30_Char_123456789');
  138. $this->productMetadataMock->method('getVersion')
  139. ->willReturn('10Char_Ver123456789');
  140. $this->model = $this->objectManager->getObject(
  141. Carrier::class,
  142. [
  143. 'scopeConfig' => $this->scope,
  144. 'rateErrorFactory' => $this->errorFactory,
  145. 'logger' => $this->logger,
  146. 'xmlSecurity' => new Security(),
  147. 'xmlElFactory' => $this->getXmlFactory(),
  148. 'rateFactory' => $this->getRateFactory(),
  149. 'rateMethodFactory' => $this->getRateMethodFactory(),
  150. 'carrierHelper' => $this->getCarrierHelper(),
  151. 'configReader' => $this->getConfigReader(),
  152. 'storeManager' => $this->getStoreManager(),
  153. 'readFactory' => $this->getReadFactory(),
  154. 'httpClientFactory' => $this->getHttpClientFactory(),
  155. 'data' => ['id' => 'dhl', 'store' => '1'],
  156. 'xmlValidator' => $this->xmlValidator,
  157. 'coreDate' => $this->coreDateMock,
  158. 'productMetadata' => $this->productMetadataMock
  159. ]
  160. );
  161. }
  162. /**
  163. * Emulates the config's `getValue` method.
  164. *
  165. * @param string $path
  166. * @return string|null
  167. */
  168. public function scopeConfigGetValue($path)
  169. {
  170. $pathMap = [
  171. 'carriers/dhl/shipment_days' => 'Mon,Tue,Wed,Thu,Fri,Sat',
  172. 'carriers/dhl/intl_shipment_days' => 'Mon,Tue,Wed,Thu,Fri,Sat',
  173. 'carriers/dhl/allowed_methods' => 'IE',
  174. 'carriers/dhl/international_searvice' => 'IE',
  175. 'carriers/dhl/gateway_url' => 'https://xmlpi-ea.dhl.com/XMLShippingServlet',
  176. 'carriers/dhl/id' => 'some ID',
  177. 'carriers/dhl/password' => 'some password',
  178. 'carriers/dhl/content_type' => 'N',
  179. 'carriers/dhl/nondoc_methods' => '1,3,4,8,P,Q,E,F,H,J,M,V,Y',
  180. 'carriers/dhl/showmethod' => 1,
  181. 'carriers/dhl/title' => 'DHL Title',
  182. 'carriers/dhl/specificerrmsg' => 'dhl error message',
  183. 'carriers/dhl/unit_of_measure' => 'K',
  184. 'carriers/dhl/size' => '1',
  185. 'carriers/dhl/height' => '1.6',
  186. 'carriers/dhl/width' => '1.6',
  187. 'carriers/dhl/depth' => '1.6',
  188. 'carriers/dhl/debug' => 1,
  189. 'shipping/origin/country_id' => 'GB'
  190. ];
  191. return isset($pathMap[$path]) ? $pathMap[$path] : null;
  192. }
  193. public function testPrepareShippingLabelContent()
  194. {
  195. $xml = simplexml_load_file(
  196. __DIR__ . '/_files/response_shipping_label.xml'
  197. );
  198. $result = $this->_invokePrepareShippingLabelContent($xml);
  199. $this->assertEquals(1111, $result->getTrackingNumber());
  200. $this->assertEquals(base64_decode('OutputImageContent'), $result->getShippingLabelContent());
  201. }
  202. /**
  203. * @dataProvider prepareShippingLabelContentExceptionDataProvider
  204. * @expectedException \Magento\Framework\Exception\LocalizedException
  205. * @expectedExceptionMessage Unable to retrieve shipping label
  206. */
  207. public function testPrepareShippingLabelContentException(\SimpleXMLElement $xml)
  208. {
  209. $this->_invokePrepareShippingLabelContent($xml);
  210. }
  211. /**
  212. * @return array
  213. */
  214. public function prepareShippingLabelContentExceptionDataProvider()
  215. {
  216. $filesPath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR;
  217. $empty = $billingNumberOnly = $outputImageOnly = simplexml_load_file(
  218. $filesPath . 'response_shipping_label.xml'
  219. );
  220. unset(
  221. $empty->{'AirwayBillNumber'},
  222. $empty->{'LabelImage'},
  223. $billingNumberOnly->{'LabelImage'},
  224. $outputImageOnly->{'AirwayBillNumber'}
  225. );
  226. return [[$empty], [$billingNumberOnly], [$outputImageOnly]];
  227. }
  228. /**
  229. * @param \SimpleXMLElement $xml
  230. * @return \Magento\Framework\DataObject
  231. */
  232. protected function _invokePrepareShippingLabelContent(\SimpleXMLElement $xml)
  233. {
  234. $model = $this->objectManager->getObject(Carrier::class);
  235. $method = new \ReflectionMethod($model, '_prepareShippingLabelContent');
  236. $method->setAccessible(true);
  237. return $method->invoke($model, $xml);
  238. }
  239. /**
  240. * Tests that valid rates are returned when sending a quotes request.
  241. */
  242. public function testCollectRates()
  243. {
  244. $requestData = require __DIR__ . '/_files/dhl_quote_request_data.php';
  245. $responseXml = file_get_contents(__DIR__ . '/_files/dhl_quote_response.xml');
  246. $this->scope->method('getValue')
  247. ->willReturnCallback([$this, 'scopeConfigGetValue']);
  248. $this->scope->method('isSetFlag')
  249. ->willReturn(true);
  250. $this->httpResponse->method('getBody')
  251. ->willReturn($responseXml);
  252. $this->coreDateMock->method('date')
  253. ->willReturnCallback(function () {
  254. return date(\DATE_RFC3339);
  255. });
  256. $request = $this->objectManager->getObject(RateRequest::class, $requestData);
  257. $reflectionClass = new \ReflectionObject($this->httpClient);
  258. $rawPostData = $reflectionClass->getProperty('raw_post_data');
  259. $rawPostData->setAccessible(true);
  260. $this->logger->expects($this->once())
  261. ->method('debug')
  262. ->with($this->stringContains('<SiteID>****</SiteID><Password>****</Password>'));
  263. $expectedRates = require __DIR__ . '/_files/dhl_quote_response_rates.php';
  264. $actualRates = $this->model->collectRates($request)->getAllRates();
  265. self::assertEquals(count($expectedRates), count($actualRates));
  266. foreach ($actualRates as $i => $actualRate) {
  267. $actualRate = $actualRate->getData();
  268. unset($actualRate['method_title']);
  269. self::assertEquals($expectedRates[$i], $actualRate);
  270. }
  271. $requestXml = $rawPostData->getValue($this->httpClient);
  272. self::assertContains('<Weight>18.223</Weight>', $requestXml);
  273. self::assertContains('<Height>0.630</Height>', $requestXml);
  274. self::assertContains('<Width>0.630</Width>', $requestXml);
  275. self::assertContains('<Depth>0.630</Depth>', $requestXml);
  276. }
  277. /**
  278. * Tests that an error is returned when attempting to collect rates for an inactive shipping method.
  279. */
  280. public function testCollectRatesErrorMessage()
  281. {
  282. $this->scope->method('getValue')
  283. ->willReturnCallback([$this, 'scopeConfigGetValue']);
  284. $this->scope->expects($this->once())->method('isSetFlag')->willReturn(false);
  285. $this->error->expects($this->once())->method('setCarrier')->with('dhl');
  286. $this->error->expects($this->once())->method('setCarrierTitle');
  287. $this->error->expects($this->once())->method('setErrorMessage');
  288. $request = new RateRequest();
  289. $request->setPackageWeight(1);
  290. $this->assertSame($this->error, $this->model->collectRates($request));
  291. }
  292. /**
  293. * Test request to shipment sends valid xml values.
  294. *
  295. * @param string $origCountryId
  296. * @param string $expectedRegionCode
  297. * @dataProvider requestToShipmentDataProvider
  298. */
  299. public function testRequestToShipment(string $origCountryId, string $expectedRegionCode)
  300. {
  301. $expectedRequestXml = file_get_contents(__DIR__ . '/_files/shipment_request.xml');
  302. $scopeConfigValueMap = [
  303. ['carriers/dhl/account', 'store', null, '1234567890'],
  304. ['carriers/dhl/gateway_url', 'store', null, 'https://xmlpi-ea.dhl.com/XMLShippingServlet'],
  305. ['carriers/dhl/id', 'store', null, 'some ID'],
  306. ['carriers/dhl/password', 'store', null, 'some password'],
  307. ['carriers/dhl/content_type', 'store', null, 'N'],
  308. ['carriers/dhl/nondoc_methods', 'store', null, '1,3,4,8,P,Q,E,F,H,J,M,V,Y'],
  309. ['shipping/origin/country_id', 'store', null, $origCountryId],
  310. ];
  311. $this->scope->method('getValue')
  312. ->willReturnMap($scopeConfigValueMap);
  313. $this->httpResponse->method('getBody')
  314. ->willReturn(utf8_encode(file_get_contents(__DIR__ . '/_files/response_shipping_label.xml')));
  315. $packages = [
  316. 'package' => [
  317. 'params' => [
  318. 'width' => '3',
  319. 'length' => '3',
  320. 'height' => '3',
  321. 'dimension_units' => 'INCH',
  322. 'weight_units' => 'POUND',
  323. 'weight' => '0.454000000001',
  324. 'customs_value' => '10.00',
  325. 'container' => Carrier::DHL_CONTENT_TYPE_NON_DOC,
  326. ],
  327. 'items' => [
  328. 'item1' => [
  329. 'name' => 'item_name',
  330. ],
  331. ],
  332. ],
  333. ];
  334. $order = $this->getMockBuilder(Order::class)
  335. ->disableOriginalConstructor()
  336. ->getMock();
  337. $order->method('getSubtotal')
  338. ->willReturn('10.00');
  339. $shipment = $this->getMockBuilder(Order\Shipment::class)
  340. ->disableOriginalConstructor()
  341. ->getMock();
  342. $shipment->method('getOrder')
  343. ->willReturn($order);
  344. $this->request->method('getPackages')
  345. ->willReturn($packages);
  346. $this->request->method('getOrigCountryId')
  347. ->willReturn($origCountryId);
  348. $this->request->method('setPackages')
  349. ->willReturnSelf();
  350. $this->request->method('setPackageWeight')
  351. ->willReturnSelf();
  352. $this->request->method('setPackageValue')
  353. ->willReturnSelf();
  354. $this->request->method('setValueWithDiscount')
  355. ->willReturnSelf();
  356. $this->request->method('setPackageCustomsValue')
  357. ->willReturnSelf();
  358. $this->request->method('setFreeMethodWeight')
  359. ->willReturnSelf();
  360. $this->request->method('getPackageWeight')
  361. ->willReturn('0.454000000001');
  362. $this->request->method('getFreeMethodWeight')
  363. ->willReturn('0.454000000001');
  364. $this->request->method('getOrderShipment')
  365. ->willReturn($shipment);
  366. $this->logger->method('debug')
  367. ->with($this->stringContains('<SiteID>****</SiteID><Password>****</Password>'));
  368. $result = $this->model->requestToShipment($this->request);
  369. $reflectionClass = new \ReflectionObject($this->httpClient);
  370. $rawPostData = $reflectionClass->getProperty('raw_post_data');
  371. $rawPostData->setAccessible(true);
  372. $this->assertNotNull($result);
  373. $requestXml = $rawPostData->getValue($this->httpClient);
  374. $requestElement = new Element($requestXml);
  375. $this->assertEquals($expectedRegionCode, $requestElement->RegionCode->__toString());
  376. $requestElement->RegionCode = 'Checked';
  377. $messageReference = $requestElement->Request->ServiceHeader->MessageReference->__toString();
  378. $this->assertStringStartsWith('MAGE_SHIP_', $messageReference);
  379. $this->assertGreaterThanOrEqual(28, strlen($messageReference));
  380. $this->assertLessThanOrEqual(32, strlen($messageReference));
  381. $requestElement->Request->ServiceHeader->MessageReference = 'MAGE_SHIP_28TO32_Char_CHECKED';
  382. $expectedRequestElement = new Element($expectedRequestXml);
  383. $this->assertXmlStringEqualsXmlString($expectedRequestElement->asXML(), $requestElement->asXML());
  384. }
  385. /**
  386. * Data provider to testRequestToShipment
  387. *
  388. * @return array
  389. */
  390. public function requestToShipmentDataProvider()
  391. {
  392. return [
  393. [
  394. 'GB', 'EU'
  395. ],
  396. [
  397. 'SG', 'AP'
  398. ]
  399. ];
  400. }
  401. /**
  402. * @dataProvider dhlProductsDataProvider
  403. *
  404. * @param string $docType
  405. * @param array $products
  406. */
  407. public function testGetDhlProducts(string $docType, array $products)
  408. {
  409. $this->assertEquals($products, $this->model->getDhlProducts($docType));
  410. }
  411. /**
  412. * @return array
  413. */
  414. public function dhlProductsDataProvider() : array
  415. {
  416. return [
  417. 'doc' => [
  418. 'docType' => Carrier::DHL_CONTENT_TYPE_DOC,
  419. 'products' => [
  420. '2' => 'Easy shop',
  421. '5' => 'Sprintline',
  422. '6' => 'Secureline',
  423. '7' => 'Express easy',
  424. '9' => 'Europack',
  425. 'B' => 'Break bulk express',
  426. 'C' => 'Medical express',
  427. 'D' => 'Express worldwide',
  428. 'U' => 'Express worldwide',
  429. 'K' => 'Express 9:00',
  430. 'L' => 'Express 10:30',
  431. 'G' => 'Domestic economy select',
  432. 'W' => 'Economy select',
  433. 'I' => 'Domestic express 9:00',
  434. 'N' => 'Domestic express',
  435. 'O' => 'Others',
  436. 'R' => 'Globalmail business',
  437. 'S' => 'Same day',
  438. 'T' => 'Express 12:00',
  439. 'X' => 'Express envelope',
  440. ],
  441. ],
  442. 'non-doc' => [
  443. 'docType' => Carrier::DHL_CONTENT_TYPE_NON_DOC,
  444. 'products' => [
  445. '1' => 'Domestic express 12:00',
  446. '3' => 'Easy shop',
  447. '4' => 'Jetline',
  448. '8' => 'Express easy',
  449. 'P' => 'Express worldwide',
  450. 'Q' => 'Medical express',
  451. 'E' => 'Express 9:00',
  452. 'F' => 'Freight worldwide',
  453. 'H' => 'Economy select',
  454. 'J' => 'Jumbo box',
  455. 'M' => 'Express 10:30',
  456. 'V' => 'Europack',
  457. 'Y' => 'Express 12:00',
  458. ],
  459. ],
  460. ];
  461. }
  462. /**
  463. * Tests that the built MessageReference string is of the appropriate format.
  464. *
  465. * @dataProvider buildMessageReferenceDataProvider
  466. * @param $servicePrefix
  467. * @throws \ReflectionException
  468. */
  469. public function testBuildMessageReference($servicePrefix)
  470. {
  471. $method = new \ReflectionMethod($this->model, 'buildMessageReference');
  472. $method->setAccessible(true);
  473. $messageReference = $method->invoke($this->model, $servicePrefix);
  474. $this->assertGreaterThanOrEqual(28, strlen($messageReference));
  475. $this->assertLessThanOrEqual(32, strlen($messageReference));
  476. }
  477. /**
  478. * @return array
  479. */
  480. public function buildMessageReferenceDataProvider()
  481. {
  482. return [
  483. 'quote_prefix' => ['QUOT'],
  484. 'shipval_prefix' => ['SHIP'],
  485. 'tracking_prefix' => ['TRCK']
  486. ];
  487. }
  488. /**
  489. * Tests that an exception is thrown when an invalid service prefix is provided.
  490. *
  491. * @expectedException \Magento\Framework\Exception\LocalizedException
  492. * @expectedExceptionMessage Invalid service prefix
  493. */
  494. public function testBuildMessageReferenceInvalidPrefix()
  495. {
  496. $method = new \ReflectionMethod($this->model, 'buildMessageReference');
  497. $method->setAccessible(true);
  498. $method->invoke($this->model, 'INVALID');
  499. }
  500. /**
  501. * Tests that the built software name string is of the appropriate format.
  502. *
  503. * @dataProvider buildSoftwareNameDataProvider
  504. * @param $productName
  505. * @throws \ReflectionException
  506. */
  507. public function testBuildSoftwareName($productName)
  508. {
  509. $method = new \ReflectionMethod($this->model, 'buildSoftwareName');
  510. $method->setAccessible(true);
  511. $this->productMetadataMock->method('getName')->willReturn($productName);
  512. $softwareName = $method->invoke($this->model);
  513. $this->assertLessThanOrEqual(30, strlen($softwareName));
  514. }
  515. /**
  516. * @return array
  517. */
  518. public function buildSoftwareNameDataProvider()
  519. {
  520. return [
  521. 'valid_length' => ['Magento'],
  522. 'exceeds_length' => ['Product_Name_Longer_Than_30_Char']
  523. ];
  524. }
  525. /**
  526. * Tests that the built software version string is of the appropriate format.
  527. *
  528. * @dataProvider buildSoftwareVersionProvider
  529. * @param $productVersion
  530. * @throws \ReflectionException
  531. */
  532. public function testBuildSoftwareVersion($productVersion)
  533. {
  534. $method = new \ReflectionMethod($this->model, 'buildSoftwareVersion');
  535. $method->setAccessible(true);
  536. $this->productMetadataMock->method('getVersion')->willReturn($productVersion);
  537. $softwareVersion = $method->invoke($this->model);
  538. $this->assertLessThanOrEqual(10, strlen($softwareVersion));
  539. }
  540. /**
  541. * @return array
  542. */
  543. public function buildSoftwareVersionProvider()
  544. {
  545. return [
  546. 'valid_length' => ['2.3.1'],
  547. 'exceeds_length' => ['dev-MC-1000']
  548. ];
  549. }
  550. /**
  551. * Creates mock for XML factory.
  552. *
  553. * @return ElementFactory|MockObject
  554. */
  555. private function getXmlFactory(): MockObject
  556. {
  557. $xmlElFactory = $this->getMockBuilder(ElementFactory::class)
  558. ->disableOriginalConstructor()
  559. ->setMethods(['create'])
  560. ->getMock();
  561. $xmlElFactory->method('create')
  562. ->willReturnCallback(
  563. function ($data) {
  564. $helper = new ObjectManager($this);
  565. return $helper->getObject(
  566. Element::class,
  567. ['data' => $data['data']]
  568. );
  569. }
  570. );
  571. return $xmlElFactory;
  572. }
  573. /**
  574. * Creates mock for rate factory.
  575. *
  576. * @return ResultFactory|MockObject
  577. */
  578. private function getRateFactory(): MockObject
  579. {
  580. $rateFactory = $this->getMockBuilder(ResultFactory::class)
  581. ->disableOriginalConstructor()
  582. ->setMethods(['create'])
  583. ->getMock();
  584. $rateResult = $this->getMockBuilder(Result::class)
  585. ->disableOriginalConstructor()
  586. ->setMethods(null)
  587. ->getMock();
  588. $rateFactory->method('create')
  589. ->willReturn($rateResult);
  590. return $rateFactory;
  591. }
  592. /**
  593. * Creates mock for rate method factory.
  594. *
  595. * @return MethodFactory|MockObject
  596. */
  597. private function getRateMethodFactory(): MockObject
  598. {
  599. $rateMethodFactory = $this->getMockBuilder(MethodFactory::class)
  600. ->disableOriginalConstructor()
  601. ->setMethods(['create'])
  602. ->getMock();
  603. $rateMethodFactory->method('create')
  604. ->willReturnCallback(function () {
  605. $rateMethod = $this->getMockBuilder(Method::class)
  606. ->disableOriginalConstructor()
  607. ->setMethods(['setPrice'])
  608. ->getMock();
  609. $rateMethod->method('setPrice')
  610. ->willReturnSelf();
  611. return $rateMethod;
  612. });
  613. return $rateMethodFactory;
  614. }
  615. /**
  616. * @return MockObject
  617. */
  618. private function getConfigReader(): MockObject
  619. {
  620. $configReader = $this->getMockBuilder(Reader::class)
  621. ->disableOriginalConstructor()
  622. ->getMock();
  623. $configReader->method('getModuleDir')
  624. ->willReturn('/etc/path');
  625. return $configReader;
  626. }
  627. /**
  628. * @return MockObject
  629. */
  630. private function getReadFactory(): MockObject
  631. {
  632. $modulesDirectory = $this->getMockBuilder(Read::class)
  633. ->disableOriginalConstructor()
  634. ->setMethods(['getRelativePath', 'readFile'])
  635. ->getMock();
  636. $modulesDirectory->method('readFile')
  637. ->willReturn(file_get_contents(__DIR__ . '/_files/countries.xml'));
  638. $readFactory = $this->createMock(ReadFactory::class);
  639. $readFactory->method('create')
  640. ->willReturn($modulesDirectory);
  641. return $readFactory;
  642. }
  643. /**
  644. * @return MockObject
  645. */
  646. private function getStoreManager(): MockObject
  647. {
  648. $storeManager = $this->getMockBuilder(StoreManager::class)
  649. ->disableOriginalConstructor()
  650. ->setMethods(['getWebsite'])
  651. ->getMock();
  652. $website = $this->getMockBuilder(Website::class)
  653. ->disableOriginalConstructor()
  654. ->setMethods(['getBaseCurrencyCode', '__wakeup'])
  655. ->getMock();
  656. $website->method('getBaseCurrencyCode')
  657. ->willReturn('USD');
  658. $storeManager->method('getWebsite')
  659. ->willReturn($website);
  660. return $storeManager;
  661. }
  662. /**
  663. * @return CarrierHelper
  664. */
  665. private function getCarrierHelper(): CarrierHelper
  666. {
  667. $localeResolver = $this->getMockForAbstractClass(ResolverInterface::class);
  668. $localeResolver->method('getLocale')
  669. ->willReturn('fr_FR');
  670. $carrierHelper = $this->objectManager->getObject(
  671. CarrierHelper::class,
  672. [
  673. 'localeResolver' => $localeResolver,
  674. ]
  675. );
  676. return $carrierHelper;
  677. }
  678. /**
  679. * @return MockObject
  680. */
  681. private function getHttpClientFactory(): MockObject
  682. {
  683. $this->httpResponse = $this->getMockBuilder(\Zend_Http_Response::class)
  684. ->disableOriginalConstructor()
  685. ->getMock();
  686. $this->httpClient = $this->getMockBuilder(ZendClient::class)
  687. ->disableOriginalConstructor()
  688. ->setMethods(['request'])
  689. ->getMock();
  690. $this->httpClient->method('request')
  691. ->willReturn($this->httpResponse);
  692. $httpClientFactory = $this->getMockBuilder(ZendClientFactory::class)
  693. ->disableOriginalConstructor()
  694. ->getMock();
  695. $httpClientFactory->method('create')
  696. ->willReturn($this->httpClient);
  697. return $httpClientFactory;
  698. }
  699. }