Skip to content

Commit 5497e9b

Browse files
committed
Merge remote-tracking branch 'tango/MC-19432' into Chaika-PR-2019-08-29
2 parents 17548f1 + 8c4bcf6 commit 5497e9b

File tree

2 files changed

+82
-81
lines changed

2 files changed

+82
-81
lines changed
Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76

87
namespace Magento\Quote\Model\Quote\Item;
98

10-
use Magento\Framework\App\ObjectManager;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
1110
use Magento\Framework\Exception\CouldNotSaveException;
11+
use Magento\Framework\Exception\InputException;
1212
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Quote\Api\CartItemRepositoryInterface;
14+
use Magento\Quote\Api\CartRepositoryInterface;
15+
use Magento\Quote\Api\Data\CartItemInterfaceFactory;
1316

14-
class Repository implements \Magento\Quote\Api\CartItemRepositoryInterface
17+
/**
18+
* Repository for quote item.
19+
*/
20+
class Repository implements CartItemRepositoryInterface
1521
{
1622
/**
1723
* Quote repository.
1824
*
19-
* @var \Magento\Quote\Api\CartRepositoryInterface
25+
* @var CartRepositoryInterface
2026
*/
2127
protected $quoteRepository;
2228

2329
/**
2430
* Product repository.
2531
*
26-
* @var \Magento\Catalog\Api\ProductRepositoryInterface
32+
* @var ProductRepositoryInterface
2733
*/
2834
protected $productRepository;
2935

3036
/**
31-
* @var \Magento\Quote\Api\Data\CartItemInterfaceFactory
37+
* @var CartItemInterfaceFactory
3238
*/
3339
protected $itemDataFactory;
3440

@@ -43,25 +49,28 @@ class Repository implements \Magento\Quote\Api\CartItemRepositoryInterface
4349
private $cartItemOptionsProcessor;
4450

4551
/**
46-
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
47-
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
48-
* @param \Magento\Quote\Api\Data\CartItemInterfaceFactory $itemDataFactory
52+
* @param CartRepositoryInterface $quoteRepository
53+
* @param ProductRepositoryInterface $productRepository
54+
* @param CartItemInterfaceFactory $itemDataFactory
55+
* @param CartItemOptionsProcessor $cartItemOptionsProcessor
4956
* @param CartItemProcessorInterface[] $cartItemProcessors
5057
*/
5158
public function __construct(
52-
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
53-
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
54-
\Magento\Quote\Api\Data\CartItemInterfaceFactory $itemDataFactory,
59+
CartRepositoryInterface $quoteRepository,
60+
ProductRepositoryInterface $productRepository,
61+
CartItemInterfaceFactory $itemDataFactory,
62+
CartItemOptionsProcessor $cartItemOptionsProcessor,
5563
array $cartItemProcessors = []
5664
) {
5765
$this->quoteRepository = $quoteRepository;
5866
$this->productRepository = $productRepository;
5967
$this->itemDataFactory = $itemDataFactory;
68+
$this->cartItemOptionsProcessor = $cartItemOptionsProcessor;
6069
$this->cartItemProcessors = $cartItemProcessors;
6170
}
6271

6372
/**
64-
* {@inheritdoc}
73+
* @inheritdoc
6574
*/
6675
public function getList($cartId)
6776
{
@@ -71,21 +80,26 @@ public function getList($cartId)
7180

7281
/** @var \Magento\Quote\Model\Quote\Item $item */
7382
foreach ($quote->getAllVisibleItems() as $item) {
74-
$item = $this->getCartItemOptionsProcessor()->addProductOptions($item->getProductType(), $item);
75-
$output[] = $this->getCartItemOptionsProcessor()->applyCustomOptions($item);
83+
$item = $this->cartItemOptionsProcessor->addProductOptions($item->getProductType(), $item);
84+
$output[] = $this->cartItemOptionsProcessor->applyCustomOptions($item);
7685
}
7786
return $output;
7887
}
7988

8089
/**
81-
* {@inheritdoc}
90+
* @inheritdoc
8291
*/
8392
public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
8493
{
8594
/** @var \Magento\Quote\Model\Quote $quote */
8695
$cartId = $cartItem->getQuoteId();
87-
$quote = $this->quoteRepository->getActive($cartId);
96+
if (!$cartId) {
97+
throw new InputException(
98+
__('"%fieldName" is required. Enter and try again.', ['fieldName' => 'quoteId'])
99+
);
100+
}
88101

102+
$quote = $this->quoteRepository->getActive($cartId);
89103
$quoteItems = $quote->getItems();
90104
$quoteItems[] = $cartItem;
91105
$quote->setItems($quoteItems);
@@ -95,7 +109,7 @@ public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
95109
}
96110

97111
/**
98-
* {@inheritdoc}
112+
* @inheritdoc
99113
*/
100114
public function deleteById($cartId, $itemId)
101115
{
@@ -116,17 +130,4 @@ public function deleteById($cartId, $itemId)
116130

117131
return true;
118132
}
119-
120-
/**
121-
* @return CartItemOptionsProcessor
122-
* @deprecated 100.1.0
123-
*/
124-
private function getCartItemOptionsProcessor()
125-
{
126-
if (!$this->cartItemOptionsProcessor instanceof CartItemOptionsProcessor) {
127-
$this->cartItemOptionsProcessor = ObjectManager::getInstance()->get(CartItemOptionsProcessor::class);
128-
}
129-
130-
return $this->cartItemOptionsProcessor;
131-
}
132133
}

