Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MarkUserNotifiedTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Psr\Log\LoggerInterface;
21 
25 class MarkUserNotifiedTest extends \PHPUnit\Framework\TestCase
26 {
30  private $storageMock;
31 
35  private $authMock;
36 
40  private $loggerMock;
41 
45  private $resultMock;
46 
50  private $productMetadataMock;
51 
55  private $notificationLoggerMock;
56 
60  private $action;
61 
62  public function setUp()
63  {
64  $this->storageMock = $this->getMockBuilder(StorageInterface::class)
65  ->setMethods(['getId'])
66  ->getMockForAbstractClass();
67  $this->authMock = $this->getMockBuilder(Auth::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70  $contextMock = $this->getMockBuilder(Context::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73  $contextMock->expects($this->once())
74  ->method('getAuth')
75  ->willReturn($this->authMock);
76  $this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class)
77  ->getMockForAbstractClass();
78  $this->notificationLoggerMock = $this->getMockBuilder(NotificationLogger::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81  $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
82  ->getMock();
83  $resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $this->resultMock = $this->getMockBuilder(Json::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $resultFactoryMock->expects($this->once())
90  ->method('create')
92  ->willReturn($this->resultMock);
93  $objectManagerHelper = new ObjectManagerHelper($this);
94  $this->action = $objectManagerHelper->getObject(
95  MarkUserNotified::class,
96  [
97  'resultFactory' => $resultFactoryMock,
98  'productMetadata' => $this->productMetadataMock,
99  'notificationLogger' => $this->notificationLoggerMock,
100  'context' => $contextMock,
101  'logger' => $this->loggerMock
102  ]
103  );
104  }
105 
106  public function testExecuteSuccess()
107  {
108  $this->authMock->expects($this->once())
109  ->method('getUser')
110  ->willReturn($this->storageMock);
111  $this->storageMock->expects($this->once())
112  ->method('getId')
113  ->willReturn(1);
114  $this->productMetadataMock->expects($this->once())
115  ->method('getVersion')
116  ->willReturn('999.999.999-alpha');
117  $this->notificationLoggerMock->expects($this->once())
118  ->method('log')
119  ->with(1, '999.999.999-alpha')
120  ->willReturn(true);
121  $this->resultMock->expects($this->once())
122  ->method('setData')
123  ->with(
124  [
125  'success' => true,
126  'error_message' => ''
127  ],
128  false,
129  []
130  )->willReturnSelf();
131  $this->assertEquals($this->resultMock, $this->action->execute());
132  }
133 
135  {
136  $this->authMock->expects($this->once())
137  ->method('getUser')
138  ->willReturn($this->storageMock);
139  $this->storageMock->expects($this->once())
140  ->method('getId')
141  ->willReturn(1);
142  $this->productMetadataMock->expects($this->once())
143  ->method('getVersion')
144  ->willReturn('999.999.999-alpha');
145  $this->notificationLoggerMock->expects($this->once())
146  ->method('log')
147  ->willThrowException(new LocalizedException(__('Error message')));
148  $this->resultMock->expects($this->once())
149  ->method('setData')
150  ->with(
151  [
152  'success' => false,
153  'error_message' => 'Error message'
154  ],
155  false,
156  []
157  )->willReturnSelf();
158  $this->assertEquals($this->resultMock, $this->action->execute());
159  }
160 
162  {
163  $this->authMock->expects($this->once())
164  ->method('getUser')
165  ->willReturn($this->storageMock);
166  $this->storageMock->expects($this->once())
167  ->method('getId')
168  ->willReturn(1);
169  $this->productMetadataMock->expects($this->once())
170  ->method('getVersion')
171  ->willReturn('999.999.999-alpha');
172  $this->notificationLoggerMock->expects($this->once())
173  ->method('log')
174  ->willThrowException(new \Exception('Any message'));
175  $this->resultMock->expects($this->once())
176  ->method('setData')
177  ->with(
178  [
179  'success' => false,
180  'error_message' => __('It is impossible to log user action')
181  ],
182  false,
183  []
184  )->willReturnSelf();
185  $this->assertEquals($this->resultMock, $this->action->execute());
186  }
187 }
__()
Definition: __.php:13