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