Skip to content

Commit e6ffb4d

Browse files
author
Mike Weis
committed
MAGETWO-33664: Refactor Sales module to use mutable data object interfaces
- relax parameter on setItems(...) - enhance unit test
1 parent 8e0a482 commit e6ffb4d

File tree

3 files changed

+27
-51
lines changed

3 files changed

+27
-51
lines changed

app/code/Magento/Sales/Api/Data/InvoiceInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ public function setDiscountDescription($description);
856856
* @param \Magento\Sales\Api\Data\InvoiceItemInterface[] $items
857857
* @return $this
858858
*/
859-
public function setItems(array $items = null);
859+
public function setItems($items);
860860

861861
/**
862862
* Sets the comments, if any, for the invoice.

app/code/Magento/Sales/Model/Order/Invoice.php

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ class Invoice extends AbstractModel implements EntityInterface, InvoiceInterface
104104
*/
105105
protected $_invoiceItemCollectionFactory;
106106

107-
/**
108-
* @var \Magento\Sales\Model\Resource\Order\Invoice\Item\Collection
109-
*/
110-
protected $_invoiceItemCollection;
111-
112107
/**
113108
* @var \Magento\Sales\Model\Order\Invoice\CommentFactory
114109
*/
@@ -482,39 +477,22 @@ public function roundPrice($price, $type = 'regular', $negative = false)
482477
}
483478

484479
/**
485-
* Get invoice items collection.
486-
* If needed, performs additional processing for these items.
480+
* Get invoice items collection
487481
*
488482
* @return \Magento\Sales\Model\Resource\Order\Invoice\Item\Collection
489483
*/
490484
public function getItemsCollection()
491485
{
492-
$collection = $this->_getItemsCollectionInstance();
493486
if (!$this->hasData(InvoiceInterface::ITEMS)) {
494-
$this->setItems($collection->getItems());
487+
$this->setItems($this->_invoiceItemCollectionFactory->create()->setInvoiceFilter($this->getId()));
495488

496489
if ($this->getId()) {
497490
foreach ($this->getItems() as $item) {
498491
$item->setInvoice($this);
499492
}
500493
}
501494
}
502-
return $collection;
503-
}
504-
505-
/**
506-
* Returns the invoice item collection
507-
*
508-
* @return $this|\Magento\Sales\Model\Resource\Order\Invoice\Item\Collection
509-
*/
510-
protected function _getItemsCollectionInstance()
511-
{
512-
$collection = $this->_invoiceItemCollection;
513-
if (empty($collection)) {
514-
$collection = $this->_invoiceItemCollectionFactory->create()->setInvoiceFilter($this->getId());
515-
$this->_invoiceItemCollection = $collection;
516-
}
517-
return $collection;
495+
return $this->getItems();
518496
}
519497

