Skip to content

Commit 98c56cf

Browse files
committed
MAGETWO-36272: Create /mine API for PaymentMethodManagement
- replaced inheritance with composition
1 parent 9a6b9e1 commit 98c56cf

File tree

2 files changed

+49
-154
lines changed

2 files changed

+49
-154
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,30 @@
1313
/**
1414
* Payment method management class for guest carts.
1515
*/
16-
class GuestPaymentMethodManagement extends PaymentMethodManagement implements GuestPaymentMethodManagementInterface
16+
class GuestPaymentMethodManagement implements GuestPaymentMethodManagementInterface
1717
{
1818
/**
1919
* @var QuoteIdMaskFactory
2020
*/
2121
protected $quoteIdMaskFactory;
2222

23+
/**
24+
* @var PaymentMethodManagement
25+
*/
26+
protected $paymentMethodManagement;
27+
2328
/**
2429
* Initialize dependencies.
2530
*
26-
* @param \Magento\Quote\Model\QuoteRepository $quoteRepository
27-
* @param \Magento\Payment\Model\Checks\ZeroTotal $zeroTotalValidator
28-
* @param \Magento\Payment\Model\MethodList $methodList
31+
* @param PaymentMethodManagement $paymentMethodManagement
2932
* @param QuoteIdMaskFactory $quoteIdMaskFactory
3033
*/
3134
public function __construct(
32-
\Magento\Quote\Model\QuoteRepository $quoteRepository,
33-
\Magento\Payment\Model\Checks\ZeroTotal $zeroTotalValidator,
34-
\Magento\Payment\Model\MethodList $methodList,
35+
PaymentMethodManagement $paymentMethodManagement,
3536
QuoteIdMaskFactory $quoteIdMaskFactory
3637
) {
37-
parent::__construct($quoteRepository, $zeroTotalValidator, $methodList);
3838
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
39+
$this->paymentMethodManagement = $paymentMethodManagement;
3940
}
4041

4142
/**
@@ -45,7 +46,7 @@ public function set($cartId, \Magento\Quote\Api\Data\PaymentInterface $method)
4546
{
4647
/** @var $quoteIdMask QuoteIdMask */
4748
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
48-
return parent::set($quoteIdMask->getId(), $method);
49+
return $this->paymentMethodManagement->set($quoteIdMask->getId(), $method);
4950
}
5051

5152
/**
@@ -55,7 +56,7 @@ public function get($cartId)
5556
{
5657
/** @var $quoteIdMask QuoteIdMask */
5758
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
58-
return parent::get($quoteIdMask->getId());
59+
return $this->paymentMethodManagement->get($quoteIdMask->getId());
5960
}
6061

6162
/**
@@ -65,6 +66,6 @@ public function getList($cartId)
6566
{
6667
/** @var $quoteIdMask QuoteIdMask */
6768
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
68-
return parent::getList($quoteIdMask->getId());
69+
return $this->paymentMethodManagement->getList($quoteIdMask->getId());
6970
}
7071
}

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

Lines changed: 37 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -13,188 +13,82 @@ class GuestPaymentMethodManagementTest extends \PHPUnit_Framework_TestCase
1313
protected $model;
1414

1515
/**
16-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
16+
* @var \PHPUnit_Framework_MockObject_MockObject
1717
*/
18-
protected $objectManager;
18+
protected $quoteIdMaskFactoryMock;
1919

2020
/**
2121
* @var \PHPUnit_Framework_MockObject_MockObject
2222
*/
23-
protected $quoteRepositoryMock;
23+
protected $quoteIdMaskMock;
2424

2525
/**
2626
* @var \PHPUnit_Framework_MockObject_MockObject
2727
*/
28-
protected $methodListMock;
28+
protected $paymentMethodManagementMock;
2929

3030
/**
3131
* @var \PHPUnit_Framework_MockObject_MockObject
3232
*/
33-
protected $zeroTotalMock;
33+
protected $paymentMock;
3434

3535
/**
36-
* @var \PHPUnit_Framework_MockObject_MockObject
36+
* @var string
3737
*/
38-
protected $quoteIdMaskFactoryMock;
38+
protected $maskedCartId;
3939

4040
/**
41-
* @var \PHPUnit_Framework_MockObject_MockObject
41+
* @var int
4242
*/
43-
protected $quoteIdMaskMock;
43+
protected $cartId;
4444

