Skip to content

Commit

Permalink
ENGCOM-7143: FIX #27299 Consecutive Requests in Integration Tests fai…
Browse files Browse the repository at this point in the history
…ling #27300
  • Loading branch information
slavvka committed Apr 18, 2020
2 parents 9f99f88 + b0333fb commit 111f27f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@
/**
* Abstract class for the controller tests
*/

namespace Magento\TestFramework\TestCase;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;
use Magento\Theme\Controller\Result\MessagePlugin;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\App\Response\Http as HttpResponse;
use PHPUnit\Framework\TestCase;

/**
* Set of methods useful for performing requests to Controllers.
*
* @SuppressWarnings(PHPMD.NumberOfChildren)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
abstract class AbstractController extends \PHPUnit\Framework\TestCase
abstract class AbstractController extends TestCase
{
protected $_runCode = '';

Expand All @@ -30,12 +34,12 @@ abstract class AbstractController extends \PHPUnit\Framework\TestCase
protected $_runOptions = [];

/**
* @var \Magento\Framework\App\RequestInterface
* @var RequestInterface
*/
protected $_request;

/**
* @var \Magento\Framework\App\ResponseInterface
* @var ResponseInterface
*/
protected $_response;

Expand Down Expand Up @@ -103,8 +107,9 @@ protected function assertPostConditions()
*/
public function dispatch($uri)
{
/** @var HttpRequest $request */
$request = $this->getRequest();

$request->setDispatched(false);
$request->setRequestUri($uri);
if ($request->isPost()
&& !array_key_exists('form_key', $request->getPost())
Expand All @@ -119,25 +124,36 @@ public function dispatch($uri)
/**
* Request getter
*
* @return \Magento\Framework\App\RequestInterface|HttpRequest
* @return RequestInterface
*/
public function getRequest()
{
if (!$this->_request) {
$this->_request = $this->_objectManager->get(\Magento\Framework\App\RequestInterface::class);
$this->_request = $this->_objectManager->get(RequestInterface::class);
}
return $this->_request;
}

/**
* Reset Request parameters
*
* @return void
*/
protected function resetRequest(): void
{
$this->_objectManager->removeSharedInstance(RequestInterface::class);
$this->_request = null;
}

/**
* Response getter
*
* @return \Magento\Framework\App\ResponseInterface|HttpResponse
* @return ResponseInterface
*/
public function getResponse()
{
if (!$this->_response) {
$this->_response = $this->_objectManager->get(\Magento\Framework\App\ResponseInterface::class);
$this->_response = $this->_objectManager->get(ResponseInterface::class);
}
return $this->_response;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Backend\Controller\Adminhtml;

use Magento\TestFramework\TestCase\AbstractBackendController;

/**
* @magentoAppArea adminhtml
*/
class ConsecutiveCallTest extends AbstractBackendController
{
/**
* Consecutive calls were failing due to `$request['dispatched']` not being reset before request
*/
public function testConsecutiveCallShouldNotFail()
{
$this->dispatch('backend/admin/auth/login');
$this->dispatch('backend/admin/auth/login');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ public function testCreatepasswordActionInvalidToken()
}

/**
* @param int $customerId
* @param int $customerId
* @param string|null $confirmation
*/
private function assertCustomerConfirmationEquals(int $customerId, string $confirmation = null)
{
/** @var \Magento\Customer\Model\Customer $customer */
$customer = Bootstrap::getObjectManager()
->create(\Magento\Customer\Model\Customer::class)->load($customerId);
->create(\Magento\Customer\Model\Customer::class)->load($customerId);
$this->assertEquals($confirmation, $customer->getConfirmation());
}

Expand Down Expand Up @@ -497,14 +497,14 @@ public function testChangePasswordEditPostAction()
->setMethod('POST')
->setPostValue(
[
'form_key' => $this->_objectManager->get(FormKey::class)->getFormKey(),
'firstname' => 'John',
'lastname' => 'Doe',
'email' => 'johndoe@email.com',
'change_password' => 1,
'change_email' => 1,
'form_key' => $this->_objectManager->get(FormKey::class)->getFormKey(),
'firstname' => 'John',
'lastname' => 'Doe',
'email' => 'johndoe@email.com',
'change_password' => 1,
'change_email' => 1,
'current_password' => 'password',
'password' => 'new-Password1',
'password' => 'new-Password1',
'password_confirmation' => 'new-Password1',
]
);
Expand Down Expand Up @@ -654,6 +654,7 @@ public function testRegisterCustomerWithEmailConfirmation(): void
/** @var CookieManagerInterface $cookieManager */
$cookieManager = $this->_objectManager->get(CookieManagerInterface::class);
$cookieManager->deleteCookie(MessagePlugin::MESSAGES_COOKIES_NAME);

$this->_objectManager->removeSharedInstance(Http::class);
$this->_objectManager->removeSharedInstance(Request::class);
$this->_request = null;
Expand Down Expand Up @@ -774,8 +775,9 @@ public function testResetPasswordWhenEmailChanged(): void
$customer->setEmail($newEmail);
$customerRepository->save($customer);

/* Goes through the link in a mail */
$this->resetRequest();

/* Goes through the link in a mail */
$this->getRequest()
->setParam('token', $token)
->setParam('id', $customerData->getId());
Expand Down Expand Up @@ -855,15 +857,13 @@ private function assertForgotPasswordEmailContent(string $token): void
}

/**
* Clear request object.
*
* @return void
* @inheritDoc
*/
private function resetRequest(): void
protected function resetRequest(): void
{
$this->_objectManager->removeSharedInstance(Http::class);
$this->_objectManager->removeSharedInstance(Request::class);
$this->_request = null;
parent::resetRequest();
}

/**
Expand Down

0 comments on commit 111f27f

Please sign in to comment.