Skip to content

#27500 PHPUnit 8 for module Cookie #27708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions app/code/Magento/Cookie/Test/Unit/Block/RequireCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,36 @@

namespace Magento\Cookie\Test\Unit\Block;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please sort these imports ascending?

use Magento\Cookie\Block\RequireCookie;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Element\Template\Context;

/**
* Class \Magento\Cookie\Test\Unit\Block\RequireCookieTest
*/
class RequireCookieTest extends \PHPUnit\Framework\TestCase
class RequireCookieTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject|RequireCookie
* @var MockObject|RequireCookie
*/
private $block;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|ScopeConfigInterface
* @var MockObject|ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|Context
* @var MockObject|Context
*/
private $context;

/**
* Setup Environment
*/
protected function setUp()
protected function setUp(): void
{
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
->disableOriginalConstructor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,68 @@
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also declare the declare(strict_types=1);?

Suggested change
*/
*/
declare(strict_types=1);

namespace Magento\Cookie\Test\Unit\Controller\Index;

use PHPUnit\Framework\TestCase;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here

use Magento\Cookie\Controller\Index\NoCookies;
use PHPUnit\Framework\MockObject\MockObject;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Framework\App\ViewInterface;
use Magento\Framework\DataObject;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class NoCookiesTest extends \PHPUnit\Framework\TestCase
class NoCookiesTest extends TestCase
{
/**
* @var \Magento\Cookie\Controller\Index\NoCookies
* @var NoCookies
*/
private $controller;

/**
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Event\ManagerInterface
* @var MockObject|ManagerInterface
*/
private $eventManagerMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Request\Http
* @var MockObject|Http
*/
private $requestMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Response\Http
* @var MockObject|\Magento\Framework\App\Response\Http
*/
private $responseMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Response\RedirectInterface
* @var MockObject|RedirectInterface
*/
private $redirectResponseMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\ViewInterface
* @var MockObject|ViewInterface
*/
protected $viewMock;

const REDIRECT_URL = 'http://www.example.com/redirect';
const REDIRECT_PATH = '\a\path';
const REDIRECT_ARGUMENTS = '&arg1key=arg1value';

public function setup()
public function setup(): void
{
$objectManager = new ObjectManager($this);
$this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)->getMock();
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
$this->eventManagerMock = $this->getMockBuilder(ManagerInterface::class)->getMock();
$this->requestMock = $this->getMockBuilder(Http::class)
->disableOriginalConstructor()
->getMock();
$this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
->disableOriginalConstructor()
->getMock();
$this->redirectResponseMock = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)
$this->redirectResponseMock = $this->getMockBuilder(RedirectInterface::class)
->getMock();
$this->viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
$this->viewMock = $this->createMock(ViewInterface::class);

$this->controller = $objectManager->getObject(
\Magento\Cookie\Controller\Index\NoCookies::class,
NoCookies::class,
[
'eventManager' => $this->eventManagerMock,
'request' => $this->requestMock,
Expand All @@ -79,7 +87,7 @@ public function testExecuteRedirectUrl()
$this->callback(
function ($dataArray) {
$redirect = $dataArray['redirect'];
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $redirect);
$this->assertInstanceOf(DataObject::class, $redirect);
$redirect->setRedirectUrl(self::REDIRECT_URL);
return true;
}
Expand Down Expand Up @@ -108,7 +116,7 @@ public function testExecuteRedirectPath()
$this->callback(
function ($dataArray) {
$redirect = $dataArray['redirect'];
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $redirect);
$this->assertInstanceOf(DataObject::class, $redirect);
$redirect->setArguments(self::REDIRECT_ARGUMENTS);
$redirect->setPath(self::REDIRECT_PATH);
$redirect->setRedirect(self::REDIRECT_URL);
Expand Down
54 changes: 32 additions & 22 deletions app/code/Magento/Cookie/Test/Unit/Helper/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,49 @@
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
*/
declare(strict_types=1);

namespace Magento\Cookie\Test\Unit\Helper;

class CookieTest extends \PHPUnit\Framework\TestCase
use PHPUnit\Framework\TestCase;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

use Magento\Cookie\Helper\Cookie;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManager;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\Store;
use PHPUnit\Framework\MockObject\MockObject;
use Magento\Store\Model\Website;

class CookieTest extends TestCase
{
/**
* @var \Magento\Cookie\Helper\Cookie
* @var Cookie
*/
protected $_object;

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

/**
* @var \Magento\Framework\App\Helper\Context
* @var Context
*/
protected $_context;

public function testIsUserNotAllowSaveCookie()
{
$this->_initMock()->_getCookieStub([1 => 1]);
$this->assertFalse($this->_object->isUserNotAllowSaveCookie());
$request = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
$request = $this->createPartialMock(Http::class, ['getCookie']);
$request->expects($this->any())->method('getCookie')->will($this->returnValue(json_encode([])));
$scopeConfig = $this->_getConfigStub();
$context = $this->createPartialMock(
\Magento\Framework\App\Helper\Context::class,
Context::class,
['getRequest', 'getScopeConfig']
);
$context->expects($this->once())->method('getRequest')->will($this->returnValue($request));
$context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
$this->_object = new \Magento\Cookie\Helper\Cookie(
$this->_object = new Cookie(
$context,
$this->createMock(\Magento\Store\Model\StoreManager::class),
$this->createMock(StoreManager::class),
['current_store' => $this->_getStoreStub(), 'website' => $this->_getWebsiteStub()]
);
$this->assertTrue($this->_object->isUserNotAllowSaveCookie());
Expand All @@ -52,8 +62,8 @@ public function testGetAcceptedSaveCookiesWebsiteIds()
public function testGetCookieRestrictionLifetime()
{
$this->_request =
$this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
$scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
$this->createPartialMock(Http::class, ['getCookie']);
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
$storeStub = $this->_getStoreStub();
$scopeConfig->expects(
$this->once()
Expand All @@ -65,15 +75,15 @@ public function testGetCookieRestrictionLifetime()
$this->equalTo('web/cookie/cookie_restriction_lifetime')
);
$this->_context = $this->createPartialMock(
\Magento\Framework\App\Helper\Context::class,
Context::class,
['getRequest', 'getScopeConfig']
);
$this->_context->expects($this->once())->method('getRequest')->will($this->returnValue($this->_request));
$this->_context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));

$this->_object = new \Magento\Cookie\Helper\Cookie(
$this->_object = new Cookie(
$this->_context,
$this->createMock(\Magento\Store\Model\StoreManager::class),
$this->createMock(StoreManager::class),
['current_store' => $storeStub, 'website' => $this->_getWebsiteStub()]
);
$this->assertEquals($this->_object->getCookieRestrictionLifetime(), 60 * 60 * 24 * 365);
Expand All @@ -86,39 +96,39 @@ protected function _initMock()
{
$scopeConfig = $this->_getConfigStub();
$this->_request =
$this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getCookie']);
$this->createPartialMock(Http::class, ['getCookie']);
$this->_context = $this->createPartialMock(
\Magento\Framework\App\Helper\Context::class,
Context::class,
['getRequest', 'getScopeConfig']
);
$this->_context->expects($this->once())->method('getRequest')->will($this->returnValue($this->_request));
$this->_context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
$this->_object = new \Magento\Cookie\Helper\Cookie(
$this->_object = new Cookie(
$this->_context,
$this->createMock(\Magento\Store\Model\StoreManager::class),
$this->createMock(StoreManager::class),
['current_store' => $this->_getStoreStub(), 'website' => $this->_getWebsiteStub()]
);
return $this;
}

/**
* Create store stub
* @return \Magento\Store\Model\Store
* @return Store
*/
protected function _getStoreStub()
{
$store = $this->createMock(\Magento\Store\Model\Store::class);
$store = $this->createMock(Store::class);
return $store;
}

/**
* Create config stub
*
* @return \PHPUnit_Framework_MockObject_MockObject
* @return MockObject
*/
protected function _getConfigStub()
{
$scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
$scopeConfig->expects(
$this->any()
)->method(
Expand Down Expand Up @@ -152,7 +162,7 @@ protected function _getCookieStub($cookieString = [])
*/
protected function _getWebsiteStub()
{
$websiteMock = $this->createMock(\Magento\Store\Model\Website::class);
$websiteMock = $this->createMock(Website::class);

$websiteMock->expects($this->any())->method('getId')->will($this->returnValue(1));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,36 @@
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
*/
declare(strict_types=1);

namespace Magento\Cookie\Test\Unit\Model\Config\Backend;

use PHPUnit\Framework\TestCase;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

use Magento\Framework\Model\ResourceModel\AbstractResource;
use PHPUnit\Framework\MockObject\MockObject;
use Magento\Cookie\Model\Config\Backend\Domain;
use Magento\Framework\Event\Manager;
use Magento\Framework\Model\Context;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Session\Config\Validator\CookieDomainValidator;

/**
* Test \Magento\Cookie\Model\Config\Backend\Domain
*/
class DomainTest extends \PHPUnit\Framework\TestCase
class DomainTest extends TestCase
{
/** @var \Magento\Framework\Model\ResourceModel\AbstractResource | \PHPUnit_Framework_MockObject_MockObject */
/** @var AbstractResource|MockObject */
protected $resourceMock;

/** @var \Magento\Cookie\Model\Config\Backend\Domain */
/** @var Domain */
protected $domain;

/**
* @var CookieDomainValidator | \PHPUnit_Framework_MockObject_MockObject
* @var CookieDomainValidator|MockObject
*/
protected $validatorMock;

protected function setUp()
protected function setUp(): void
{
$eventDispatcherMock = $this->createMock(\Magento\Framework\Event\Manager::class);
$contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
$eventDispatcherMock = $this->createMock(Manager::class);
$contextMock = $this->createMock(Context::class);
$contextMock->expects(
$this->any()
)->method(
Expand All @@ -36,7 +43,7 @@ protected function setUp()
$this->returnValue($eventDispatcherMock)
);

$this->resourceMock = $this->createPartialMock(\Magento\Framework\Model\ResourceModel\AbstractResource::class, [
$this->resourceMock = $this->createPartialMock(AbstractResource::class, [
'_construct',
'getConnection',
'getIdFieldName',
Expand All @@ -48,12 +55,12 @@ protected function setUp()
]);

$this->validatorMock = $this->getMockBuilder(
\Magento\Framework\Session\Config\Validator\CookieDomainValidator::class
CookieDomainValidator::class
)->disableOriginalConstructor()
->getMock();
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$helper = new ObjectManager($this);
$this->domain = $helper->getObject(
\Magento\Cookie\Model\Config\Backend\Domain::class,
Domain::class,
[
'context' => $contextMock,
'resource' => $this->resourceMock,
Expand Down
Loading