Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NoCookiesTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class NoCookiesTest extends \PHPUnit\Framework\TestCase
11 {
15  private $controller;
16 
20  private $eventManagerMock;
21 
25  private $requestMock;
26 
30  private $responseMock;
31 
35  private $redirectResponseMock;
36 
40  protected $viewMock;
41 
42  const REDIRECT_URL = 'http://www.example.com/redirect';
43  const REDIRECT_PATH = '\a\path';
44  const REDIRECT_ARGUMENTS = '&arg1key=arg1value';
45 
46  public function setup()
47  {
48  $objectManager = new ObjectManager($this);
49  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)->getMock();
50  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53  $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->redirectResponseMock = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)
57  ->getMock();
58  $this->viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
59 
60  $this->controller = $objectManager->getObject(
61  \Magento\Cookie\Controller\Index\NoCookies::class,
62  [
63  'eventManager' => $this->eventManagerMock,
64  'request' => $this->requestMock,
65  'response' => $this->responseMock,
66  'redirect' => $this->redirectResponseMock,
67  'view' => $this->viewMock,
68  ]
69  );
70  }
71 
72  public function testExecuteRedirectUrl()
73  {
74  // redirect is new'ed in the execute function, so need to set the redirect URL in dispatch call
75  $this->eventManagerMock->expects($this->once())
76  ->method('dispatch')
77  ->with(
78  $this->equalTo('controller_action_nocookies'),
79  $this->callback(
80  function ($dataArray) {
81  $redirect = $dataArray['redirect'];
82  $this->assertInstanceOf(\Magento\Framework\DataObject::class, $redirect);
83  $redirect->setRedirectUrl(self::REDIRECT_URL);
84  return true;
85  }
86  )
87  );
88 
89  // Verify response is set with redirect url
90  $this->responseMock->expects($this->once())
91  ->method('setRedirect')
92  ->with(self::REDIRECT_URL);
93 
94  // Verify request is set to dispatched
95  $this->requestMock->expects($this->once())->method('setDispatched')->with($this->isTrue());
96 
97  // Make the call to test
98  $this->controller->execute();
99  }
100 
101  public function testExecuteRedirectPath()
102  {
103  // redirect is new'ed in the execute function, so need to set the redirect in dispatch call
104  $this->eventManagerMock->expects($this->once())
105  ->method('dispatch')
106  ->with(
107  $this->equalTo('controller_action_nocookies'),
108  $this->callback(
109  function ($dataArray) {
110  $redirect = $dataArray['redirect'];
111  $this->assertInstanceOf(\Magento\Framework\DataObject::class, $redirect);
112  $redirect->setArguments(self::REDIRECT_ARGUMENTS);
113  $redirect->setPath(self::REDIRECT_PATH);
114  $redirect->setRedirect(self::REDIRECT_URL);
115  return true;
116  }
117  )
118  );
119 
120  // Verify response is set with redirect, which
121  $this->redirectResponseMock->expects($this->once())
122  ->method('redirect')
123  ->with($this->responseMock, $this->equalTo('\a\path'), $this->equalTo('&arg1key=arg1value'));
124 
125  // Verify request is set to dispatched
126  $this->requestMock->expects($this->once())->method('setDispatched')->with($this->isTrue());
127 
128  // Make the call to test
129  $this->controller->execute();
130  }
131 
132  public function testExecuteDefault()
133  {
134  // Verify view is called to load and render
135  $this->viewMock->expects($this->once())->method('loadLayout')->with(['default', 'noCookie']);
136  $this->viewMock->expects($this->once())->method('renderLayout');
137 
138  // Verify request is set to dispatched
139  $this->requestMock->expects($this->once())->method('setDispatched')->with($this->isTrue());
140 
141  // Make the call to test
142  $this->controller->execute();
143  }
144 }
$objectManager
Definition: bootstrap.php:17