Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions app/code/Magento/Sales/Model/CronJob/CleanExpiredOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sales\Model\CronJob;

use Magento\Framework\App\ObjectManager;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Store\Model\StoresConfig;
use Magento\Sales\Model\Order;

/**
* Class that provides functionality of cleaning expired quotes by cron
*/
class CleanExpiredOrders
{
/**
Expand All @@ -16,20 +24,28 @@ class CleanExpiredOrders
protected $storesConfig;

/**
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
* @var CollectionFactory
*/
protected $orderCollectionFactory;

/**
* @var OrderManagementInterface
*/
private $orderManagement;

/**
* @param StoresConfig $storesConfig
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory
* @param CollectionFactory $collectionFactory
* @param OrderManagementInterface|null $orderManagement
*/
public function __construct(
StoresConfig $storesConfig,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory
CollectionFactory $collectionFactory,
OrderManagementInterface $orderManagement = null
) {
$this->storesConfig = $storesConfig;
$this->orderCollectionFactory = $collectionFactory;
$this->orderManagement = $orderManagement ?: ObjectManager::getInstance()->get(OrderManagementInterface::class);
}

/**
Expand All @@ -48,8 +64,10 @@ public function execute()
$orders->getSelect()->where(
new \Zend_Db_Expr('TIME_TO_SEC(TIMEDIFF(CURRENT_TIMESTAMP, `updated_at`)) >= ' . $lifetime * 60)
);
$orders->walk('cancel');
$orders->walk('save');

foreach ($orders->getAllIds() as $entityId) {
$this->orderManagement->cancel((int) $entityId);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class CleanExpiredOrdersTest extends \PHPUnit\Framework\TestCase
*/
protected $orderCollectionMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $orderManagementMock;

/**
* @var ObjectManager
*/
Expand All @@ -44,10 +49,12 @@ protected function setUp()
['create']
);
$this->orderCollectionMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class);
$this->orderManagementMock = $this->createMock(\Magento\Sales\Api\OrderManagementInterface::class);

$this->model = new CleanExpiredOrders(
$this->storesConfigMock,
$this->collectionFactoryMock
$this->collectionFactoryMock,
$this->orderManagementMock
);
}

Expand All @@ -64,8 +71,11 @@ public function testExecute()
$this->collectionFactoryMock->expects($this->exactly(2))
->method('create')
->willReturn($this->orderCollectionMock);
$this->orderCollectionMock->expects($this->exactly(2))
->method('getAllIds')
->willReturn([1, 2]);
$this->orderCollectionMock->expects($this->exactly(4))->method('addFieldToFilter');
$this->orderCollectionMock->expects($this->exactly(4))->method('walk');
$this->orderManagementMock->expects($this->exactly(4))->method('cancel');

$selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
$selectMock->expects($this->exactly(2))->method('where')->willReturnSelf();
Expand All @@ -92,14 +102,18 @@ public function testExecuteWithException()
$this->collectionFactoryMock->expects($this->once())
->method('create')
->willReturn($this->orderCollectionMock);
$this->orderCollectionMock->expects($this->once())
->method('getAllIds')
->willReturn([1]);
$this->orderCollectionMock->expects($this->exactly(2))->method('addFieldToFilter');
$this->orderManagementMock->expects($this->once())->method('cancel');

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

$this->orderCollectionMock->expects($this->once())
->method('walk')
$this->orderManagementMock->expects($this->once())
->method('cancel')
->willThrowException(new \Exception($exceptionMessage));

$this->model->execute();
Expand Down