CleanExpiredOrdersTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model\CronJob;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Sales\Model\CronJob\CleanExpiredOrders;
  9. class CleanExpiredOrdersTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \PHPUnit_Framework_MockObject_MockObject
  13. */
  14. protected $storesConfigMock;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $collectionFactoryMock;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $orderCollectionMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $orderManagementMock;
  27. /**
  28. * @var ObjectManager
  29. */
  30. protected $objectManager;
  31. /**
  32. * @var CleanExpiredOrders
  33. */
  34. protected $model;
  35. protected function setUp()
  36. {
  37. $this->storesConfigMock = $this->createMock(\Magento\Store\Model\StoresConfig::class);
  38. $this->collectionFactoryMock = $this->createPartialMock(
  39. \Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class,
  40. ['create']
  41. );
  42. $this->orderCollectionMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class);
  43. $this->orderManagementMock = $this->createMock(\Magento\Sales\Api\OrderManagementInterface::class);
  44. $this->model = new CleanExpiredOrders(
  45. $this->storesConfigMock,
  46. $this->collectionFactoryMock,
  47. $this->orderManagementMock
  48. );
  49. }
  50. public function testExecute()
  51. {
  52. $schedule = [
  53. 0 => 300,
  54. 1 => 20,
  55. ];
  56. $this->storesConfigMock->expects($this->once())
  57. ->method('getStoresConfigByPath')
  58. ->with('sales/orders/delete_pending_after')
  59. ->willReturn($schedule);
  60. $this->collectionFactoryMock->expects($this->exactly(2))
  61. ->method('create')
  62. ->willReturn($this->orderCollectionMock);
  63. $this->orderCollectionMock->expects($this->exactly(2))
  64. ->method('getAllIds')
  65. ->willReturn([1, 2]);
  66. $this->orderCollectionMock->expects($this->exactly(4))->method('addFieldToFilter');
  67. $this->orderManagementMock->expects($this->exactly(4))->method('cancel');
  68. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  69. $selectMock->expects($this->exactly(2))->method('where')->willReturnSelf();
  70. $this->orderCollectionMock->expects($this->exactly(2))->method('getSelect')->willReturn($selectMock);
  71. $this->model->execute();
  72. }
  73. /**
  74. * @expectedException \Exception
  75. * @expectedExceptionMessage Error500
  76. */
  77. public function testExecuteWithException()
  78. {
  79. $schedule = [
  80. 1 => 20,
  81. ];
  82. $exceptionMessage = 'Error500';
  83. $this->storesConfigMock->expects($this->once())
  84. ->method('getStoresConfigByPath')
  85. ->with('sales/orders/delete_pending_after')
  86. ->willReturn($schedule);
  87. $this->collectionFactoryMock->expects($this->once())
  88. ->method('create')
  89. ->willReturn($this->orderCollectionMock);
  90. $this->orderCollectionMock->expects($this->once())
  91. ->method('getAllIds')
  92. ->willReturn([1]);
  93. $this->orderCollectionMock->expects($this->exactly(2))->method('addFieldToFilter');
  94. $this->orderManagementMock->expects($this->once())->method('cancel');
  95. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  96. $selectMock->expects($this->once())->method('where')->willReturnSelf();
  97. $this->orderCollectionMock->expects($this->once())->method('getSelect')->willReturn($selectMock);
  98. $this->orderManagementMock->expects($this->once())
  99. ->method('cancel')
  100. ->willThrowException(new \Exception($exceptionMessage));
  101. $this->model->execute();
  102. }
  103. }