Skip to content

Commit 03748b7

Browse files
authored
ENGCOM-3679: Use repository to load order when manually creating an invoice #19727
2 parents d8a33b5 + 0576154 commit 03748b7

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Order/Invoice/NewAction.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76

87
namespace Magento\Sales\Controller\Adminhtml\Order\Invoice;
98

10-
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
9+
use Magento\Framework\App\Action\HttpGetActionInterface;
1110
use Magento\Backend\App\Action;
1211
use Magento\Framework\Registry;
1312
use Magento\Framework\View\Result\PageFactory;
13+
use Magento\Sales\Api\OrderRepositoryInterface;
1414
use Magento\Sales\Model\Service\InvoiceService;
1515

16+
/**
17+
* Create new invoice action.
18+
*/
1619
class NewAction extends \Magento\Backend\App\Action implements HttpGetActionInterface
1720
{
1821
/**
@@ -37,22 +40,32 @@ class NewAction extends \Magento\Backend\App\Action implements HttpGetActionInte
3740
*/
3841
private $invoiceService;
3942

43+
/**
44+
* @var OrderRepositoryInterface
45+
*/
46+
private $orderRepository;
47+
4048
/**
4149
* @param Action\Context $context
4250
* @param Registry $registry
4351
* @param PageFactory $resultPageFactory
4452
* @param InvoiceService $invoiceService
53+
* @param OrderRepositoryInterface|null $orderRepository
4554
*/
4655
public function __construct(
4756
Action\Context $context,
4857
Registry $registry,
4958
PageFactory $resultPageFactory,
50-
InvoiceService $invoiceService
59+
InvoiceService $invoiceService,
60+
OrderRepositoryInterface $orderRepository = null
5161
) {
62+
parent::__construct($context);
63+
5264
$this->registry = $registry;
5365
$this->resultPageFactory = $resultPageFactory;
54-
parent::__construct($context);
5566
$this->invoiceService = $invoiceService;
67+
$this->orderRepository = $orderRepository ?: \Magento\Framework\App\ObjectManager::getInstance()
68+
->get(OrderRepositoryInterface::class);
5669
}
5770

5871
/**
@@ -78,14 +91,11 @@ public function execute()
7891
{
7992
$orderId = $this->getRequest()->getParam('order_id');
8093
$invoiceData = $this->getRequest()->getParam('invoice', []);
81-
$invoiceItems = isset($invoiceData['items']) ? $invoiceData['items'] : [];
94+
$invoiceItems = $invoiceData['items'] ?? [];
8295

8396
try {
8497
/** @var \Magento\Sales\Model\Order $order */
85-
$order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
86-
if (!$order->getId()) {
87-
throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.'));
88-
}
98+
$order = $this->orderRepository->get($orderId);
8999

90100
if (!$order->canInvoice()) {
91101
throw new \Magento\Framework\Exception\LocalizedException(

app/code/Magento/Sales/Test/Unit/Controller/Adminhtml/Order/Invoice/NewActionTest.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
namespace Magento\Sales\Test\Unit\Controller\Adminhtml\Order\Invoice;
77

88
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
use Magento\Sales\Api\OrderRepositoryInterface;
910

1011
/**
1112
* Class NewActionTest
1213
* @package Magento\Sales\Controller\Adminhtml\Order\Invoice
1314
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1415
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
16+
* @SuppressWarnings(PHPMD.TooManyFields)
1517
*/
1618
class NewActionTest extends \PHPUnit\Framework\TestCase
1719
{
@@ -90,6 +92,11 @@ class NewActionTest extends \PHPUnit\Framework\TestCase
9092
*/
9193
protected $invoiceServiceMock;
9294

95+
/**
96+
* @var OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
97+
*/
98+
private $orderRepositoryMock;
99+
93100
protected function setUp()
94101
{
95102
$objectManager = new ObjectManager($this);
@@ -215,12 +222,15 @@ protected function setUp()
215222
->disableOriginalConstructor()
216223
->getMock();
217224

225+
$this->orderRepositoryMock = $this->createMock(OrderRepositoryInterface::class);
226+
218227
$this->controller = $objectManager->getObject(
219228
\Magento\Sales\Controller\Adminhtml\Order\Invoice\NewAction::class,
220229
[
221230
'context' => $contextMock,
222231
'resultPageFactory' => $this->resultPageFactoryMock,
223-
'invoiceService' => $this->invoiceServiceMock
232+
'invoiceService' => $this->invoiceServiceMock,
233+
'orderRepository' => $this->orderRepositoryMock
224234
]
225235
);
226236
}
@@ -250,19 +260,17 @@ public function testExecute()
250260

251261
$orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
252262
->disableOriginalConstructor()
253-
->setMethods(['load', 'getId', 'canInvoice'])
263+
->setMethods(['load', 'canInvoice'])
254264
->getMock();
255-
$orderMock->expects($this->once())
256-
->method('load')
257-
->with($orderId)
258-
->willReturnSelf();
259-
$orderMock->expects($this->once())
260-
->method('getId')
261-
->willReturn($orderId);
262265
$orderMock->expects($this->once())
263266
->method('canInvoice')
264267
->willReturn(true);
265268

269+
$this->orderRepositoryMock->expects($this->once())
270+
->method('get')
271+
->with($orderId)
272+
->willReturn($orderMock);
273+
266274
$this->invoiceServiceMock->expects($this->once())
267275
->method('prepareInvoice')
268276
->with($orderMock, [])
@@ -285,11 +293,7 @@ public function testExecute()
285293
->with(true)
286294
->will($this->returnValue($commentText));
287295

288-
$this->objectManagerMock->expects($this->at(0))
289-
->method('create')
290-
->with(\Magento\Sales\Model\Order::class)
291-
->willReturn($orderMock);
292-
$this->objectManagerMock->expects($this->at(1))
296+
$this->objectManagerMock->expects($this->once())
293297
->method('get')
294298
->with(\Magento\Backend\Model\Session::class)
295299
->will($this->returnValue($this->sessionMock));
@@ -318,19 +322,12 @@ public function testExecuteNoOrder()
318322

319323
$orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
320324
->disableOriginalConstructor()
321-
->setMethods(['load', 'getId', 'canInvoice'])
325+
->setMethods(['canInvoice'])
322326
->getMock();
323-
$orderMock->expects($this->once())
324-
->method('load')
325-
->with($orderId)
326-
->willReturnSelf();
327-
$orderMock->expects($this->once())
328-
->method('getId')
329-
->willReturn(null);
330327

331-
$this->objectManagerMock->expects($this->at(0))
332-
->method('create')
333-
->with(\Magento\Sales\Model\Order::class)
328+
$this->orderRepositoryMock->expects($this->once())
329+
->method('get')
330+
->with($orderId)
334331
->willReturn($orderMock);
335332

336333
$resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)

0 commit comments

Comments
 (0)