PageRenderTime 31ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/framework/Message/Test/Unit/ManagerTest.php

https://gitlab.com/daigiangaitu91/magento
PHP | 339 lines | 255 code | 36 blank | 48 comment | 0 complexity | 8feef9de3eebfb46c31001a5f4ee2c85 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2015 Magento. 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. /**
  15. * \Magento\Framework\Message\Manager test case
  16. */
  17. class ManagerTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  21. */
  22. protected $objectManager;
  23. /**
  24. * @var Factory|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $messageFactory;
  27. /**
  28. * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $messagesFactory;
  31. /**
  32. * @var Session|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $session;
  35. /**
  36. * @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $eventManager;
  39. /**
  40. * @var Manager
  41. */
  42. protected $model;
  43. /**
  44. * @var MessageInterface |\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $messageMock;
  47. /**
  48. * @var LoggerInterface | \PHPUnit_Framework_MockObject_MockObject
  49. */
  50. private $logger;
  51. public function setUp()
  52. {
  53. $this->messagesFactory = $this->getMockBuilder(
  54. 'Magento\Framework\Message\CollectionFactory'
  55. )
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->messageFactory = $this->getMockBuilder(
  59. 'Magento\Framework\Message\Factory'
  60. )
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->session = $this->getMockBuilder(
  64. 'Magento\Framework\Message\Session'
  65. )
  66. ->disableOriginalConstructor()
  67. ->setMethods(
  68. ['getData', 'setData']
  69. )
  70. ->getMock();
  71. $this->eventManager = $this->getMock('Magento\Framework\Event\ManagerInterface');
  72. $this->logger = $this->getMock('Psr\Log\LoggerInterface');
  73. $this->messageMock = $this->getMock('Magento\Framework\Message\MessageInterface');
  74. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  75. $this->model = new Manager(
  76. $this->session,
  77. $this->messageFactory,
  78. $this->messagesFactory,
  79. $this->eventManager,
  80. $this->logger
  81. );
  82. }
  83. public function testGetDefaultGroup()
  84. {
  85. $this->assertEquals(Manager::DEFAULT_GROUP, $this->model->getDefaultGroup());
  86. }
  87. public function testGetMessages()
  88. {
  89. $messageCollection = $this->getMockBuilder(
  90. 'Magento\Framework\Message\Collection'
  91. )->disableOriginalConstructor()->setMethods(
  92. ['addMessage']
  93. )->getMock();
  94. $this->messagesFactory->expects(
  95. $this->atLeastOnce()
  96. )->method(
  97. 'create'
  98. )->will(
  99. $this->returnValue($messageCollection)
  100. );
  101. $this->session->expects(
  102. $this->at(0)
  103. )->method(
  104. 'getData'
  105. )->with(
  106. Manager::DEFAULT_GROUP
  107. )->will(
  108. $this->returnValue(null)
  109. );
  110. $this->session->expects(
  111. $this->at(1)
  112. )->method(
  113. 'setData'
  114. )->with(
  115. Manager::DEFAULT_GROUP,
  116. $messageCollection
  117. )->will(
  118. $this->returnValue($this->session)
  119. );
  120. $this->session->expects(
  121. $this->at(2)
  122. )->method(
  123. 'getData'
  124. )->with(
  125. Manager::DEFAULT_GROUP
  126. )->will(
  127. $this->returnValue($messageCollection)
  128. );
  129. $this->eventManager->expects($this->never())->method('dispatch');
  130. $this->assertEquals($messageCollection, $this->model->getMessages());
  131. }
  132. public function testGetMessagesWithClear()
  133. {
  134. $messageCollection = $this->getMockBuilder(
  135. 'Magento\Framework\Message\Collection'
  136. )->disableOriginalConstructor()->setMethods(
  137. ['addMessage', 'clear']
  138. )->getMock();
  139. $messageCollection->expects($this->once())->method('clear');
  140. $this->session->expects(
  141. $this->any()
  142. )->method(
  143. 'getData'
  144. )->with(
  145. Manager::DEFAULT_GROUP
  146. )->will(
  147. $this->returnValue($messageCollection)
  148. );
  149. $this->eventManager->expects($this->once())->method('dispatch')->with('session_abstract_clear_messages');
  150. $this->assertEquals($messageCollection, $this->model->getMessages(true));
  151. }
  152. /**
  153. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  154. */
  155. public function testAddException()
  156. {
  157. $exceptionMessage = 'exception message';
  158. $alternativeText = 'alternative text';
  159. $logText = "Exception message: {$exceptionMessage}\nTrace:";
  160. $messageError = $this->getMockBuilder(
  161. 'Magento\Framework\Message\Error'
  162. )->setConstructorArgs(
  163. ['text' => $alternativeText]
  164. )->getMock();
  165. $this->messageFactory->expects(
  166. $this->atLeastOnce()
  167. )->method(
  168. 'create'
  169. )->with(
  170. MessageInterface::TYPE_ERROR,
  171. $alternativeText
  172. )->will(
  173. $this->returnValue($messageError)
  174. );
  175. $messageCollection = $this->getMockBuilder(
  176. 'Magento\Framework\Message\Collection'
  177. )->disableOriginalConstructor()->setMethods(
  178. ['addMessage']
  179. )->getMock();
  180. $messageCollection->expects($this->atLeastOnce())->method('addMessage')->with($messageError);
  181. $this->session->expects(
  182. $this->atLeastOnce()
  183. )->method(
  184. 'getData'
  185. )->with(
  186. Manager::DEFAULT_GROUP
  187. )->will(
  188. $this->returnValue($messageCollection)
  189. );
  190. $exception = new \Exception($exceptionMessage);
  191. $this->assertEquals($this->model, $this->model->addException($exception, $alternativeText));
  192. }
  193. /**
  194. * @param string $type
  195. * @param string $methodName
  196. * @dataProvider addMessageDataProvider
  197. */
  198. public function testAddMessage($type, $methodName)
  199. {
  200. $this->assertFalse($this->model->hasMessages());
  201. $message = 'Message';
  202. $messageCollection = $this->getMock(
  203. 'Magento\Framework\Message\Collection',
  204. ['addMessage'],
  205. [],
  206. '',
  207. false
  208. );
  209. $this->session->expects($this->any())
  210. ->method('getData')
  211. ->will($this->returnValue($messageCollection));
  212. $this->eventManager->expects($this->once())
  213. ->method('dispatch')->with('session_abstract_add_message');
  214. $this->messageFactory->expects($this->once())
  215. ->method('create')->with($type, $message)
  216. ->will($this->returnValue($this->messageMock));
  217. $this->model->$methodName($message, 'group');
  218. $this->assertTrue($this->model->hasMessages());
  219. }
  220. public function addMessageDataProvider()
  221. {
  222. return [
  223. 'error' => [MessageInterface::TYPE_ERROR, 'addError'],
  224. 'warning' => [MessageInterface::TYPE_WARNING, 'addWarning'],
  225. 'notice' => [MessageInterface::TYPE_NOTICE, 'addNotice'],
  226. 'success' => [MessageInterface::TYPE_SUCCESS, 'addSuccess']
  227. ];
  228. }
  229. /**
  230. * @param \PHPUnit_Framework_MockObject_MockObject $messages
  231. * @param string $expectation
  232. * @dataProvider addUniqueMessagesWhenMessagesImplementMessageInterfaceDataProvider
  233. */
  234. public function testAddUniqueMessagesWhenMessagesImplementMessageInterface($messages, $expectation)
  235. {
  236. $messageCollection =
  237. $this->getMock('Magento\Framework\Message\Collection', ['getItems', 'addMessage'], [], '', false);
  238. $this->session->expects($this->any())
  239. ->method('getData')
  240. ->will($this->returnValue($messageCollection));
  241. $messageCollection
  242. ->expects($this->once())
  243. ->method('getItems')
  244. ->will($this->returnValue([new TestingMessage('text')]));
  245. $messageCollection->expects($this->$expectation())->method('addMessage');
  246. $this->model->addUniqueMessages([$messages]);
  247. }
  248. public function addUniqueMessagesWhenMessagesImplementMessageInterfaceDataProvider()
  249. {
  250. return [
  251. 'message_text_is_unique' => [
  252. new TestingMessage('text1'),
  253. 'once',
  254. ],
  255. 'message_text_already_exists' => [
  256. new TestingMessage('text'),
  257. 'never',
  258. ]
  259. ];
  260. }
  261. /**
  262. * @param string|array $messages
  263. * @dataProvider addUniqueMessagesDataProvider
  264. */
  265. public function testAddUniqueMessages($messages)
  266. {
  267. $messageCollection =
  268. $this->getMock('Magento\Framework\Message\Collection', ['getItems', 'addMessage'], [], '', false);
  269. $this->session->expects($this->any())
  270. ->method('getData')
  271. ->will($this->returnValue($messageCollection));
  272. $messageCollection
  273. ->expects($this->any())
  274. ->method('getItems')
  275. ->will($this->returnValue(['message']));
  276. $messageCollection->expects($this->never())->method('addMessage');
  277. $this->model->addUniqueMessages($messages);
  278. }
  279. public function addUniqueMessagesDataProvider()
  280. {
  281. return [
  282. 'messages_are_text' => [['message']],
  283. 'messages_are_empty' => [[]]
  284. ];
  285. }
  286. public function testAddMessages()
  287. {
  288. $messageCollection = $this->getMock(
  289. 'Magento\Framework\Message\Collection',
  290. ['getItems', 'addMessage'],
  291. [],
  292. '',
  293. false
  294. );
  295. $this->session->expects($this->any())
  296. ->method('getData')
  297. ->will($this->returnValue($messageCollection));
  298. $this->eventManager->expects($this->once())
  299. ->method('dispatch')->with('session_abstract_add_message');
  300. $messageCollection->expects($this->once())->method('addMessage')->with($this->messageMock);
  301. $this->model->addMessages([$this->messageMock]);
  302. }
  303. }