app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,75 @@
77

88
namespace Magento\Quote\Test\Unit\Model\Quote\Item;
99

10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\CustomOptions\CustomOptionProcessor;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\Quote\Api\Data\CartItemInterfaceFactory;
15+
use Magento\Quote\Model\Quote;
16+
use Magento\Quote\Model\Quote\Address;
17+
use Magento\Quote\Model\Quote\Item;
18+
use Magento\Quote\Model\Quote\Item\CartItemOptionsProcessor;
19+
use Magento\Quote\Model\Quote\Item\Repository;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
1022
/**
1123
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1224
*/
1325
class RepositoryTest extends \PHPUnit\Framework\TestCase
1426
{
1527
/**
16-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
28+
* @var Repository
1729
*/
18-
private $objectManager;
30+
private $repository;
1931

2032
/**
21-
* @var \Magento\Quote\Api\CartItemRepositoryInterface
33+
* @var CartRepositoryInterface|MockObject
2234
*/
23-
protected $repository;
35+
private $quoteRepositoryMock;
2436

2537
/**
26-
* @var \PHPUnit_Framework_MockObject_MockObject
38+
* @var ProductRepositoryInterface|MockObject
2739
*/
28-
protected $quoteRepositoryMock;
40+
private $productRepositoryMock;
2941

3042
/**
31-
* @var \PHPUnit_Framework_MockObject_MockObject
43+
* @var MockObject
3244
*/
33-
protected $productRepositoryMock;
45+
private $itemMock;
3446

3547
/**
36-
* @var \PHPUnit_Framework_MockObject_MockObject
48+
* @var MockObject
3749
*/
38-
protected $itemMock;
50+
private $quoteMock;
3951

4052
/**
41-
* @var \PHPUnit_Framework_MockObject_MockObject
53+
* @var MockObject
4254
*/
43-
protected $quoteMock;
55+
private $productMock;
4456

4557
/**
46-
* @var \PHPUnit_Framework_MockObject_MockObject
58+
* @var MockObject
4759
*/
48-
protected $productMock;
60+
private $quoteItemMock;
4961

5062
/**
51-
* @var \PHPUnit_Framework_MockObject_MockObject
63+
* @var CartItemInterfaceFactory|MockObject
5264
*/
53-
protected $quoteItemMock;
65+
private $itemDataFactoryMock;
5466

5567
/**
56-
* @var \PHPUnit_Framework_MockObject_MockObject
68+
* @var CustomOptionProcessor|MockObject
5769
*/
58-
protected $itemDataFactoryMock;
59-
60-
/** @var \Magento\Catalog\Model\CustomOptions\CustomOptionProcessor|\PHPUnit_Framework_MockObject_MockObject */
61-
protected $customOptionProcessor;
70+
private $customOptionProcessor;
6271

63-
/** @var \PHPUnit_Framework_MockObject_MockObject */
64-
protected $shippingAddressMock;
72+
/**
73+
* @var Address|MockObject
74+
*/
75+
private $shippingAddressMock;
6576

6677
/**
67-
* @var \PHPUnit_Framework_MockObject_MockObject
78+
* @var CartItemOptionsProcessor|MockObject
6879
*/
6980
private $optionsProcessorMock;
7081

@@ -73,40 +84,29 @@ class RepositoryTest extends \PHPUnit\Framework\TestCase
7384
*/
7485
protected function setUp()
7586
{
76-
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
77-
$this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
78-
$this->productRepositoryMock = $this->createMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);
79-
$this->itemDataFactoryMock =
80-
$this->createPartialMock(\Magento\Quote\Api\Data\CartItemInterfaceFactory::class, ['create']);
81-
$this->itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
82-
$this->quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
83-
$this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
87+
$this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class);
88+
$this->productRepositoryMock = $this->createMock(ProductRepositoryInterface::class);
89+
$this->itemDataFactoryMock = $this->createPartialMock(CartItemInterfaceFactory::class, ['create']);
90+
$this->itemMock = $this->createMock(Item::class);
91+
$this->quoteMock = $this->createMock(Quote::class);
92+
$this->productMock = $this->createMock(Product::class);
8493
$methods = ['getId', 'getSku', 'getQty', 'setData', '__wakeUp', 'getProduct', 'addProduct'];
8594
$this->quoteItemMock =
86-
$this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, $methods);
87-
$this->customOptionProcessor = $this->createMock(
88-
\Magento\Catalog\Model\CustomOptions\CustomOptionProcessor::class
89-
);
95+
$this->createPartialMock(Item::class, $methods);
96+
$this->customOptionProcessor = $this->createMock(CustomOptionProcessor::class);
9097
$this->shippingAddressMock = $this->createPartialMock(
91-
\Magento\Quote\Model\Quote\Address::class,
98+
Address::class,
9299
['setCollectShippingRates']
93100
);
101+
$this->optionsProcessorMock = $this->createMock(CartItemOptionsProcessor::class);
94102

