Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreditmemoDocumentFactoryTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Sales\Api\Data\CreditmemoCommentInterfaceFactory;
22 
27 class CreditmemoDocumentFactoryTest extends \PHPUnit\Framework\TestCase
28 {
32  private $objectManager;
33 
37  private $factory;
38 
42  private $creditmemoFactoryMock;
43 
47  private $commentFactoryMock;
48 
52  private $hydratorPoolMock;
53 
57  private $hydratorMock;
58 
62  private $orderMock;
63 
67  private $invoiceMock;
68 
72  private $creditmemoItemCreationMock;
73 
77  private $commentCreationMock;
78 
82  private $commentCreationArgumentsMock;
83 
87  private $creditmemoMock;
88 
92  private $commentMock;
93 
97  private $orderRepositoryMock;
98 
99  public function setUp()
100  {
101  $this->objectManager = new ObjectManager($this);
102  $this->creditmemoFactoryMock = $this->getMockBuilder(CreditmemoFactory::class)
103  ->disableOriginalConstructor()
104  ->getMock();
105  $this->commentFactoryMock =
106  $this->getMockBuilder(CreditmemoCommentInterfaceFactory::class)
107  ->disableOriginalConstructor()
108  ->getMock();
109  $this->hydratorPoolMock = $this->getMockBuilder(HydratorPool::class)
110  ->disableOriginalConstructor()
111  ->getMock();
112  $this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class)
113  ->disableOriginalConstructor()
114  ->getMock();
115  $this->orderMock = $this->getMockBuilder(Order::class)
116  ->disableOriginalConstructor()
117  ->getMock();
118  $this->invoiceMock = $this->getMockBuilder(Invoice::class)
119  ->disableOriginalConstructor()
120  ->getMock();
121  $this->creditmemoItemCreationMock = $this->getMockBuilder(CreditmemoItemCreationInterface::class)
122  ->disableOriginalConstructor()
123  ->getMock();
124  $this->creditmemoMock = $this->getMockBuilder(CreditmemoInterface::class)
125  ->disableOriginalConstructor()
126  ->getMock();
127  $this->hydratorMock = $this->getMockBuilder(HydratorInterface::class)
128  ->disableOriginalConstructor()
129  ->getMock();
130  $this->commentCreationArgumentsMock = $this->getMockBuilder(CreditmemoCreationArgumentsInterface::class)
131  ->disableOriginalConstructor()
132  ->getMock();
133  $this->commentCreationMock = $this->getMockBuilder(CreditmemoCommentCreationInterface::class)
134  ->disableOriginalConstructor()
135  ->getMock();
136  $this->creditmemoMock->expects($this->once())
137  ->method('getEntityId')
138  ->willReturn(11);
139 
140  $this->commentMock = $this->getMockBuilder(CreditmemoCommentInterface::class)
141  ->disableOriginalConstructor()
142  ->setMethods(
143  array_merge(
144  get_class_methods(CreditmemoCommentInterface::class),
145  ['setStoreId', 'setCreditmemo']
146  )
147  )
148  ->getMock();
149  $this->factory = $this->objectManager->getObject(
150  CreditmemoDocumentFactory::class,
151  [
152  'creditmemoFactory' => $this->creditmemoFactoryMock,
153  'commentFactory' => $this->commentFactoryMock,
154  'hydratorPool' => $this->hydratorPoolMock,
155  'orderRepository' => $this->orderRepositoryMock
156  ]
157  );
158  }
159 
160  private function commonFactoryFlow()
161  {
162  $this->creditmemoItemCreationMock->expects($this->once())
163  ->method('getOrderItemId')
164  ->willReturn(7);
165  $this->creditmemoItemCreationMock->expects($this->once())
166  ->method('getQty')
167  ->willReturn(3);
168  $this->hydratorPoolMock->expects($this->exactly(2))
169  ->method('getHydrator')
170  ->willReturnMap(
171  [
172  [CreditmemoCreationArgumentsInterface::class, $this->hydratorMock],
173  [CreditmemoCommentCreationInterface::class, $this->hydratorMock],
174  ]
175  );
176  $this->hydratorMock->expects($this->exactly(2))
177  ->method('extract')
178  ->willReturnMap([
179  [$this->commentCreationArgumentsMock, ['shipping_amount' => '20.00']],
180  [$this->commentCreationMock, ['comment' => 'text']]
181  ]);
182  $this->commentFactoryMock->expects($this->once())
183  ->method('create')
184  ->with(
185  [
186  'data' => [
187  'comment' => 'text'
188  ]
189  ]
190  )
191  ->willReturn($this->commentMock);
192  $this->creditmemoMock->expects($this->once())
193  ->method('getEntityId')
194  ->willReturn(11);
195  $this->creditmemoMock->expects($this->once())
196  ->method('getStoreId')
197  ->willReturn(1);
198  $this->commentMock->expects($this->once())
199  ->method('setParentId')
200  ->with(11)
201  ->willReturnSelf();
202  $this->commentMock->expects($this->once())
203  ->method('setStoreId')
204  ->with(1)
205  ->willReturnSelf();
206  $this->commentMock->expects($this->once())
207  ->method('setIsCustomerNotified')
208  ->with(true)
209  ->willReturnSelf();
210  $this->commentMock->expects($this->once())
211  ->method('setCreditmemo')
212  ->with($this->creditmemoMock)
213  ->willReturnSelf();
214  }
215 
216  public function testCreateFromOrder()
217  {
218  $this->commonFactoryFlow();
219  $this->creditmemoFactoryMock->expects($this->once())
220  ->method('createByOrder')
221  ->with(
222  $this->orderMock,
223  [
224  'shipping_amount' => '20.00',
225  'qtys' => [7 => 3]
226  ]
227  )
228  ->willReturn($this->creditmemoMock);
229  $this->factory->createFromOrder(
230  $this->orderMock,
231  [$this->creditmemoItemCreationMock],
232  $this->commentCreationMock,
233  true,
234  $this->commentCreationArgumentsMock
235  );
236  }
237 
238  public function testCreateFromInvoice()
239  {
240  $this->commonFactoryFlow();
241  $this->creditmemoFactoryMock->expects($this->once())
242  ->method('createByInvoice')
243  ->with(
244  $this->invoiceMock,
245  [
246  'shipping_amount' => '20.00',
247  'qtys' => [7 => 3]
248  ]
249  )
250  ->willReturn($this->creditmemoMock);
251  $this->orderRepositoryMock->expects($this->once())
252  ->method('get')
253  ->willReturn($this->orderMock);
254  $this->invoiceMock->expects($this->once())
255  ->method('setOrder')
256  ->with($this->orderMock)
257  ->willReturnSelf();
258  $this->factory->createFromInvoice(
259  $this->invoiceMock,
260  [$this->creditmemoItemCreationMock],
261  $this->commentCreationMock,
262  true,
263  $this->commentCreationArgumentsMock
264  );
265  }
266 }