Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingAssignmentProcessorTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Quote\Model\ShippingAssignmentFactory;
18 use Magento\Quote\Model\Quote\Item as QuoteItem;
20 
21 class ShippingAssignmentProcessorTest extends \PHPUnit\Framework\TestCase
22 {
26  private $shippingAssignmentProcessor;
27 
31  private $objectManagerHelper;
32 
36  private $shippingAssignmentFactoryMock;
37 
41  private $shippingProcessorMock;
42 
46  private $cartItemPersisterMock;
47 
51  private $addressRepositoryMock;
52 
56  private $quoteMock;
57 
61  private $shippingAssignmentMock;
62 
66  private $shippingAddressMock;
67 
71  private $shippingMock;
72 
73  protected function setUp()
74  {
75  $this->shippingAssignmentFactoryMock = $this->getMockBuilder(ShippingAssignmentFactory::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78  $this->shippingProcessorMock = $this->getMockBuilder(ShippingProcessor::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81  $this->cartItemPersisterMock = $this->getMockBuilder(CartItemPersister::class)
82  ->disableOriginalConstructor()
83  ->getMock();
84  $this->addressRepositoryMock = $this->getMockBuilder(AddressRepositoryInterface::class)
85  ->getMockForAbstractClass();
86  $this->quoteMock = $this->getMockBuilder(Quote::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $this->shippingAssignmentMock = $this->getMockBuilder(ShippingAssignmentInterface::class)
90  ->getMockForAbstractClass();
91  $this->shippingAddressMock = $this->getMockBuilder(QuoteAddress::class)
92  ->disableOriginalConstructor()
93  ->getMock();
94  $this->shippingMock = $this->getMockBuilder(ShippingInterface::class)
95  ->getMockForAbstractClass();
96 
97  $this->quoteMock->expects(static::any())
98  ->method('getShippingAddress')
99  ->willReturn($this->shippingAddressMock);
100  $this->shippingAssignmentMock->expects(static::any())
101  ->method('getShipping')
102  ->willReturn($this->shippingMock);
103  $this->shippingMock->expects(static::any())
104  ->method('getAddress')
105  ->willReturn($this->shippingAddressMock);
106 
107  $this->objectManagerHelper = new ObjectManagerHelper($this);
108  $this->shippingAssignmentProcessor = $this->objectManagerHelper->getObject(
109  ShippingAssignmentProcessor::class,
110  [
111  'shippingAssignmentFactory' => $this->shippingAssignmentFactoryMock,
112  'shippingProcessor' => $this->shippingProcessorMock,
113  'cartItemPersister' => $this->cartItemPersisterMock,
114  'addressRepository' => $this->addressRepositoryMock
115  ]
116  );
117  }
118 
120  {
121  $quoteItemId = 1;
122 
123  $this->shippingAssignmentMock->expects(static::once())
124  ->method('getItems')
125  ->willReturn([$this->createQuoteItemMock($quoteItemId, true)]);
126  $this->quoteMock->expects(static::atLeastOnce())
127  ->method('getItemById')
128  ->with($quoteItemId)
129  ->willReturn(null);
130  $this->cartItemPersisterMock->expects(static::never())
131  ->method('save');
132  $this->shippingAddressMock->expects(static::atLeastOnce())
133  ->method('getCustomerAddressId')
134  ->willReturn(null);
135  $this->addressRepositoryMock->expects(static::never())
136  ->method('getById');
137  $this->shippingProcessorMock->expects(static::once())
138  ->method('save')
139  ->with($this->shippingMock, $this->quoteMock);
140 
141  $this->shippingAssignmentProcessor->save($this->quoteMock, $this->shippingAssignmentMock);
142  }
143 
145  {
146  $customerAddressId = 11;
147 
148  $this->shippingAssignmentMock->expects(static::atLeastOnce())
149  ->method('getItems')
150  ->willReturn([]);
151  $this->shippingAddressMock->expects(static::atLeastOnce())
152  ->method('getCustomerAddressId')
153  ->willReturn($customerAddressId);
154  $this->addressRepositoryMock->expects(static::once())
155  ->method('getById')
156  ->with($customerAddressId)
157  ->willThrowException(new NoSuchEntityException());
158  $this->shippingAddressMock->expects(static::once())
159  ->method('setCustomerAddressId')
160  ->with(null)
161  ->willReturn($this->shippingAddressMock);
162  $this->shippingProcessorMock->expects(static::once())
163  ->method('save')
164  ->with($this->shippingMock, $this->quoteMock);
165 
166  $this->shippingAssignmentProcessor->save($this->quoteMock, $this->shippingAssignmentMock);
167  }
168 
176  private function createQuoteItemMock($id, $isDeleted)
177  {
178  $quoteItemMock = $this->getMockBuilder(QuoteItem::class)
179  ->disableOriginalConstructor()
180  ->getMock();
181 
182  $quoteItemMock->expects(static::any())
183  ->method('getItemId')
184  ->willReturn($id);
185  $quoteItemMock->expects(static::any())
186  ->method('isDeleted')
187  ->willReturn($isDeleted);
188 
189  return $quoteItemMock;
190  }
191 }
$id
Definition: fieldset.phtml:14
$quoteItemId
Definition: cart.php:17