Skip to content

Commit 709ae39

Browse files
author
Bryant Luk
committed
MAGETWO-36426: Magento\Quote\Api\GuestCartTotalRepository
- Refactor GuestCartTotalRepository to use composition instead of inheritance - Modify unit test to match changes
1 parent d8b75a0 commit 709ae39

File tree

2 files changed

+42
-68
lines changed

2 files changed

+42
-68
lines changed

app/code/Magento/Quote/Model/GuestCart/GuestCartTotalRepository.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,38 @@
66

77
namespace Magento\Quote\Model\GuestCart;
88

9+
use Magento\Quote\Api\CartTotalRepositoryInterface;
910
use Magento\Quote\Api\GuestCartTotalRepositoryInterface;
10-
use Magento\Quote\Model\Cart\CartTotalRepository;
11-
use Magento\Quote\Model\QuoteRepository;
1211
use Magento\Quote\Model\QuoteIdMask;
1312
use Magento\Quote\Model\QuoteIdMaskFactory;
1413

1514
/**
1615
* Cart totals repository class for guest carts.
1716
*/
18-
class GuestCartTotalRepository extends CartTotalRepository implements GuestCartTotalRepositoryInterface
17+
class GuestCartTotalRepository implements GuestCartTotalRepositoryInterface
1918
{
2019
/**
2120
* @var QuoteIdMaskFactory
2221
*/
2322
private $quoteIdMaskFactory;
2423

24+
/**
25+
* @var CartTotalRepositoryInterface
26+
*/
27+
private $cartTotalRepository;
28+
2529
/**
2630
* Constructs a cart totals data object.
2731
*
28-
* @param \Magento\Quote\Api\Data\TotalsInterfaceFactory $totalsFactory Cart totals factory.
29-
* @param QuoteRepository $quoteRepository Quote repository.
30-
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
32+
* @param CartTotalRepositoryInterface $cartTotalRepository
3133
* @param QuoteIdMaskFactory $quoteIdMaskFactory
3234
*/
3335
public function __construct(
34-
\Magento\Quote\Api\Data\TotalsInterfaceFactory $totalsFactory,
35-
QuoteRepository $quoteRepository,
36-
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
36+
CartTotalRepositoryInterface $cartTotalRepository,
3737
QuoteIdMaskFactory $quoteIdMaskFactory
3838
) {
39+
$this->cartTotalRepository = $cartTotalRepository;
3940
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
40-
parent::__construct($totalsFactory, $quoteRepository, $dataObjectHelper);
4141
}
4242

4343
/**
@@ -47,6 +47,6 @@ public function get($cartId)
4747
{
4848
/** @var $quoteIdMask QuoteIdMask */
4949
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
50-
return parent::get($quoteIdMask->getId());
50+
return $this->cartTotalRepository->get($quoteIdMask->getId());
5151
}
5252
}

app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartTotalRepositoryTest.php

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,96 +21,70 @@ class GuestCartTotalRepositoryTest extends \PHPUnit_Framework_TestCase
2121
/**
2222
* @var \PHPUnit_Framework_MockObject_MockObject
2323
*/
24-
private $quoteRepositoryMock;
24+
protected $cartTotalRepository;
2525

2626
/**
2727
* @var \PHPUnit_Framework_MockObject_MockObject
2828
*/
29-
private $quoteMock;
30-
31-
/**
32-
* @var \PHPUnit_Framework_MockObject_MockObject
33-
*/
34-
private $totalsFactoryMock;
29+
protected $quoteIdMaskFactoryMock;
3530

3631
/**
3732
* @var \PHPUnit_Framework_MockObject_MockObject
3833
*/
39-
protected $addressMock;
40-
41-
/**
42-
* @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
43-
*/
44-
private $dataObjectHelperMock;
34+
protected $quoteIdMaskMock;
4535

4636
/**
47-
* @var \PHPUnit_Framework_MockObject_MockObject
37+
* @var string
4838
*/
49-
protected $quoteIdMaskFactoryMock;
39+
protected $maskedCartId = 'f216207248d65c789b17be8545e0aa73';
5040

5141
/**
52-
* @var \PHPUnit_Framework_MockObject_MockObject
42+
* @var int
5343
*/
54-
protected $quoteIdMaskMock;
44+
protected $cartId = 12;
5545

