Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExpressRedirectTest.php
Go to the documentation of this file.
1 <?php
7 
8 class ExpressRedirectTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_actionFlag;
14 
18  protected $_objectManager;
19 
25  protected $_customerSession;
26 
30  protected $_context;
31 
35  protected $_helper;
36 
37  protected function setUp()
38  {
39  $this->_actionFlag = $this->getMockBuilder(
40  \Magento\Framework\App\ActionFlag::class
41  )->disableOriginalConstructor()->setMethods(
42  ['set']
43  )->getMock();
44 
45  $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
46 
47  $this->_customerSession = $this->getMockBuilder(
48  \Magento\Customer\Model\Session::class
49  )->disableOriginalConstructor()->setMethods(
50  ['setBeforeAuthUrl']
51  )->getMock();
52 
53  $this->_context = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56 
57  $this->_helper = new \Magento\Checkout\Helper\ExpressRedirect(
58  $this->_actionFlag,
59  $this->_objectManager,
60  $this->_customerSession,
61  $this->_context
62  );
63  }
64 
71  public function testRedirectLogin($actionFlagList, $customerBeforeAuthUrl, $customerBeforeAuthUrlDefault)
72  {
73  $expressRedirectMock = $this->getMockBuilder(
74  \Magento\Checkout\Controller\Express\RedirectLoginInterface::class
75  )->disableOriginalConstructor()->setMethods(
76  [
77  'getActionFlagList',
78  'getResponse',
79  'getCustomerBeforeAuthUrl',
80  'getLoginUrl',
81  'getRedirectActionName',
82  ]
83  )->getMock();
84  $expressRedirectMock->expects(
85  $this->any()
86  )->method(
87  'getActionFlagList'
88  )->will(
89  $this->returnValue($actionFlagList)
90  );
91 
92  $atIndex = 0;
93  $actionFlagList = array_merge(['no-dispatch' => true], $actionFlagList);
94  foreach ($actionFlagList as $actionKey => $actionFlag) {
95  $this->_actionFlag->expects($this->at($atIndex))->method('set')->with('', $actionKey, $actionFlag);
96  $atIndex++;
97  }
98 
99  $expectedLoginUrl = 'loginURL';
100  $expressRedirectMock->expects(
101  $this->once()
102  )->method(
103  'getLoginUrl'
104  )->will(
105  $this->returnValue($expectedLoginUrl)
106  );
107 
108  $urlMock = $this->getMockBuilder(
109  \Magento\Framework\Url\Helper\Data::class
110  )->disableOriginalConstructor()->setMethods(
111  ['addRequestParam']
112  )->getMock();
113  $urlMock->expects(
114  $this->once()
115  )->method(
116  'addRequestParam'
117  )->with(
118  $expectedLoginUrl,
119  ['context' => 'checkout']
120  )->will(
121  $this->returnValue($expectedLoginUrl)
122  );
123 
124  $this->_objectManager->expects(
125  $this->once()
126  )->method(
127  'get'
128  )->with(
129  \Magento\Framework\Url\Helper\Data::class
130  )->will(
131  $this->returnValue($urlMock)
132  );
133 
134  $responseMock = $this->getMockBuilder(
135  \Magento\Framework\App\Response\Http::class
136  )->disableOriginalConstructor()->setMethods(
137  ['setRedirect', '__wakeup']
138  )->getMock();
139  $responseMock->expects($this->once())->method('setRedirect')->with($expectedLoginUrl);
140 
141  $expressRedirectMock->expects($this->once())->method('getResponse')->will($this->returnValue($responseMock));
142 
143  $expressRedirectMock->expects(
144  $this->any()
145  )->method(
146  'getCustomerBeforeAuthUrl'
147  )->will(
148  $this->returnValue($customerBeforeAuthUrl)
149  );
150  $expectedCustomerBeforeAuthUrl = $customerBeforeAuthUrl !== null
151  ? $customerBeforeAuthUrl : $customerBeforeAuthUrlDefault;
152  if ($expectedCustomerBeforeAuthUrl) {
153  $this->_customerSession->expects(
154  $this->once()
155  )->method(
156  'setBeforeAuthUrl'
157  )->with(
158  $expectedCustomerBeforeAuthUrl
159  );
160  }
161  $this->_helper->redirectLogin($expressRedirectMock, $customerBeforeAuthUrlDefault);
162  }
163 
168  public function redirectLoginDataProvider()
169  {
170  return [
171  [[], 'beforeCustomerUrl', 'beforeCustomerUrlDEFAULT'],
172  [['actionKey' => true], null, 'beforeCustomerUrlDEFAULT'],
173  [[], 'beforeCustomerUrl', null]
174  ];
175  }
176 }
testRedirectLogin($actionFlagList, $customerBeforeAuthUrl, $customerBeforeAuthUrlDefault)