61 return \Magento\TestFramework\Helper\Bootstrap::getInstance();
69 $this->_assertSessionErrors =
false;
71 $this->_objectManager->removeSharedInstance(\
Magento\Framework\
App\ResponseInterface::class);
72 $this->_objectManager->removeSharedInstance(\
Magento\Framework\
App\RequestInterface::class);
80 $this->_request =
null;
81 $this->_response =
null;
82 $this->_objectManager =
null;
90 if ($this->_assertSessionErrors) {
92 $this->assertSessionMessages(
104 public function dispatch($uri)
110 && !array_key_exists(
'form_key',
$request->getPost())
113 $formKey = $this->_objectManager->get(FormKey::class);
114 $request->setPostValue(
'form_key', $formKey->getFormKey());
126 if (!$this->_request) {
127 $this->_request = $this->_objectManager->get(\
Magento\Framework\
App\RequestInterface::class);
139 if (!$this->_response) {
140 $this->_response = $this->_objectManager->get(\
Magento\Framework\
App\ResponseInterface::class);
150 $this->assertEquals(
'noroute', $this->
getRequest()->getControllerName());
151 $this->assertContains(
'404 Not Found', $this->
getResponse()->getBody());
163 $headerFound =
false;
165 foreach ($headers as $header) {
166 if ($header->getFieldName() === $headerName) {
168 $this->assertRegExp($valueRegex, $header->getFieldValue());
172 $this->fail(
"Header '{$headerName}' was not found. Headers dump:\n" . var_export($headers, 1));
187 public function assertRedirect(\PHPUnit\Framework\Constraint\Constraint $urlConstraint =
null)
189 $this->assertTrue($this->
getResponse()->isRedirect(),
'Redirect was expected, but none was performed.');
190 if ($urlConstraint) {
192 foreach ($this->
getResponse()->getHeaders() as $header) {
193 if ($header->getFieldName() ==
'Location') {
194 $actualUrl = $header->getFieldValue();
198 $this->assertThat($actualUrl, $urlConstraint,
'Redirection URL does not match expectations');
214 public function assertSessionMessages(
215 \PHPUnit\Framework\Constraint\Constraint
$constraint,
217 $messageManagerClass = \
Magento\Framework\Message\Manager::class
219 $this->_assertSessionErrors =
false;
221 $messages = $this->
getMessages($messageType, $messageManagerClass);
223 $messagesFiltered = array_map(
234 'Session messages do not meet expectations ' . var_export($messagesFiltered,
true)
247 $messageManagerClass = \
Magento\Framework\Message\Manager::class
250 $this->getSessionMessages($messageType, $messageManagerClass),
251 $this->getCookieMessages($messageType)
262 protected function getSessionMessages(
264 $messageManagerClass = \
Magento\Framework\Message\Manager::class
267 $messageManager = $this->_objectManager->get($messageManagerClass);
269 if ($messageType ===
null) {
270 $messages = $messageManager->getMessages()->getItems();
272 $messages = $messageManager->getMessages()->getItemsByType($messageType);
276 $interpretationStrategy = $this->_objectManager->get(InterpretationStrategyInterface::class);
278 $actualMessages = [];
280 $actualMessages[] = $interpretationStrategy->interpret(
$message);
283 return $actualMessages;
292 protected function getCookieMessages($messageType =
null)
295 $cookieManager = $this->_objectManager->get(CookieManagerInterface::class);
298 $jsonSerializer = $this->_objectManager->get(\
Magento\Framework\Serialize\Serializer\Json::class);
300 $messages = $jsonSerializer->unserialize(
301 $cookieManager->getCookie(
303 $jsonSerializer->serialize([])
307 if (!is_array($messages)) {
310 }
catch (\InvalidArgumentException $e) {
314 $actualMessages = [];
316 if ($messageType ===
null ||
$message[
'type'] == $messageType) {
317 $actualMessages[] =
$message[
'text'];
321 return $actualMessages;
assertRedirect(\PHPUnit\Framework\Constraint\Constraint $urlConstraint=null)
getMessages( $messageType=null, $messageManagerClass=\Magento\Framework\Message\Manager::class)
const MESSAGES_COOKIES_NAME
assertHeaderPcre($headerName, $valueRegex)
static getObjectManager()