Skip to content

Commit 120ee13

Browse files
author
Oleksii Korshenko
authored
Merge pull request #27718 from lbajsarowicz/phpunit8/module-Authorization
#27500 Prepare Authorization module Tests for PHPUnit 8
2 parents 12dc579 + cfc65f5 commit 120ee13

File tree

5 files changed

+74
-69
lines changed

5 files changed

+74
-69
lines changed
Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
@@ -7,28 +7,34 @@
77
namespace Magento\Authorization\Test\Unit\Model\Acl;
88

99
use Magento\Authorization\Model\Acl\AclRetriever;
10-
1110
use Magento\Authorization\Model\ResourceModel\Role\Collection as RoleCollection;
1211
use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory as RoleCollectionFactory;
1312
use Magento\Authorization\Model\ResourceModel\Rules\Collection as RulesCollection;
1413
use Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory as RulesCollectionFactory;
1514
use Magento\Authorization\Model\Role;
15+
use Magento\Authorization\Model\Rules;
16+
1617
use Magento\Authorization\Model\UserContextInterface;
18+
use Magento\Framework\Acl;
19+
use Magento\Framework\Acl\Builder;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use PHPUnit\Framework\TestCase;
22+
use Psr\Log\LoggerInterface;
1723

1824
/**
1925
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2026
*/
21-
class AclRetrieverTest extends \PHPUnit\Framework\TestCase
27+
class AclRetrieverTest extends TestCase
2228
{
2329
/**
2430
* @var AclRetriever
2531
*/
2632
protected $aclRetriever;
2733

28-
/** @var \PHPUnit_Framework_MockObject_MockObject|Role $roleMock */
34+
/** @var MockObject|Role $roleMock */
2935
protected $roleMock;
3036

31-
protected function setup()
37+
protected function setup(): void
3238
{
3339
$this->aclRetriever = $this->createAclRetriever();
3440
}
@@ -58,19 +64,17 @@ public function testGetAllowedResourcesByUserTypeCustomer()
5864
);
5965
}
6066

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

