Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/phpunit8' into phpunit8/module-O…
Browse files Browse the repository at this point in the history
…fflineShipping
  • Loading branch information
lbajsarowicz committed Apr 21, 2020
2 parents 36b8d42 + d968b17 commit 930d4e2
Show file tree
Hide file tree
Showing 373 changed files with 5,639 additions and 3,302 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
<?php declare(strict_types=1);
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

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 Magento\Framework\Exception\AuthorizationException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

/**
* @covers \Magento\Authorization\Model\Acl\AclRetriever
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AclRetrieverTest extends TestCase
{
/**
* @var AclRetriever
*/
protected $aclRetriever;
private $aclRetriever;

/** @var MockObject|Role $roleMock */
protected $roleMock;
/**
* @var Role|MockObject
*/
private $roleMock;

protected function setup(): void
protected function setUp(): void
{
$this->aclRetriever = $this->createAclRetriever();
}
Expand Down Expand Up @@ -66,8 +72,9 @@ public function testGetAllowedResourcesByUserTypeCustomer()

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->expectException(AuthorizationException::class);
$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);
}
Expand All @@ -89,41 +96,51 @@ protected function createAclRetriever()
{
$this->roleMock = $this->createPartialMock(Role::class, ['getId', '__wakeup']);

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

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

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

/** @var MockObject|RulesCollection $rulesCollectionMock */
/**
* @var RulesCollection|MockObject $rulesCollectionMock
*/
$rulesCollectionMock = $this->createPartialMock(
\Magento\Authorization\Model\ResourceModel\Rules\Collection::class,
RulesCollection::class,
['getByRoles', 'load', 'getItems']
);
$rulesCollectionMock->method('getByRoles')->will($this->returnSelf());
Expand All @@ -132,23 +149,29 @@ protected function createAclRetriever()
$this->returnValue([$rulesMock1, $rulesMock2])
);

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

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

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

