InfoTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Shipping\Model\Info;
  9. use Magento\Shipping\Model\ResourceModel\Order\Track\CollectionFactory;
  10. /**
  11. * Test for \Magento\Shipping\Model\Info.
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class InfoTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var Info
  19. */
  20. private $info;
  21. /**
  22. * @var \Magento\Shipping\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $helper;
  25. /**
  26. * @var \Magento\Sales\Model\OrderFactory|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $orderFactory;
  29. /**
  30. * @var \Magento\Sales\Api\ShipmentRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $shipmentRepository;
  33. /**
  34. * @var \Magento\Shipping\Model\Order\TrackFactory|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $trackFactory;
  37. /**
  38. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $trackCollectionFactory;
  41. /**
  42. * @inheritdoc
  43. */
  44. protected function setUp()
  45. {
  46. $this->helper = $this->getMockBuilder(\Magento\Shipping\Helper\Data::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->orderFactory = $this->getMockBuilder(\Magento\Sales\Model\OrderFactory::class)
  50. ->disableOriginalConstructor()
  51. ->setMethods(['create'])
  52. ->getMock();
  53. $this->shipmentRepository = $this->getMockBuilder(\Magento\Sales\Api\ShipmentRepositoryInterface::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->trackFactory = $this->getMockBuilder(\Magento\Shipping\Model\Order\TrackFactory::class)
  57. ->disableOriginalConstructor()
  58. ->setMethods(['create'])
  59. ->getMock();
  60. $this->trackCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
  61. ->disableOriginalConstructor()
  62. ->setMethods(['create'])
  63. ->getMock();
  64. $objectManagerHelper = new ObjectManager($this);
  65. $this->info = $objectManagerHelper->getObject(
  66. Info::class,
  67. [
  68. 'shippingData' => $this->helper,
  69. 'orderFactory' => $this->orderFactory,
  70. 'shipmentRepository' => $this->shipmentRepository,
  71. 'trackFactory' => $this->trackFactory,
  72. 'trackCollectionFactory' => $this->trackCollectionFactory,
  73. ]
  74. );
  75. }
  76. public function testLoadByHashWithOrderId()
  77. {
  78. $hash = strtr(base64_encode('order_id:1:protected_code'), '+/=', '-_,');
  79. $decodedHash = [
  80. 'key' => 'order_id',
  81. 'id' => 1,
  82. 'hash' => 'protected_code',
  83. ];
  84. $shipmentId = 1;
  85. $shipmentIncrementId = 3;
  86. $trackDetails = 'track_details';
  87. $this->helper->expects($this->atLeastOnce())
  88. ->method('decodeTrackingHash')
  89. ->with($hash)
  90. ->willReturn($decodedHash);
  91. $shipmentCollection = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Shipment\Collection::class)
  92. ->disableOriginalConstructor()
  93. ->setMethods(['getIterator'])
  94. ->getMock();
  95. $order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  96. ->disableOriginalConstructor()
  97. ->setMethods(['load', 'getId', 'getProtectCode', 'getShipmentsCollection'])
  98. ->getMock();
  99. $order->expects($this->atLeastOnce())->method('load')->with($decodedHash['id'])->willReturnSelf();
  100. $order->expects($this->atLeastOnce())->method('getId')->willReturn($decodedHash['id']);
  101. $order->expects($this->atLeastOnce())->method('getProtectCode')->willReturn($decodedHash['hash']);
  102. $order->expects($this->atLeastOnce())->method('getShipmentsCollection')->willReturn($shipmentCollection);
  103. $this->orderFactory->expects($this->atLeastOnce())->method('create')->willReturn($order);
  104. $shipment = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment::class)
  105. ->disableOriginalConstructor()
  106. ->setMethods(['getIncrementId', 'getId'])
  107. ->getMock();
  108. $shipment->expects($this->atLeastOnce())->method('getIncrementId')->willReturn($shipmentIncrementId);
  109. $shipment->expects($this->atLeastOnce())->method('getId')->willReturn($shipmentId);
  110. $shipmentCollection->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([$shipment]));
  111. $track = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment\Track::class)
  112. ->disableOriginalConstructor()
  113. ->setMethods(['setShipment', 'getNumberDetail'])
  114. ->getMock();
  115. $track->expects($this->atLeastOnce())->method('setShipment')->with($shipment)->willReturnSelf();
  116. $track->expects($this->atLeastOnce())->method('getNumberDetail')->willReturn($trackDetails);
  117. $trackCollection = $this->getMockBuilder(\Magento\Shipping\Model\ResourceModel\Order\Track\Collection::class)
  118. ->disableOriginalConstructor()
  119. ->setMethods(['getIterator', 'setShipmentFilter'])
  120. ->getMock();
  121. $trackCollection->expects($this->atLeastOnce())
  122. ->method('setShipmentFilter')
  123. ->with($shipmentId)
  124. ->willReturnSelf();
  125. $trackCollection->expects($this->atLeastOnce())
  126. ->method('getIterator')
  127. ->willReturn(new \ArrayIterator([$track]));
  128. $this->trackCollectionFactory->expects($this->atLeastOnce())->method('create')->willReturn($trackCollection);
  129. $this->info->loadByHash($hash);
  130. $this->assertEquals([$shipmentIncrementId => [$trackDetails]], $this->info->getTrackingInfo());
  131. }
  132. public function testLoadByHashWithOrderIdWrongCode()
  133. {
  134. $hash = strtr(base64_encode('order_id:1:0'), '+/=', '-_,');
  135. $decodedHash = [
  136. 'key' => 'order_id',
  137. 'id' => 1,
  138. 'hash' => '0',
  139. ];
  140. $this->helper->expects($this->atLeastOnce())
  141. ->method('decodeTrackingHash')
  142. ->with($hash)
  143. ->willReturn($decodedHash);
  144. $order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  145. ->disableOriginalConstructor()
  146. ->setMethods(['load', 'getId', 'getProtectCode'])
  147. ->getMock();
  148. $order->expects($this->atLeastOnce())->method('load')->with($decodedHash['id'])->willReturnSelf();
  149. $order->expects($this->atLeastOnce())->method('getId')->willReturn($decodedHash['id']);
  150. $order->expects($this->atLeastOnce())->method('getProtectCode')->willReturn('0e123123123');
  151. $this->orderFactory->expects($this->atLeastOnce())->method('create')->willReturn($order);
  152. $this->info->loadByHash($hash);
  153. $this->assertEmpty($this->info->getTrackingInfo());
  154. }
  155. public function testLoadByHashWithShipmentId()
  156. {
  157. $hash = strtr(base64_encode('ship_id:1:protected_code'), '+/=', '-_,');
  158. $decodedHash = [
  159. 'key' => 'ship_id',
  160. 'id' => 1,
  161. 'hash' => 'protected_code',
  162. ];
  163. $shipmentIncrementId = 3;
  164. $trackDetails = 'track_details';
  165. $this->helper->expects($this->atLeastOnce())
  166. ->method('decodeTrackingHash')
  167. ->with($hash)
  168. ->willReturn($decodedHash);
  169. $shipment = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment::class)
  170. ->disableOriginalConstructor()
  171. ->setMethods(['getEntityId', 'getProtectCode', 'getIncrementId', 'getId'])
  172. ->getMock();
  173. $shipment->expects($this->atLeastOnce())->method('getIncrementId')->willReturn($shipmentIncrementId);
  174. $shipment->expects($this->atLeastOnce())->method('getId')->willReturn($decodedHash['id']);
  175. $shipment->expects($this->atLeastOnce())->method('getEntityId')->willReturn(3);
  176. $shipment->expects($this->atLeastOnce())->method('getProtectCode')->willReturn($decodedHash['hash']);
  177. $this->shipmentRepository->expects($this->atLeastOnce())
  178. ->method('get')
  179. ->with($decodedHash['id'])
  180. ->willReturn($shipment);
  181. $track = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment\Track::class)
  182. ->disableOriginalConstructor()
  183. ->setMethods(['setShipment', 'getNumberDetail'])
  184. ->getMock();
  185. $track->expects($this->atLeastOnce())->method('setShipment')->with($shipment)->willReturnSelf();
  186. $track->expects($this->atLeastOnce())->method('getNumberDetail')->willReturn($trackDetails);
  187. $trackCollection = $this->getMockBuilder(\Magento\Shipping\Model\ResourceModel\Order\Track\Collection::class)
  188. ->disableOriginalConstructor()
  189. ->setMethods(['getIterator', 'setShipmentFilter'])
  190. ->getMock();
  191. $trackCollection->expects($this->atLeastOnce())
  192. ->method('setShipmentFilter')
  193. ->with($decodedHash['id'])
  194. ->willReturnSelf();
  195. $trackCollection->expects($this->atLeastOnce())
  196. ->method('getIterator')
  197. ->willReturn(new \ArrayIterator([$track]));
  198. $this->trackCollectionFactory->expects($this->atLeastOnce())->method('create')->willReturn($trackCollection);
  199. $this->info->loadByHash($hash);
  200. $this->assertEquals([$shipmentIncrementId => [$trackDetails]], $this->info->getTrackingInfo());
  201. }
  202. public function testLoadByHashWithShipmentIdWrongCode()
  203. {
  204. $hash = strtr(base64_encode('ship_id:1:0'), '+/=', '-_,');
  205. $decodedHash = [
  206. 'key' => 'ship_id',
  207. 'id' => 1,
  208. 'hash' => '0',
  209. ];
  210. $this->helper->expects($this->atLeastOnce())
  211. ->method('decodeTrackingHash')
  212. ->with($hash)
  213. ->willReturn($decodedHash);
  214. $shipment = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment::class)
  215. ->disableOriginalConstructor()
  216. ->setMethods(['getEntityId', 'getProtectCode'])
  217. ->getMock();
  218. $shipment->expects($this->atLeastOnce())->method('getEntityId')->willReturn(3);
  219. $shipment->expects($this->atLeastOnce())->method('getProtectCode')->willReturn('0e123123123');
  220. $this->shipmentRepository->expects($this->atLeastOnce())
  221. ->method('get')
  222. ->with($decodedHash['id'])
  223. ->willReturn($shipment);
  224. $this->info->loadByHash($hash);
  225. $this->assertEmpty($this->info->getTrackingInfo());
  226. }
  227. /**
  228. * @dataProvider loadByHashWithTrackIdDataProvider
  229. * @param string $protectCodeHash
  230. * @param string $protectCode
  231. * @param string $numberDetail
  232. * @param array $trackDetails
  233. * @return void
  234. */
  235. public function testLoadByHashWithTrackId(
  236. string $protectCodeHash,
  237. string $protectCode,
  238. string $numberDetail,
  239. array $trackDetails
  240. ) {
  241. $hash = base64_encode('hash');
  242. $decodedHash = [
  243. 'key' => 'track_id',
  244. 'id' => 1,
  245. 'hash' => $protectCodeHash,
  246. ];
  247. $this->helper->expects($this->atLeastOnce())
  248. ->method('decodeTrackingHash')
  249. ->with($hash)
  250. ->willReturn($decodedHash);
  251. $track = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment\Track::class)
  252. ->disableOriginalConstructor()
  253. ->setMethods(['load', 'getId', 'getProtectCode', 'getNumberDetail'])
  254. ->getMock();
  255. $track->expects($this->atLeastOnce())->method('load')->with($decodedHash['id'])->willReturnSelf();
  256. $track->expects($this->atLeastOnce())->method('getId')->willReturn($decodedHash['id']);
  257. $track->expects($this->atLeastOnce())->method('getProtectCode')->willReturn($protectCode);
  258. $track->expects($this->any())->method('getNumberDetail')->willReturn($numberDetail);
  259. $this->trackFactory->expects($this->atLeastOnce())->method('create')->willReturn($track);
  260. $this->info->loadByHash($hash);
  261. $this->assertEquals($trackDetails, $this->info->getTrackingInfo());
  262. }
  263. /**
  264. * @return array
  265. */
  266. public function loadByHashWithTrackIdDataProvider()
  267. {
  268. return [
  269. [
  270. 'hash' => 'protected_code',
  271. 'protect_code' => 'protected_code',
  272. 'number_detail' => 'track_details',
  273. 'track_details' => [['track_details']],
  274. ],
  275. [
  276. 'hash' => '0',
  277. 'protect_code' => '0e6640',
  278. 'number_detail' => '',
  279. 'track_details' => [],
  280. ],
  281. ];
  282. }
  283. }