Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ControllerAbstractTest.php
Go to the documentation of this file.
1 <?php
7 
11 
16 {
17  protected $_bootstrap;
18 
20  private $messageManager;
21 
23  private $interpretationStrategyMock;
24 
26  private $cookieManagerMock;
27 
31  private $serializerMock;
32 
33  protected function setUp()
34  {
35  $testObjectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
36 
37  $this->messageManager = $this->createMock(\Magento\Framework\Message\Manager::class);
38  $this->cookieManagerMock = $this->createMock(CookieManagerInterface::class);
39  $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
40  ->disableOriginalConstructor()
41  ->getMock();
42  $this->serializerMock->expects($this->any())->method('unserialize')->willReturnCallback(
43  function ($serializedData) {
44  return json_decode($serializedData, true);
45  }
46  );
47  $this->interpretationStrategyMock = $this->createMock(InterpretationStrategyInterface::class);
48  $this->interpretationStrategyMock->expects($this->any())
49  ->method('interpret')
50  ->willReturnCallback(
51  function (MessageInterface $message) {
52  return $message->getText();
53  }
54  );
55 
56  $request = $testObjectManager->getObject(\Magento\TestFramework\Request::class);
57  $response = $testObjectManager->getObject(\Magento\TestFramework\Response::class);
58  $this->_objectManager =
59  $this->createPartialMock(\Magento\TestFramework\ObjectManager::class, ['get', 'create']);
60  $this->_objectManager->expects($this->any())
61  ->method('get')
62  ->will(
63  $this->returnValueMap(
64  [
65  [\Magento\Framework\App\RequestInterface::class, $request],
66  [\Magento\Framework\App\ResponseInterface::class, $response],
67  [\Magento\Framework\Message\Manager::class, $this->messageManager],
68  [CookieManagerInterface::class, $this->cookieManagerMock],
69  [\Magento\Framework\Serialize\Serializer\Json::class, $this->serializerMock],
70  [InterpretationStrategyInterface::class, $this->interpretationStrategyMock],
71  ]
72  )
73  );
74  }
75 
82  protected function _getBootstrap()
83  {
84  if (!$this->_bootstrap) {
85  $this->_bootstrap = $this->createPartialMock(\Magento\TestFramework\Bootstrap::class, ['getAllOptions']);
86  }
87  return $this->_bootstrap;
88  }
89 
90  public function testGetRequest()
91  {
92  $request = $this->getRequest();
93  $this->assertInstanceOf(\Magento\TestFramework\Request::class, $request);
94  }
95 
96  public function testGetResponse()
97  {
98  $response = $this->getResponse();
99  $this->assertInstanceOf(\Magento\TestFramework\Response::class, $response);
100  }
101 
105  public function testAssert404NotFound()
106  {
107  $this->getRequest()->setControllerName('noroute');
108  $this->getResponse()->setBody(
109  '404 Not Found test <h3>We are sorry, but the page you are looking for cannot be found.</h3>'
110  );
111  $this->assert404NotFound();
112 
113  $this->getResponse()->setBody('');
114  try {
115  $this->assert404NotFound();
116  } catch (\PHPUnit\Framework\AssertionFailedError $e) {
117  return;
118  }
119  $this->fail('Failed response body validation');
120  }
121 
125  public function testAssertRedirectFailure()
126  {
127  $this->assertRedirect();
128  }
129 
133  public function testAssertRedirect()
134  {
135  /*
136  * Prevent calling \Magento\Framework\App\Response\Http::setRedirect() because it dispatches event,
137  * which requires fully initialized application environment intentionally not available
138  * for unit tests
139  */
140  $setRedirectMethod = new \ReflectionMethod(\Magento\Framework\App\Response\Http::class, 'setRedirect');
141  $setRedirectMethod->invoke($this->getResponse(), 'http://magentocommerce.com');
142  $this->assertRedirect();
143  $this->assertRedirect($this->equalTo('http://magentocommerce.com'));
144  }
145 
151  public function testAssertSessionMessagesSuccess(array $expectedMessages, $messageTypeFilter)
152  {
153  $this->addSessionMessages();
155  $constraint =
156  $this->createPartialMock(\PHPUnit\Framework\Constraint\Constraint::class, ['toString', 'matches']);
157  $constraint->expects(
158  $this->once()
159  )->method('matches')
160  ->with($expectedMessages)
161  ->will($this->returnValue(true));
162  $this->assertSessionMessages($constraint, $messageTypeFilter);
163  }
164 
166  {
167  return [
168  'message warning type filtering' => [
169  ['some_warning', 'warning_cookie'],
171  ],
172  'message error type filtering' => [
173  ['error_one', 'error_two', 'error_cookie'],
175  ],
176  'message notice type filtering' => [
177  ['some_notice', 'notice_cookie'],
179  ],
180  'message success type filtering' => [
181  ['success!', 'success_cookie'],
183  ],
184  ];
185  }
186 
188  {
189  $this->addSessionMessages();
190 
191  $this->assertSessionMessages(
192  $this->equalTo(
193  [
194  'some_warning',
195  'error_one',
196  'error_two',
197  'some_notice',
198  'success!',
199  'warning_cookie',
200  'notice_cookie',
201  'success_cookie',
202  'error_cookie',
203  ]
204  )
205  );
206  }
207 
209  {
210  $messagesCollection = new \Magento\Framework\Message\Collection();
211  $this->messageManager->expects($this->any())->method('getMessages')
212  ->will($this->returnValue($messagesCollection));
213 
214  $this->assertSessionMessages($this->isEmpty());
215  }
216 
217  private function addSessionMessages()
218  {
219  // emulate session messages
220  $messagesCollection = new \Magento\Framework\Message\Collection();
221  $messagesCollection
222  ->addMessage(new \Magento\Framework\Message\Warning('some_warning'))
223  ->addMessage(new \Magento\Framework\Message\Error('error_one'))
224  ->addMessage(new \Magento\Framework\Message\Error('error_two'))
225  ->addMessage(new \Magento\Framework\Message\Notice('some_notice'))
226  ->addMessage(new \Magento\Framework\Message\Success('success!'));
227  $this->messageManager->expects($this->any())->method('getMessages')
228  ->will($this->returnValue($messagesCollection));
229 
230  $cookieMessages = [
231  [
232  'type' => 'warning',
233  'text' => 'warning_cookie',
234  ],
235  [
236  'type' => 'notice',
237  'text' => 'notice_cookie',
238  ],
239  [
240  'type' => 'success',
241  'text' => 'success_cookie',
242  ],
243  [
244  'type' => 'error',
245  'text' => 'error_cookie',
246  ],
247  ];
248 
249  $this->cookieManagerMock->expects($this->any())
250  ->method('getCookie')
251  ->willReturn(json_encode($cookieMessages));
252  }
253 }
$response
Definition: 404.php:11
assertRedirect(\PHPUnit\Framework\Constraint\Constraint $urlConstraint=null)
$message