MarkUserNotifiedTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\ReleaseNotification\Test\Unit\Controller\Notification;
  8. use Psr\Log\LoggerInterface;
  9. use Magento\Backend\App\Action\Context;
  10. use Magento\Backend\Model\Auth\Credential\StorageInterface;
  11. use Magento\Backend\Model\Auth;
  12. use Magento\Framework\Controller\Result\Json;
  13. use Magento\Framework\Controller\ResultFactory;
  14. use Magento\Framework\Exception\LocalizedException;
  15. use Magento\ReleaseNotification\Model\ResourceModel\Viewer\Logger as NotificationLogger;
  16. use Magento\Framework\App\ProductMetadataInterface;
  17. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  18. use Magento\ReleaseNotification\Controller\Adminhtml\Notification\MarkUserNotified;
  19. /**
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class MarkUserNotifiedTest extends \PHPUnit\Framework\TestCase
  23. {
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject|StorageInterface
  26. */
  27. private $storageMock;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject|Auth
  30. */
  31. private $authMock;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject|LoggerInterface
  34. */
  35. private $loggerMock;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject|Json
  38. */
  39. private $resultMock;
  40. /**
  41. * @var \PHPUnit_Framework_MockObject_MockObject|ProductMetadataInterface
  42. */
  43. private $productMetadataMock;
  44. /**
  45. * @var \PHPUnit_Framework_MockObject_MockObject|NotificationLogger
  46. */
  47. private $notificationLoggerMock;
  48. /**
  49. * @var MarkUserNotified
  50. */
  51. private $action;
  52. public function setUp()
  53. {
  54. $this->storageMock = $this->getMockBuilder(StorageInterface::class)
  55. ->setMethods(['getId'])
  56. ->getMockForAbstractClass();
  57. $this->authMock = $this->getMockBuilder(Auth::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $contextMock = $this->getMockBuilder(Context::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $contextMock->expects($this->once())
  64. ->method('getAuth')
  65. ->willReturn($this->authMock);
  66. $this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class)
  67. ->getMockForAbstractClass();
  68. $this->notificationLoggerMock = $this->getMockBuilder(NotificationLogger::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
  72. ->getMock();
  73. $resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->resultMock = $this->getMockBuilder(Json::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $resultFactoryMock->expects($this->once())
  80. ->method('create')
  81. ->with(ResultFactory::TYPE_JSON)
  82. ->willReturn($this->resultMock);
  83. $objectManagerHelper = new ObjectManagerHelper($this);
  84. $this->action = $objectManagerHelper->getObject(
  85. MarkUserNotified::class,
  86. [
  87. 'resultFactory' => $resultFactoryMock,
  88. 'productMetadata' => $this->productMetadataMock,
  89. 'notificationLogger' => $this->notificationLoggerMock,
  90. 'context' => $contextMock,
  91. 'logger' => $this->loggerMock
  92. ]
  93. );
  94. }
  95. public function testExecuteSuccess()
  96. {
  97. $this->authMock->expects($this->once())
  98. ->method('getUser')
  99. ->willReturn($this->storageMock);
  100. $this->storageMock->expects($this->once())
  101. ->method('getId')
  102. ->willReturn(1);
  103. $this->productMetadataMock->expects($this->once())
  104. ->method('getVersion')
  105. ->willReturn('999.999.999-alpha');
  106. $this->notificationLoggerMock->expects($this->once())
  107. ->method('log')
  108. ->with(1, '999.999.999-alpha')
  109. ->willReturn(true);
  110. $this->resultMock->expects($this->once())
  111. ->method('setData')
  112. ->with(
  113. [
  114. 'success' => true,
  115. 'error_message' => ''
  116. ],
  117. false,
  118. []
  119. )->willReturnSelf();
  120. $this->assertEquals($this->resultMock, $this->action->execute());
  121. }
  122. public function testExecuteFailedWithLocalizedException()
  123. {
  124. $this->authMock->expects($this->once())
  125. ->method('getUser')
  126. ->willReturn($this->storageMock);
  127. $this->storageMock->expects($this->once())
  128. ->method('getId')
  129. ->willReturn(1);
  130. $this->productMetadataMock->expects($this->once())
  131. ->method('getVersion')
  132. ->willReturn('999.999.999-alpha');
  133. $this->notificationLoggerMock->expects($this->once())
  134. ->method('log')
  135. ->willThrowException(new LocalizedException(__('Error message')));
  136. $this->resultMock->expects($this->once())
  137. ->method('setData')
  138. ->with(
  139. [
  140. 'success' => false,
  141. 'error_message' => 'Error message'
  142. ],
  143. false,
  144. []
  145. )->willReturnSelf();
  146. $this->assertEquals($this->resultMock, $this->action->execute());
  147. }
  148. public function testExecuteFailedWithException()
  149. {
  150. $this->authMock->expects($this->once())
  151. ->method('getUser')
  152. ->willReturn($this->storageMock);
  153. $this->storageMock->expects($this->once())
  154. ->method('getId')
  155. ->willReturn(1);
  156. $this->productMetadataMock->expects($this->once())
  157. ->method('getVersion')
  158. ->willReturn('999.999.999-alpha');
  159. $this->notificationLoggerMock->expects($this->once())
  160. ->method('log')
  161. ->willThrowException(new \Exception('Any message'));
  162. $this->resultMock->expects($this->once())
  163. ->method('setData')
  164. ->with(
  165. [
  166. 'success' => false,
  167. 'error_message' => __('It is impossible to log user action')
  168. ],
  169. false,
  170. []
  171. )->willReturnSelf();
  172. $this->assertEquals($this->resultMock, $this->action->execute());
  173. }
  174. }