CarrierTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Fedex\Test\Unit\Model;
  7. use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
  8. use Magento\CatalogInventory\Model\StockRegistry;
  9. use Magento\Directory\Helper\Data;
  10. use Magento\Directory\Model\Country;
  11. use Magento\Directory\Model\CountryFactory;
  12. use Magento\Directory\Model\CurrencyFactory;
  13. use Magento\Directory\Model\RegionFactory;
  14. use Magento\Fedex\Model\Carrier;
  15. use Magento\Framework\App\Config\ScopeConfigInterface;
  16. use Magento\Framework\Module\Dir\Reader;
  17. use Magento\Framework\Pricing\PriceCurrencyInterface;
  18. use Magento\Framework\Serialize\Serializer\Json;
  19. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  20. use Magento\Framework\Xml\Security;
  21. use Magento\Quote\Model\Quote\Address\RateRequest;
  22. use Magento\Quote\Model\Quote\Address\RateResult\Error as RateResultError;
  23. use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory as RateErrorFactory;
  24. use Magento\Quote\Model\Quote\Address\RateResult\Method;
  25. use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
  26. use Magento\Shipping\Model\Rate\Result as RateResult;
  27. use Magento\Shipping\Model\Rate\ResultFactory as RateResultFactory;
  28. use Magento\Shipping\Model\Simplexml\ElementFactory;
  29. use Magento\Shipping\Model\Tracking\Result;
  30. use Magento\Shipping\Model\Tracking\Result\Error;
  31. use Magento\Shipping\Model\Tracking\Result\ErrorFactory;
  32. use Magento\Shipping\Model\Tracking\Result\Status;
  33. use Magento\Shipping\Model\Tracking\Result\StatusFactory;
  34. use Magento\Shipping\Model\Tracking\ResultFactory;
  35. use Magento\Store\Model\Store;
  36. use Magento\Store\Model\StoreManagerInterface;
  37. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  38. use Psr\Log\LoggerInterface;
  39. /**
  40. * CarrierTest contains units test for Fedex carrier methods
  41. *
  42. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  43. */
  44. class CarrierTest extends \PHPUnit\Framework\TestCase
  45. {
  46. /**
  47. * @var ObjectManager
  48. */
  49. private $helper;
  50. /**
  51. * @var Carrier|MockObject
  52. */
  53. private $carrier;
  54. /**
  55. * @var ScopeConfigInterface|MockObject
  56. */
  57. private $scope;
  58. /**
  59. * @var Error|MockObject
  60. */
  61. private $error;
  62. /**
  63. * @var ErrorFactory|MockObject
  64. */
  65. private $errorFactory;
  66. /**
  67. * @var ErrorFactory|MockObject
  68. */
  69. private $trackErrorFactory;
  70. /**
  71. * @var StatusFactory|MockObject
  72. */
  73. private $statusFactory;
  74. /**
  75. * @var Result
  76. */
  77. private $result;
  78. /**
  79. * @var \SoapClient|MockObject
  80. */
  81. private $soapClient;
  82. /**
  83. * @var Json|MockObject
  84. */
  85. private $serializer;
  86. /**
  87. * @var LoggerInterface|MockObject
  88. */
  89. private $logger;
  90. protected function setUp()
  91. {
  92. $this->helper = new ObjectManager($this);
  93. $this->scope = $this->getMockBuilder(ScopeConfigInterface::class)
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96. $this->scope->expects($this->any())
  97. ->method('getValue')
  98. ->willReturnCallback([$this, 'scopeConfigGetValue']);
  99. $countryFactory = $this->getCountryFactory();
  100. $rateFactory = $this->getRateFactory();
  101. $storeManager = $this->getStoreManager();
  102. $resultFactory = $this->getResultFactory();
  103. $this->initRateErrorFactory();
  104. $rateMethodFactory = $this->getRateMethodFactory();
  105. $this->trackErrorFactory = $this->getMockBuilder(ErrorFactory::class)
  106. ->disableOriginalConstructor()
  107. ->setMethods(['create'])
  108. ->getMock();
  109. $this->statusFactory = $this->getMockBuilder(StatusFactory::class)
  110. ->disableOriginalConstructor()
  111. ->setMethods(['create'])
  112. ->getMock();
  113. $elementFactory = $this->getMockBuilder(ElementFactory::class)
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $collectionFactory = $this->getMockBuilder(CollectionFactory::class)
  117. ->disableOriginalConstructor()
  118. ->getMock();
  119. $regionFactory = $this->getMockBuilder(RegionFactory::class)
  120. ->disableOriginalConstructor()
  121. ->getMock();
  122. $currencyFactory = $this->getMockBuilder(CurrencyFactory::class)
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $data = $this->getMockBuilder(Data::class)
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. $stockRegistry = $this->getMockBuilder(StockRegistry::class)
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $reader = $this->getMockBuilder(Reader::class)
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $this->serializer = $this->getMockBuilder(Json::class)
  135. ->disableOriginalConstructor()
  136. ->getMock();
  137. $this->logger = $this->getMockForAbstractClass(LoggerInterface::class);
  138. $this->carrier = $this->getMockBuilder(Carrier::class)
  139. ->setMethods(['_createSoapClient'])
  140. ->setConstructorArgs(
  141. [
  142. 'scopeConfig' => $this->scope,
  143. 'rateErrorFactory' => $this->errorFactory,
  144. 'logger' => $this->logger,
  145. 'xmlSecurity' => new Security(),
  146. 'xmlElFactory' => $elementFactory,
  147. 'rateFactory' => $rateFactory,
  148. 'rateMethodFactory' => $rateMethodFactory,
  149. 'trackFactory' => $resultFactory,
  150. 'trackErrorFactory' => $this->trackErrorFactory,
  151. 'trackStatusFactory' => $this->statusFactory,
  152. 'regionFactory' => $regionFactory,
  153. 'countryFactory' => $countryFactory,
  154. 'currencyFactory' => $currencyFactory,
  155. 'directoryData' => $data,
  156. 'stockRegistry' => $stockRegistry,
  157. 'storeManager' => $storeManager,
  158. 'configReader' => $reader,
  159. 'productCollectionFactory' => $collectionFactory,
  160. 'data' => [],
  161. 'serializer' => $this->serializer,
  162. ]
  163. )->getMock();
  164. $this->soapClient = $this->getMockBuilder(\SoapClient::class)
  165. ->disableOriginalConstructor()
  166. ->setMethods(['getRates', 'track'])
  167. ->getMock();
  168. $this->carrier->method('_createSoapClient')
  169. ->willReturn($this->soapClient);
  170. }
  171. public function testSetRequestWithoutCity()
  172. {
  173. $request = $this->getMockBuilder(RateRequest::class)
  174. ->disableOriginalConstructor()
  175. ->setMethods(['getDestCity'])
  176. ->getMock();
  177. $request->expects($this->once())
  178. ->method('getDestCity')
  179. ->willReturn(null);
  180. $this->carrier->setRequest($request);
  181. }
  182. public function testSetRequestWithCity()
  183. {
  184. $request = $this->getMockBuilder(RateRequest::class)
  185. ->disableOriginalConstructor()
  186. ->setMethods(['getDestCity'])
  187. ->getMock();
  188. $request->expects($this->exactly(2))
  189. ->method('getDestCity')
  190. ->willReturn('Small Town');
  191. $this->carrier->setRequest($request);
  192. }
  193. /**
  194. * Callback function, emulates getValue function.
  195. *
  196. * @param string $path
  197. * @return string|null
  198. */
  199. public function scopeConfigGetValue(string $path)
  200. {
  201. $pathMap = [
  202. 'carriers/fedex/showmethod' => 1,
  203. 'carriers/fedex/allowed_methods' => 'ServiceType',
  204. 'carriers/fedex/debug' => 1,
  205. ];
  206. return isset($pathMap[$path]) ? $pathMap[$path] : null;
  207. }
  208. /**
  209. * @param float $amount
  210. * @param string $rateType
  211. * @param float $expected
  212. * @param int $callNum
  213. * @dataProvider collectRatesDataProvider
  214. */
  215. public function testCollectRatesRateAmountOriginBased($amount, $rateType, $expected, $callNum = 1)
  216. {
  217. $this->scope->expects($this->any())
  218. ->method('isSetFlag')
  219. ->willReturn(true);
  220. // @codingStandardsIgnoreStart
  221. $netAmount = new \stdClass();
  222. $netAmount->Amount = $amount;
  223. $totalNetCharge = new \stdClass();
  224. $totalNetCharge->TotalNetCharge = $netAmount;
  225. $totalNetCharge->RateType = $rateType;
  226. $ratedShipmentDetail = new \stdClass();
  227. $ratedShipmentDetail->ShipmentRateDetail = $totalNetCharge;
  228. $rate = new \stdClass();
  229. $rate->ServiceType = 'ServiceType';
  230. $rate->RatedShipmentDetails = [$ratedShipmentDetail];
  231. $response = new \stdClass();
  232. $response->HighestSeverity = 'SUCCESS';
  233. $response->RateReplyDetails = $rate;
  234. // @codingStandardsIgnoreEnd
  235. $this->serializer->method('serialize')
  236. ->willReturn('CollectRateString' . $amount);
  237. $request = $this->getMockBuilder(RateRequest::class)
  238. ->disableOriginalConstructor()
  239. ->getMock();
  240. $this->soapClient->expects($this->exactly($callNum))
  241. ->method('getRates')
  242. ->willReturn($response);
  243. $allRates1 = $this->carrier->collectRates($request)->getAllRates();
  244. foreach ($allRates1 as $rate) {
  245. $this->assertEquals($expected, $rate->getData('cost'));
  246. }
  247. }
  248. /**
  249. * Get list of rates variations
  250. * @return array
  251. */
  252. public function collectRatesDataProvider()
  253. {
  254. return [
  255. [10.0, 'RATED_ACCOUNT_PACKAGE', 10],
  256. [10.0, 'RATED_ACCOUNT_PACKAGE', 10, 0],
  257. [11.50, 'PAYOR_ACCOUNT_PACKAGE', 11.5],
  258. [11.50, 'PAYOR_ACCOUNT_PACKAGE', 11.5, 0],
  259. [100.01, 'RATED_ACCOUNT_SHIPMENT', 100.01],
  260. [100.01, 'RATED_ACCOUNT_SHIPMENT', 100.01, 0],
  261. [32.2, 'PAYOR_ACCOUNT_SHIPMENT', 32.2],
  262. [32.2, 'PAYOR_ACCOUNT_SHIPMENT', 32.2, 0],
  263. [15.0, 'RATED_LIST_PACKAGE', 15],
  264. [15.0, 'RATED_LIST_PACKAGE', 15, 0],
  265. [123.25, 'PAYOR_LIST_PACKAGE', 123.25],
  266. [123.25, 'PAYOR_LIST_PACKAGE', 123.25, 0],
  267. [12.12, 'RATED_LIST_SHIPMENT', 12.12],
  268. [12.12, 'RATED_LIST_SHIPMENT', 12.12, 0],
  269. [38.9, 'PAYOR_LIST_SHIPMENT', 38.9],
  270. [38.9, 'PAYOR_LIST_SHIPMENT', 38.9, 0],
  271. ];
  272. }
  273. public function testCollectRatesErrorMessage()
  274. {
  275. $this->scope->expects($this->once())
  276. ->method('isSetFlag')
  277. ->willReturn(false);
  278. $this->error->expects($this->once())
  279. ->method('setCarrier')
  280. ->with('fedex');
  281. $this->error->expects($this->once())
  282. ->method('setCarrierTitle');
  283. $this->error->expects($this->once())
  284. ->method('setErrorMessage');
  285. $request = new RateRequest();
  286. $request->setPackageWeight(1);
  287. $this->assertSame($this->error, $this->carrier->collectRates($request));
  288. }
  289. /**
  290. * @param string $data
  291. * @param array $maskFields
  292. * @param string $expected
  293. * @dataProvider logDataProvider
  294. */
  295. public function testFilterDebugData($data, array $maskFields, $expected)
  296. {
  297. $refClass = new \ReflectionClass(Carrier::class);
  298. $property = $refClass->getProperty('_debugReplacePrivateDataKeys');
  299. $property->setAccessible(true);
  300. $property->setValue($this->carrier, $maskFields);
  301. $refMethod = $refClass->getMethod('filterDebugData');
  302. $refMethod->setAccessible(true);
  303. $result = $refMethod->invoke($this->carrier, $data);
  304. $this->assertEquals($expected, $result);
  305. }
  306. /**
  307. * Get list of variations
  308. */
  309. public function logDataProvider()
  310. {
  311. return [
  312. [
  313. [
  314. 'WebAuthenticationDetail' => [
  315. 'UserCredential' => [
  316. 'Key' => 'testKey',
  317. 'Password' => 'testPassword',
  318. ],
  319. ],
  320. 'ClientDetail' => [
  321. 'AccountNumber' => 4121213,
  322. 'MeterNumber' => 'testMeterNumber',
  323. ],
  324. ],
  325. ['Key', 'Password', 'MeterNumber'],
  326. [
  327. 'WebAuthenticationDetail' => [
  328. 'UserCredential' => [
  329. 'Key' => '****',
  330. 'Password' => '****',
  331. ],
  332. ],
  333. 'ClientDetail' => [
  334. 'AccountNumber' => 4121213,
  335. 'MeterNumber' => '****',
  336. ],
  337. ],
  338. ],
  339. ];
  340. }
  341. public function testGetTrackingErrorResponse()
  342. {
  343. $tracking = '123456789012';
  344. $errorMessage = 'Tracking information is unavailable.';
  345. // @codingStandardsIgnoreStart
  346. $response = new \stdClass();
  347. $response->HighestSeverity = 'ERROR';
  348. $response->Notifications = new \stdClass();
  349. $response->Notifications->Message = $errorMessage;
  350. // @codingStandardsIgnoreEnd
  351. $error = $this->helper->getObject(Error::class);
  352. $this->trackErrorFactory->expects($this->once())
  353. ->method('create')
  354. ->willReturn($error);
  355. $this->carrier->getTracking($tracking);
  356. $tracks = $this->carrier->getResult()->getAllTrackings();
  357. $this->assertEquals(1, count($tracks));
  358. /** @var Error $current */
  359. $current = $tracks[0];
  360. $this->assertInstanceOf(Error::class, $current);
  361. $this->assertEquals(__($errorMessage), $current->getErrorMessage());
  362. }
  363. /**
  364. * @param string $tracking
  365. * @param string $shipTimeStamp
  366. * @param string $expectedDate
  367. * @param string $expectedTime
  368. * @dataProvider shipDateDataProvider
  369. */
  370. public function testGetTracking($tracking, $shipTimeStamp, $expectedDate, $expectedTime, $callNum = 1)
  371. {
  372. // @codingStandardsIgnoreStart
  373. $response = new \stdClass();
  374. $response->HighestSeverity = 'SUCCESS';
  375. $response->CompletedTrackDetails = new \stdClass();
  376. $trackDetails = new \stdClass();
  377. $trackDetails->ShipTimestamp = $shipTimeStamp;
  378. $trackDetails->DeliverySignatureName = 'signature';
  379. $trackDetails->StatusDetail = new \stdClass();
  380. $trackDetails->StatusDetail->Description = 'SUCCESS';
  381. $trackDetails->Service = new \stdClass();
  382. $trackDetails->Service->Description = 'ground';
  383. $trackDetails->EstimatedDeliveryTimestamp = $shipTimeStamp;
  384. $trackDetails->EstimatedDeliveryAddress = new \stdClass();
  385. $trackDetails->EstimatedDeliveryAddress->City = 'Culver City';
  386. $trackDetails->EstimatedDeliveryAddress->StateOrProvinceCode = 'CA';
  387. $trackDetails->EstimatedDeliveryAddress->CountryCode = 'US';
  388. $trackDetails->PackageWeight = new \stdClass();
  389. $trackDetails->PackageWeight->Value = 23;
  390. $trackDetails->PackageWeight->Units = 'LB';
  391. $response->CompletedTrackDetails->TrackDetails = [$trackDetails];
  392. // @codingStandardsIgnoreEnd
  393. $this->soapClient->expects($this->exactly($callNum))
  394. ->method('track')
  395. ->willReturn($response);
  396. $this->serializer->method('serialize')
  397. ->willReturn('TrackingString' . $tracking);
  398. $status = $this->helper->getObject(Status::class);
  399. $this->statusFactory->method('create')
  400. ->willReturn($status);
  401. $tracks = $this->carrier->getTracking($tracking)->getAllTrackings();
  402. $this->assertEquals(1, count($tracks));
  403. $current = $tracks[0];
  404. $fields = [
  405. 'signedby',
  406. 'status',
  407. 'service',
  408. 'deliverylocation',
  409. 'weight',
  410. ];
  411. array_walk($fields, function ($field) use ($current) {
  412. $this->assertNotEmpty($current[$field]);
  413. });
  414. $this->assertEquals($expectedDate, $current['deliverydate']);
  415. $this->assertEquals($expectedTime, $current['deliverytime']);
  416. $this->assertEquals($expectedDate, $current['shippeddate']);
  417. }
  418. /**
  419. * Gets list of variations for testing ship date.
  420. *
  421. * @return array
  422. */
  423. public function shipDateDataProvider()
  424. {
  425. return [
  426. 'tracking1' => [
  427. 'tracking1',
  428. 'shipTimestamp' => '2016-08-05T14:06:35+01:00',
  429. 'expectedDate' => '2016-08-05',
  430. '13:06:35',
  431. ],
  432. 'tracking1-again' => [
  433. 'tracking1',
  434. 'shipTimestamp' => '2016-08-05T02:06:35+03:00',
  435. 'expectedDate' => '2016-08-05',
  436. '13:06:35',
  437. 0,
  438. ],
  439. 'tracking2' => [
  440. 'tracking2',
  441. 'shipTimestamp' => '2016-08-05T02:06:35+03:00',
  442. 'expectedDate' => '2016-08-04',
  443. '23:06:35',
  444. ],
  445. 'tracking3' => [
  446. 'tracking3',
  447. 'shipTimestamp' => '2016-08-05T14:06:35',
  448. 'expectedDate' => '2016-08-05',
  449. '14:06:35',
  450. ],
  451. 'tracking4' => [
  452. 'tracking4',
  453. 'shipTimestamp' => '2016-08-05 14:06:35',
  454. 'expectedDate' => null,
  455. null,
  456. ],
  457. 'tracking5' => [
  458. 'tracking5',
  459. 'shipTimestamp' => '2016-08-05 14:06:35+00:00',
  460. 'expectedDate' => null,
  461. null,
  462. ],
  463. 'tracking6' => [
  464. 'tracking6',
  465. 'shipTimestamp' => '2016-08-05',
  466. 'expectedDate' => null,
  467. null,
  468. ],
  469. 'tracking7' => [
  470. 'tracking7',
  471. 'shipTimestamp' => '2016/08/05',
  472. 'expectedDate' => null,
  473. null
  474. ],
  475. ];
  476. }
  477. /**
  478. * @param string $tracking
  479. * @param string $shipTimeStamp
  480. * @param string $expectedDate
  481. * @param string $expectedTime
  482. * @param int $callNum
  483. * @dataProvider shipDateDataProvider
  484. */
  485. public function testGetTrackingWithEvents($tracking, $shipTimeStamp, $expectedDate, $expectedTime, $callNum = 1)
  486. {
  487. $tracking = $tracking . 'WithEvent';
  488. // @codingStandardsIgnoreStart
  489. $response = new \stdClass();
  490. $response->HighestSeverity = 'SUCCESS';
  491. $response->CompletedTrackDetails = new \stdClass();
  492. $event = new \stdClass();
  493. $event->EventDescription = 'Test';
  494. $event->Timestamp = $shipTimeStamp;
  495. $event->Address = new \stdClass();
  496. $event->Address->City = 'Culver City';
  497. $event->Address->StateOrProvinceCode = 'CA';
  498. $event->Address->CountryCode = 'US';
  499. $trackDetails = new \stdClass();
  500. $trackDetails->Events = $event;
  501. $response->CompletedTrackDetails->TrackDetails = $trackDetails;
  502. // @codingStandardsIgnoreEnd
  503. $this->soapClient->expects($this->exactly($callNum))
  504. ->method('track')
  505. ->willReturn($response);
  506. $this->serializer->method('serialize')
  507. ->willReturn('TrackingWithEventsString' . $tracking);
  508. $status = $this->helper->getObject(Status::class);
  509. $this->statusFactory->method('create')
  510. ->willReturn($status);
  511. $this->carrier->getTracking($tracking);
  512. $tracks = $this->carrier->getResult()->getAllTrackings();
  513. $this->assertEquals(1, count($tracks));
  514. $current = $tracks[0];
  515. $this->assertNotEmpty($current['progressdetail']);
  516. $this->assertEquals(1, count($current['progressdetail']));
  517. $event = $current['progressdetail'][0];
  518. $fields = ['activity', 'deliverylocation'];
  519. array_walk($fields, function ($field) use ($event) {
  520. $this->assertNotEmpty($event[$field]);
  521. });
  522. $this->assertEquals($expectedDate, $event['deliverydate']);
  523. $this->assertEquals($expectedTime, $event['deliverytime']);
  524. }
  525. /**
  526. * Init RateErrorFactory and RateResultErrors mocks
  527. * @return void
  528. */
  529. private function initRateErrorFactory()
  530. {
  531. $this->error = $this->getMockBuilder(RateResultError::class)
  532. ->disableOriginalConstructor()
  533. ->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])
  534. ->getMock();
  535. $this->errorFactory = $this->getMockBuilder(RateErrorFactory::class)
  536. ->disableOriginalConstructor()
  537. ->setMethods(['create'])
  538. ->getMock();
  539. $this->errorFactory->expects($this->any())
  540. ->method('create')
  541. ->willReturn($this->error);
  542. }
  543. /**
  544. * Creates mock rate result factory
  545. * @return RateResultFactory|MockObject
  546. */
  547. private function getRateFactory()
  548. {
  549. $rate = $this->getMockBuilder(RateResult::class)
  550. ->disableOriginalConstructor()
  551. ->setMethods(['getError'])
  552. ->getMock();
  553. $rateFactory = $this->getMockBuilder(RateResultFactory::class)
  554. ->disableOriginalConstructor()
  555. ->setMethods(['create'])
  556. ->getMock();
  557. $rateFactory->expects($this->any())
  558. ->method('create')
  559. ->willReturn($rate);
  560. return $rateFactory;
  561. }
  562. /**
  563. * Creates mock object for CountryFactory class
  564. * @return CountryFactory|MockObject
  565. */
  566. private function getCountryFactory()
  567. {
  568. $country = $this->getMockBuilder(Country::class)
  569. ->disableOriginalConstructor()
  570. ->setMethods(['load', 'getData'])
  571. ->getMock();
  572. $country->expects($this->any())
  573. ->method('load')
  574. ->willReturnSelf();
  575. $countryFactory = $this->getMockBuilder(CountryFactory::class)
  576. ->disableOriginalConstructor()
  577. ->setMethods(['create'])
  578. ->getMock();
  579. $countryFactory->expects($this->any())
  580. ->method('create')
  581. ->willReturn($country);
  582. return $countryFactory;
  583. }
  584. /**
  585. * Creates mock object for ResultFactory class
  586. * @return ResultFactory|MockObject
  587. */
  588. private function getResultFactory()
  589. {
  590. $resultFactory = $this->getMockBuilder(ResultFactory::class)
  591. ->disableOriginalConstructor()
  592. ->setMethods(['create'])
  593. ->getMock();
  594. $this->result = $this->helper->getObject(Result::class);
  595. $resultFactory->expects($this->any())
  596. ->method('create')
  597. ->willReturn($this->result);
  598. return $resultFactory;
  599. }
  600. /**
  601. * Creates mock object for store manager
  602. * @return StoreManagerInterface|MockObject
  603. */
  604. private function getStoreManager()
  605. {
  606. $store = $this->getMockBuilder(Store::class)
  607. ->disableOriginalConstructor()
  608. ->setMethods(['getBaseCurrencyCode'])
  609. ->getMock();
  610. $storeManager = $this->createMock(StoreManagerInterface::class);
  611. $storeManager->expects($this->any())
  612. ->method('getStore')
  613. ->willReturn($store);
  614. return $storeManager;
  615. }
  616. /**
  617. * Creates mock object for rate method factory
  618. * @return MethodFactory|MockObject
  619. */
  620. private function getRateMethodFactory()
  621. {
  622. $priceCurrency = $this->createMock(PriceCurrencyInterface::class);
  623. $rateMethod = $this->getMockBuilder(Method::class)
  624. ->setConstructorArgs(['priceCurrency' => $priceCurrency])
  625. ->setMethods(null)
  626. ->getMock();
  627. $rateMethodFactory = $this->getMockBuilder(MethodFactory::class)
  628. ->disableOriginalConstructor()
  629. ->setMethods(['create'])
  630. ->getMock();
  631. $rateMethodFactory->expects($this->any())
  632. ->method('create')
  633. ->willReturn($rateMethod);
  634. return $rateMethodFactory;
  635. }
  636. }