Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PreventExpressCheckoutObserverTest.php
Go to the documentation of this file.
1 <?php
7 
11 class PreventExpressCheckoutObserverTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $_model;
17 
21  protected $_event;
22 
26  protected $_observer;
27 
33  protected $_customerSession;
34 
41 
46  protected $_messageManager;
47 
53  protected $_url;
54 
59 
60  protected function setUp()
61  {
62  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
63 
64  $this->_event = new \Magento\Framework\Event();
65  $this->_observer = new \Magento\Framework\Event\Observer();
66  $this->_observer->setEvent($this->_event);
67 
68  $this->_customerSession = $this->getMockBuilder(
69  \Magento\Customer\Model\Session::class
70  )->disableOriginalConstructor()->setMethods(
71  ['isLoggedIn']
72  )->getMock();
73 
74  $this->_persistentSession = $this->getMockBuilder(
75  \Magento\Persistent\Helper\Session::class
76  )->disableOriginalConstructor()->setMethods(
77  ['isPersistent']
78  )->getMock();
79 
80  $this->_messageManager = $this->getMockBuilder(
81  \Magento\Framework\Message\ManagerInterface::class
82  )->disableOriginalConstructor()->setMethods(
83  []
84  )->getMock();
85 
86  $this->_url = $this->getMockBuilder(
87  \Magento\Framework\UrlInterface::class
88  )->disableOriginalConstructor()->setMethods(
89  []
90  )->getMock();
91 
92  $this->_expressRedirectHelper = $this->getMockBuilder(
93  \Magento\Checkout\Helper\ExpressRedirect::class
94  )->disableOriginalConstructor()->setMethods(
95  ['redirectLogin']
96  )->getMock();
97 
98  $this->_model = $helper->getObject(
99  \Magento\Persistent\Observer\PreventExpressCheckoutObserver::class,
100  [
101  'customerSession' => $this->_customerSession,
102  'persistentSession' => $this->_persistentSession,
103  'messageManager' => $this->_messageManager,
104  'url' => $this->_url,
105  'expressRedirectHelper' => $this->_expressRedirectHelper
106  ]
107  );
108  }
109 
111  {
112  $this->_customerSession->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
113  $this->_persistentSession->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
114  $this->_model->execute($this->_observer);
115  }
116 
118  {
119  $this->_customerSession->expects($this->any())->method('isLoggedIn')->will($this->returnValue(false));
120  $this->_persistentSession->expects($this->any())->method('isPersistent')->will($this->returnValue(true));
121 
122  $this->_event->setControllerAction(null);
123  $this->_model->execute($this->_observer);
124 
125  $this->_event->setControllerAction(new \StdClass());
126  $this->_model->execute($this->_observer);
127 
128  $expectedActionName = 'realAction';
129  $unexpectedActionName = 'notAction';
130  $request = new \Magento\Framework\DataObject();
131  $request->setActionName($unexpectedActionName);
132  $expressRedirectMock = $this->getMockBuilder(
133  \Magento\Checkout\Controller\Express\RedirectLoginInterface::class
134  )->disableOriginalConstructor()->setMethods(
135  [
136  'getActionFlagList',
137  'getResponse',
138  'getCustomerBeforeAuthUrl',
139  'getLoginUrl',
140  'getRedirectActionName',
141  'getRequest',
142  ]
143  )->getMock();
144  $expressRedirectMock->expects($this->any())->method('getRequest')->will($this->returnValue($request));
145  $expressRedirectMock->expects(
146  $this->any()
147  )->method(
148  'getRedirectActionName'
149  )->will(
150  $this->returnValue($expectedActionName)
151  );
152  $this->_event->setControllerAction($expressRedirectMock);
153  $this->_model->execute($this->_observer);
154 
155  $expectedAuthUrl = 'expectedAuthUrl';
156  $request->setActionName($expectedActionName);
157  $this->_url->expects($this->once())->method('getUrl')->will($this->returnValue($expectedAuthUrl));
158  $this->_expressRedirectHelper->expects(
159  $this->once()
160  )->method(
161  'redirectLogin'
162  )->with(
163  $expressRedirectMock,
164  $expectedAuthUrl
165  );
166  $this->_model->execute($this->_observer);
167  }
168 }
$helper
Definition: iframe.phtml:13