Skip to content
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

#27500 Prepare Authorization module Tests for PHPUnit 8 #27718

Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
Expand All @@ -7,28 +7,34 @@
namespace Magento\Authorization\Test\Unit\Model\Acl;

use Magento\Authorization\Model\Acl\AclRetriever;

use Magento\Authorization\Model\ResourceModel\Role\Collection as RoleCollection;
use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory as RoleCollectionFactory;
use Magento\Authorization\Model\ResourceModel\Rules\Collection as RulesCollection;
use Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory as RulesCollectionFactory;
use Magento\Authorization\Model\Role;
use Magento\Authorization\Model\Rules;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\Acl;
use Magento\Framework\Acl\Builder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AclRetrieverTest extends \PHPUnit\Framework\TestCase
class AclRetrieverTest extends TestCase
{
/**
* @var AclRetriever
*/
protected $aclRetriever;

/** @var \PHPUnit_Framework_MockObject_MockObject|Role $roleMock */
/** @var MockObject|Role $roleMock */
protected $roleMock;

protected function setup()
protected function setup(): void
{
$this->aclRetriever = $this->createAclRetriever();
}
Expand Down Expand Up @@ -58,19 +64,17 @@ public function testGetAllowedResourcesByUserTypeCustomer()
);
}

/**
* @expectedException \Magento\Framework\Exception\AuthorizationException
* @expectedExceptionMessage The role wasn't found for the user. Verify the role and try again.
*/
public function testGetAllowedResourcesByUserRoleNotFound()
{
$this->expectException('Magento\Framework\Exception\AuthorizationException');
$this->expectExceptionMessage('The role wasn\'t found for the user. Verify the role and try again.');
$this->roleMock->expects($this->once())->method('getId')->will($this->returnValue(null));
$this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, null);
}

