123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Message\Test\Unit;
- use Magento\Framework\Event\ManagerInterface;
- use Magento\Framework\Message\CollectionFactory;
- use Magento\Framework\Message\Factory;
- use Magento\Framework\Message\Manager;
- use Magento\Framework\Message\MessageInterface;
- use Magento\Framework\Message\Session;
- use Psr\Log\LoggerInterface;
- use Magento\Framework\Message\ExceptionMessageLookupFactory;
- /**
- * \Magento\Framework\Message\Manager test case
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class ManagerTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- protected $objectManager;
- /**
- * @var Factory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $messageFactory;
- /**
- * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $messagesFactory;
- /**
- * @var Session|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $session;
- /**
- * @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $eventManager;
- /**
- * @var Manager
- */
- protected $model;
- /**
- * @var MessageInterface |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $messageMock;
- /**
- * @var LoggerInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- private $logger;
- /**
- * @var ExceptionMessageLookupFactory | \PHPUnit_Framework_MockObject_MockObject
- */
- private $exceptionMessageFactory;
- protected function setUp()
- {
- $this->messagesFactory = $this->getMockBuilder(
- \Magento\Framework\Message\CollectionFactory::class
- )
- ->disableOriginalConstructor()
- ->getMock();
- $this->messageFactory = $this->getMockBuilder(
- \Magento\Framework\Message\Factory::class
- )
- ->disableOriginalConstructor()
- ->getMock();
- $this->session = $this->getMockBuilder(
- \Magento\Framework\Message\Session::class
- )
- ->disableOriginalConstructor()
- ->setMethods(
- ['getData', 'setData']
- )
- ->getMock();
- $this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
- $this->logger = $this->createMock(\Psr\Log\LoggerInterface::class);
- $this->exceptionMessageFactory = $this->getMockBuilder(
- \Magento\Framework\Message\ExceptionMessageLookupFactory::class
- )
- ->disableOriginalConstructor()
- ->getMock();
- $this->messageMock = $this->createMock(\Magento\Framework\Message\MessageInterface::class);
- $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->model = new Manager(
- $this->session,
- $this->messageFactory,
- $this->messagesFactory,
- $this->eventManager,
- $this->logger,
- Manager::DEFAULT_GROUP,
- $this->exceptionMessageFactory
- );
- }
- public function testGetDefaultGroup()
- {
- $this->assertEquals(Manager::DEFAULT_GROUP, $this->model->getDefaultGroup());
- }
- public function testGetMessages()
- {
- $messageCollection = $this->getMockBuilder(
- \Magento\Framework\Message\Collection::class
- )->disableOriginalConstructor()->setMethods(
- ['addMessage']
- )->getMock();
- $this->messagesFactory->expects(
- $this->atLeastOnce()
- )->method(
- 'create'
- )->will(
- $this->returnValue($messageCollection)
- );
- $this->session->expects(
- $this->at(0)
- )->method(
- 'getData'
- )->with(
- Manager::DEFAULT_GROUP
- )->will(
- $this->returnValue(null)
- );
- $this->session->expects(
- $this->at(1)
- )->method(
- 'setData'
- )->with(
- Manager::DEFAULT_GROUP,
- $messageCollection
- )->will(
- $this->returnValue($this->session)
- );
- $this->session->expects(
- $this->at(2)
- )->method(
- 'getData'
- )->with(
- Manager::DEFAULT_GROUP
- )->will(
- $this->returnValue($messageCollection)
- );
- $this->eventManager->expects($this->never())->method('dispatch');
- $this->assertEquals($messageCollection, $this->model->getMessages());
- }
- public function testGetMessagesWithClear()
- {
- $messageCollection = $this->getMockBuilder(
- \Magento\Framework\Message\Collection::class
- )->disableOriginalConstructor()->setMethods(
- ['addMessage', 'clear']
- )->getMock();
- $messageCollection->expects($this->once())->method('clear');
- $this->session->expects(
- $this->any()
- )->method(
- 'getData'
- )->with(
- Manager::DEFAULT_GROUP
- )->will(
- $this->returnValue($messageCollection)
- );
- $this->eventManager->expects($this->once())->method('dispatch')->with('session_abstract_clear_messages');
- $this->assertEquals($messageCollection, $this->model->getMessages(true));
- }
- public function testAddExceptionWithAlternativeText()
- {
- $exceptionMessage = 'exception message';
- $alternativeText = 'alternative text';
- $this->logger->expects(
- $this->once()
- )->method(
- 'critical'
- );
- $messageError = $this->getMockBuilder(
- \Magento\Framework\Message\Error::class
- )->setConstructorArgs(
- ['text' => $alternativeText]
- )->getMock();
- $this->messageFactory->expects(
- $this->atLeastOnce()
- )->method(
- 'create'
- )->with(
- MessageInterface::TYPE_ERROR,
- $alternativeText
- )->will(
- $this->returnValue($messageError)
- );
- $messageCollection = $this->getMockBuilder(
- \Magento\Framework\Message\Collection::class
- )->disableOriginalConstructor()->setMethods(
- ['addMessage']
- )->getMock();
- $messageCollection->expects($this->atLeastOnce())->method('addMessage')->with($messageError);
- $this->session->expects(
- $this->atLeastOnce()
- )->method(
- 'getData'
- )->with(
- Manager::DEFAULT_GROUP
- )->will(
- $this->returnValue($messageCollection)
- );
- $exception = new \Exception($exceptionMessage);
- $this->assertEquals($this->model, $this->model->addException($exception, $alternativeText));
- }
- public function testAddExceptionRenderable()
- {
- $exceptionMessage = 'exception message';
- $exception = new \Exception($exceptionMessage);
- $this->logger->expects(
- $this->once()
- )->method(
- 'critical'
- );
- $message = $this->createMock(\Magento\Framework\Message\MessageInterface::class);
- $this->messageFactory->expects(
- $this->never()
- )->method(
- 'create'
- );
- $this->exceptionMessageFactory->expects(
- $this->once()
- )->method(
- 'createMessage'
- )->with(
- $exception
- )->will(
- $this->returnValue($message)
- );
- $messageCollection = $this->getMockBuilder(
- \Magento\Framework\Message\Collection::class
- )->disableOriginalConstructor()->setMethods(
- ['addMessage']
- )->getMock();
- $messageCollection->expects($this->atLeastOnce())->method('addMessage')->with($message);
- $this->session->expects(
- $this->atLeastOnce()
- )->method(
- 'getData'
- )->with(
- Manager::DEFAULT_GROUP
- )->will(
- $this->returnValue($messageCollection)
- );
- $this->assertEquals($this->model, $this->model->addExceptionMessage($exception));
- }
- /**
- * @param string $type
- * @param string $methodName
- * @dataProvider addMessageDataProvider
- */
- public function testAddMessage($type, $methodName)
- {
- $this->assertFalse($this->model->hasMessages());
- $message = 'Message';
- $messageCollection = $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['addMessage']);
- $this->session->expects($this->any())
- ->method('getData')
- ->will($this->returnValue($messageCollection));
- $this->eventManager->expects($this->once())
- ->method('dispatch')->with('session_abstract_add_message');
- $this->messageFactory->expects($this->once())
- ->method('create')->with($type, $message)
- ->will($this->returnValue($this->messageMock));
- $this->model->$methodName($message, 'group');
- $this->assertTrue($this->model->hasMessages());
- }
- /**
- * @return array
- */
- public function addMessageDataProvider()
- {
- return [
- 'error' => [MessageInterface::TYPE_ERROR, 'addError'],
- 'warning' => [MessageInterface::TYPE_WARNING, 'addWarning'],
- 'notice' => [MessageInterface::TYPE_NOTICE, 'addNotice'],
- 'success' => [MessageInterface::TYPE_SUCCESS, 'addSuccess']
- ];
- }
- /**
- * @param \PHPUnit_Framework_MockObject_MockObject $messages
- * @param string $expectation
- * @dataProvider addUniqueMessagesWhenMessagesImplementMessageInterfaceDataProvider
- */
- public function testAddUniqueMessagesWhenMessagesImplementMessageInterface($messages, $expectation)
- {
- $messageCollection =
- $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['getItems', 'addMessage']);
- $this->session->expects($this->any())
- ->method('getData')
- ->will($this->returnValue($messageCollection));
- $messageCollection
- ->expects($this->once())
- ->method('getItems')
- ->will($this->returnValue([new TestingMessage('text')]));
- $messageCollection->expects($this->$expectation())->method('addMessage');
- $this->model->addUniqueMessages([$messages]);
- }
- /**
- * @return array
- */
- public function addUniqueMessagesWhenMessagesImplementMessageInterfaceDataProvider()
- {
- return [
- 'message_text_is_unique' => [
- new TestingMessage('text1'),
- 'once',
- ],
- 'message_text_already_exists' => [
- new TestingMessage('text'),
- 'never',
- ]
- ];
- }
- /**
- * @param string|array $messages
- * @dataProvider addUniqueMessagesDataProvider
- */
- public function testAddUniqueMessages($messages)
- {
- $messageCollection =
- $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['getItems', 'addMessage']);
- $this->session->expects($this->any())
- ->method('getData')
- ->will($this->returnValue($messageCollection));
- $messageCollection
- ->expects($this->any())
- ->method('getItems')
- ->will($this->returnValue(['message']));
- $messageCollection->expects($this->never())->method('addMessage');
- $this->model->addUniqueMessages($messages);
- }
- /**
- * @return array
- */
- public function addUniqueMessagesDataProvider()
- {
- return [
- 'messages_are_text' => [['message']],
- 'messages_are_empty' => [[]]
- ];
- }
- public function testAddMessages()
- {
- $messageCollection =
- $this->createPartialMock(\Magento\Framework\Message\Collection::class, ['getItems', 'addMessage']);
- $this->session->expects($this->any())
- ->method('getData')
- ->will($this->returnValue($messageCollection));
- $this->eventManager->expects($this->once())
- ->method('dispatch')->with('session_abstract_add_message');
- $messageCollection->expects($this->once())->method('addMessage')->with($this->messageMock);
- $this->model->addMessages([$this->messageMock]);
- }
- }
|