10 use Magento\Shipping\Model\ResourceModel\Order\Track\CollectionFactory;
17 class InfoTest extends \PHPUnit\Framework\TestCase
32 private $orderFactory;
37 private $shipmentRepository;
42 private $trackFactory;
47 private $trackCollectionFactory;
54 $this->helper = $this->getMockBuilder(\
Magento\
Shipping\Helper\Data::class)
55 ->disableOriginalConstructor()
57 $this->orderFactory = $this->getMockBuilder(\
Magento\Sales\Model\OrderFactory::class)
58 ->disableOriginalConstructor()
59 ->setMethods([
'create'])
61 $this->shipmentRepository = $this->getMockBuilder(\
Magento\Sales\Api\ShipmentRepositoryInterface::class)
62 ->disableOriginalConstructor()
65 ->disableOriginalConstructor()
66 ->setMethods([
'create'])
68 $this->trackCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
69 ->disableOriginalConstructor()
70 ->setMethods([
'create'])
74 $this->info = $objectManagerHelper->getObject(
77 'shippingData' => $this->helper,
78 'orderFactory' => $this->orderFactory,
79 'shipmentRepository' => $this->shipmentRepository,
80 'trackFactory' => $this->trackFactory,
81 'trackCollectionFactory' => $this->trackCollectionFactory,
88 $hash = strtr(base64_encode(
'order_id:1:protected_code'),
'+/=',
'-_,');
92 'hash' =>
'protected_code',
95 $shipmentIncrementId = 3;
96 $trackDetails =
'track_details';
98 $this->helper->expects($this->atLeastOnce())
99 ->method(
'decodeTrackingHash')
101 ->willReturn($decodedHash);
103 ->disableOriginalConstructor()
104 ->setMethods([
'getIterator'])
107 $order = $this->getMockBuilder(\
Magento\Sales\Model\Order::class)
108 ->disableOriginalConstructor()
109 ->setMethods([
'load',
'getId',
'getProtectCode',
'getShipmentsCollection'])
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);
118 ->disableOriginalConstructor()
119 ->setMethods([
'getIncrementId',
'getId'])
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]));
126 ->disableOriginalConstructor()
127 ->setMethods([
'setShipment',
'getNumberDetail'])
129 $track->expects($this->atLeastOnce())->method(
'setShipment')->with(
$shipment)->willReturnSelf();
130 $track->expects($this->atLeastOnce())->method(
'getNumberDetail')->willReturn($trackDetails);
132 ->disableOriginalConstructor()
133 ->setMethods([
'getIterator',
'setShipmentFilter'])
135 $trackCollection->expects($this->atLeastOnce())
136 ->method(
'setShipmentFilter')
139 $trackCollection->expects($this->atLeastOnce())
140 ->method(
'getIterator')
141 ->willReturn(
new \ArrayIterator([
$track]));
143 $this->trackCollectionFactory->expects($this->atLeastOnce())->method(
'create')->willReturn($trackCollection);
144 $this->info->loadByHash($hash);
146 $this->assertEquals([$shipmentIncrementId => [$trackDetails]], $this->info->getTrackingInfo());
151 $hash = strtr(base64_encode(
'order_id:1:0'),
'+/=',
'-_,');
157 $this->helper->expects($this->atLeastOnce())
158 ->method(
'decodeTrackingHash')
160 ->willReturn($decodedHash);
161 $order = $this->getMockBuilder(\
Magento\Sales\Model\Order::class)
162 ->disableOriginalConstructor()
163 ->setMethods([
'load',
'getId',
'getProtectCode'])
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);
171 $this->assertEmpty($this->info->getTrackingInfo());
176 $hash = strtr(base64_encode(
'ship_id:1:protected_code'),
'+/=',
'-_,');
180 'hash' =>
'protected_code',
182 $shipmentIncrementId = 3;
183 $trackDetails =
'track_details';
185 $this->helper->expects($this->atLeastOnce())
186 ->method(
'decodeTrackingHash')
188 ->willReturn($decodedHash);
190 ->disableOriginalConstructor()
191 ->setMethods([
'getEntityId',
'getProtectCode',
'getIncrementId',
'getId'])
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())
199 ->with($decodedHash[
'id'])
202 ->disableOriginalConstructor()
203 ->setMethods([
'setShipment',
'getNumberDetail'])
205 $track->expects($this->atLeastOnce())->method(
'setShipment')->with(
$shipment)->willReturnSelf();
206 $track->expects($this->atLeastOnce())->method(
'getNumberDetail')->willReturn($trackDetails);
208 ->disableOriginalConstructor()
209 ->setMethods([
'getIterator',
'setShipmentFilter'])
211 $trackCollection->expects($this->atLeastOnce())
212 ->method(
'setShipmentFilter')
213 ->with($decodedHash[
'id'])
215 $trackCollection->expects($this->atLeastOnce())
216 ->method(
'getIterator')
217 ->willReturn(
new \ArrayIterator([
$track]));
218 $this->trackCollectionFactory->expects($this->atLeastOnce())->method(
'create')->willReturn($trackCollection);
220 $this->info->loadByHash($hash);
222 $this->assertEquals([$shipmentIncrementId => [$trackDetails]], $this->info->getTrackingInfo());
227 $hash = strtr(base64_encode(
'ship_id:1:0'),
'+/=',
'-_,');
233 $this->helper->expects($this->atLeastOnce())
234 ->method(
'decodeTrackingHash')
236 ->willReturn($decodedHash);
238 ->disableOriginalConstructor()
239 ->setMethods([
'getEntityId',
'getProtectCode'])
241 $shipment->expects($this->atLeastOnce())->method(
'getEntityId')->willReturn(3);
242 $shipment->expects($this->atLeastOnce())->method(
'getProtectCode')->willReturn(
'0e123123123');
243 $this->shipmentRepository->expects($this->atLeastOnce())
245 ->with($decodedHash[
'id'])
248 $this->info->loadByHash($hash);
250 $this->assertEmpty($this->info->getTrackingInfo());
262 string $protectCodeHash,
264 string $numberDetail,
267 $hash = base64_encode(
'hash');
271 'hash' => $protectCodeHash,
273 $this->helper->expects($this->atLeastOnce())
274 ->method(
'decodeTrackingHash')
276 ->willReturn($decodedHash);
278 ->disableOriginalConstructor()
279 ->setMethods([
'load',
'getId',
'getProtectCode',
'getNumberDetail'])
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);
286 $this->trackFactory->expects($this->atLeastOnce())->method(
'create')->willReturn(
$track);
287 $this->info->loadByHash($hash);
289 $this->assertEquals($trackDetails, $this->info->getTrackingInfo());
299 'hash' =>
'protected_code',
300 'protect_code' =>
'protected_code',
301 'number_detail' =>
'track_details',
302 'track_details' => [[
'track_details']],
306 'protect_code' =>
'0e6640',
307 'number_detail' =>
'',
308 'track_details' => [],
testLoadByHashWithOrderId()
testLoadByHashWithOrderIdWrongCode()
testLoadByHashWithTrackId(string $protectCodeHash, string $protectCode, string $numberDetail, array $trackDetails)
testLoadByHashWithShipmentIdWrongCode()
loadByHashWithTrackIdDataProvider()
testLoadByHashWithShipmentId()
foreach($order->getItems() as $orderItem) $shipment