4545
protected function setUp()
4646
{
47-
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
48-
$this->quoteRepositoryMock = $this->getMock('Magento\Quote\Model\QuoteRepository', [], [], '', false);
49-
$this->methodListMock = $this->getMock('Magento\Payment\Model\MethodList', [], [], '', false);
50-
$this->zeroTotalMock = $this->getMock('Magento\Payment\Model\Checks\ZeroTotal', [], [], '', false);
51-
$this->quoteIdMaskFactoryMock = $this->getMock('Magento\Quote\Model\QuoteIdMaskFactory', [], [], '', false);
52-
$this->quoteIdMaskMock = $this->getMock('Magento\Quote\Model\QuoteIdMask', [], [], '', false);
47+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
48+
$this->paymentMethodManagementMock = $this->getMock(
49+
'Magento\Quote\Model\PaymentMethodManagement',
50+
[],
51+
[],
52+
'',
53+
false
54+
);
55+
$this->paymentMock = $this->getMock('Magento\Quote\Model\Quote\Payment', [], [], '', false);
56+
57+
$this->maskedCartId = 'f216207248d65c789b17be8545e0aa73';
58+
$this->cartId = 11;
59+
60+
$guestCartTestHelper = new GuestCartTestHelper($this);
61+
list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask(
62+
$this->maskedCartId,
63+
$this->cartId
64+
);
5365

54-
$this->model = $this->objectManager->getObject(
66+
$this->model = $objectManager->getObject(
5567
'Magento\Quote\Model\GuestCart\GuestPaymentMethodManagement',
5668
[
57-
'quoteRepository' => $this->quoteRepositoryMock,
58-
'methodList' => $this->methodListMock,
59-
'zeroTotalValidator' => $this->zeroTotalMock,
69+
'paymentMethodManagement' => $this->paymentMethodManagementMock,
6070
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock
6171
]
6272
);
6373
}
6474

65-
public function testGetPaymentSuccess()
75+
public function testGet()
6676
{
67-
$maskedCartId = 'f216207248d65c789b17be8545e0aa73';
68-
$cartId = 11;
69-
70-
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
71-
$this->quoteIdMaskMock->expects($this->once())
72-
->method('load')
73-
->with($maskedCartId, 'masked_id')
74-
->willReturn($this->quoteIdMaskMock);
75-
$this->quoteIdMaskMock->expects($this->once())
76-
->method('getId')
77-
->willReturn($cartId);
78-
79-
$paymentMock = $this->getMock('Magento\Quote\Model\Quote\Payment', [], [], '', false);
80-
$paymentMock->expects($this->once())->method('getId')->will($this->returnValue(1));
81-
82-
$quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false);
83-
$quoteMock->expects($this->once())->method('getPayment')->will($this->returnValue($paymentMock));
84-
85-
$this->quoteRepositoryMock->expects($this->once())
86-
->method('getActive')
87-
->with($cartId)
88-
->will($this->returnValue($quoteMock));
89-
$this->assertEquals($paymentMock, $this->model->get($maskedCartId));
77+
$this->paymentMethodManagementMock->expects($this->once())->method('get')->willReturn($this->paymentMock);
78+
$this->assertEquals($this->paymentMock, $this->model->get($this->maskedCartId));
9079
}
9180

9281
public function testGetList()
9382
{
94-
$maskedCartId = 'f216207248d65c789b17be8545e0aa73';
95-
$cartId = 10;
96-
97-
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
98-
$this->quoteIdMaskMock->expects($this->once())
99-
->method('load')
100-
->with($maskedCartId, 'masked_id')
101-
->willReturn($this->quoteIdMaskMock);
102-
$this->quoteIdMaskMock->expects($this->once())
103-
->method('getId')
104-
->willReturn($cartId);
105-
106-
$quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false);
107-
$this->quoteRepositoryMock->expects($this->once())
108-
->method('getActive')
109-
->with($cartId)
110-
->will($this->returnValue($quoteMock));
111-
112-
$paymentMethod = $this->getMock('Magento\Quote\Api\Data\PaymentMethodInterface');
113-
$this->methodListMock->expects($this->once())
114-
->method('getAvailableMethods')
115-
->with($quoteMock)
116-
->will($this->returnValue([$paymentMethod]));
117-
$this->assertEquals([$paymentMethod], $this->model->getList($maskedCartId));
83+
$paymentMethod = $this->getMock('Magento\Quote\Api\Data\PaymentMethodInterface', [], [], '', false);
84+
$this->paymentMethodManagementMock->expects($this->once())->method('getList')->willReturn([$paymentMethod]);
85+
$this->assertEquals([$paymentMethod], $this->model->getList($this->maskedCartId));
11886
}
11987

