Skip to content

Commit 5e319ed

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-77969' into 2.1-develop-pr35
2 parents 86c9cad + 4c72030 commit 5e319ed

File tree

6 files changed

+224
-6
lines changed

6 files changed

+224
-6
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
use Magento\Framework\Model\AbstractExtensibleModel;
1111
use Magento\Quote\Api\Data\PaymentInterface;
1212
use Magento\Quote\Model\Quote\Address;
13-
use Magento\Sales\Model\ResourceModel;
1413
use Magento\Sales\Model\Status;
1514
use Magento\Framework\Api\AttributeValueFactory;
1615
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
16+
use Magento\Framework\App\ObjectManager;
17+
use Magento\Sales\Model\OrderIncrementIdChecker;
1718

1819
/**
1920
* Quote model
@@ -351,6 +352,11 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C
351352
*/
352353
protected $shippingAddressesItems;
353354

355+
/**
356+
* @var \Magento\Sales\Model\OrderIncrementIdChecker
357+
*/
358+
private $orderIncrementIdChecker;
359+
354360
/**
355361
* @param \Magento\Framework\Model\Context $context
356362
* @param \Magento\Framework\Registry $registry
@@ -392,6 +398,7 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C
392398
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
393399
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
394400
* @param array $data
401+
* @param OrderIncrementIdChecker|null $orderIncrementIdChecker
395402
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
396403
*/
397404
public function __construct(
@@ -434,7 +441,8 @@ public function __construct(
434441
\Magento\Quote\Model\ShippingAssignmentFactory $shippingAssignmentFactory,
435442
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
436443
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
437-
array $data = []
444+
array $data = [],
445+
OrderIncrementIdChecker $orderIncrementIdChecker = null
438446
) {
439447
$this->quoteValidator = $quoteValidator;
440448
$this->_catalogProduct = $catalogProduct;
@@ -469,6 +477,8 @@ public function __construct(
469477
$this->totalsReader = $totalsReader;
470478
$this->shippingFactory = $shippingFactory;
471479
$this->shippingAssignmentFactory = $shippingAssignmentFactory;
480+
$this->orderIncrementIdChecker = $orderIncrementIdChecker ?: ObjectManager::getInstance()
481+
->get(OrderIncrementIdChecker::class);
472482
parent::__construct(
473483
$context,
474484
$registry,
@@ -2164,7 +2174,7 @@ public function reserveOrderId()
21642174
} else {
21652175
//checking if reserved order id was already used for some order
21662176
//if yes reserving new one if not using old one
2167-
if ($this->_getResource()->isOrderIncrementIdUsed($this->getReservedOrderId())) {
2177+
if ($this->orderIncrementIdChecker->isIncrementIdUsed($this->getReservedOrderId())) {
21682178
$this->setReservedOrderId($this->_getResource()->getReservedOrderId($this));
21692179
}
21702180
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ public function getReservedOrderId($quote)
177177
*
178178
* @param int $orderIncrementId
179179
* @return bool
180+
* @deprecated
181+
* @see \Magento\Sales\Model\OrderIncrementIdChecker::isIncrementIdUsed()
180182
*/
181183
public function isOrderIncrementIdUsed($orderIncrementId)
182184
{

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ class QuoteTest extends \PHPUnit_Framework_TestCase
142142
*/
143143
private $customerDataFactoryMock;
144144

145+
/**
146+
* @var \Magento\Sales\Model\OrderIncrementIdChecker|\PHPUnit_Framework_MockObject_MockObject
147+
*/
148+
private $orderIncrementIdCheckerMock;
149+
145150
/**
146151
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
147152
*/
@@ -285,6 +290,14 @@ protected function setUp()
285290
'',
286291
false
287292
);
293+
$this->orderIncrementIdCheckerMock = $this->getMock(
294+
\Magento\Sales\Model\OrderIncrementIdChecker::class,
295+
['isIncrementIdUsed'],
296+
[],
297+
'',
298+
false
299+
);
300+
288301
$this->quote = (new ObjectManager($this))
289302
->getObject(
290303
\Magento\Quote\Model\Quote::class,
@@ -310,7 +323,8 @@ protected function setUp()
310323
'customerDataFactory' => $this->customerDataFactoryMock,
311324
'data' => [
312325
'reserved_order_id' => 1000001
313-
]
326+
],
327+
'orderIncrementIdChecker' => $this->orderIncrementIdCheckerMock,
314328
]
315329
);
316330
}
@@ -1246,9 +1260,9 @@ public function testGetAllItems()
12461260
*/
12471261
public function testReserveOrderId($isReservedOrderIdExist, $reservedOrderId)
12481262
{
1249-
$this->resourceMock
1263+
$this->orderIncrementIdCheckerMock
12501264
->expects($this->once())
1251-
->method('isOrderIncrementIdUsed')
1265+
->method('isIncrementIdUsed')
12521266
->with(1000001)
12531267
->willReturn($isReservedOrderIdExist);
12541268
$this->resourceMock
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 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+
$entityId = $adapter->fetchOne($select, $bind);
45+
if ($entityId > 0) {
46+
return true;
47+
}
48+
49+
return false;
50+
}
51+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Sales\Test\Unit\Model;
8+
9+
/**
10+
* Tests \Magento\Sales\Model\OrderIncrementIdChecker.
11+
*/
12+
class OrderIncrementIdCheckerTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var \Magento\Sales\Model\OrderIncrementIdChecker
16+
*/
17+
private $model;
18+
19+
/**
20+
* @var \Magento\Framework\App\ResourceConnection
21+
*/
22+
private $resourceMock;
23+
24+
/**
25+
* @var \Magento\Framework\DB\Adapter\Pdo\Mysql
26+
*/
27+
private $adapterMock;
28+
29+
/**
30+
* @var \Magento\Framework\DB\Select
31+
*/
32+
private $selectMock;
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
protected function setUp()
38+
{
39+
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
40+
$this->selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
41+
->disableOriginalConstructor()
42+
->getMock();
43+
$this->selectMock->expects($this->any())->method('from')->will($this->returnSelf());
44+
$this->selectMock->expects($this->any())->method('where');
45+
46+
$this->adapterMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\Pdo\Mysql::class)
47+
->disableOriginalConstructor()
48+
->getMock();
49+
$this->adapterMock->expects($this->any())->method('select')->will($this->returnValue($this->selectMock));
50+
51+
$this->resourceMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order::class)
52+
->disableOriginalConstructor()
53+
->getMock();
54+
$this->resourceMock->expects(
55+
$this->any()
56+
)->method(
57+
'getConnection'
58+
)->will(
59+
$this->returnValue($this->adapterMock)
60+
);
61+
62+
$this->model = $objectManagerHelper->getObject(
63+
\Magento\Sales\Model\OrderIncrementIdChecker::class,
64+
[
65+
'resourceModel' => $this->resourceMock,
66+
]
67+
);
68+
}
69+
70+
/**
71+
* Unit test to verify if isOrderIncrementIdUsed method works with different types increment ids.
72+
*
73+
* @param array $value
74+
* @return void
75+
* @dataProvider isOrderIncrementIdUsedDataProvider
76+
*/
77+
public function testIsIncrementIdUsed($value)
78+
{
79+
$expectedBind = [':increment_id' => $value];
80+
$this->adapterMock->expects($this->once())->method('fetchOne')->with($this->selectMock, $expectedBind);
81+
$this->model->isIncrementIdUsed($value);
82+
}
83+
84+
/**
85+
* @return array
86+
*/
87+
public function isOrderIncrementIdUsedDataProvider()
88+
{
89+
return [[100000001], ['10000000001'], ['M10000000001']];
90+
}
91+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Sales\Model;
8+
9+
/**
10+
* Class OrderIncrementIdCheckerTest to verify isIncrementIdUsed method behaviour.
11+
*/
12+
class OrderIncrementIdCheckerTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var \Magento\Sales\Model\OrderIncrementIdChecker
16+
*/
17+
private $checker;
18+
19+
/**
20+
* @inheritdoc
21+
*/
22+
protected function setUp()
23+
{
24+
$this->checker = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
25+
\Magento\Sales\Model\OrderIncrementIdChecker::class
26+
);
27+
}
28+
29+
/**
30+
* Test to verify if isIncrementIdUsed method works with numeric increment ids.
31+
*
32+
* @magentoDataFixture Magento/Sales/_files/order.php
33+
* @return void
34+
*/
35+
public function testIsIncrementIdUsedNumericIncrementId()
36+
{
37+
$this->assertTrue($this->checker->isIncrementIdUsed('100000001'));
38+
}
39+
40+
/**
41+
* Test to verify if IsIncrementIdUsed method works with alphanumeric increment ids.
42+
*
43+
* @magentoDataFixture Magento/Sales/_files/order_alphanumeric_id.php
44+
* @return void
45+
*/
46+
public function testIsIncrementIdUsedAlphanumericIncrementId()
47+
{
48+
$this->assertTrue($this->checker->isIncrementIdUsed('M00000001'));
49+
}
50+
}

0 commit comments

Comments
 (0)