Skip to content

Commit ce1f336

Browse files
committed
MAGETWO-36377: Magento\Quote\Api\GuestCartRepositoryInterface
- replaced inheritance with composition
1 parent f0c3b5c commit ce1f336

File tree

3 files changed

+39
-179
lines changed

3 files changed

+39
-179
lines changed

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

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,46 @@
77
namespace Magento\Quote\Model\GuestCart;
88

99
use Magento\Quote\Api\GuestCartRepositoryInterface;
10-
use Magento\Quote\Model\Quote;
11-
use Magento\Quote\Model\QuoteFactory;
1210
use Magento\Quote\Model\QuoteIdMask;
1311
use Magento\Quote\Model\QuoteRepository;
14-
use Magento\Store\Model\StoreManagerInterface;
1512
use Magento\Quote\Model\QuoteIdMaskFactory;
1613

1714
/**
1815
* Cart Repository class for guest carts.
1916
*/
20-
class GuestCartRepository extends QuoteRepository implements GuestCartRepositoryInterface
17+
class GuestCartRepository implements GuestCartRepositoryInterface
2118
{
2219
/**
2320
* @var QuoteIdMaskFactory
2421
*/
2522
protected $quoteIdMaskFactory;
2623

24+
/**
25+
* @var QuoteRepository
26+
*/
27+
protected $quoteRepository;
28+
2729
/**
2830
* Initialize dependencies.
2931
*
30-
* @param QuoteFactory $quoteFactory
31-
* @param StoreManagerInterface $storeManager
32-
* @param \Magento\Quote\Model\Resource\Quote\Collection $quoteCollection
33-
* @param \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory $searchResultsDataFactory
32+
* @param QuoteRepository $quoteRepository
3433
* @param QuoteIdMaskFactory $quoteIdMaskFactory
3534
*/
3635
public function __construct(
37-
QuoteFactory $quoteFactory,
38-
StoreManagerInterface $storeManager,
39-
\Magento\Quote\Model\Resource\Quote\Collection $quoteCollection,
40-
\Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory $searchResultsDataFactory,
36+
QuoteRepository $quoteRepository,
4137
QuoteIdMaskFactory $quoteIdMaskFactory
4238
) {
43-
parent::__construct($quoteFactory, $storeManager, $quoteCollection, $searchResultsDataFactory);
4439
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
40+
$this->quoteRepository = $quoteRepository;
4541
}
4642

4743
/**
4844
* {@inheritdoc}
4945
*/
50-
public function get($cartId, array $sharedStoreIds = [])
46+
public function get($cartId)
5147
{
5248
/** @var $quoteIdMask QuoteIdMask */
5349
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
54-
return parent::get($quoteIdMask->getId(), $sharedStoreIds);
55-
}
56-
57-
/**
58-
* {@inheritdoc}
59-
*/
60-
public function save(Quote $quote)
61-
{
62-
if ($quote->getId()) {
63-
/** @var $quoteIdMask QuoteIdMask */
64-
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($quote->getId(), 'masked_id');
65-
$quote->setId($quoteIdMask->getId());
66-
}
67-
parent::save($quote);
68-
}
69-
70-
/**
71-
* {@inheritdoc}
72-
*/
73-
public function delete(Quote $quote)
74-
{
75-
if ($quote->getId()) {
76-
/** @var $quoteIdMask QuoteIdMask */
77-
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($quote->getId(), 'masked_id');
78-
$quote->setId($quoteIdMask->getId());
79-
}
80-
parent::delete($quote);
50+
return $this->quoteRepository->get($quoteIdMask->getId());
8151
}
8252
}

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

Lines changed: 20 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -17,161 +17,60 @@ class GuestCartRepositoryTest extends \PHPUnit_Framework_TestCase
1717
/**
1818
* @var \PHPUnit_Framework_MockObject_MockObject
1919
*/
20-
protected $quoteFactoryMock;
21-
22-
/**
23-
* @var \PHPUnit_Framework_MockObject_MockObject
24-
*/
25-
protected $storeManagerMock;
26-
27-
/**
28-
* @var \PHPUnit_Framework_MockObject_MockObject
29-
*/
30-
protected $storeMock;
20+
protected $quoteMock;
3121