return new AclRetriever(
$aclBuilderMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use PHPUnit\Framework\TestCase;

/**
* Test class for \Magento\Authorization\Model\Acl\Loader\Role
* @covers \Magento\Authorization\Model\Acl\Loader\Role
*/
class RoleTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use PHPUnit\Framework\TestCase;

/**
* Test class for \Magento\Authorization\Model\Acl\Loader\Rule
* @covers \Magento\Authorization\Model\Acl\Loader\Rule
*/
class RuleTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
<?php declare(strict_types=1);
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Authorization\Test\Unit\Model;

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

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @covers \Magento\Authorization\Model\CompositeUserContext
*/
class CompositeUserContextTest extends TestCase
{
/**
* @var CompositeUserContext
*/
protected $userContext;
private $userContext;

/**
* @var CompositeHelper
* @var CompositeHelper|MockObject
*/
protected $compositeHelperMock;
private $compositeHelperMock;

/**
* @var ObjectManager
*/
protected $objectManager;
private $objectManager;

protected function setUp(): void
{
$this->objectManager = new ObjectManager($this);
$this->compositeHelperMock = $this->getMockBuilder(\Magento\Framework\ObjectManager\Helper\Composite::class)
$this->compositeHelperMock = $this->getMockBuilder(CompositeHelper::class)
->disableOriginalConstructor()
->setMethods(['filterAndSortDeclaredComponents'])
->getMock();
$this->compositeHelperMock
->expects($this->any())
->method('filterAndSortDeclaredComponents')
->will($this->returnArgument(0));
$this->userContext = $this->objectManager->getObject(
Expand Down Expand Up @@ -68,8 +72,8 @@ public function testGetUserId()
$expectedUserType = 'Customer';
$userContextMock = $this->getMockBuilder(CompositeUserContext::class)
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
$userContextMock->method('getUserId')->will($this->returnValue($expectedUserId));
$userContextMock->method('getUserType')->will($this->returnValue($expectedUserType));
$userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
$userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
$contexts = [
[
'sortOrder' => 10,
Expand All @@ -90,8 +94,8 @@ public function testGetUserType()
$expectedUserType = 'Customer';
$userContextMock = $this->getMockBuilder(CompositeUserContext::class)
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
$userContextMock->method('getUserId')->will($this->returnValue($expectedUserId));
$userContextMock->method('getUserType')->will($this->returnValue($expectedUserType));
$userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
$userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
$contexts = [
[
'sortOrder' => 10,
Expand Down Expand Up @@ -137,7 +141,7 @@ public function testEmptyUserContext()
$expectedUserId = null;
$userContextMock = $this->getMockBuilder(CompositeUserContext::class)
->disableOriginalConstructor()->setMethods(['getUserId'])->getMock();
$userContextMock->method('getUserId')
$userContextMock->expects($this->any())->method('getUserId')
->will($this->returnValue($expectedUserId));
$contexts = [
[
Expand All @@ -163,8 +167,8 @@ protected function createUserContextMock($userId = null, $userType = null)
$useContextMock = $this->getMockBuilder(CompositeUserContext::class)
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
if ($userId !== null && $userType !== null) {
$useContextMock->method('getUserId')->will($this->returnValue($userId));
$useContextMock->method('getUserType')->will($this->returnValue($userType));
$useContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($userId));
$useContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($userType));
}
return $useContextMock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\Authorization\Test\Unit\Model\ResourceModel;

use Magento\Authorization\Model\ResourceModel\Rules;
use Magento\Authorization\Model\Rules as RulesModel;
use Magento\Framework\Acl\Builder;
use Magento\Framework\Acl\Data\CacheInterface;
use Magento\Framework\Acl\RootResource;
Expand All @@ -22,7 +23,7 @@
use Psr\Log\LoggerInterface;

/**
* Unit test for Rules resource model.
* @covers \Magento\Authorization\Model\ResourceModel\Rules
*
* Covers control flow logic.
* The resource saving is covered with integration test in \Magento\Authorization\Model\RulesTest.
Expand Down Expand Up @@ -77,9 +78,9 @@ class RulesTest extends TestCase
private $connectionMock;

/**
* @var \Magento\Authorization\Model\Rules|MockObject
* @var RulesModel|MockObject
*/
private $ruleMock;
private $rulesModelMock;

/**
* @inheritDoc
Expand Down Expand Up @@ -137,12 +138,12 @@ protected function setUp(): void
$this->aclBuilderMock->method('getConfigCache')
->willReturn($this->aclDataCacheMock);

$this->ruleMock = $this->getMockBuilder(\Magento\Authorization\Model\Rules::class)
$this->rulesModelMock = $this->getMockBuilder(RulesModel::class)
->disableOriginalConstructor()
->setMethods(['getRoleId'])
->getMock();

$this->ruleMock->method('getRoleId')
$this->rulesModelMock->method('getRoleId')
->willReturn(self::TEST_ROLE_ID);

$objectManager = new ObjectManager($this);
Expand Down Expand Up @@ -175,16 +176,17 @@ public function testSaveRelNoResources()
$this->aclDataCacheMock->expects($this->once())
->method('clean');

$this->model->saveRel($this->ruleMock);
$this->model->saveRel($this->rulesModelMock);
}

/**
* Test LocalizedException throw case.
*/
public function testLocalizedExceptionOccurance()
{
$this->expectException('Magento\Framework\Exception\LocalizedException');
$this->expectExceptionMessage('TestException');
$this->expectException(LocalizedException::class);
$this->expectExceptionMessage("TestException");

$exceptionPhrase = $this->getMockBuilder(Phrase::class)
->disableOriginalConstructor()
->setMethods(['render'])
Expand All @@ -204,7 +206,7 @@ public function testLocalizedExceptionOccurance()

$this->connectionMock->expects($this->once())->method('rollBack');

$this->model->saveRel($this->ruleMock);
$this->model->saveRel($this->rulesModelMock);
}

/**
Expand All @@ -225,6 +227,6 @@ public function testGenericExceptionOccurance()
$this->connectionMock->expects($this->once())->method('rollBack');
$this->loggerMock->expects($this->once())->method('critical')->with($exception);

$this->model->saveRel($this->ruleMock);
$this->model->saveRel($this->rulesModelMock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
</annotations>

<waitForElementVisible selector="{{AdminHeaderSection.adminUserAccountText}}" stepKey="waitForAdminAccountTextVisible"/>
<seeElement selector="{{AdminHeaderSection.adminUserAccountText}}" stepKey="assertAdminAccountTextElement"/>
</actionGroup>
</actionGroups>
Loading

0 comments on commit 930d4e2

Please sign in to comment.