RetryTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Test\Unit\Controller\Adminhtml\Subscription;
  7. use Magento\Analytics\Controller\Adminhtml\Subscription\Retry;
  8. use Magento\Analytics\Model\Config\Backend\Enabled\SubscriptionHandler;
  9. use Magento\Framework\Controller\Result\Redirect;
  10. use Magento\Framework\Controller\ResultFactory;
  11. use Magento\Framework\Exception\LocalizedException;
  12. use Magento\Framework\Message\ManagerInterface;
  13. use Magento\Framework\Phrase;
  14. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  15. class RetryTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var ResultFactory|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $resultFactoryMock;
  21. /**
  22. * @var Redirect|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $resultRedirectMock;
  25. /**
  26. * @var SubscriptionHandler|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $subscriptionHandlerMock;
  29. /**
  30. * @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $messageManagerMock;
  33. /**
  34. * @var ObjectManagerHelper
  35. */
  36. private $objectManagerHelper;
  37. /**
  38. * @var Retry
  39. */
  40. private $retryController;
  41. /**
  42. * @return void
  43. */
  44. protected function setUp()
  45. {
  46. $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->resultRedirectMock = $this->getMockBuilder(Redirect::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->subscriptionHandlerMock = $this->getMockBuilder(SubscriptionHandler::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->messageManagerMock = $this->getMockBuilder(ManagerInterface::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->objectManagerHelper = new ObjectManagerHelper($this);
  59. $this->retryController = $this->objectManagerHelper->getObject(
  60. Retry::class,
  61. [
  62. 'resultFactory' => $this->resultFactoryMock,
  63. 'subscriptionHandler' => $this->subscriptionHandlerMock,
  64. 'messageManager' => $this->messageManagerMock,
  65. ]
  66. );
  67. }
  68. /**
  69. * @return void
  70. */
  71. public function testExecute()
  72. {
  73. $this->resultFactoryMock
  74. ->expects($this->once())
  75. ->method('create')
  76. ->with(ResultFactory::TYPE_REDIRECT)
  77. ->willReturn($this->resultRedirectMock);
  78. $this->resultRedirectMock
  79. ->expects($this->once())
  80. ->method('setPath')
  81. ->with('adminhtml')
  82. ->willReturnSelf();
  83. $this->subscriptionHandlerMock
  84. ->expects($this->once())
  85. ->method('processEnabled')
  86. ->with()
  87. ->willReturn(true);
  88. $this->assertSame(
  89. $this->resultRedirectMock,
  90. $this->retryController->execute()
  91. );
  92. }
  93. /**
  94. * @dataProvider executeExceptionsDataProvider
  95. *
  96. * @param \Exception $exception
  97. * @param Phrase $message
  98. */
  99. public function testExecuteWithException(\Exception $exception, Phrase $message)
  100. {
  101. $this->resultFactoryMock
  102. ->expects($this->once())
  103. ->method('create')
  104. ->with(ResultFactory::TYPE_REDIRECT)
  105. ->willReturn($this->resultRedirectMock);
  106. $this->resultRedirectMock
  107. ->expects($this->once())
  108. ->method('setPath')
  109. ->with('adminhtml')
  110. ->willReturnSelf();
  111. $this->subscriptionHandlerMock
  112. ->expects($this->once())
  113. ->method('processEnabled')
  114. ->with()
  115. ->willThrowException($exception);
  116. $this->messageManagerMock
  117. ->expects($this->once())
  118. ->method('addExceptionMessage')
  119. ->with($exception, $message);
  120. $this->assertSame(
  121. $this->resultRedirectMock,
  122. $this->retryController->execute()
  123. );
  124. }
  125. /**
  126. * @return array
  127. */
  128. public function executeExceptionsDataProvider()
  129. {
  130. return [
  131. [new LocalizedException(__('TestMessage')), __('TestMessage')],
  132. [
  133. new \Exception('TestMessage'),
  134. __('Sorry, there has been an error processing your request. Please try again later.')
  135. ],
  136. ];
  137. }
  138. }