Skip to content

Commit c6c7821

Browse files
authored
ENGCOM-3354: Cancel expired orders using OrderManagementInterface #18832
2 parents bd36872 + cf304a8 commit c6c7821

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

app/code/Magento/Sales/Model/CronJob/CleanExpiredOrders.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sales\Model\CronJob;
79

10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Sales\Api\OrderManagementInterface;
12+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
813
use Magento\Store\Model\StoresConfig;
914
use Magento\Sales\Model\Order;
1015

16+
/**
17+
* Class that provides functionality of cleaning expired quotes by cron
18+
*/
1119
class CleanExpiredOrders
1220
{
1321
/**
@@ -16,20 +24,28 @@ class CleanExpiredOrders
1624
protected $storesConfig;
1725

1826
/**
19-
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
27+
* @var CollectionFactory
2028
*/
2129
protected $orderCollectionFactory;
2230

31+
/**
32+
* @var OrderManagementInterface
33+
*/
34+
private $orderManagement;
35+
2336
/**
2437
* @param StoresConfig $storesConfig
25-
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory
38+
* @param CollectionFactory $collectionFactory
39+
* @param OrderManagementInterface|null $orderManagement
2640
*/
2741
public function __construct(
2842
StoresConfig $storesConfig,
29-
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory
43+
CollectionFactory $collectionFactory,
44+
OrderManagementInterface $orderManagement = null
3045
) {
3146
$this->storesConfig = $storesConfig;
3247
$this->orderCollectionFactory = $collectionFactory;
48+
$this->orderManagement = $orderManagement ?: ObjectManager::getInstance()->get(OrderManagementInterface::class);
3349
}
3450

3551
/**
@@ -48,8 +64,10 @@ public function execute()
4864
$orders->getSelect()->where(
4965
new \Zend_Db_Expr('TIME_TO_SEC(TIMEDIFF(CURRENT_TIMESTAMP, `updated_at`)) >= ' . $lifetime * 60)
5066
);
51-
$orders->walk('cancel');
52-
$orders->walk('save');
67+
68+
foreach ($orders->getAllIds() as $entityId) {
69+
$this->orderManagement->cancel((int) $entityId);
70+
}
5371
}
5472
}
5573
}

app/code/Magento/Sales/Test/Unit/Model/CronJob/CleanExpiredOrdersTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class CleanExpiredOrdersTest extends \PHPUnit\Framework\TestCase
2626
*/
2727
protected $orderCollectionMock;
2828

29+
/**
30+
* @var \PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $orderManagementMock;
33+
2934
/**
3035
* @var ObjectManager
3136
*/
@@ -44,10 +49,12 @@ protected function setUp()
4449
['create']
4550
);
4651
$this->orderCollectionMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class);
52+
$this->orderManagementMock = $this->createMock(\Magento\Sales\Api\OrderManagementInterface::class);
4753

4854
$this->model = new CleanExpiredOrders(
4955
$this->storesConfigMock,
50-
$this->collectionFactoryMock
56+
$this->collectionFactoryMock,
57+
$this->orderManagementMock
5158
);
5259
}
5360

@@ -64,8 +71,11 @@ public function testExecute()
6471
$this->collectionFactoryMock->expects($this->exactly(2))
6572
->method('create')
6673
->willReturn($this->orderCollectionMock);
74+
$this->orderCollectionMock->expects($this->exactly(2))
75+
->method('getAllIds')
76+
->willReturn([1, 2]);
6777
$this->orderCollectionMock->expects($this->exactly(4))->method('addFieldToFilter');
68-
$this->orderCollectionMock->expects($this->exactly(4))->method('walk');
78+
$this->orderManagementMock->expects($this->exactly(4))->method('cancel');
6979

7080
$selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
7181
$selectMock->expects($this->exactly(2))->method('where')->willReturnSelf();
@@ -92,14 +102,18 @@ public function testExecuteWithException()
92102
$this->collectionFactoryMock->expects($this->once())
93103
->method('create')
94104
->willReturn($this->orderCollectionMock);
105+
$this->orderCollectionMock->expects($this->once())
106+
->method('getAllIds')
107+
->willReturn([1]);
95108
$this->orderCollectionMock->expects($this->exactly(2))->method('addFieldToFilter');
109+
$this->orderManagementMock->expects($this->once())->method('cancel');
96110

97111
$selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
98112
$selectMock->expects($this->once())->method('where')->willReturnSelf();
99113
$this->orderCollectionMock->expects($this->once())->method('getSelect')->willReturn($selectMock);
100114

101-
$this->orderCollectionMock->expects($this->once())
102-
->method('walk')
115+
$this->orderManagementMock->expects($this->once())
116+
->method('cancel')
103117
->willThrowException(new \Exception($exceptionMessage));
104118

105119
$this->model->execute();

0 commit comments

Comments
 (0)