520498
/**
@@ -555,15 +533,6 @@ public function addItem(\Magento\Sales\Model\Order\Invoice\Item $item)
555533

556534
if (!$item->getId()) {
557535
$this->getItemsCollection()->addItem($item);
558-
559-
$item->setInvoice($this);
560-
561-
$items = $this->getData(InvoiceInterface::ITEMS);
562-
if (empty($items)) {
563-
$items = [];
564-
}
565-
$items[] = $item;
566-
$this->setData(InvoiceInterface::ITEMS, $items);
567536
}
568537
return $this;
569538
}
@@ -824,9 +793,8 @@ public function getDiscountDescription()
824793
*/
825794
public function getItems()
826795
{
827-
$items = $this->getData(InvoiceInterface::ITEMS);
828-
if (($items === null || empty($items)) && $this->getId()) {
829-
$collection = $this->_getItemsCollectionInstance();
796+
if ($this->getData(InvoiceInterface::ITEMS) === null && $this->getId()) {
797+
$collection = $this->_invoiceItemCollectionFactory->create()->setInvoiceFilter($this->getId());
830798
foreach ($collection as $item) {
831799
$item->setInvoice($this);
832800
}
@@ -1584,7 +1552,7 @@ public function setDiscountDescription($description)
15841552
/**
15851553
* {@inheritdoc}
15861554
*/
1587-
public function setItems(array $items = null)
1555+
public function setItems($items)
15881556
{
15891557
return $this->setData(InvoiceInterface::ITEMS, $items);
15901558
}

dev/tests/unit/testsuite/Magento/Quote/Model/QuoteManagementTest.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ protected function setUp()
9191
$this->eventManager = $this->getMockForAbstractClass('Magento\Framework\Event\ManagerInterface');
9292
$this->orderFactory = $this->getMock(
9393
'Magento\Sales\Api\Data\OrderInterfaceFactory',
94-
[
95-
'populate', 'setShippingAddress', 'setBillingAddress', 'setAddresses', 'setPayments',
96-
'setItems', 'setCustomerId', 'setQuoteId', 'create', 'setCustomerEmail', 'setCustomerFirstname',
97-
'setCustomerMiddlename', 'setCustomerLastname'
98-
],
94+
[ 'create' ],
9995
[],
10096
'',
10197
false
@@ -500,7 +496,10 @@ public function testSubmit()
500496
$convertedPayment = $this->getMock('Magento\Sales\Api\Data\OrderPaymentInterface', [], [], '', false);
501497
$convertedQuoteItem = $this->getMock('Magento\Sales\Api\Data\OrderItemInterface', [], [], '', false);
502498

499+
$addresses = [$convertedShippingAddress, $convertedBillingAddress];
500+
$payments = [$convertedPayment];
503501
$quoteItems = [$quoteItem];
502+
$convertedItems = [$convertedQuoteItem];
504503

505504
$quote = $this->getQuote(
506505
$isGuest,
@@ -553,6 +552,10 @@ public function testSubmit()
553552
$order = $this->prepareOrderFactory(
554553
$baseOrder,
555554
$convertedBillingAddress,
555+
$addresses,
556+
$payments,
557+
$convertedItems,
558+
$quoteId,
556559
$convertedShippingAddress
557560
);
558561

@@ -649,12 +652,16 @@ protected function getQuote(
649652
protected function prepareOrderFactory(
650653
\Magento\Sales\Api\Data\OrderInterface $baseOrder,
651654
\Magento\Sales\Api\Data\OrderAddressInterface $billingAddress,
655+
array $addresses,
656+
array $payments,
657+
array $items,
658+
$quoteId,
652659
\Magento\Sales\Api\Data\OrderAddressInterface $shippingAddress = null
653660
) {
654661
$order = $this->getMock(
655662
'Magento\Sales\Model\Order',
656663
['setShippingAddress', 'getAddressesCollection', 'getAddresses', 'getBillingAddress', 'addAddresses',
657-
'setBillingAddress'],
664+
'setBillingAddress', 'setAddresses', 'setPayments', 'setItems', 'setQuoteId'],
658665
[],
659666
'',
660667
false
@@ -668,17 +675,18 @@ protected function prepareOrderFactory(
668675
->with($baseOrder);
669676

670677
if ($shippingAddress) {
671-
$order->expects($this->once())
672-
->method('setShippingAddress')
673-
->with($shippingAddress);
678+
$order->expects($this->once())->method('setShippingAddress')->with($shippingAddress);
674679
}
675680
$order->expects($this->any())->method('getAddressesCollection');
676681
$order->expects($this->any())->method('getAddresses');
677682
$order->expects($this->any())->method('getBillingAddress')->willReturn(false);
678683
$order->expects($this->any())->method('addAddresses')->withAnyParameters()->willReturnSelf();
679-
$order->expects($this->once())
680-
->method('setBillingAddress')
681-
->with($billingAddress);
684+
$order->expects($this->once())->method('setBillingAddress')->with($billingAddress);
685+
$order->expects($this->once())->method('setAddresses')->with($addresses);
686+
$order->expects($this->once())->method('setPayments')->with($payments);
687+
$order->expects($this->once())->method('setItems')->with($items);
688+
$order->expects($this->once())->method('setQuoteId')->with($quoteId);
689+
682690
return $order;
683691
}
684692

0 commit comments

Comments
 (0)