RetryTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AsynchronousOperations\Test\Unit\Controller\Adminhtml\Bulk;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Backend\Model\View\Result\RedirectFactory;
  10. use Magento\Backend\Model\View\Result\Redirect;
  11. use Magento\AsynchronousOperations\Controller\Adminhtml\Bulk\Retry;
  12. use Magento\AsynchronousOperations\Model\BulkManagement;
  13. use Magento\AsynchronousOperations\Model\BulkNotificationManagement;
  14. use Magento\Framework\Controller\ResultFactory;
  15. use Magento\Framework\Controller\Result\Json;
  16. class RetryTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var Retry
  20. */
  21. private $model;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $bulkManagementMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $notificationManagementMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $requestMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $resultRedirectFactoryMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $resultRedirectMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $resultFactoryMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $jsonResultMock;
  50. protected function setUp()
  51. {
  52. $objectManager = new ObjectManager($this);
  53. $this->bulkManagementMock = $this->createMock(BulkManagement::class);
  54. $this->notificationManagementMock = $this->createMock(BulkNotificationManagement::class);
  55. $this->requestMock = $this->createMock(RequestInterface::class);
  56. $this->resultFactoryMock = $this->createPartialMock(ResultFactory::class, ['create']);
  57. $this->jsonResultMock = $this->createMock(Json::class);
  58. $this->resultRedirectFactoryMock = $this->createPartialMock(RedirectFactory::class, ['create']);
  59. $this->resultRedirectMock = $this->createMock(Redirect::class);
  60. $this->model = $objectManager->getObject(
  61. Retry::class,
  62. [
  63. 'bulkManagement' => $this->bulkManagementMock,
  64. 'notificationManagement' => $this->notificationManagementMock,
  65. 'request' => $this->requestMock,
  66. 'resultRedirectFactory' => $this->resultRedirectFactoryMock,
  67. 'resultFactory' => $this->resultFactoryMock,
  68. ]
  69. );
  70. }
  71. public function testExecute()
  72. {
  73. $bulkUuid = '49da7406-1ec3-4100-95ae-9654c83a6801';
  74. $operationsToRetry = [
  75. [
  76. 'key' => 'value',
  77. 'error_code' => 1111,
  78. ],
  79. [
  80. 'error_code' => 2222,
  81. ],
  82. [
  83. 'error_code' => '3333',
  84. ],
  85. ];
  86. $this->requestMock->expects($this->any())
  87. ->method('getParam')
  88. ->willReturnMap([
  89. ['uuid', null, $bulkUuid],
  90. ['operations_to_retry', [], $operationsToRetry],
  91. ['isAjax', null, false],
  92. ]);
  93. $this->bulkManagementMock->expects($this->once())
  94. ->method('retryBulk')
  95. ->with($bulkUuid, [1111, 2222, 3333]);
  96. $this->notificationManagementMock->expects($this->once())
  97. ->method('ignoreBulks')
  98. ->with([$bulkUuid])
  99. ->willReturn(true);
  100. $this->resultRedirectFactoryMock->expects($this->once())
  101. ->method('create')
  102. ->willReturn($this->resultRedirectMock);
  103. $this->resultRedirectMock->expects($this->once())
  104. ->method('setPath')
  105. ->with('bulk/index');
  106. $this->model->execute();
  107. }
  108. public function testExecuteReturnsJsonResultWhenRequestIsSentViaAjax()
  109. {
  110. $bulkUuid = '49da7406-1ec3-4100-95ae-9654c83a6801';
  111. $operationsToRetry = [
  112. [
  113. 'key' => 'value',
  114. 'error_code' => 1111,
  115. ],
  116. ];
  117. $this->requestMock->expects($this->any())
  118. ->method('getParam')
  119. ->willReturnMap([
  120. ['uuid', null, $bulkUuid],
  121. ['operations_to_retry', [], $operationsToRetry],
  122. ['isAjax', null, true],
  123. ]);
  124. $this->bulkManagementMock->expects($this->once())
  125. ->method('retryBulk')
  126. ->with($bulkUuid, [1111]);
  127. $this->notificationManagementMock->expects($this->once())
  128. ->method('ignoreBulks')
  129. ->with([$bulkUuid])
  130. ->willReturn(true);
  131. $this->resultFactoryMock->expects($this->once())
  132. ->method('create')
  133. ->with(ResultFactory::TYPE_JSON, [])
  134. ->willReturn($this->jsonResultMock);
  135. $this->jsonResultMock->expects($this->once())
  136. ->method('setHttpResponseCode')
  137. ->with(200);
  138. $this->assertEquals($this->jsonResultMock, $this->model->execute());
  139. }
  140. }