Skip to content

Commit

Permalink
Cancel expired orders using OrderManagementInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen committed Dec 10, 2018
1 parent aa0c29d commit 07aa7d5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
25 changes: 20 additions & 5 deletions app/code/Magento/Sales/Model/CronJob/CleanExpiredOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
* 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;

Expand All @@ -16,20 +21,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 +61,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

0 comments on commit 07aa7d5

Please sign in to comment.