Skip to content

Commit a25f386

Browse files
author
Robert He
committed
MAGETWO-33665 : Refactor Quote module to use mutable data object interfaces
-- fixes from code review -- fixes to testcases
1 parent 50477f2 commit a25f386

File tree

6 files changed

+55
-42
lines changed

6 files changed

+55
-42
lines changed

app/code/Magento/Quote/Model/Cart/CartTotalRepository.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
class CartTotalRepository implements CartTotalRepositoryInterface
1616
{
1717
/**
18-
* Cart totals builder.
18+
* Cart totals factory.
1919
*
20-
* @var Api\Data\TotalsDataBuilder
20+
* @var Api\Data\TotalsInterfaceFactory
2121
*/
22-
private $totalsBuilder;
22+
private $totalsFactory;
2323

2424
/**
2525
* Quote repository.
@@ -28,18 +28,26 @@ class CartTotalRepository implements CartTotalRepositoryInterface
2828
*/
2929
private $quoteRepository;
3030

31+
/**
32+
* @var \Magento\Framework\Api\DataObjectHelper
33+
*/
34+
private $dataObjectHelper;
35+
3136
/**
3237
* Constructs a cart totals data object.
3338
*
34-
* @param Api\Data\TotalsDataBuilder $totalsBuilder Cart totals builder.
39+
* @param Api\Data\TotalsInterfaceFactory $totalsFactory Cart totals factory.
3540
* @param QuoteRepository $quoteRepository Quote repository.
41+
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
3642
*/
3743
public function __construct(
38-
Api\Data\TotalsDataBuilder $totalsBuilder,
39-
QuoteRepository $quoteRepository
44+
Api\Data\TotalsInterfaceFactory $totalsFactory,
45+
QuoteRepository $quoteRepository,
46+
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper
4047
) {
41-
$this->totalsBuilder = $totalsBuilder;
48+
$this->totalsFactory = $totalsFactory;
4249
$this->quoteRepository = $quoteRepository;
50+
$this->dataObjectHelper = $dataObjectHelper;
4351
}
4452

4553
/**
@@ -57,10 +65,11 @@ public function get($cartId)
5765
*/
5866
$quote = $this->quoteRepository->getActive($cartId);
5967
$shippingAddress = $quote->getShippingAddress();
60-
$totals = array_merge($shippingAddress->getData(), $quote->getData());
61-
$this->totalsBuilder->populateWithArray($totals);
62-
$this->totalsBuilder->setItems($quote->getAllItems());
68+
$totalsData = array_merge($shippingAddress->getData(), $quote->getData());
69+
$totals = $this->totalsFactory->create();
70+
$this->dataObjectHelper->populateWithArray($totals, $totalsData, '\Magento\Quote\Api\Data\TotalsInterface');
71+
$totals->setItems($quote->getAllItems());
6372

64-
return $this->totalsBuilder->create();
73+
return $totals;
6574
}
6675
}