95-
$this->optionsProcessorMock = $this->createMock(
96-
\Magento\Quote\Model\Quote\Item\CartItemOptionsProcessor::class
97-
);
98-
99-
$this->repository = new \Magento\Quote\Model\Quote\Item\Repository(
103+
$this->repository = new Repository(
100104
$this->quoteRepositoryMock,
101105
$this->productRepositoryMock,
102106
$this->itemDataFactoryMock,
107+
$this->optionsProcessorMock,
103108
['custom_options' => $this->customOptionProcessor]
104109
);
105-
$this->objectManager->setBackwardCompatibleProperty(
106-
$this->repository,
107-
'cartItemOptionsProcessor',
108-
$this->optionsProcessorMock
109-
);
110110
}
111111

112112
/**
@@ -118,7 +118,7 @@ public function testSave()
118118
$itemId = 20;
119119

120120
$quoteMock = $this->createPartialMock(
121-
\Magento\Quote\Model\Quote::class,
121+
Quote::class,
122122
['getItems', 'setItems', 'collectTotals', 'getLastAddedItem']
123123
);
124124

@@ -197,11 +197,11 @@ public function testDeleteWithCouldNotSaveException()
197197
public function testGetList()
198198
{
199199
$productType = 'type';
200-
$quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
200+
$quoteMock = $this->createMock(Quote::class);
201201
$this->quoteRepositoryMock->expects($this->once())->method('getActive')
202202
->with(33)
203203
->will($this->returnValue($quoteMock));
204-
$itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
204+
$itemMock = $this->createMock(Item::class);
205205
$quoteMock->expects($this->once())->method('getAllVisibleItems')->will($this->returnValue([$itemMock]));
206206
$itemMock->expects($this->once())->method('getProductType')->willReturn($productType);
207207

0 commit comments

Comments
 (0)