Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 14e613b

Browse files
committed
Merge remote-tracking branch 'austin/MAGETWO-77966' into bugs
2 parents b745d6f + 1259516 commit 14e613b

File tree

7 files changed

+93
-44
lines changed

7 files changed

+93
-44
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Magento\Quote\Model\Quote\Address;
1515
use Magento\Quote\Model\Quote\Address\Total as AddressTotal;
1616
use Magento\Sales\Model\Status;
17+
use Magento\Framework\App\ObjectManager;
18+
use Magento\Sales\Model\OrderIncrementIdChecker;
1719

1820
/**
1921
* Quote model
@@ -353,6 +355,11 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C
353355
*/
354356
protected $shippingAddressesItems;
355357

358+
/**
359+
* @var \Magento\Sales\Model\OrderIncrementIdChecker
360+
*/
361+
private $orderIncrementIdChecker;
362+
356363
/**
357364
* @param \Magento\Framework\Model\Context $context
358365
* @param \Magento\Framework\Registry $registry
@@ -394,6 +401,7 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C
394401
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
395402
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
396403
* @param array $data
404+
* @param OrderIncrementIdChecker|null $orderIncrementIdChecker
397405
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
398406
*/
399407
public function __construct(
@@ -436,7 +444,8 @@ public function __construct(
436444
\Magento\Quote\Model\ShippingAssignmentFactory $shippingAssignmentFactory,
437445
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
438446
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
439-
array $data = []
447+
array $data = [],
448+
OrderIncrementIdChecker $orderIncrementIdChecker = null
440449
) {
441450
$this->quoteValidator = $quoteValidator;
442451
$this->_catalogProduct = $catalogProduct;
@@ -471,6 +480,8 @@ public function __construct(
471480
$this->totalsReader = $totalsReader;
472481
$this->shippingFactory = $shippingFactory;
473482
$this->shippingAssignmentFactory = $shippingAssignmentFactory;
483+
$this->orderIncrementIdChecker = $orderIncrementIdChecker ?: ObjectManager::getInstance()
484+
->get(OrderIncrementIdChecker::class);
474485
parent::__construct(
475486
$context,
476487
$registry,
@@ -2184,7 +2195,7 @@ public function reserveOrderId()
21842195
} else {
21852196
//checking if reserved order id was already used for some order
21862197
//if yes reserving new one if not using old one
2187-
if ($this->_getResource()->isOrderIncrementIdUsed($this->getReservedOrderId())) {
2198+
if ($this->orderIncrementIdChecker->isIncrementIdUsed($this->getReservedOrderId())) {
21882199
$this->setReservedOrderId($this->_getResource()->getReservedOrderId($this));
21892200
}
21902201
}

app/code/Magento/Quote/Model/ResourceModel/Quote.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -172,29 +172,6 @@ public function getReservedOrderId($quote)
172172
->getNextValue();
173173
}
174174

175-
/**
176-
* Check if order increment ID is already used.
177-
* Method can be used to avoid collisions of order IDs.
178-
*
179-
* @param int $orderIncrementId
180-
* @return bool
181-
*/
182-
public function isOrderIncrementIdUsed($orderIncrementId)
183-
{
184-
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $adapter */
185-
$adapter = $this->getConnection();
186-
$bind = [':increment_id' => $orderIncrementId];
187-
/** @var \Magento\Framework\DB\Select $select */
188-
$select = $adapter->select();
189-
$select->from($this->getTable('sales_order'), 'entity_id')->where('increment_id = :increment_id');
190-
$entity_id = $adapter->fetchOne($select, $bind);
191-
if ($entity_id > 0) {
192-
return true;
193-
}
194-
195-
return false;
196-
}
197-
198175
/**
199176
* Mark quotes - that depend on catalog price rules - to be recollected on demand
200177
*

app/code/Magento/Quote/Test/Unit/Model/QuoteTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ class QuoteTest extends \PHPUnit\Framework\TestCase
147147
*/
148148
private $itemProcessor;
149149

150+
/**
151+
* @var \PHPUnit_Framework_MockObject_MockObject
152+
*/
153+
private $orderIncrementIdChecker;
154+
150155
/**
151156
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
152157
*/
@@ -233,6 +238,9 @@ protected function setUp()
233238
->getMock();
234239
$this->extensionAttributesJoinProcessorMock = $this->createMock(\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class);
235240
$this->customerDataFactoryMock = $this->createPartialMock(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class, ['create']);
241+
$this->orderIncrementIdChecker = $this->getMockBuilder(\Magento\Sales\Model\OrderIncrementIdChecker::class)
242+
->disableOriginalConstructor()
243+
->getMock();
236244
$this->quote = (new ObjectManager($this))
237245
->getObject(
238246
\Magento\Quote\Model\Quote::class,
@@ -257,6 +265,7 @@ protected function setUp()
257265
'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessorMock,
258266
'customerDataFactory' => $this->customerDataFactoryMock,
259267
'itemProcessor' => $this->itemProcessor,
268+
'orderIncrementIdChecker' => $this->orderIncrementIdChecker,
260269
'data' => [
261270
'reserved_order_id' => 1000001
262271
]
@@ -1186,9 +1195,9 @@ public function testGetAllItems()
11861195
*/
11871196
public function testReserveOrderId($isReservedOrderIdExist, $reservedOrderId)
11881197
{
1189-
$this->resourceMock
1198+
$this->orderIncrementIdChecker
11901199
->expects($this->once())
1191-
->method('isOrderIncrementIdUsed')
1200+
->method('isIncrementIdUsed')
11921201
->with(1000001)->willReturn($isReservedOrderIdExist);
11931202
$this->resourceMock->expects($this->any())->method('getReservedOrderId')->willReturn($reservedOrderId);
11941203
$this->quote->reserveOrderId();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Sales\Model;
8+
9+
/**
10+
* This class uses for checking if reserved order id was already used for some order
11+
*/
12+
class OrderIncrementIdChecker
13+
{
14+
/**
15+
* @var \Magento\Sales\Model\ResourceModel\Order
16+
*/
17+
private $resourceModel;
18+
19+
/**
20+
* OrderIncrementIdChecker constructor.
21+
* @param ResourceModel\Order $resourceModel
22+
*/
23+
public function __construct(ResourceModel\Order $resourceModel)
24+
{
25+
$this->resourceModel = $resourceModel;
26+
}
27+
28+
/**
29+
* Check if order increment ID is already used.
30+
*
31+
* Method can be used to avoid collisions of order IDs.
32+
*
33+
* @param int $orderIncrementId
34+
* @return bool
35+
*/
36+
public function isIncrementIdUsed($orderIncrementId)
37+
{
38+
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $adapter */
39+
$adapter = $this->resourceModel->getConnection();
40+
$bind = [':increment_id' => $orderIncrementId];
41+
/** @var \Magento\Framework\DB\Select $select */
42+
$select = $adapter->select();
43+
$select->from($this->resourceModel->getMainTable(), 'entity_id')->where('increment_id = :increment_id');
44+
$entity_id = $adapter->fetchOne($select, $bind);
45+
if ($entity_id > 0) {
46+
return true;
47+
}
48+
49+
return false;
50+
}
51+
}

app/code/Magento/Quote/Test/Unit/Model/ResourceModel/QuoteTest.php renamed to app/code/Magento/Sales/Test/Unit/Model/OrderIncrementIdCheckerTest.php

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

7-
namespace Magento\Quote\Test\Unit\Model\ResourceModel;
7+
namespace Magento\Sales\Test\Unit\Model;
88

9-
class QuoteTest extends \PHPUnit\Framework\TestCase
9+
class OrderIncrementIdCheckerTest extends \PHPUnit\Framework\TestCase
1010
{
1111
/**
12-
* @var \Magento\Quote\Model\ResourceModel\Quote
12+
* @var \Magento\Sales\Model\OrderIncrementIdChecker
1313
*/
1414
private $model;
1515

@@ -42,7 +42,7 @@ protected function setUp()
4242
->getMock();
4343
$this->adapterMock->expects($this->any())->method('select')->will($this->returnValue($this->selectMock));
4444

45-
$this->resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
45+
$this->resourceMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order::class)
4646
->disableOriginalConstructor()
4747
->getMock();
4848
$this->resourceMock->expects(
@@ -54,9 +54,9 @@ protected function setUp()
5454
);
5555

5656
$this->model = $objectManagerHelper->getObject(
57-
\Magento\Quote\Model\ResourceModel\Quote::class,
57+
\Magento\Sales\Model\OrderIncrementIdChecker::class,
5858
[
59-
'resource' => $this->resourceMock
59+
'resourceModel' => $this->resourceMock
6060
]
6161
);
6262
}
@@ -67,11 +67,11 @@ protected function setUp()
6767
* @param array $value
6868
* @dataProvider isOrderIncrementIdUsedDataProvider
6969
*/
70-
public function testIsOrderIncrementIdUsed($value)
70+
public function testIsIncrementIdUsed($value)
7171
{
7272
$expectedBind = [':increment_id' => $value];
7373
$this->adapterMock->expects($this->once())->method('fetchOne')->with($this->selectMock, $expectedBind);
74-
$this->model->isOrderIncrementIdUsed($value);
74+
$this->model->isIncrementIdUsed($value);
7575
}
7676

7777
/**

dev/tests/integration/testsuite/Magento/Quote/Model/ResourceModel/QuoteTest.php renamed to dev/tests/integration/testsuite/Magento/Sales/Model/OrderIncrementIdCheckerTest.php

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

7-
namespace Magento\Quote\Model\ResourceModel;
7+
namespace Magento\Sales\Model;
88

99
/**
1010
* Class QuoteTest to verify isOrderIncrementIdUsed method behaviour
1111
*/
12-
class QuoteTest extends \PHPUnit\Framework\TestCase
12+
class OrderIncrementIdCheckerTest extends \PHPUnit\Framework\TestCase
1313
{
1414
/**
15-
* @var \Magento\Quote\Model\ResourceModel\Quote
15+
* @var \Magento\Sales\Model\OrderIncrementIdChecker
1616
*/
17-
private $_resourceModel;
17+
private $checker;
1818

1919
protected function setUp()
2020
{
21-
$this->_resourceModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
22-
\Magento\Quote\Model\ResourceModel\Quote::class
21+
$this->checker = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
22+
\Magento\Sales\Model\OrderIncrementIdChecker::class
2323
);
2424
}
2525

@@ -30,7 +30,7 @@ protected function setUp()
3030
*/
3131
public function testIsOrderIncrementIdUsedNumericIncrementId()
3232
{
33-
$this->assertTrue($this->_resourceModel->isOrderIncrementIdUsed('100000001'));
33+
$this->assertTrue($this->checker->isIncrementIdUsed('100000001'));
3434
}
3535

3636
/**
@@ -40,6 +40,6 @@ public function testIsOrderIncrementIdUsedNumericIncrementId()
4040
*/
4141
public function testIsOrderIncrementIdUsedAlphanumericIncrementId()
4242
{
43-
$this->assertTrue($this->_resourceModel->isOrderIncrementIdUsed('M00000001'));
43+
$this->assertTrue($this->checker->isIncrementIdUsed('M00000001'));
4444
}
4545
}

dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2521,5 +2521,6 @@
25212521
['getDataFormTab', 'Magento\Backend\Test\Block\Widget\Tab', 'Magento\Ui\Test\Block\Adminhtml\AbstractContainer::getFieldsData'],
25222522
['getBunchImages', 'Magento\CatalogImportExport\Model\Import\Product'],
25232523
['_isAttributeValueEmpty', 'Magento\Catalog\Model\ResourceModel\AbstractResource'],
2524-
['var_dump', '']
2524+
['var_dump', ''],
2525+
['isOrderIncrementIdUsed', 'Magento\Quote\Model\ResourceModel\Quote', 'Magento\Sales\Model\OrderIncrementIdChecker::isIncrementIdUsed']
25252526
];

0 commit comments

Comments
 (0)