Skip to content

Commit 04af36e

Browse files
author
Jeroen
committed
Cancel expired orders using OrderManagementInterface
1 parent 20f1162 commit 04af36e

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
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,20 +21,28 @@ class CleanExpiredOrders
1621
protected $storesConfig;
1722

1823
/**
19-
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
24+
* @var CollectionFactory
2025
*/
2126
protected $orderCollectionFactory;
2227

28+
/**
29+
* @var OrderManagementInterface
30+
*/
31+
private $orderManagement;
32+
2333
/**
2434
* @param StoresConfig $storesConfig
25-
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory
35+
* @param CollectionFactory $collectionFactory
36+
* @param OrderManagementInterface|null $orderManagement
2637
*/
2738
public function __construct(
2839
StoresConfig $storesConfig,
29-
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory
40+
CollectionFactory $collectionFactory,
41+
OrderManagementInterface $orderManagement = null
3042
) {
3143
$this->storesConfig = $storesConfig;
3244
$this->orderCollectionFactory = $collectionFactory;
45+
$this->orderManagement = $orderManagement ?: ObjectManager::getInstance()->get(OrderManagementInterface::class);
3346
}
3447

3548
/**
@@ -48,8 +61,10 @@ public function execute()
4861
$orders->getSelect()->where(
4962
new \Zend_Db_Expr('TIME_TO_SEC(TIMEDIFF(CURRENT_TIMESTAMP, `updated_at`)) >= ' . $lifetime * 60)
5063
);
51-
$orders->walk('cancel');
52-
$orders->walk('save');
64+
65+
foreach ($orders->getAllIds() as $entityId) {
66+
$this->orderManagement->cancel((int) $entityId);
67+
}
5368
}
5469
}
5570
}

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)