ManagerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Message\Test\Unit;
  7. use Magento\Framework\Event\ManagerInterface;
  8. use Magento\Framework\Message\CollectionFactory;
  9. use Magento\Framework\Message\Factory;
  10. use Magento\Framework\Message\Manager;
  11. use Magento\Framework\Message\MessageInterface;
  12. use Magento\Framework\Message\Session;
  13. use Psr\Log\LoggerInterface;
  14. use Magento\Framework\Message\ExceptionMessageLookupFactory;
  15. /**
  16. * \Magento\Framework\Message\Manager test case
  17. *
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class ManagerTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  24. */
  25. protected $objectManager;
  26. /**
  27. * @var Factory|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $messageFactory;
  30. /**
  31. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $messagesFactory;
  34. /**
  35. * @var Session|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $session;
  38. /**
  39. * @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $eventManager;
  42. /**
  43. * @var Manager
  44. */
  45. protected $model;
  46. /**
  47. * @var MessageInterface |\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $messageMock;
  50. /**
  51. * @var LoggerInterface | \PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $logger;
  54. /**
  55. * @var ExceptionMessageLookupFactory | \PHPUnit_Framework_MockObject_MockObject
  56. */
  57. private $exceptionMessageFactory;
  58. protected function setUp()
  59. {
  60. $this->messagesFactory = $this->getMockBuilder(
  61. \Magento\Framework\Message\CollectionFactory::class
  62. )
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->messageFactory = $this->getMockBuilder(
  66. \Magento\Framework\Message\Factory::class
  67. )
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->session = $this->getMockBuilder(
  71. \Magento\Framework\Message\Session::class
  72. )
  73. ->disableOriginalConstructor()
  74. ->setMethods(
  75. ['getData', 'setData']
  76. )
  77. ->getMock();
  78. $this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  79. $this->logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  80. $this->exceptionMessageFactory = $this->getMockBuilder(
  81. \Magento\Framework\Message\ExceptionMessageLookupFactory::class
  82. )
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $this->messageMock = $this->createMock(\Magento\Framework\Message\MessageInterface::class);
  86. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  87. $this->model = new Manager(
  88. $this->session,
  89. $this->messageFactory,
  90. $this->messagesFactory,
  91. $this->eventManager,
  92. $this->logger,
  93. Manager::DEFAULT_GROUP,
  94. $this->exceptionMessageFactory
  95. );
  96. }
  97. public function testGetDefaultGroup()
  98. {
  99. $this->assertEquals(Manager::DEFAULT_GROUP, $this->model->getDefaultGroup());
  100. }
  101. public function testGetMessages()
  102. {
  103. $messageCollection = $this->getMockBuilder(
  104. \Magento\Framework\Message\Collection::class
  105. )->disableOriginalConstructor()->setMethods(
  106. ['addMessage']
  107. )->getMock();
  108. $this->messagesFactory->expects(
  109. $this->atLeastOnce()
  110. )->method(
  111. 'create'
  112. )->will(
  113. $this->returnValue($messageCollection)
  114. );
  115. $this->session->expects(
  116. $this->at(0)
  117. )->method(
  118. 'getData'
  119. )->with(
  120. Manager::DEFAULT_GROUP
  121. )->will(
  122. $this->returnValue(null)
  123. );
  124. $this->session->expects(
  125. $this->at(1)
  126. )->method(
  127. 'setData'
  128. )->with(
  129. Manager::DEFAULT_GROUP,
  130. $messageCollection
  131. )->will(
  132. $this->returnValue($this->session)
  133. );
  134. $this->session->expects(
  135. $this->at(2)
  136. )->method(
  137. 'getData'
  138. )->with(
  139. Manager::DEFAULT_GROUP
  140. )->will(
  141. $this->returnValue($messageCollection)
  142. );
  143. $this->eventManager->expects($this->never())->method('dispatch');
  144. $this->assertEquals($messageCollection, $this->model->getMessages());
  145. }
  146. public function testGetMessagesWithClear()
  147. {
  148. $messageCollection = $this->getMockBuilder(
  149. \Magento\Framework\Message\Collection::class
  150. )->disableOriginalConstructor()->setMethods(
  151. ['addMessage', 'clear']
  152. )->getMock();
  153. $messageCollection->expects($this->once())->method('clear');
  154. $this->session->expects(
  155. $this->any()
  156. )->method(
  157. 'getData'
  158. )->with(
  159. Manager::DEFAULT_GROUP
  160. )->will(
  161. $this->returnValue($messageCollection)
  162. );
  163. $this->eventManager->expects($this->once())->method('dispatch')->with('session_abstract_clear_messages');
  164. $this->assertEquals($messageCollection, $this->model->getMessages(true));
  165. }
  166. public function testAddExceptionWithAlternativeText()
  167. {
  168. $exceptionMessage = 'exception message';
  169. $alternativeText = 'alternative text';
  170. $this->logger->expects(
  171. $this->once()
  172. )->method(
  173. 'critical'
  174. );
  175. $messageError = $this->getMockBuilder(
  176. \Magento\Framework\Message\Error::class
  177. )->setConstructorArgs(
  178. ['text' => $alternativeText]
  179. )->getMock();
  180. $this->messageFactory->expects(
  181. $this->atLeastOnce()
  182. )->method(
  183. 'create'
  184. )->with(
  185. MessageInterface::TYPE_ERROR,
  186. $alternativeText
  187. )->will(
  188. $this->returnValue($messageError)
  189. );
  190. $messageCollection = $this->getMockBuilder(
  191. \Magento\Framework\Message\Collection::class
  192. )->disableOriginalConstructor()->setMethods(
  193. ['addMessage']
  194. )->getMock();
  195. $messageCollection->expects($this->atLeastOnce())->method('addMessage')->with($messageError);
  196. $this->session->expects(
  197. $this->atLeastOnce()
  198. )->method(
  199. 'getData'
  200. )->with(
  201. Manager::DEFAULT_GROUP
  202. )->will(
  203. $this->returnValue($messageCollection)
  204. );
  205. $exception = new \Exception($exceptionMessage);
  206. $this->assertEquals($this->model, $this->model->addException($exception, $alternativeText));
  207. }
  208. public function testAddExceptionRenderable()
  209. {
  210. $exceptionMessage = 'exception message';
  211. $exception = new \Exception($exceptionMessage);
  212. $this->logger->expects(
  213. $this->once()
  214. )->method(
  215. 'critical'
  216. );
  217. $message = $this->createMock(\Magento\Framework\Message\MessageInterface::class);
  218. $this->messageFactory->expects(
  219. $this->never()
  220. )->method(
  221. 'create'
  222. );
  223. $this->exceptionMessageFactory->expects(
  224. $this->once()
  225. )->method(
  226. 'createMessage'
  227. )->with(
  228. $exception
  229. )->will(
  230. $this->returnValue($message)
  231. );
  232. $messageCollection = $this->getMockBuilder(
  233. \Magento\Framework\Message\Collection::class
  234. )->disableOriginalConstructor()->setMethods(
  235. ['addMessage']
  236. )->getMock();
  237. $messageCollection->expects($this->atLeastOnce())->method('addMessage')->with($message);
  238. $this->session->expects(
  239. $this->atLeastOnce()
  240. )->method(
  241. 'getData'
  242. )->with(
  243. Manager::DEFAULT_GROUP
  244. )->will(
  245. $this->returnValue($messageCollection)
  246. );
  247. $this->assertEquals($this->model, $this->model->addExceptionMessage($exception));
  248. }
  249. /**
  250. * @param string $type
  251. * @param string $methodName
  252. * @dataProvider addMessageDataProvider
  253. */
  254. public function testAddMessage($type, $methodName)
  255. {
  256. $this->assertFalse($this->model->hasMessages());
  257. $message = 'Message';
  258. $messageCollection = $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['addMessage']);
  259. $this->session->expects($this->any())
  260. ->method('getData')
  261. ->will($this->returnValue($messageCollection));
  262. $this->eventManager->expects($this->once())
  263. ->method('dispatch')->with('session_abstract_add_message');
  264. $this->messageFactory->expects($this->once())
  265. ->method('create')->with($type, $message)
  266. ->will($this->returnValue($this->messageMock));
  267. $this->model->$methodName($message, 'group');
  268. $this->assertTrue($this->model->hasMessages());
  269. }
  270. /**
  271. * @return array
  272. */
  273. public function addMessageDataProvider()
  274. {
  275. return [
  276. 'error' => [MessageInterface::TYPE_ERROR, 'addError'],
  277. 'warning' => [MessageInterface::TYPE_WARNING, 'addWarning'],
  278. 'notice' => [MessageInterface::TYPE_NOTICE, 'addNotice'],
  279. 'success' => [MessageInterface::TYPE_SUCCESS, 'addSuccess']
  280. ];
  281. }
  282. /**
  283. * @param \PHPUnit_Framework_MockObject_MockObject $messages
  284. * @param string $expectation
  285. * @dataProvider addUniqueMessagesWhenMessagesImplementMessageInterfaceDataProvider
  286. */
  287. public function testAddUniqueMessagesWhenMessagesImplementMessageInterface($messages, $expectation)
  288. {
  289. $messageCollection =
  290. $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['getItems', 'addMessage']);
  291. $this->session->expects($this->any())
  292. ->method('getData')
  293. ->will($this->returnValue($messageCollection));
  294. $messageCollection
  295. ->expects($this->once())
  296. ->method('getItems')
  297. ->will($this->returnValue([new TestingMessage('text')]));
  298. $messageCollection->expects($this->$expectation())->method('addMessage');
  299. $this->model->addUniqueMessages([$messages]);
  300. }
  301. /**
  302. * @return array
  303. */
  304. public function addUniqueMessagesWhenMessagesImplementMessageInterfaceDataProvider()
  305. {
  306. return [
  307. 'message_text_is_unique' => [
  308. new TestingMessage('text1'),
  309. 'once',
  310. ],
  311. 'message_text_already_exists' => [
  312. new TestingMessage('text'),
  313. 'never',
  314. ]
  315. ];
  316. }
  317. /**
  318. * @param string|array $messages
  319. * @dataProvider addUniqueMessagesDataProvider
  320. */
  321. public function testAddUniqueMessages($messages)
  322. {
  323. $messageCollection =
  324. $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['getItems', 'addMessage']);
  325. $this->session->expects($this->any())
  326. ->method('getData')
  327. ->will($this->returnValue($messageCollection));
  328. $messageCollection
  329. ->expects($this->any())
  330. ->method('getItems')
  331. ->will($this->returnValue(['message']));
  332. $messageCollection->expects($this->never())->method('addMessage');
  333. $this->model->addUniqueMessages($messages);
  334. }
  335. /**
  336. * @return array
  337. */
  338. public function addUniqueMessagesDataProvider()
  339. {
  340. return [
  341. 'messages_are_text' => [['message']],
  342. 'messages_are_empty' => [[]]
  343. ];
  344. }
  345. public function testAddMessages()
  346. {
  347. $messageCollection =
  348. $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['getItems', 'addMessage']);
  349. $this->session->expects($this->any())
  350. ->method('getData')
  351. ->will($this->returnValue($messageCollection));
  352. $this->eventManager->expects($this->once())
  353. ->method('dispatch')->with('session_abstract_add_message');
  354. $messageCollection->expects($this->once())->method('addMessage')->with($this->messageMock);
  355. $this->model->addMessages([$this->messageMock]);
  356. }
  357. }