Skip to content

Commit d66049b

Browse files
committed
Merge remote-tracking branch 'mpi/MAGETWO-52552' into MPI-PR-3005
2 parents 614e933 + 7fbcf95 commit d66049b

File tree

6 files changed

+125
-160
lines changed

6 files changed

+125
-160
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Invoice/AbstractInvoice/View.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
namespace Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice;
88

99
use Magento\Backend\App\Action\Context;
10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Framework\Registry;
12+
use Magento\Sales\Api\InvoiceRepositoryInterface;
13+
use Magento\Sales\Model\Order\InvoiceRepository;
1114

1215
abstract class View extends \Magento\Backend\App\Action
1316
{
@@ -28,6 +31,11 @@ abstract class View extends \Magento\Backend\App\Action
2831
*/
2932
protected $resultForwardFactory;
3033

34+
/**
35+
* @var InvoiceRepositoryInterface
36+
*/
37+
protected $invoiceRepository;
38+
3139
/**
3240
* @param Context $context
3341
* @param Registry $registry
@@ -66,16 +74,28 @@ public function execute()
6674
*/
6775
protected function getInvoice()
6876
{
69-
$invoiceId = $this->getRequest()->getParam('invoice_id');
70-
if (!$invoiceId) {
77+
try {
78+
$invoice = $this->getInvoiceRepository()
79+
->get($this->getRequest()->getParam('invoice_id'));
80+
$this->registry->register('current_invoice', $invoice);
81+
} catch (\Exception $e) {
82+
$this->messageManager->addError(__('Invoice capturing error'));
7183
return false;
7284
}
73-
/** @var \Magento\Sales\Model\Order\Invoice $invoice */
74-
$invoice = $this->_objectManager->create('Magento\Sales\Api\InvoiceRepositoryInterface')->get($invoiceId);
75-
if (!$invoice) {
76-
return false;
77-
}
78-
$this->registry->register('current_invoice', $invoice);
85+
7986
return $invoice;
8087
}
88+
89+
/**
90+
* @return InvoiceRepository
91+
*/
92+
private function getInvoiceRepository()
93+
{
94+
if ($this->invoiceRepository === null) {
95+
$this->invoiceRepository = ObjectManager::getInstance()
96+
->get(InvoiceRepositoryInterface::class);
97+
}
98+
99+
return $this->invoiceRepository;
100+
}
81101
}

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

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
use Magento\Backend\App\Action;
99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\Sales\Api\InvoiceRepositoryInterface;
1011

1112
/**
1213
* Class AddCommentTest
1314
* @package Magento\Sales\Controller\Adminhtml\Order\Invoice
15+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1416
*/
1517
class AddCommentTest extends \PHPUnit_Framework_TestCase
1618
{
@@ -34,11 +36,6 @@ class AddCommentTest extends \PHPUnit_Framework_TestCase
3436
*/
3537
protected $viewMock;
3638

37-
/**
38-
* @var \PHPUnit_Framework_MockObject_MockObject
39-
*/
40-
protected $objectManagerMock;
41-
4239
/**
4340
* @var \Magento\Framework\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject
4441
*/
@@ -79,6 +76,11 @@ class AddCommentTest extends \PHPUnit_Framework_TestCase
7976
*/
8077
protected $resultJsonMock;
8178

79+
/**
80+
* @var InvoiceRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
81+
*/
82+
protected $invoiceRepository;
83+
8284
/**
8385
* SetUp method
8486
*
@@ -104,10 +106,6 @@ protected function setUp()
104106
->disableOriginalConstructor()
105107
->setMethods([])
106108
->getMock();
107-
$this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface')
108-
->disableOriginalConstructor()
109-
->setMethods([])
110-
->getMock();
111109
$this->resultPageMock = $this->getMockBuilder('Magento\Framework\View\Result\Page')
112110
->disableOriginalConstructor()
113111
->getMock();
@@ -133,9 +131,6 @@ protected function setUp()
133131
$contextMock->expects($this->any())
134132
->method('getView')
135133
->will($this->returnValue($this->viewMock));
136-
$contextMock->expects($this->any())
137-
->method('getObjectManager')
138-
->will($this->returnValue($this->objectManagerMock));
139134
$this->viewMock->expects($this->any())
140135
->method('getPage')
141136
->willReturn($this->resultPageMock);
@@ -150,26 +145,26 @@ protected function setUp()
150145
->disableOriginalConstructor()
151146
->setMethods(['create'])
152147
->getMock();
153-
154148
$this->resultJsonMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Json')
155149
->disableOriginalConstructor()
156150
->setMethods([])
157151
->getMock();
158-
159152
$this->resultRawFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\Result\RawFactory')
160153
->disableOriginalConstructor()
161154
->setMethods(['create'])
162155
->getMock();
163-
164156
$this->resultJsonFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\Result\JsonFactory')
165157
->disableOriginalConstructor()
166158
->setMethods(['create'])
167159
->getMock();
168-
169160
$this->commentSenderMock = $this->getMockBuilder('Magento\Sales\Model\Order\Email\Sender\InvoiceCommentSender')
170161
->disableOriginalConstructor()
171162
->setMethods([])
172163
->getMock();
164+
$this->invoiceRepository = $this->getMockBuilder(InvoiceRepositoryInterface::class)
165+
->disableOriginalConstructor()
166+
->getMockForAbstractClass();
167+
173168
$this->controller = $objectManager->getObject(
174169
'Magento\Sales\Controller\Adminhtml\Order\Invoice\AddComment',
175170
[
@@ -180,6 +175,12 @@ protected function setUp()
180175
'resultJsonFactory' => $this->resultJsonFactoryMock
181176
]
182177
);
178+
179+
$objectManager->setBackwardCompatibleProperty(
180+
$this->controller,
181+
'invoiceRepository',
182+
$this->invoiceRepository
183+
);
183184
}
184185

185186
/**
@@ -218,16 +219,9 @@ public function testExecute()
218219
$invoiceMock->expects($this->once())
219220
->method('save');
220221

221-
$invoiceRepository = $this->getMockBuilder('Magento\Sales\Api\InvoiceRepositoryInterface')
222-
->disableOriginalConstructor()
223-
->getMock();
224-
$invoiceRepository->expects($this->any())
222+
$this->invoiceRepository->expects($this->once())
225223
->method('get')
226224
->willReturn($invoiceMock);
227-
$this->objectManagerMock->expects($this->once())
228-
->method('create')
229-
->with('Magento\Sales\Api\InvoiceRepositoryInterface')
230-
->willReturn($invoiceRepository);
231225

232226
$commentsBlockMock = $this->getMockBuilder('Magento\Sales\Block\Adminhtml\Order\Invoice\View\Comments')
233227
->disableOriginalConstructor()

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

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Backend\App\Action;
99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\Sales\Api\InvoiceRepositoryInterface;
1011

1112
/**
1213
* Class CancelTest
@@ -64,6 +65,11 @@ class CancelTest extends \PHPUnit_Framework_TestCase
6465
*/
6566
protected $controller;
6667

68+
/**
69+
* @var InvoiceRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
protected $invoiceRepository;
72+
6773
/**
6874
* @return void
6975
*/
@@ -141,13 +147,23 @@ protected function setUp()
141147
->method('getResultRedirectFactory')
142148
->willReturn($this->resultRedirectFactoryMock);
143149

150+
$this->invoiceRepository = $this->getMockBuilder(InvoiceRepositoryInterface::class)
151+
->disableOriginalConstructor()
152+
->getMockForAbstractClass();
153+
144154
$this->controller = $objectManager->getObject(
145155
'Magento\Sales\Controller\Adminhtml\Order\Invoice\Cancel',
146156
[
147157
'context' => $contextMock,
148158
'resultForwardFactory' => $this->resultForwardFactoryMock
149159
]
150160
);
161+
162+
$objectManager->setBackwardCompatibleProperty(
163+
$this->controller,
164+
'invoiceRepository',
165+
$this->invoiceRepository
166+
);
151167
}
152168

153169
/**
@@ -196,18 +212,11 @@ public function testExecute()
196212
->method('addSuccess')
197213
->with('You canceled the invoice.');
198214

199-
$invoiceRepository = $this->getMockBuilder('Magento\Sales\Api\InvoiceRepositoryInterface')
200-
->disableOriginalConstructor()
201-
->getMock();
202-
$invoiceRepository->expects($this->any())
215+
$this->invoiceRepository->expects($this->once())
203216
->method('get')
204217
->willReturn($invoiceMock);
205218

206-
$this->objectManagerMock->expects($this->at(0))
207-
->method('create')
208-
->with('Magento\Sales\Api\InvoiceRepositoryInterface')
209-
->willReturn($invoiceRepository);
210-
$this->objectManagerMock->expects($this->at(1))
219+
$this->objectManagerMock->expects($this->once())
211220
->method('create')
212221
->with('Magento\Framework\DB\Transaction')
213222
->will($this->returnValue($transactionMock));
@@ -241,18 +250,10 @@ public function testExecuteNoInvoice()
241250
->with('invoice_id')
242251
->will($this->returnValue($invoiceId));
243252

244-
$invoiceRepository = $this->getMockBuilder('Magento\Sales\Api\InvoiceRepositoryInterface')
245-
->disableOriginalConstructor()
246-
->getMock();
247-
$invoiceRepository->expects($this->any())
253+
$this->invoiceRepository->expects($this->once())
248254
->method('get')
249255
->willReturn(null);
250256

251-
$this->objectManagerMock->expects($this->at(0))
252-
->method('create')
253-
->with('Magento\Sales\Api\InvoiceRepositoryInterface')
254-
->willReturn($invoiceRepository);
255-
256257
$resultForward = $this->getMockBuilder('Magento\Backend\Model\View\Result\Forward')
257258
->disableOriginalConstructor()
258259
->setMethods([])
@@ -297,18 +298,10 @@ public function testExecuteModelException()
297298
->method('getId')
298299
->will($this->returnValue($invoiceId));
299300

300-
$invoiceRepository = $this->getMockBuilder('Magento\Sales\Api\InvoiceRepositoryInterface')
301-
->disableOriginalConstructor()
302-
->getMock();
303-
$invoiceRepository->expects($this->any())
301+
$this->invoiceRepository->expects($this->once())
304302
->method('get')
305303
->willReturn($invoiceMock);
306304

307-
$this->objectManagerMock->expects($this->once())
308-
->method('create')
309-
->with('Magento\Sales\Api\InvoiceRepositoryInterface')
310-
->willReturn($invoiceRepository);
311-
312305
$resultRedirect = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect')
313306
->disableOriginalConstructor()
314307
->setMethods([])
@@ -353,18 +346,10 @@ public function testExecuteException()
353346
->method('getId')
354347
->will($this->returnValue($invoiceId));
355348

356-
$invoiceRepository = $this->getMockBuilder('Magento\Sales\Api\InvoiceRepositoryInterface')
357-
->disableOriginalConstructor()
358-
->getMock();
359-
$invoiceRepository->expects($this->any())
349+
$this->invoiceRepository->expects($this->once())
360350
->method('get')
361351
->willReturn($invoiceMock);
362352

363-
$this->objectManagerMock->expects($this->once())
364-
->method('create')
365-
->with('Magento\Sales\Api\InvoiceRepositoryInterface')
366-
->willReturn($invoiceRepository);
367-
368353
$resultRedirect = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect')
369354
->disableOriginalConstructor()
370355
->setMethods([])

0 commit comments

Comments
 (0)