3222
/**
3323
* @var \PHPUnit_Framework_MockObject_MockObject
3424
*/
35-
protected $quoteMock;
25+
protected $quoteRepositoryMock;
3626

3727
/**
3828
* @var \PHPUnit_Framework_MockObject_MockObject
3929
*/
40-
protected $searchResultsDataFactory;
30+
protected $quoteIdMaskFactoryMock;
4131

4232
/**
4333
* @var \PHPUnit_Framework_MockObject_MockObject
4434
*/
45-
protected $quoteCollectionMock;
35+
protected $quoteIdMaskMock;
4636

4737
/**
48-
* @var \PHPUnit_Framework_MockObject_MockObject
38+
* @var string
4939
*/
50-
protected $quoteIdMaskFactoryMock;
40+
protected $maskedCartId;
5141

5242
/**
53-
* @var \PHPUnit_Framework_MockObject_MockObject
43+
* @var int
5444
*/
55-
protected $quoteIdMaskMock;
45+
protected $cartId;
5646

5747
protected function setUp()
5848
{
5949
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
50+
$this->quoteRepositoryMock = $this->getMock( 'Magento\Quote\Model\QuoteRepository', [], [], '', false);
51+
$this->quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false);
6052

61-
$this->quoteFactoryMock = $this->getMock('Magento\Quote\Model\QuoteFactory', ['create'], [], '', false);
62-
$this->storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface');
63-
$this->quoteMock = $this->getMock(
64-
'Magento\Quote\Model\Quote',
65-
['load', 'getId', 'save', 'delete', 'getCustomerId'],
66-
[],
67-
'',
68-
false
69-
);
70-
$this->storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
71-
$this->searchResultsDataFactory = $this->getMock(
72-
'Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory',
73-
['create'],
74-
[],
75-
'',
76-
false
77-
);
53+
$this->maskedCartId = 'f216207248d65c789b17be8545e0aa73';
54+
$this->cartId = 123;
7855

79-
$this->quoteCollectionMock = $this->getMock('Magento\Quote\Model\Resource\Quote\Collection', [], [], '', false);
80-
$this->quoteIdMaskFactoryMock = $this->getMock('Magento\Quote\Model\QuoteIdMaskFactory', [], [], '', false);
81-
$this->quoteIdMaskMock = $this->getMock('Magento\Quote\Model\QuoteIdMask', [], [], '', false);
56+
$guestCartTestHelper = new GuestCartTestHelper($this);
57+
list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask(
58+
$this->maskedCartId,
59+
$this->cartId
60+
);
8261

8362
$this->model = $objectManager->getObject(
8463
'Magento\Quote\Model\GuestCart\GuestCartRepository',
8564
[
86-
'quoteFactory' => $this->quoteFactoryMock,
87-
'storeManager' => $this->storeManagerMock,
88-
'searchResultsDataFactory' => $this->searchResultsDataFactory,
89-
'quoteCollection' => $this->quoteCollectionMock,
65+
'quoteRepository' => $this->quoteRepositoryMock,
9066
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock
9167
]
9268
);
9369
}
9470