12088
public function testSetSimpleProduct()
12189
{
122-
$maskedCartId = 'f216207248d65c789b17be8545e0aa73';
123-
$cartId = 100;
12490
$paymentId = 20;
125-
$methodData = ['method' => 'data'];
126-
$paymentMethod = 'checkmo';
127-
128-
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
129-
$this->quoteIdMaskMock->expects($this->once())
130-
->method('load')
131-
->with($maskedCartId, 'masked_id')
132-
->willReturn($this->quoteIdMaskMock);
133-
$this->quoteIdMaskMock->expects($this->once())
134-
->method('getId')
135-
->willReturn($cartId);
136-
137-
$quoteMock = $this->getMock(
138-
'\Magento\Quote\Model\Quote',
139-
['getPayment', 'isVirtual', 'getShippingAddress', 'setTotalsCollectedFlag', 'collectTotals', 'save'],
140-
[],
141-
'',
142-
false
143-
);
144-
$this->quoteRepositoryMock->expects($this->once())->method('getActive')->with($cartId)->willReturn($quoteMock);
145-
146-
$methodMock = $this->getMock('Magento\Quote\Model\Quote\Payment', ['setChecks', 'getData'], [], '', false);
147-
$methodMock->expects($this->once())
148-
->method('setChecks')
149-
->with([
150-
\Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,
151-
\Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
152-
\Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
153-
\Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
154-
])
155-
->willReturnSelf();
156-
$methodMock->expects($this->once())->method('getData')->willReturn($methodData);
157-
158-
$paymentMock = $this->getMock(
159-
'Magento\Quote\Model\Quote\Payment',
160-
['importData', 'getMethod', 'getMethodInstance', 'getId'],
161-
[],
162-
'',
163-
false
164-
);
165-
$paymentMock->expects($this->once())->method('importData')->with($methodData)->willReturnSelf();
166-
$paymentMock->expects($this->once())->method('getMethod')->willReturn($paymentMethod);
167-
168-
$shippingAddressMock = $this->getMock(
169-
'Magento\Quote\Model\Quote\Address',
170-
['getCountryId', 'setPaymentMethod'],
171-
[],
172-
'',
173-
false
174-
);
175-
$shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(100);
176-
$shippingAddressMock->expects($this->once())
177-
->method('setPaymentMethod')
178-
->with($paymentMethod)
179-
->willReturnSelf();
180-
181-
$quoteMock->expects($this->exactly(2))->method('getPayment')->willReturn($paymentMock);
182-
$quoteMock->expects($this->exactly(2))->method('isVirtual')->willReturn(false);
183-
$quoteMock->expects($this->exactly(4))->method('getShippingAddress')->willReturn($shippingAddressMock);
184-
185-
$methodInstance = $this->getMock('\Magento\Payment\Model\Checks\PaymentMethodChecksInterface');
186-
$paymentMock->expects($this->once())->method('getMethodInstance')->willReturn($methodInstance);
187-
188-
$this->zeroTotalMock->expects($this->once())
189-
->method('isApplicable')
190-
->with($methodInstance, $quoteMock)
191-
->willReturn(true);
192-
193-
$quoteMock->expects($this->once())->method('setTotalsCollectedFlag')->with(false)->willReturnSelf();
194-
$quoteMock->expects($this->once())->method('collectTotals')->willReturnSelf();
195-
$quoteMock->expects($this->once())->method('save')->willReturnSelf();
196-
197-
$paymentMock->expects($this->once())->method('getId')->willReturn($paymentId);
198-
$this->assertEquals($paymentId, $this->model->set($maskedCartId, $methodMock));
91+
$this->paymentMethodManagementMock->expects($this->once())->method('set')->willReturn($paymentId);
92+
$this->assertEquals($paymentId, $this->model->set($this->maskedCartId, $this->paymentMock));
19993
}
20094
}

0 commit comments

Comments
 (0)