public function testGetAllowedResourcesByUser()
{
$this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(1));
$this->roleMock->method('getId')->will($this->returnValue(1));
$expectedResources = ['Magento_Backend::dashboard', 'Magento_Cms::page'];
$this->assertEquals(
$expectedResources,
Expand All @@ -83,74 +87,74 @@ public function testGetAllowedResourcesByUser()
*/
protected function createAclRetriever()
{
$this->roleMock = $this->createPartialMock(\Magento\Authorization\Model\Role::class, ['getId', '__wakeup']);
$this->roleMock = $this->createPartialMock(Role::class, ['getId', '__wakeup']);

/** @var \PHPUnit_Framework_MockObject_MockObject|RoleCollection $roleCollectionMock */
/** @var MockObject|RoleCollection $roleCollectionMock */
$roleCollectionMock = $this->createPartialMock(
\Magento\Authorization\Model\ResourceModel\Role\Collection::class,
['setUserFilter', 'getFirstItem']
);
$roleCollectionMock->expects($this->any())->method('setUserFilter')->will($this->returnSelf());
$roleCollectionMock->expects($this->any())->method('getFirstItem')->will($this->returnValue($this->roleMock));
$roleCollectionMock->method('setUserFilter')->will($this->returnSelf());
$roleCollectionMock->method('getFirstItem')->will($this->returnValue($this->roleMock));

/** @var \PHPUnit_Framework_MockObject_MockObject|RoleCollectionFactory $roleCollectionFactoryMock */
/** @var MockObject|RoleCollectionFactory $roleCollectionFactoryMock */
$roleCollectionFactoryMock = $this->createPartialMock(
\Magento\Authorization\Model\ResourceModel\Role\CollectionFactory::class,
['create']
);
$roleCollectionFactoryMock->expects($this->any())->method('create')->will(
$roleCollectionFactoryMock->method('create')->will(
$this->returnValue($roleCollectionMock)
);

/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Authorization\Model\Rules $rulesMock1 */
/** @var MockObject|Rules $rulesMock1 */
$rulesMock1 = $this->createPartialMock(
\Magento\Authorization\Model\Rules::class,
Rules::class,
['getResourceId', '__wakeup']
);
$rulesMock1->expects($this->any())->method('getResourceId')->will(
$rulesMock1->method('getResourceId')->will(
$this->returnValue('Magento_Backend::dashboard')
);
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Authorization\Model\Rules $rulesMock1 */
/** @var MockObject|Rules $rulesMock1 */
$rulesMock2 = $this->createPartialMock(
\Magento\Authorization\Model\Rules::class,
Rules::class,
['getResourceId', '__wakeup']
);
$rulesMock2->expects($this->any())->method('getResourceId')->will($this->returnValue('Magento_Cms::page'));
$rulesMock2->method('getResourceId')->will($this->returnValue('Magento_Cms::page'));

/** @var \PHPUnit_Framework_MockObject_MockObject|RulesCollection $rulesCollectionMock */
/** @var MockObject|RulesCollection $rulesCollectionMock */
$rulesCollectionMock = $this->createPartialMock(
\Magento\Authorization\Model\ResourceModel\Rules\Collection::class,
['getByRoles', 'load', 'getItems']
);
$rulesCollectionMock->expects($this->any())->method('getByRoles')->will($this->returnSelf());
$rulesCollectionMock->expects($this->any())->method('load')->will($this->returnSelf());
$rulesCollectionMock->expects($this->any())->method('getItems')->will(
$rulesCollectionMock->method('getByRoles')->will($this->returnSelf());
$rulesCollectionMock->method('load')->will($this->returnSelf());
$rulesCollectionMock->method('getItems')->will(
$this->returnValue([$rulesMock1, $rulesMock2])
);

/** @var \PHPUnit_Framework_MockObject_MockObject|RulesCollectionFactory $rulesCollectionFactoryMock */
/** @var MockObject|RulesCollectionFactory $rulesCollectionFactoryMock */
$rulesCollectionFactoryMock = $this->createPartialMock(
\Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory::class,
['create']
);
$rulesCollectionFactoryMock->expects($this->any())->method('create')->will(
$rulesCollectionFactoryMock->method('create')->will(
$this->returnValue($rulesCollectionMock)
);

/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Acl $aclMock */
$aclMock = $this->createPartialMock(\Magento\Framework\Acl::class, ['has', 'isAllowed']);
$aclMock->expects($this->any())->method('has')->will($this->returnValue(true));
$aclMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true));
/** @var MockObject|Acl $aclMock */
$aclMock = $this->createPartialMock(Acl::class, ['has', 'isAllowed']);
$aclMock->method('has')->will($this->returnValue(true));
$aclMock->method('isAllowed')->will($this->returnValue(true));

/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Acl\Builder $aclBuilderMock */
$aclBuilderMock = $this->createPartialMock(\Magento\Framework\Acl\Builder::class, ['getAcl']);
$aclBuilderMock->expects($this->any())->method('getAcl')->will($this->returnValue($aclMock));
/** @var MockObject|Builder $aclBuilderMock */
$aclBuilderMock = $this->createPartialMock(Builder::class, ['getAcl']);
$aclBuilderMock->method('getAcl')->will($this->returnValue($aclMock));

return new AclRetriever(
$aclBuilderMock,
$roleCollectionFactoryMock,
$rulesCollectionFactoryMock,
$this->createMock(\Psr\Log\LoggerInterface::class)
$this->createMock(LoggerInterface::class)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class RoleTest extends TestCase
/**
* @inheritDoc
*/
protected function setUp()
protected function setUp(): void
{
$this->groupFactoryMock = $this->getMockBuilder(GroupFactory::class)
->setMethods(['create', 'getModelInstance'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class RuleTest extends TestCase
/**
* @inheritDoc
*/
protected function setUp()
protected function setUp(): void
{
$this->rootResource = new RootResource('Magento_Backend::all');
$this->resourceMock = $this->createPartialMock(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?php
<?php declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Authorization\Test\Unit\Model;

use \Magento\Authorization\Model\CompositeUserContext;

use Magento\Authorization\Model\CompositeUserContext;
use Magento\Framework\ObjectManager\Helper\Composite as CompositeHelper;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class CompositeUserContextTest extends \PHPUnit\Framework\TestCase
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class CompositeUserContextTest extends TestCase
{
/**
* @var CompositeUserContext
Expand All @@ -28,19 +30,18 @@ class CompositeUserContextTest extends \PHPUnit\Framework\TestCase
*/
protected $objectManager;

protected function setUp()
protected function setUp(): void
{
$this->objectManager = new ObjectManager($this);
$this->compositeHelperMock = $this->getMockBuilder(\Magento\Framework\ObjectManager\Helper\Composite::class)
->disableOriginalConstructor()
->setMethods(['filterAndSortDeclaredComponents'])
->getMock();
$this->compositeHelperMock
->expects($this->any())
->method('filterAndSortDeclaredComponents')
->will($this->returnArgument(0));
$this->userContext = $this->objectManager->getObject(
\Magento\Authorization\Model\CompositeUserContext::class,
CompositeUserContext::class,
['compositeHelper' => $this->compositeHelperMock]
);
}
Expand All @@ -55,7 +56,7 @@ public function testConstructor()
],
];
$model = $this->objectManager->getObject(
\Magento\Authorization\Model\CompositeUserContext::class,
CompositeUserContext::class,
['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
);
$this->verifyUserContextIsAdded($model, $userContextMock);
Expand All @@ -65,18 +66,18 @@ public function testGetUserId()
{
$expectedUserId = 1;
$expectedUserType = 'Customer';
$userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
$userContextMock = $this->getMockBuilder(CompositeUserContext::class)
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
$userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
$userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
$userContextMock->method('getUserId')->will($this->returnValue($expectedUserId));
$userContextMock->method('getUserType')->will($this->returnValue($expectedUserType));
$contexts = [
[
'sortOrder' => 10,
'type' => $userContextMock,
],
];
$this->userContext = $this->objectManager->getObject(
\Magento\Authorization\Model\CompositeUserContext::class,
CompositeUserContext::class,
['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
);
$actualUserId = $this->userContext->getUserId();
Expand All @@ -87,18 +88,18 @@ public function testGetUserType()
{
$expectedUserId = 1;
$expectedUserType = 'Customer';
$userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
$userContextMock = $this->getMockBuilder(CompositeUserContext::class)
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
$userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
$userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
$userContextMock->method('getUserId')->will($this->returnValue($expectedUserId));
$userContextMock->method('getUserType')->will($this->returnValue($expectedUserType));
$contexts = [
[
'sortOrder' => 10,
'type' => $userContextMock,
],
];
$this->userContext = $this->objectManager->getObject(
\Magento\Authorization\Model\CompositeUserContext::class,
CompositeUserContext::class,
['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
);
$actualUserType = $this->userContext->getUserType();
Expand All @@ -109,7 +110,7 @@ public function testUserContextCaching()
{
$expectedUserId = 1;
$expectedUserType = 'Customer';
$userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
$userContextMock = $this->getMockBuilder(CompositeUserContext::class)
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
$userContextMock->expects($this->exactly(3))->method('getUserType')
->will($this->returnValue($expectedUserType));
Expand All @@ -122,7 +123,7 @@ public function testUserContextCaching()
],
];
$this->userContext = $this->objectManager->getObject(
\Magento\Authorization\Model\CompositeUserContext::class,
CompositeUserContext::class,
['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
);
$this->userContext->getUserId();
Expand All @@ -134,9 +135,9 @@ public function testUserContextCaching()
public function testEmptyUserContext()
{
$expectedUserId = null;
$userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
$userContextMock = $this->getMockBuilder(CompositeUserContext::class)
->disableOriginalConstructor()->setMethods(['getUserId'])->getMock();
$userContextMock->expects($this->any())->method('getUserId')
$userContextMock->method('getUserId')
->will($this->returnValue($expectedUserId));
$contexts = [
[
Expand All @@ -145,7 +146,7 @@ public function testEmptyUserContext()
],
];
$this->userContext = $this->objectManager->getObject(
\Magento\Authorization\Model\CompositeUserContext::class,
CompositeUserContext::class,
['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
);
$actualUserId = $this->userContext->getUserId();
Expand All @@ -155,15 +156,15 @@ public function testEmptyUserContext()
/**
* @param int|null $userId
* @param string|null $userType
* @return \PHPUnit_Framework_MockObject_MockObject
* @return MockObject
*/
protected function createUserContextMock($userId = null, $userType = null)
{
$useContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
$useContextMock = $this->getMockBuilder(CompositeUserContext::class)
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
if ($userId !== null && $userType !== null) {
$useContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($userId));
$useContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($userType));
$useContextMock->method('getUserId')->will($this->returnValue($userId));
$useContextMock->method('getUserType')->will($this->returnValue($userType));
}
return $useContextMock;
}
Expand All @@ -175,7 +176,7 @@ protected function createUserContextMock($userId = null, $userType = null)
protected function verifyUserContextIsAdded($model, $userContextMock)
{
$userContext = new \ReflectionProperty(
\Magento\Authorization\Model\CompositeUserContext::class,
CompositeUserContext::class,
'userContexts'
);
$userContext->setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Framework\Acl\RootResource;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Framework\Phrase;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
Expand Down Expand Up @@ -83,7 +84,7 @@ class RulesTest extends TestCase
/**
* @inheritDoc
*/
protected function setUp()
protected function setUp(): void
{
$this->contextMock = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
Expand Down Expand Up @@ -179,20 +180,19 @@ public function testSaveRelNoResources()

/**
* Test LocalizedException throw case.
*
* @expectedException \Magento\Framework\Exception\LocalizedException
* @expectedExceptionMessage TestException
*/
public function testLocalizedExceptionOccurance()
{
$this->expectException('Magento\Framework\Exception\LocalizedException');
$this->expectExceptionMessage('TestException');
$exceptionPhrase = $this->getMockBuilder(Phrase::class)
->disableOriginalConstructor()
->setMethods(['render'])
->getMock();

$exceptionPhrase->method('render')->willReturn('TestException');

$exception = new \Magento\Framework\Exception\LocalizedException($exceptionPhrase);
$exception = new LocalizedException($exceptionPhrase);

$this->connectionMock->expects($this->once())
->method('beginTransaction');
Expand Down