Skip to content

[Cookie] Fix PHPUnit 8 compatibility issues #27658

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

Merged
merged 2 commits into from
Apr 15, 2020
Merged
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
53 changes: 29 additions & 24 deletions app/code/Magento/Cookie/Test/Unit/Block/RequireCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,80 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Cookie\Test\Unit\Block;

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

/**
* Class \Magento\Cookie\Test\Unit\Block\RequireCookieTest
* @covers \Magento\Cookie\Test\Unit\Block\RequireCookieTest
*/
class RequireCookieTest extends \PHPUnit\Framework\TestCase
class RequireCookieTest extends TestCase
{
private const STUB_NOCOOKIES_URL = 'http://magento.com/cookie/index/noCookies/';

/**
* @var \PHPUnit_Framework_MockObject_MockObject|RequireCookie
* @var MockObject|RequireCookie
*/
private $block;

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

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

/**
* Setup Environment
*/
protected function setUp()
protected function setUp(): void
{
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->disableOriginalConstructor()
->setMethods(['getValue'])
->getMockForAbstractClass();
$this->context = $this->getMockBuilder(Context::class)

$this->contextMock = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();
$this->context->expects($this->any())->method('getScopeConfig')
->willReturn($this->scopeConfig);
$this->contextMock->expects($this->any())->method('getScopeConfig')
->willReturn($this->scopeConfigMock);

$this->block = $this->getMockBuilder(RequireCookie::class)
->setMethods(['escapeHtml', 'escapeUrl', 'getUrl', 'getTriggers'])
->setConstructorArgs(
[
'context' => $this->context
'context' => $this->contextMock
]
)->getMock();
}

/**
* Test getScriptOptions() when the settings "Redirect to CMS-page if Cookies are Disabled" is "Yes"
*/
public function testGetScriptOptionsWhenRedirectToCmsIsYes()
public function testGetScriptOptionsWhenRedirectToCmsIsYes(): void
{
$this->scopeConfig->expects($this->any())->method('getValue')
$this->scopeConfigMock->expects($this->any())->method('getValue')
->with('web/browser_capabilities/cookies')
->willReturn('1');

$this->block->expects($this->any())->method('getUrl')
->with('cookie/index/noCookies/')
->willReturn('http://magento.com/cookie/index/noCookies/');
->willReturn(self::STUB_NOCOOKIES_URL);
$this->block->expects($this->any())->method('getTriggers')
->willReturn('test');
$this->block->expects($this->any())->method('escapeUrl')
->with('http://magento.com/cookie/index/noCookies/')
->willReturn('http://magento.com/cookie/index/noCookies/');
->with(self::STUB_NOCOOKIES_URL)
->willReturn(self::STUB_NOCOOKIES_URL);
$this->block->expects($this->any())->method('escapeHtml')
->with('test')
->willReturn('test');
Expand All @@ -86,20 +91,20 @@ public function testGetScriptOptionsWhenRedirectToCmsIsYes()
/**
* Test getScriptOptions() when the settings "Redirect to CMS-page if Cookies are Disabled" is "No"
*/
public function testGetScriptOptionsWhenRedirectToCmsIsNo()
public function testGetScriptOptionsWhenRedirectToCmsIsNo(): void
{
$this->scopeConfig->expects($this->any())->method('getValue')
$this->scopeConfigMock->expects($this->any())->method('getValue')
->with('web/browser_capabilities/cookies')
->willReturn('0');

$this->block->expects($this->any())->method('getUrl')
->with('cookie/index/noCookies/')
->willReturn('http://magento.com/cookie/index/noCookies/');
->willReturn(self::STUB_NOCOOKIES_URL);
$this->block->expects($this->any())->method('getTriggers')
->willReturn('test');
$this->block->expects($this->any())->method('escapeUrl')
->with('http://magento.com/cookie/index/noCookies/')
->willReturn('http://magento.com/cookie/index/noCookies/');
->with(self::STUB_NOCOOKIES_URL)
->willReturn(self::STUB_NOCOOKIES_URL);
$this->block->expects($this->any())->method('escapeHtml')
->with('test')
->willReturn('test');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,78 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

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

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

class NoCookiesTest extends \PHPUnit\Framework\TestCase
/**
* @covers \Magento\Cookie\Controller\Index\NoCookies
*/
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|HttpRequest
*/
private $requestMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Response\Http
* @var MockObject|HttpResponse
*/
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;
private $viewMock;

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

public function setup()
/**
* @inheritDoc
*/
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(HttpRequest::class)
->disableOriginalConstructor()
->getMock();
$this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
$this->responseMock = $this->getMockBuilder(HttpResponse::class)
->disableOriginalConstructor()
->getMock();
$this->redirectResponseMock = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)
->getMock();
$this->viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
$this->redirectResponseMock = $this->getMockBuilder(RedirectInterface::class)->getMock();
$this->viewMock = $this->createMock(ViewInterface::class);

$objectManager = new ObjectManagerHelper($this);
$this->controller = $objectManager->getObject(
\Magento\Cookie\Controller\Index\NoCookies::class,
NoCookies::class,
[
'eventManager' => $this->eventManagerMock,
'request' => $this->requestMock,
Expand All @@ -69,7 +85,10 @@ public function setup()
);
}

public function testExecuteRedirectUrl()
/**
* Test execute redirect url
*/
public function testExecuteRedirectUrl(): void
{
// redirect is new'ed in the execute function, so need to set the redirect URL in dispatch call
$this->eventManagerMock->expects($this->once())
Expand All @@ -79,7 +98,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 All @@ -92,13 +111,18 @@ function ($dataArray) {
->with(self::REDIRECT_URL);

// Verify request is set to dispatched
$this->requestMock->expects($this->once())->method('setDispatched')->with($this->isTrue());
$this->requestMock->expects($this->once())
->method('setDispatched')
->with($this->isTrue());

// Make the call to test
$this->controller->execute();
}

public function testExecuteRedirectPath()
/**
* Test execute redirect path
*/
public function testExecuteRedirectPath(): void
{
// redirect is new'ed in the execute function, so need to set the redirect in dispatch call
$this->eventManagerMock->expects($this->once())
Expand All @@ -108,7 +132,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 All @@ -129,7 +153,10 @@ function ($dataArray) {
$this->controller->execute();
}

public function testExecuteDefault()
/**
* Test execute default
*/
public function testExecuteDefault(): void
{
// Verify view is called to load and render
$this->viewMock->expects($this->once())->method('loadLayout')->with(['default', 'noCookie']);
Expand Down
Loading