7175
public function testGetAllowedResourcesByUser()
7276
{
73-
$this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(1));
77+
$this->roleMock->method('getId')->will($this->returnValue(1));
7478
$expectedResources = ['Magento_Backend::dashboard', 'Magento_Cms::page'];
7579
$this->assertEquals(
7680
$expectedResources,
@@ -83,74 +87,74 @@ public function testGetAllowedResourcesByUser()
8387
*/
8488
protected function createAclRetriever()
8589
{
86-
$this->roleMock = $this->createPartialMock(\Magento\Authorization\Model\Role::class, ['getId', '__wakeup']);
90+
$this->roleMock = $this->createPartialMock(Role::class, ['getId', '__wakeup']);
8791

88-
/** @var \PHPUnit_Framework_MockObject_MockObject|RoleCollection $roleCollectionMock */
92+
/** @var MockObject|RoleCollection $roleCollectionMock */
8993
$roleCollectionMock = $this->createPartialMock(
9094
\Magento\Authorization\Model\ResourceModel\Role\Collection::class,
9195
['setUserFilter', 'getFirstItem']
9296
);
93-
$roleCollectionMock->expects($this->any())->method('setUserFilter')->will($this->returnSelf());
94-
$roleCollectionMock->expects($this->any())->method('getFirstItem')->will($this->returnValue($this->roleMock));
97+
$roleCollectionMock->method('setUserFilter')->will($this->returnSelf());
98+
$roleCollectionMock->method('getFirstItem')->will($this->returnValue($this->roleMock));
9599

96-
/** @var \PHPUnit_Framework_MockObject_MockObject|RoleCollectionFactory $roleCollectionFactoryMock */
100+
/** @var MockObject|RoleCollectionFactory $roleCollectionFactoryMock */
97101
$roleCollectionFactoryMock = $this->createPartialMock(
98102
\Magento\Authorization\Model\ResourceModel\Role\CollectionFactory::class,
99103
['create']
100104
);
101-
$roleCollectionFactoryMock->expects($this->any())->method('create')->will(
105+
$roleCollectionFactoryMock->method('create')->will(
102106
$this->returnValue($roleCollectionMock)
103107
);
104108

105-
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Authorization\Model\Rules $rulesMock1 */
109+
/** @var MockObject|Rules $rulesMock1 */
106110
$rulesMock1 = $this->createPartialMock(
107-
\Magento\Authorization\Model\Rules::class,
111+
Rules::class,
108112
['getResourceId', '__wakeup']
109113
);
110-
$rulesMock1->expects($this->any())->method('getResourceId')->will(
114+
$rulesMock1->method('getResourceId')->will(
111115
$this->returnValue('Magento_Backend::dashboard')
112116
);
113-
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Authorization\Model\Rules $rulesMock1 */
117+
/** @var MockObject|Rules $rulesMock1 */
114118
$rulesMock2 = $this->createPartialMock(
115-
\Magento\Authorization\Model\Rules::class,
119+
Rules::class,
116120
['getResourceId', '__wakeup']
117121
);
118-
$rulesMock2->expects($this->any())->method('getResourceId')->will($this->returnValue('Magento_Cms::page'));
122+
$rulesMock2->method('getResourceId')->will($this->returnValue('Magento_Cms::page'));
119123

120-
/** @var \PHPUnit_Framework_MockObject_MockObject|RulesCollection $rulesCollectionMock */
124+
/** @var MockObject|RulesCollection $rulesCollectionMock */
121125
$rulesCollectionMock = $this->createPartialMock(
122126
\Magento\Authorization\Model\ResourceModel\Rules\Collection::class,
123127
['getByRoles', 'load', 'getItems']
124128
);
125-
$rulesCollectionMock->expects($this->any())->method('getByRoles')->will($this->returnSelf());
126-
$rulesCollectionMock->expects($this->any())->method('load')->will($this->returnSelf());
127-
$rulesCollectionMock->expects($this->any())->method('getItems')->will(
129+
$rulesCollectionMock->method('getByRoles')->will($this->returnSelf());
130+
$rulesCollectionMock->method('load')->will($this->returnSelf());
131+
$rulesCollectionMock->method('getItems')->will(
128132
$this->returnValue([$rulesMock1, $rulesMock2])
129133
);
130134

131-
/** @var \PHPUnit_Framework_MockObject_MockObject|RulesCollectionFactory $rulesCollectionFactoryMock */
135+
/** @var MockObject|RulesCollectionFactory $rulesCollectionFactoryMock */
132136
$rulesCollectionFactoryMock = $this->createPartialMock(
133137
\Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory::class,
134138
['create']
135139
);
136-
$rulesCollectionFactoryMock->expects($this->any())->method('create')->will(
140+
$rulesCollectionFactoryMock->method('create')->will(
137141
$this->returnValue($rulesCollectionMock)
138142
);
139143

140-
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Acl $aclMock */
141-
$aclMock = $this->createPartialMock(\Magento\Framework\Acl::class, ['has', 'isAllowed']);
142-
$aclMock->expects($this->any())->method('has')->will($this->returnValue(true));
143-
$aclMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true));
144+
/** @var MockObject|Acl $aclMock */
145+
$aclMock = $this->createPartialMock(Acl::class, ['has', 'isAllowed']);
146+
$aclMock->method('has')->will($this->returnValue(true));
147+
$aclMock->method('isAllowed')->will($this->returnValue(true));
144148

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

149153
return new AclRetriever(
150154
$aclBuilderMock,
151155
$roleCollectionFactoryMock,
152156
$rulesCollectionFactoryMock,
153-
$this->createMock(\Psr\Log\LoggerInterface::class)
157+
$this->createMock(LoggerInterface::class)
154158
);
155159
}
156160
}

app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RoleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class RoleTest extends TestCase
6868
/**
6969
* @inheritDoc
7070
*/
71-
protected function setUp()
71+
protected function setUp(): void
7272
{
7373
$this->groupFactoryMock = $this->getMockBuilder(GroupFactory::class)
7474
->setMethods(['create', 'getModelInstance'])

app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RuleTest extends TestCase
5050
/**
5151
* @inheritDoc
5252
*/
53-
protected function setUp()
53+
protected function setUp(): void
5454
{
5555
$this->rootResource = new RootResource('Magento_Backend::all');
5656
$this->resourceMock = $this->createPartialMock(

app/code/Magento/Authorization/Test/Unit/Model/CompositeUserContextTest.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
66

77
namespace Magento\Authorization\Test\Unit\Model;
88

9-
use \Magento\Authorization\Model\CompositeUserContext;
10-
9+
use Magento\Authorization\Model\CompositeUserContext;
1110
use Magento\Framework\ObjectManager\Helper\Composite as CompositeHelper;
1211
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1312

14-
class CompositeUserContextTest extends \PHPUnit\Framework\TestCase
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class CompositeUserContextTest extends TestCase
1517
{
1618
/**
1719
* @var CompositeUserContext
@@ -28,19 +30,18 @@ class CompositeUserContextTest extends \PHPUnit\Framework\TestCase
2830
*/
2931
protected $objectManager;
3032

31-
protected function setUp()
33+
protected function setUp(): void
3234
{
3335
$this->objectManager = new ObjectManager($this);
3436
$this->compositeHelperMock = $this->getMockBuilder(\Magento\Framework\ObjectManager\Helper\Composite::class)
3537
->disableOriginalConstructor()
3638
->setMethods(['filterAndSortDeclaredComponents'])
3739
->getMock();
3840
$this->compositeHelperMock
39-
->expects($this->any())
4041
->method('filterAndSortDeclaredComponents')
4142
->will($this->returnArgument(0));
4243
$this->userContext = $this->objectManager->getObject(
43-
\Magento\Authorization\Model\CompositeUserContext::class,
44+
CompositeUserContext::class,
4445
['compositeHelper' => $this->compositeHelperMock]
4546
);
4647
}
@@ -55,7 +56,7 @@ public function testConstructor()
5556
],
5657
];
5758
$model = $this->objectManager->getObject(
58-
\Magento\Authorization\Model\CompositeUserContext::class,
59+
CompositeUserContext::class,
5960
['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
6061
);
6162
$this->verifyUserContextIsAdded($model, $userContextMock);
@@ -65,18 +66,18 @@ public function testGetUserId()
6566
{
6667
$expectedUserId = 1;
6768
$expectedUserType = 'Customer';
68-
$userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
69+
$userContextMock = $this->getMockBuilder(CompositeUserContext::class)
6970
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
70-
$userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
71-
$userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
71+
$userContextMock->method('getUserId')->will($this->returnValue($expectedUserId));
72+
$userContextMock->method('getUserType')->will($this->returnValue($expectedUserType));
7273
$contexts = [
7374
[
7475
'sortOrder' => 10,
7576
'type' => $userContextMock,
7677
],
7778
];
7879
$this->userContext = $this->objectManager->getObject(
79-
\Magento\Authorization\Model\CompositeUserContext::class,
80+
CompositeUserContext::class,
8081
['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
8182
);
8283
$actualUserId = $this->userContext->getUserId();
@@ -87,18 +88,18 @@ public function testGetUserType()
8788
{
8889
$expectedUserId = 1;
8990
$expectedUserType = 'Customer';
90-
$userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
91+
$userContextMock = $this->getMockBuilder(CompositeUserContext::class)
9192
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
92-
$userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
93-
$userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
93+
$userContextMock->method('getUserId')->will($this->returnValue($expectedUserId));
94+
$userContextMock->method('getUserType')->will($this->returnValue($expectedUserType));
9495
$contexts = [
9596
[
9697
'sortOrder' => 10,
9798
'type' => $userContextMock,
9899
],
99100
];
100101
$this->userContext = $this->objectManager->getObject(
101-
\Magento\Authorization\Model\CompositeUserContext::class,
102+
CompositeUserContext::class,
102103
['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
103104
);
104105
$actualUserType = $this->userContext->getUserType();
@@ -109,7 +110,7 @@ public function testUserContextCaching()
109110
{
110111
$expectedUserId = 1;
111112
$expectedUserType = 'Customer';
112-
$userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
113+
$userContextMock = $this->getMockBuilder(CompositeUserContext::class)
113114
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
114115
$userContextMock->expects($this->exactly(3))->method('getUserType')
115116
->will($this->returnValue($expectedUserType));
@@ -122,7 +123,7 @@ public function testUserContextCaching()
122123
],
123124
];
124125
$this->userContext = $this->objectManager->getObject(
125-
\Magento\Authorization\Model\CompositeUserContext::class,
126+
CompositeUserContext::class,
126127
['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
127128
);
128129
$this->userContext->getUserId();
@@ -134,9 +135,9 @@ public function testUserContextCaching()
134135
public function testEmptyUserContext()
135136
{
136137
$expectedUserId = null;
137-
$userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
138+
$userContextMock = $this->getMockBuilder(CompositeUserContext::class)
138139
->disableOriginalConstructor()->setMethods(['getUserId'])->getMock();
139-
$userContextMock->expects($this->any())->method('getUserId')
140+
$userContextMock->method('getUserId')
140141
->will($this->returnValue($expectedUserId));
141142
$contexts = [
142143
[
@@ -145,7 +146,7 @@ public function testEmptyUserContext()
145146
],
146147
];
147148
$this->userContext = $this->objectManager->getObject(
148-
\Magento\Authorization\Model\CompositeUserContext::class,
149+
CompositeUserContext::class,
149150
['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
150151
);
151152
$actualUserId = $this->userContext->getUserId();
@@ -155,15 +156,15 @@ public function testEmptyUserContext()
155156
/**
156157
* @param int|null $userId
157158
* @param string|null $userType
158-
* @return \PHPUnit_Framework_MockObject_MockObject
159+
* @return MockObject
159160
*/
160161
protected function createUserContextMock($userId = null, $userType = null)
161162
{
162-
$useContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
163+
$useContextMock = $this->getMockBuilder(CompositeUserContext::class)
163164
->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
164165
if ($userId !== null && $userType !== null) {
165-
$useContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($userId));
166-
$useContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($userType));
166+
$useContextMock->method('getUserId')->will($this->returnValue($userId));
167+
$useContextMock->method('getUserType')->will($this->returnValue($userType));
167168
}
168169
return $useContextMock;
169170
}
@@ -175,7 +176,7 @@ protected function createUserContextMock($userId = null, $userType = null)
175176
protected function verifyUserContextIsAdded($model, $userContextMock)
176177
{
177178
$userContext = new \ReflectionProperty(
178-
\Magento\Authorization\Model\CompositeUserContext::class,
179+
CompositeUserContext::class,
179180
'userContexts'
180181
);
181182
$userContext->setAccessible(true);

app/code/Magento/Authorization/Test/Unit/Model/ResourceModel/RulesTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Acl\RootResource;
1414
use Magento\Framework\App\ResourceConnection;
1515
use Magento\Framework\DB\Adapter\AdapterInterface;
16+
use Magento\Framework\Exception\LocalizedException;
1617
use Magento\Framework\Model\ResourceModel\Db\Context;
1718
use Magento\Framework\Phrase;
1819
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -83,7 +84,7 @@ class RulesTest extends TestCase
8384
/**
8485
* @inheritDoc
8586
*/
86-
protected function setUp()
87+
protected function setUp(): void
8788
{
8889
$this->contextMock = $this->getMockBuilder(Context::class)
8990
->disableOriginalConstructor()
@@ -179,20 +180,19 @@ public function testSaveRelNoResources()
179180

180181
/**
181182
* Test LocalizedException throw case.
182-
*
183-
* @expectedException \Magento\Framework\Exception\LocalizedException
184-
* @expectedExceptionMessage TestException
185183
*/
186184
public function testLocalizedExceptionOccurance()
187185
{
186+
$this->expectException('Magento\Framework\Exception\LocalizedException');
187+
$this->expectExceptionMessage('TestException');
188188
$exceptionPhrase = $this->getMockBuilder(Phrase::class)
189189
->disableOriginalConstructor()
190190
->setMethods(['render'])
191191
->getMock();
192192

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

195-
$exception = new \Magento\Framework\Exception\LocalizedException($exceptionPhrase);
195+
$exception = new LocalizedException($exceptionPhrase);
196196

197197
$this->connectionMock->expects($this->once())
198198
->method('beginTransaction');

0 commit comments

Comments
 (0)