app/code/Magento/Quote/Model/Cart/ShippingMethodConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
class ShippingMethodConverter
1313
{
1414
/**
15-
* Shipping method builder.
15+
* Shipping method data factory.
1616
*
1717
* @var \Magento\Quote\Api\Data\ShippingMethodInterfaceFactory
1818
*/
1919
protected $shippingMethodDataFactory;
2020

2121
/**
22-
* Constructs a shipping method builder object.
22+
* Constructs a shipping method converter object.
2323
*
2424
* @param \Magento\Quote\Api\Data\ShippingMethodInterfaceFactory $shippingMethodDataFactory Shipping method factory.
2525
* @param \Magento\Store\Model\StoreManagerInterface $storeManager Store manager interface.

app/code/Magento/Quote/Model/ShippingMethodManagement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class ShippingMethodManagement implements ShippingMethodManagementInterface
4545
* Constructs a shipping method read service object.
4646
*
4747
* @param QuoteRepository $quoteRepository Quote repository.
48-
* @param \Magento\Quote\Api\Data\ShippingMethodInterfaceFactory $methodDataFactory Shipping method builder.
49-
* @param \Magento\Quote\Model\Cart\ShippingMethodConverter $converter Shipping method builder converter.
48+
* @param \Magento\Quote\Api\Data\ShippingMethodInterfaceFactory $methodDataFactory Shipping method factory.
49+
* @param \Magento\Quote\Model\Cart\ShippingMethodConverter $converter Shipping method converter.
5050
*/
5151
public function __construct(
5252
QuoteRepository $quoteRepository,

dev/tests/unit/testsuite/Magento/Quote/Model/Cart/CartTotalRepositoryTest.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,38 @@ class CartTotalRepositoryTest extends \PHPUnit_Framework_TestCase
2626
/**
2727
* @var \PHPUnit_Framework_MockObject_MockObject
2828
*/
29-
private $totalsBuilderMock;
29+
private $totalsFactoryMock;
3030

3131
/**
3232
* @var \PHPUnit_Framework_MockObject_MockObject
3333
*/
3434
protected $addressMock;
3535

36+
/**
37+
* @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $dataObjectHelperMock;
40+
3641
public function setUp()
3742
{
38-
$this->totalsBuilderMock = $this->getMock(
39-
'Magento\Quote\Api\Data\TotalsDataBuilder',
40-
['populateWithArray', 'setItems', 'create'],
43+
$this->totalsFactoryMock = $this->getMock(
44+
'Magento\Quote\Api\Data\TotalsInterfaceFactory',
45+
['create'],
4146
[],
4247
'',
4348
false
4449
);
4550
$this->quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false);
4651
$this->quoteRepositoryMock = $this->getMock('Magento\Quote\Model\QuoteRepository', [], [], '', false);
4752
$this->addressMock = $this->getMock('Magento\Quote\Model\Quote\Address', [], [], '', false);
53+
$this->dataObjectHelperMock = $this->getMockBuilder('\Magento\Framework\Api\DataObjectHelper')
54+
->disableOriginalConstructor()
55+
->getMock();
4856

4957
$this->model = new CartTotalRepository(
50-
$this->totalsBuilderMock,
51-
$this->quoteRepositoryMock
58+
$this->totalsFactoryMock,
59+
$this->quoteRepositoryMock,
60+
$this->dataObjectHelperMock
5261
);
5362
}
5463

@@ -63,6 +72,12 @@ public function testGetTotals()
6372

6473
$item = $this->getMock('Magento\Quote\Model\Quote\Item', [], [], '', false);
6574
$this->quoteMock->expects($this->once())->method('getAllItems')->will($this->returnValue([$item]));
75+
76+
$totals = $this->getMock('Magento\Quote\Model\Cart\Totals', ['setItems'], [], '', false);
77+
$this->totalsFactoryMock->expects($this->once())->method('create')->willReturn($totals);
78+
$this->dataObjectHelperMock->expects($this->once())->method('populateWithArray');
79+
$totals->expects($this->once())->method('setItems');
80+
6681
$this->model->get($cartId);
6782
}
6883
}

dev/tests/unit/testsuite/Magento/Quote/Model/Cart/ShippingMethodConverterTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ShippingMethodConverterTest extends \PHPUnit_Framework_TestCase
1919
/**
2020
* @var \PHPUnit_Framework_MockObject_MockObject
2121
*/
22-
protected $builderMock;
22+
protected $shippingMethodDataFactoryMock;
2323

2424
/**
2525
* @var \PHPUnit_Framework_MockObject_MockObject
@@ -49,16 +49,16 @@ class ShippingMethodConverterTest extends \PHPUnit_Framework_TestCase
4949
protected function setUp()
5050
{
5151
$objectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
52-
$this->builderMock = $this->getMock(
53-
'\Magento\Quote\Api\Data\ShippingMethodDataBuilder',
54-
['populateWithArray', 'create'],
52+
$this->shippingMethodDataFactoryMock = $this->getMock(
53+
'\Magento\Quote\Api\Data\ShippingMethodInterfaceFactory',
54+
['create'],
5555
[],
5656
'',
5757
false
5858
);
5959
$this->storeManagerMock = $this->getMock('\Magento\Store\Model\StoreManagerInterface');
6060
$this->currencyMock = $this->getMock('\Magento\Directory\Model\Currency', [], [], '', false);
61-
$this->shippingMethodMock = $this->getMock('\Magento\Quote\Api\Data\ShippingMethodInterfaceFactory',
61+
$this->shippingMethodMock = $this->getMock('\Magento\Quote\Model\Cart\ShippingMethod',
6262
[
6363
'create',
6464
'setCarrierCode',
@@ -89,7 +89,7 @@ protected function setUp()
8989
$this->converter = $objectManager->getObject(
9090
'Magento\Quote\Model\Cart\ShippingMethodConverter',
9191
[
92-
'shippingMethodDataFactory' => $this->shippingMethodMock,
92+
'shippingMethodDataFactory' => $this->shippingMethodDataFactoryMock,
9393
'storeManager' => $this->storeManagerMock,
9494
]
9595
);
@@ -111,9 +111,11 @@ public function testModelToDataObject()
111111
->method('getCarrierTitle')->will($this->returnValue('CARRIER_TITLE'));
112112
$this->rateModelMock->expects($this->once())
113113
->method('getMethodTitle')->will($this->returnValue('METHOD_TITLE'));
114-
$this->shippingMethodMock->expects($this->once())
114+
$this->shippingMethodDataFactoryMock->expects($this->once())
115115
->method('create')
116116
->will($this->returnValue($this->shippingMethodMock));
117+
118+
117119
$this->shippingMethodMock->expects($this->once())
118120
->method('setCarrierCode')
119121
->with('CARRIER_CODE')

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,19 @@ class PaymentMethodManagementTest extends \PHPUnit_Framework_TestCase
3131
*/
3232
protected $zeroTotalMock;
3333

34-
/**
35-
* @var \PHPUnit_Framework_MockObject_MockObject
36-
*/
37-
protected $paymentMethodBuilder;
38-
3934
protected function setUp()
4035
{
4136
$this->objectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
4237
$this->quoteRepositoryMock = $this->getMock('\Magento\Quote\Model\QuoteRepository', [], [], '', false);
4338
$this->methodListMock = $this->getMock('\Magento\Payment\Model\MethodList', [], [], '', false);
4439
$this->zeroTotalMock = $this->getMock('\Magento\Payment\Model\Checks\ZeroTotal', [], [], '', false);
45-
$this->paymentMethodBuilder = $this->getMock(
46-
'\Magento\Quote\Api\Data\PaymentMethodDataBuilder',
47-
[],
48-
[],
49-
'',
50-
false
51-
);
5240

5341
$this->model = $this->objectManager->getObject(
5442
'\Magento\Quote\Model\PaymentMethodManagement',
5543
[
5644
'quoteRepository' => $this->quoteRepositoryMock,
5745
'methodList' => $this->methodListMock,
58-
'zeroTotalValidator' => $this->zeroTotalMock,
59-
'paymentMethodBuilder' => $this->paymentMethodBuilder,
46+
'zeroTotalValidator' => $this->zeroTotalMock
6047
]
6148
);
6249
}

0 commit comments

Comments
 (0)