Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ManagerTest.php
Go to the documentation of this file.
1 <?php
7 
14 use Psr\Log\LoggerInterface;
16 
22 class ManagerTest extends \PHPUnit\Framework\TestCase
23 {
27  protected $objectManager;
28 
32  protected $messageFactory;
33 
37  protected $messagesFactory;
38 
42  protected $session;
43 
47  protected $eventManager;
48 
52  protected $model;
53 
57  protected $messageMock;
58 
62  private $logger;
63 
67  private $exceptionMessageFactory;
68 
69  protected function setUp()
70  {
71  $this->messagesFactory = $this->getMockBuilder(
72  \Magento\Framework\Message\CollectionFactory::class
73  )
74  ->disableOriginalConstructor()
75  ->getMock();
76  $this->messageFactory = $this->getMockBuilder(
77  \Magento\Framework\Message\Factory::class
78  )
79  ->disableOriginalConstructor()
80  ->getMock();
81  $this->session = $this->getMockBuilder(
82  \Magento\Framework\Message\Session::class
83  )
84  ->disableOriginalConstructor()
85  ->setMethods(
86  ['getData', 'setData']
87  )
88  ->getMock();
89  $this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
90  $this->logger = $this->createMock(\Psr\Log\LoggerInterface::class);
91 
92  $this->exceptionMessageFactory = $this->getMockBuilder(
93  \Magento\Framework\Message\ExceptionMessageLookupFactory::class
94  )
95  ->disableOriginalConstructor()
96  ->getMock();
97 
98  $this->messageMock = $this->createMock(\Magento\Framework\Message\MessageInterface::class);
99  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
100  $this->model = new Manager(
101  $this->session,
102  $this->messageFactory,
103  $this->messagesFactory,
104  $this->eventManager,
105  $this->logger,
107  $this->exceptionMessageFactory
108  );
109  }
110 
111  public function testGetDefaultGroup()
112  {
113  $this->assertEquals(Manager::DEFAULT_GROUP, $this->model->getDefaultGroup());
114  }
115 
116  public function testGetMessages()
117  {
118  $messageCollection = $this->getMockBuilder(
119  \Magento\Framework\Message\Collection::class
120  )->disableOriginalConstructor()->setMethods(
121  ['addMessage']
122  )->getMock();
123 
124  $this->messagesFactory->expects(
125  $this->atLeastOnce()
126  )->method(
127  'create'
128  )->will(
129  $this->returnValue($messageCollection)
130  );
131 
132  $this->session->expects(
133  $this->at(0)
134  )->method(
135  'getData'
136  )->with(
138  )->will(
139  $this->returnValue(null)
140  );
141  $this->session->expects(
142  $this->at(1)
143  )->method(
144  'setData'
145  )->with(
147  $messageCollection
148  )->will(
149  $this->returnValue($this->session)
150  );
151  $this->session->expects(
152  $this->at(2)
153  )->method(
154  'getData'
155  )->with(
157  )->will(
158  $this->returnValue($messageCollection)
159  );
160 
161  $this->eventManager->expects($this->never())->method('dispatch');
162 
163  $this->assertEquals($messageCollection, $this->model->getMessages());
164  }
165 
166  public function testGetMessagesWithClear()
167  {
168  $messageCollection = $this->getMockBuilder(
169  \Magento\Framework\Message\Collection::class
170  )->disableOriginalConstructor()->setMethods(
171  ['addMessage', 'clear']
172  )->getMock();
173 
174  $messageCollection->expects($this->once())->method('clear');
175 
176  $this->session->expects(
177  $this->any()
178  )->method(
179  'getData'
180  )->with(
182  )->will(
183  $this->returnValue($messageCollection)
184  );
185 
186  $this->eventManager->expects($this->once())->method('dispatch')->with('session_abstract_clear_messages');
187 
188  $this->assertEquals($messageCollection, $this->model->getMessages(true));
189  }
190 
192  {
193  $exceptionMessage = 'exception message';
194  $alternativeText = 'alternative text';
195 
196  $this->logger->expects(
197  $this->once()
198  )->method(
199  'critical'
200  );
201 
202  $messageError = $this->getMockBuilder(
203  \Magento\Framework\Message\Error::class
204  )->setConstructorArgs(
205  ['text' => $alternativeText]
206  )->getMock();
207 
208  $this->messageFactory->expects(
209  $this->atLeastOnce()
210  )->method(
211  'create'
212  )->with(
214  $alternativeText
215  )->will(
216  $this->returnValue($messageError)
217  );
218 
219  $messageCollection = $this->getMockBuilder(
220  \Magento\Framework\Message\Collection::class
221  )->disableOriginalConstructor()->setMethods(
222  ['addMessage']
223  )->getMock();
224  $messageCollection->expects($this->atLeastOnce())->method('addMessage')->with($messageError);
225 
226  $this->session->expects(
227  $this->atLeastOnce()
228  )->method(
229  'getData'
230  )->with(
232  )->will(
233  $this->returnValue($messageCollection)
234  );
235 
236  $exception = new \Exception($exceptionMessage);
237  $this->assertEquals($this->model, $this->model->addException($exception, $alternativeText));
238  }
239 
240  public function testAddExceptionRenderable()
241  {
242  $exceptionMessage = 'exception message';
243  $exception = new \Exception($exceptionMessage);
244 
245  $this->logger->expects(
246  $this->once()
247  )->method(
248  'critical'
249  );
250 
251  $message = $this->createMock(\Magento\Framework\Message\MessageInterface::class);
252 
253  $this->messageFactory->expects(
254  $this->never()
255  )->method(
256  'create'
257  );
258 
259  $this->exceptionMessageFactory->expects(
260  $this->once()
261  )->method(
262  'createMessage'
263  )->with(
264  $exception
265  )->will(
266  $this->returnValue($message)
267  );
268 
269  $messageCollection = $this->getMockBuilder(
270  \Magento\Framework\Message\Collection::class
271  )->disableOriginalConstructor()->setMethods(
272  ['addMessage']
273  )->getMock();
274  $messageCollection->expects($this->atLeastOnce())->method('addMessage')->with($message);
275 
276  $this->session->expects(
277  $this->atLeastOnce()
278  )->method(
279  'getData'
280  )->with(
282  )->will(
283  $this->returnValue($messageCollection)
284  );
285 
286  $this->assertEquals($this->model, $this->model->addExceptionMessage($exception));
287  }
288 
294  public function testAddMessage($type, $methodName)
295  {
296  $this->assertFalse($this->model->hasMessages());
297  $message = 'Message';
298  $messageCollection = $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['addMessage']);
299  $this->session->expects($this->any())
300  ->method('getData')
301  ->will($this->returnValue($messageCollection));
302  $this->eventManager->expects($this->once())
303  ->method('dispatch')->with('session_abstract_add_message');
304  $this->messageFactory->expects($this->once())
305  ->method('create')->with($type, $message)
306  ->will($this->returnValue($this->messageMock));
307  $this->model->$methodName($message, 'group');
308  $this->assertTrue($this->model->hasMessages());
309  }
310 
314  public function addMessageDataProvider()
315  {
316  return [
317  'error' => [MessageInterface::TYPE_ERROR, 'addError'],
318  'warning' => [MessageInterface::TYPE_WARNING, 'addWarning'],
319  'notice' => [MessageInterface::TYPE_NOTICE, 'addNotice'],
320  'success' => [MessageInterface::TYPE_SUCCESS, 'addSuccess']
321  ];
322  }
323 
329  public function testAddUniqueMessagesWhenMessagesImplementMessageInterface($messages, $expectation)
330  {
331  $messageCollection =
332  $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['getItems', 'addMessage']);
333  $this->session->expects($this->any())
334  ->method('getData')
335  ->will($this->returnValue($messageCollection));
336  $messageCollection
337  ->expects($this->once())
338  ->method('getItems')
339  ->will($this->returnValue([new TestingMessage('text')]));
340  $messageCollection->expects($this->$expectation())->method('addMessage');
341  $this->model->addUniqueMessages([$messages]);
342  }
343 
348  {
349  return [
350  'message_text_is_unique' => [
351  new TestingMessage('text1'),
352  'once',
353  ],
354  'message_text_already_exists' => [
355  new TestingMessage('text'),
356  'never',
357  ]
358  ];
359  }
360 
365  public function testAddUniqueMessages($messages)
366  {
367  $messageCollection =
368  $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['getItems', 'addMessage']);
369  $this->session->expects($this->any())
370  ->method('getData')
371  ->will($this->returnValue($messageCollection));
372  $messageCollection
373  ->expects($this->any())
374  ->method('getItems')
375  ->will($this->returnValue(['message']));
376  $messageCollection->expects($this->never())->method('addMessage');
377  $this->model->addUniqueMessages($messages);
378  }
379 
384  {
385  return [
386  'messages_are_text' => [['message']],
387  'messages_are_empty' => [[]]
388  ];
389  }
390 
391  public function testAddMessages()
392  {
393  $messageCollection =
394  $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['getItems', 'addMessage']);
395  $this->session->expects($this->any())
396  ->method('getData')
397  ->will($this->returnValue($messageCollection));
398  $this->eventManager->expects($this->once())
399  ->method('dispatch')->with('session_abstract_add_message');
400 
401  $messageCollection->expects($this->once())->method('addMessage')->with($this->messageMock);
402  $this->model->addMessages([$this->messageMock]);
403  }
404 }
testAddUniqueMessagesWhenMessagesImplementMessageInterface($messages, $expectation)
$message
$type
Definition: item.phtml:13