11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2014 Adobe
4+ * All Rights Reserved .
55 */
66declare (strict_types=1 );
77
1818use Magento \Customer \Model \Metadata \FormFactory ;
1919use Magento \Framework \Api \DataObjectHelper ;
2020use Magento \Framework \App \RequestInterface ;
21+ use Magento \Framework \Exception \LocalizedException ;
22+ use Magento \Framework \Message \ManagerInterface ;
23+ use Magento \Framework \ObjectManagerInterface ;
2124use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
2225use Magento \Quote \Api \CartRepositoryInterface ;
2326use Magento \Quote \Model \Quote ;
@@ -44,57 +47,67 @@ class CreateTest extends TestCase
4447 /**
4548 * @var Create
4649 */
47- private $ adminOrderCreate ;
50+ private Create $ adminOrderCreate ;
4851
4952 /**
5053 * @var CartRepositoryInterface|MockObject
5154 */
52- private $ quoteRepository ;
55+ private CartRepositoryInterface $ quoteRepository ;
5356
5457 /**
5558 * @var QuoteFactory|MockObject
5659 */
57- private $ quoteFactory ;
60+ private QuoteFactory $ quoteFactory ;
5861
5962 /**
6063 * @var SessionQuote|MockObject
6164 */
62- private $ sessionQuote ;
65+ private SessionQuote $ sessionQuote ;
6366
6467 /**
6568 * @var FormFactory|MockObject
6669 */
67- private $ formFactory ;
70+ private FormFactory $ formFactory ;
6871
6972 /**
7073 * @var CustomerInterfaceFactory|MockObject
7174 */
72- private $ customerFactory ;
75+ private CustomerInterfaceFactory $ customerFactory ;
7376
7477 /**
7578 * @var Updater|MockObject
7679 */
77- private $ itemUpdater ;
80+ private Updater $ itemUpdater ;
7881
7982 /**
8083 * @var Mapper|MockObject
8184 */
82- private $ customerMapper ;
85+ private Mapper $ customerMapper ;
8386
8487 /**
8588 * @var GroupRepositoryInterface|MockObject
8689 */
87- private $ groupRepository ;
90+ private GroupRepositoryInterface $ groupRepository ;
8891
8992 /**
9093 * @var DataObjectHelper|MockObject
9194 */
92- private $ dataObjectHelper ;
95+ private DataObjectHelper $ dataObjectHelper ;
9396
9497 /**
9598 * @var Order|MockObject
9699 */
97- private $ orderMock ;
100+ private Order $ orderMock ;
101+
102+ /**
103+ * @var ObjectManagerInterface|ObjectManagerInterface&MockObject|MockObject
104+ */
105+ private ObjectManagerInterface $ objectManager ;
106+
107+ /**
108+ * @var ManagerInterface|ManagerInterface&MockObject|MockObject
109+ */
110+ private ManagerInterface $ messageManager ;
98111
99112 /**
100113 * @inheritdoc
@@ -166,10 +179,14 @@ protected function setUp(): void
166179 )
167180 ->getMock ();
168181
182+ $ this ->objectManager = $ this ->createMock (ObjectManagerInterface::class);
183+ $ this ->messageManager = $ this ->createMock (ManagerInterface::class);
169184 $ objectManagerHelper = new ObjectManagerHelper ($ this );
170185 $ this ->adminOrderCreate = $ objectManagerHelper ->getObject (
171186 Create::class,
172187 [
188+ '_objectManager ' => $ this ->objectManager ,
189+ 'messageManager ' => $ this ->messageManager ,
173190 'quoteSession ' => $ this ->sessionQuote ,
174191 'metadataFormFactory ' => $ this ->formFactory ,
175192 'customerFactory ' => $ this ->customerFactory ,
@@ -183,6 +200,46 @@ protected function setUp(): void
183200 );
184201 }
185202
203+ /**
204+ * @return void
205+ * @throws \PHPUnit\Framework\MockObject\Exception|LocalizedException
206+ */
207+ public function testInitFromOrderItemNoExceptionThrownOnAddProduct (): void
208+ {
209+ $ orderItemId = $ productId = 1 ;
210+ $ exceptionMessage = 'Exception message ' ;
211+
212+ $ buyRequest = $ this ->createMock (\Magento \Framework \DataObject::class);
213+
214+ $ orderItem = $ this ->createMock (\Magento \Sales \Model \Order \Item::class);
215+ $ orderItem ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ orderItemId );
216+ $ orderItem ->expects ($ this ->once ())->method ('getProductId ' )->willReturn ($ productId );
217+ $ orderItem ->expects ($ this ->once ())->method ('getBuyRequest ' )->willReturn ($ buyRequest );
218+ $ orderItem ->expects ($ this ->once ())->method ('getProductOptions ' )->willReturn (null );
219+
220+ $ product = $ this ->createMock (\Magento \Catalog \Model \Product::class);
221+ $ product ->expects ($ this ->once ())->method ('setStoreId ' )->willReturnSelf ();
222+ $ product ->expects ($ this ->once ())->method ('load ' )->willReturnSelf ();
223+ $ product ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ productId );
224+ $ this ->objectManager ->expects ($ this ->once ())->method ('create ' )->willReturn ($ product );
225+
226+ $ exception = new LocalizedException (__ ($ exceptionMessage ));
227+ $ quote = $ this ->createMock (Quote::class);
228+ $ quote ->expects ($ this ->once ())
229+ ->method ('addProduct ' )
230+ ->with ($ product , $ buyRequest )
231+ ->willThrowException ($ exception );
232+ $ this ->sessionQuote ->method ('getQuote ' )
233+ ->willReturn ($ quote );
234+
235+ $ this ->messageManager ->expects ($ this ->once ())
236+ ->method ('addErrorMessage ' )
237+ ->with (__ ($ exceptionMessage ))
238+ ->willReturnSelf ();
239+
240+ $ this ->adminOrderCreate ->initFromOrderItem ($ orderItem );
241+ }
242+
186243 public function testSetAccountData ()
187244 {
188245 $ taxClassId = 1 ;
0 commit comments