Skip to content

Commit 790182c

Browse files
author
Bryant Luk
committed
MAGETWO-36427: Magento\Quote\Api\GuestShippingMethodManagement
- Use composition instead of inheritance for GuestShippingMethodManagement - Add unit tests for GuestShippingMethodManagement
1 parent 1f7b5d8 commit 790182c

File tree

2 files changed

+127
-19
lines changed

2 files changed

+127
-19
lines changed

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,39 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
// @codingStandardsIgnoreFile
8-
97
namespace Magento\Quote\Model\GuestCart;
108

119
use Magento\Quote\Api\GuestShippingMethodManagementInterface;
12-
use Magento\Quote\Model\Cart\ShippingMethodConverter;
10+
use Magento\Quote\Api\ShippingMethodManagementInterface;
1311
use Magento\Quote\Model\QuoteIdMask;
1412
use Magento\Quote\Model\QuoteIdMaskFactory;
15-
use Magento\Quote\Model\QuoteRepository;
16-
use Magento\Quote\Model\ShippingMethodManagement;
1713

1814
/**
1915
* Shipping method management class for guest carts.
2016
*/
21-
class GuestShippingMethodManagement extends ShippingMethodManagement implements GuestShippingMethodManagementInterface
17+
class GuestShippingMethodManagement implements GuestShippingMethodManagementInterface
2218
{
19+
/**
20+
* @var ShippingMethodManagementInterface
21+
*/
22+
private $shippingMethodManagement;
23+
2324
/**
2425
* @var QuoteIdMaskFactory
2526
*/
26-
protected $quoteIdMaskFactory;
27+
private $quoteIdMaskFactory;
2728

2829
/**
2930
* Constructs a shipping method read service object.
3031
*
31-
* @param QuoteRepository $quoteRepository Quote repository.
32-
* @param \Magento\Quote\Api\Data\ShippingMethodInterfaceFactory $methodDataFactory Shipping method factory.
33-
* @param ShippingMethodConverter $converter Shipping method converter.
32+
* @param ShippingMethodManagementInterface $shippingMethodManagement
3433
* @param QuoteIdMaskFactory $quoteIdMaskFactory
3534
*/
3635
public function __construct(
37-
QuoteRepository $quoteRepository,
38-
\Magento\Quote\Api\Data\ShippingMethodInterfaceFactory $methodDataFactory,
39-
ShippingMethodConverter $converter,
36+
ShippingMethodManagementInterface $shippingMethodManagement,
4037
QuoteIdMaskFactory $quoteIdMaskFactory
4138
) {
42-
$this->quoteRepository = $quoteRepository;
43-
$this->methodDataFactory = $methodDataFactory;
44-
$this->converter = $converter;
39+
$this->shippingMethodManagement = $shippingMethodManagement;
4540
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
4641
}
4742

@@ -52,7 +47,7 @@ public function get($cartId)
5247
{
5348
/** @var $quoteIdMask QuoteIdMask */
5449
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
55-
return parent::get($quoteIdMask->getId());
50+
return $this->shippingMethodManagement->get($quoteIdMask->getId());
5651
}
5752

5853
/**
@@ -62,7 +57,7 @@ public function getList($cartId)
6257
{
6358
/** @var $quoteIdMask QuoteIdMask */
6459
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
65-
return parent::getList($quoteIdMask->getId());
60+
return $this->shippingMethodManagement->getList($quoteIdMask->getId());
6661
}
6762

6863
/**
@@ -72,6 +67,6 @@ public function set($cartId, $carrierCode, $methodCode)
7267
{
7368
/** @var $quoteIdMask QuoteIdMask */
7469
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
75-
return parent::set($quoteIdMask->getId(), $carrierCode, $methodCode);
70+
return $this->shippingMethodManagement->set($quoteIdMask->getId(), $carrierCode, $methodCode);
7671
}
7772
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
namespace Magento\Quote\Test\Unit\Model\GuestCart;
9+
10+
class GuestShippingMethodManagementTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @var \Magento\Quote\Api\GuestShippingMethodManagementInterface
14+
*/
15+
private $model;
16+
17+
/**
18+
* @var \PHPUnit_Framework_MockObject_MockObject
19+
*/
20+
private $shippingMethodManagementMock;
21+
22+
/**
23+
* @var \PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $quoteIdMaskFactoryMock;
26+
27+
/**
28+
* @var \PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $quoteIdMaskMock;
31+
32+
/**
33+
* @var string
34+
*/
35+
private $maskedCartId;
36+
37+
/**
38+
* @var string
39+
*/
40+
private $cartId;
41+
42+
protected function setUp()
43+
{
44+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
45+
46+
$this->shippingMethodManagementMock =
47+
$this->getMockBuilder('Magento\Quote\Api\ShippingMethodManagementInterface')
48+
->getMockForAbstractClass();
49+
$this->quoteIdMaskFactoryMock = $this->getMockBuilder('Magento\Quote\Model\QuoteIdMaskFactory')
50+
->disableOriginalConstructor()
51+
->getMock();
52+
$this->quoteIdMaskMock = $this->getMockBuilder('Magento\Quote\Model\QuoteIdMask')
53+
->disableOriginalConstructor()
54+
->getMock();
55+
$this->model = $objectManager->getObject(
56+
'Magento\Quote\Model\GuestCart\GuestShippingMethodManagement',
57+
[
58+
'shippingMethodManagement' => $this->shippingMethodManagementMock,
59+
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
60+
]
61+
);
62+
63+
$this->maskedCartId = 'f216207248d65c789b17be8545e0aa73';
64+
$this->cartId = 867;
65+
66+
$this->quoteIdMaskFactoryMock->expects($this->once())
67+
->method('create')
68+
->willReturn($this->quoteIdMaskMock);
69+
$this->quoteIdMaskMock->expects($this->once())
70+
->method('load')
71+
->with($this->maskedCartId, 'masked_id')
72+
->willReturn($this->quoteIdMaskMock);
73+
$this->quoteIdMaskMock->expects($this->once())
74+
->method('getId')
75+
->willReturn($this->cartId);
76+
}
77+
78+
public function testSet()
79+
{
80+
$carrierCode = 'carrierCode';
81+
$methodCode = 'methodCode';
82+
83+
$retValue = 'retValue';
84+
$this->shippingMethodManagementMock->expects($this->once())
85+
->method('set')
86+
->with($this->cartId, $carrierCode, $methodCode)
87+
->will($this->returnValue($retValue));
88+
89+
$this->assertEquals($retValue, $this->model->set($this->maskedCartId, $carrierCode, $methodCode));
90+
}
91+
92+
public function testGetList()
93+
{
94+
$retValue = 'retValue';
95+
$this->shippingMethodManagementMock->expects($this->once())
96+
->method('getList')
97+
->with($this->cartId)
98+
->will($this->returnValue($retValue));
99+
100+
$this->assertEquals($retValue, $this->model->getList($this->maskedCartId));
101+
}
102+
103+
public function testGet()
104+
{
105+
$retValue = 'retValue';
106+
$this->shippingMethodManagementMock->expects($this->once())
107+
->method('get')
108+
->with($this->cartId)
109+
->will($this->returnValue($retValue));
110+
111+
$this->assertEquals($retValue, $this->model->get($this->maskedCartId));
112+
}
113+
}

0 commit comments

Comments
 (0)