5646
public function setUp()
5747
{
5848
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
59-
$this->totalsFactoryMock = $this->getMock(
60-
'Magento\Quote\Api\Data\TotalsInterfaceFactory',
61-
['create'],
62-
[],
63-
'',
64-
false
65-
);
66-
$this->quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false);
67-
$this->quoteRepositoryMock = $this->getMock('Magento\Quote\Model\QuoteRepository', [], [], '', false);
68-
$this->addressMock = $this->getMock('Magento\Quote\Model\Quote\Address', [], [], '', false);
69-
$this->dataObjectHelperMock = $this->getMockBuilder('\Magento\Framework\Api\DataObjectHelper')
49+
50+
$this->quoteIdMaskFactoryMock = $this->getMockBuilder('Magento\Quote\Model\QuoteIdMaskFactory')
51+
->disableOriginalConstructor()
52+
->getMock();
53+
$this->quoteIdMaskMock = $this->getMockBuilder('Magento\Quote\Model\QuoteIdMask')
54+
->disableOriginalConstructor()
55+
->getMock();
56+
$this->cartTotalRepository = $this->getMockBuilder('Magento\Quote\Api\CartTotalRepositoryInterface')
7057
->disableOriginalConstructor()
7158
->getMock();
72-
$this->quoteIdMaskFactoryMock = $this->getMock('Magento\Quote\Model\QuoteIdMaskFactory', [], [], '', false);
73-
$this->quoteIdMaskMock = $this->getMock('Magento\Quote\Model\QuoteIdMask', [], [], '', false);
7459

7560
$this->model = $this->objectManager->getObject(
7661
'Magento\Quote\Model\GuestCart\GuestCartTotalRepository',
7762
[
78-
'totalsFactory' => $this->totalsFactoryMock,
79-
'quoteRepository' => $this->quoteRepositoryMock,
80-
'dataObjectHelper' => $this->dataObjectHelperMock,
81-
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock
63+
'cartTotalRepository' => $this->cartTotalRepository,
64+
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
8265
]
8366
);
84-
}
85-
86-
public function testGetTotals()
87-
{
88-
$maskedCartId = 'f216207248d65c789b17be8545e0aa73';
89-
$cartId = 12;
9067

91-
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
68+
$this->quoteIdMaskFactoryMock->expects($this->once())
69+
->method('create')
70+
->willReturn($this->quoteIdMaskMock);
9271
$this->quoteIdMaskMock->expects($this->once())
9372
->method('load')
94-
->with($maskedCartId, 'masked_id')
73+
->with($this->maskedCartId, 'masked_id')
9574
->willReturn($this->quoteIdMaskMock);
9675
$this->quoteIdMaskMock->expects($this->once())
9776
->method('getId')
98-
->willReturn($cartId);
99-
100-
$this->quoteRepositoryMock->expects($this->once())->method('getActive')->with($cartId)
101-
->will($this->returnValue($this->quoteMock));
102-
$this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($this->addressMock);
103-
$this->addressMock->expects($this->once())->method('getData')->willReturn(['addressData']);
104-
$this->quoteMock->expects($this->once())->method('getData')->willReturn(['quoteData']);
105-
106-
$item = $this->getMock('Magento\Quote\Model\Quote\Item', [], [], '', false);
107-
$this->quoteMock->expects($this->once())->method('getAllItems')->will($this->returnValue([$item]));
77+
->willReturn($this->cartId);
78+
}
10879

109-
$totals = $this->getMock('Magento\Quote\Model\Cart\Totals', ['setItems'], [], '', false);
110-
$this->totalsFactoryMock->expects($this->once())->method('create')->willReturn($totals);
111-
$this->dataObjectHelperMock->expects($this->once())->method('populateWithArray');
112-
$totals->expects($this->once())->method('setItems');
80+
public function testGetTotals()
81+
{
82+
$retValue = 'retValue';
11383

114-
$this->model->get($maskedCartId);
84+
$this->cartTotalRepository->expects($this->once())
85+
->method('get')
86+
->with($this->cartId)
87+
->will($this->returnValue($retValue));
88+
$this->assertSame($retValue, $this->model->get($this->maskedCartId));
11589
}
11690
}

0 commit comments

Comments
 (0)