9571
public function testGet()
9672
{
97-
$maskedCartId = 'f216207248d65c789b17be8545e0aa73';
98-
$cartId = 15;
99-
100-
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
101-
$this->quoteIdMaskMock->expects($this->once())
102-
->method('load')
103-
->with($maskedCartId, 'masked_id')
104-
->willReturn($this->quoteIdMaskMock);
105-
$this->quoteIdMaskMock->expects($this->once())
106-
->method('getId')
107-
->willReturn($cartId);
108-
109-
$this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
110-
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
111-
$this->storeMock->expects($this->once())->method('getId')->willReturn($this->storeMock);
112-
$this->quoteMock->expects($this->never())->method('setSharedStoreIds');
113-
$this->quoteMock->expects($this->once())
114-
->method('load')
115-
->with($cartId)
116-
->willReturn($this->storeMock);
117-
$this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId);
118-
119-
$this->assertEquals($this->quoteMock, $this->model->get($maskedCartId));
120-
}
121-
122-
public function testSaveEdited()
123-
{
124-
$maskedCartId = 'f216207248d65c789b17be8545e0aa73';
125-
$cartId = 1;
126-
127-
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
128-
$this->quoteIdMaskMock->expects($this->once())
129-
->method('load')
130-
->with($maskedCartId, 'masked_id')
131-
->willReturn($this->quoteIdMaskMock);
132-
$this->quoteIdMaskMock->expects($this->once())->method('getId')->willReturn($cartId);
133-
134-
135-
$this->quoteMock->expects($this->once())
136-
->method('save');
137-
$this->quoteMock->expects($this->exactly(3))->method('getId')->willReturn($maskedCartId);
138-
$this->quoteMock->expects($this->exactly(1))->method('getCustomerId')->willReturn(2);
139-
140-
$this->model->save($this->quoteMock);
141-
}
142-
143-
public function testSaveNew()
144-
{
145-
$cartId = 1;
146-
147-
$this->quoteIdMaskFactoryMock->expects($this->never())->method('create');
148-
$this->quoteMock->expects($this->at(0))->method('getId')->willReturn(false);
149-
150-
$this->quoteMock->expects($this->once())
151-
->method('save');
152-
$this->quoteMock->expects($this->at(1))->method('getId')->willReturn($cartId);
153-
$this->quoteMock->expects($this->exactly(1))->method('getCustomerId')->willReturn(2);
154-
155-
$this->model->save($this->quoteMock);
156-
}
157-
158-
public function testDelete()
159-
{
160-
$maskedCartId = 'f216207248d65c789b17be8545e0aa73';
161-
$cartId = 1;
162-
163-
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
164-
$this->quoteIdMaskMock->expects($this->once())
165-
->method('load')
166-
->with($maskedCartId, 'masked_id')
167-
->willReturn($this->quoteIdMaskMock);
168-
$this->quoteIdMaskMock->expects($this->once())->method('getId')->willReturn($cartId);
169-
170-
$this->quoteMock->expects($this->once())
171-
->method('delete');
172-
$this->quoteMock->expects($this->exactly(3))->method('getId')->willReturn($maskedCartId);
173-
$this->quoteMock->expects($this->exactly(1))->method('getCustomerId')->willReturn(2);
174-
175-
$this->model->delete($this->quoteMock);
73+
$this->quoteRepositoryMock->expects($this->once())->method('get')->willReturn($this->quoteMock);
74+
$this->assertEquals($this->quoteMock, $this->model->get($this->maskedCartId));
17675
}
17776
}

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,19 @@ public function __construct(\PHPUnit_Framework_TestCase $testCase)
3030
/**
3131
* Return mocks with expected invokes
3232
*
33+
* First element is quoteIdMaskFactoryMock, second one is quoteIdMaskMock
34+
*
3335
* @param $maskedCartId
3436
* @param $cartId
3537
* @return array
3638
*/
37-
public function mockQuoteIdMask(
38-
$maskedCartId,
39-
$cartId
40-
) {
39+
public function mockQuoteIdMask( $maskedCartId, $cartId)
40+
{
4141
$quoteIdMaskMock = $this->testCase->getMock('Magento\Quote\Model\QuoteIdMask', [], [], '', false);
4242
$quoteIdMaskFactoryMock = $this->testCase->getMock('Magento\Quote\Model\QuoteIdMaskFactory', [], [], '', false);
43-
44-
$quoteIdMaskFactoryMock->expects($this->testCase->once())->method('create')->willReturn(
45-
$quoteIdMaskMock
46-
);
47-
$quoteIdMaskMock->expects($this->testCase->once())
48-
->method('load')
49-
->with($maskedCartId, 'masked_id')
50-
->willReturn($quoteIdMaskMock);
51-
$quoteIdMaskMock->expects($this->testCase->once())
52-
->method('getId')
53-
->willReturn($cartId);
54-
55-
return [$quoteIdMaskFactoryMock,$quoteIdMaskMock];
43+
$quoteIdMaskFactoryMock->expects($this->testCase->once())->method('create')->willReturn($quoteIdMaskMock);
44+
$quoteIdMaskMock->expects($this->testCase->once())->method('load')->with($maskedCartId)->willReturnSelf();
45+
$quoteIdMaskMock->expects($this->testCase->once())->method('getId')->willReturn($cartId);
46+
return [$quoteIdMaskFactoryMock, $quoteIdMaskMock];
5647
}
5748
}

0 commit comments

Comments
 (0)