Skip to content

Commit aefefb7

Browse files
committed
bug #62796 [Security] do not use PHPUnit mock objects without configured expectations (xabbuh)
This PR was merged into the 6.4 branch. Discussion ---------- [Security] do not use PHPUnit mock objects without configured expectations | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | part of #62669 | License | MIT Commits ------- 65648c9 do not use PHPUnit mock objects without configured expectations
2 parents 9e4fb25 + 65648c9 commit aefefb7

File tree

47 files changed

+910
-830
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+910
-830
lines changed

src/Symfony/Component/Security/Core/Test/AccessDecisionStrategyTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Core\Test;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
1516
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1617
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
1718
use Symfony\Component\Security\Core\Authorization\Strategy\AccessDecisionStrategyInterface;
@@ -31,10 +32,9 @@ abstract class AccessDecisionStrategyTestCase extends TestCase
3132
*/
3233
final public function testDecide(AccessDecisionStrategyInterface $strategy, array $voters, bool $expected)
3334
{
34-
$token = $this->createMock(TokenInterface::class);
3535
$manager = new AccessDecisionManager($voters, $strategy);
3636

37-
$this->assertSame($expected, $manager->decide($token, ['ROLE_FOO']));
37+
$this->assertSame($expected, $manager->decide(new NullToken(), ['ROLE_FOO']));
3838
}
3939

4040
/**

src/Symfony/Component/Security/Core/Tests/Authentication/Token/RememberMeTokenTest.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
16-
use Symfony\Component\Security\Core\User\UserInterface;
16+
use Symfony\Component\Security\Core\User\InMemoryUser;
1717

1818
class RememberMeTokenTest extends TestCase
1919
{
@@ -40,13 +40,6 @@ public function testConstructorSecretCannotBeEmptyString()
4040

4141
protected function getUser($roles = ['ROLE_FOO'])
4242
{
43-
$user = $this->createMock(UserInterface::class);
44-
$user
45-
->expects($this->any())
46-
->method('getRoles')
47-
->willReturn($roles)
48-
;
49-
50-
return $user;
43+
return new InMemoryUser('John', 'password', $roles);
5144
}
5245
}

src/Symfony/Component/Security/Core/Tests/Authorization/AccessDecisionManagerTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\Assert;
1515
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
1617
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1718
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
1819
use Symfony\Component\Security\Core\Authorization\Strategy\AccessDecisionStrategyInterface;
@@ -31,8 +32,6 @@ public function provideBadVoterResults(): array
3132

3233
public function testVoterCalls()
3334
{
34-
$token = $this->createMock(TokenInterface::class);
35-
3635
$voters = [
3736
$this->getExpectedVoter(VoterInterface::ACCESS_DENIED),
3837
$this->getExpectedVoter(VoterInterface::ACCESS_GRANTED),
@@ -62,12 +61,12 @@ public function decide(\Traversable $results): bool
6261

6362
$manager = new AccessDecisionManager($voters, $strategy);
6463

65-
$this->assertTrue($manager->decide($token, ['ROLE_FOO']));
64+
$this->assertTrue($manager->decide(new NullToken(), ['ROLE_FOO']));
6665
}
6766

6867
public function testCacheableVoters()
6968
{
70-
$token = $this->createMock(TokenInterface::class);
69+
$token = new NullToken();
7170
$voter = $this->createMock(CacheableVoterInterface::class);
7271

7372
$voter
@@ -92,7 +91,7 @@ public function testCacheableVoters()
9291

9392
public function testCacheableVotersIgnoresNonStringAttributes()
9493
{
95-
$token = $this->createMock(TokenInterface::class);
94+
$token = new NullToken();
9695
$voter = $this->createMock(CacheableVoterInterface::class);
9796
$voter
9897
->expects($this->never())
@@ -114,7 +113,7 @@ public function testCacheableVotersIgnoresNonStringAttributes()
114113

115114
public function testCacheableVotersWithMultipleAttributes()
116115
{
117-
$token = $this->createMock(TokenInterface::class);
116+
$token = new NullToken();
118117
$voter = $this->createMock(CacheableVoterInterface::class);
119118
$voter
120119
->expects($this->exactly(2))
@@ -147,7 +146,7 @@ public function testCacheableVotersWithMultipleAttributes()
147146

148147
public function testCacheableVotersWithEmptyAttributes()
149148
{
150-
$token = $this->createMock(TokenInterface::class);
149+
$token = new NullToken();
151150
$voter = $this->createMock(CacheableVoterInterface::class);
152151
$voter
153152
->expects($this->never())
@@ -169,7 +168,7 @@ public function testCacheableVotersWithEmptyAttributes()
169168

170169
public function testCacheableVotersSupportsMethodsCalledOnce()
171170
{
172-
$token = $this->createMock(TokenInterface::class);
171+
$token = new NullToken();
173172
$voter = $this->createMock(CacheableVoterInterface::class);
174173
$voter
175174
->expects($this->once())
@@ -194,7 +193,7 @@ public function testCacheableVotersSupportsMethodsCalledOnce()
194193

195194
public function testCacheableVotersNotCalled()
196195
{
197-
$token = $this->createMock(TokenInterface::class);
196+
$token = new NullToken();
198197
$voter = $this->createMock(CacheableVoterInterface::class);
199198
$voter
200199
->expects($this->once())
@@ -214,7 +213,7 @@ public function testCacheableVotersNotCalled()
214213

215214
public function testCacheableVotersWithMultipleAttributesAndNonString()
216215
{
217-
$token = $this->createMock(TokenInterface::class);
216+
$token = new NullToken();
218217
$voter = $this->createMock(CacheableVoterInterface::class);
219218
$voter
220219
->expects($this->once())

src/Symfony/Component/Security/Core/Tests/Authorization/TraceableAccessDecisionManagerTest.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Core\Tests\Authorization;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
1516
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1617
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
1718
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
@@ -26,7 +27,7 @@ class TraceableAccessDecisionManagerTest extends TestCase
2627
*/
2728
public function testDecideLog(array $expectedLog, array $attributes, $object, array $voterVotes, bool $result)
2829
{
29-
$token = $this->createMock(TokenInterface::class);
30+
$token = new NullToken();
3031
$admMock = $this->createMock(AccessDecisionManagerInterface::class);
3132

3233
$adm = new TraceableAccessDecisionManager($admMock);
@@ -176,25 +177,15 @@ public static function provideObjectsAndLogs(): \Generator
176177
*/
177178
public function testAccessDecisionManagerCalledByVoter()
178179
{
179-
$voter1 = $this
180-
->getMockBuilder(VoterInterface::class)
181-
->onlyMethods(['vote'])
182-
->getMock();
180+
$voter1 = $this->createStub(VoterInterface::class);
183181

184-
$voter2 = $this
185-
->getMockBuilder(VoterInterface::class)
186-
->onlyMethods(['vote'])
187-
->getMock();
182+
$voter2 = $this->createStub(VoterInterface::class);
188183

189-
$voter3 = $this
190-
->getMockBuilder(VoterInterface::class)
191-
->onlyMethods(['vote'])
192-
->getMock();
184+
$voter3 = $this->createStub(VoterInterface::class);
193185

194186
$sut = new TraceableAccessDecisionManager(new AccessDecisionManager([$voter1, $voter2, $voter3]));
195187

196188
$voter1
197-
->expects($this->any())
198189
->method('vote')
199190
->willReturnCallback(function (TokenInterface $token, $subject, array $attributes) use ($sut, $voter1) {
200191
$vote = \in_array('attr1', $attributes) ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_ABSTAIN;
@@ -204,7 +195,6 @@ public function testAccessDecisionManagerCalledByVoter()
204195
});
205196

206197
$voter2
207-
->expects($this->any())
208198
->method('vote')
209199
->willReturnCallback(function (TokenInterface $token, $subject, array $attributes) use ($sut, $voter2) {
210200
if (\in_array('attr2', $attributes)) {
@@ -219,7 +209,6 @@ public function testAccessDecisionManagerCalledByVoter()
219209
});
220210

221211
$voter3
222-
->expects($this->any())
223212
->method('vote')
224213
->willReturnCallback(function (TokenInterface $token, $subject, array $attributes) use ($sut, $voter3) {
225214
if (\in_array('attr2', $attributes) && $subject) {
@@ -233,7 +222,7 @@ public function testAccessDecisionManagerCalledByVoter()
233222
return $vote;
234223
});
235224

236-
$token = $this->createMock(TokenInterface::class);
225+
$token = new NullToken();
237226
$sut->decide($token, ['attr1'], null);
238227
$sut->decide($token, ['attr2'], $obj = new \stdClass());
239228

@@ -270,7 +259,7 @@ public function testAccessDecisionManagerCalledByVoter()
270259

271260
public function testCustomAccessDecisionManagerReturnsEmptyStrategy()
272261
{
273-
$admMock = $this->createMock(AccessDecisionManagerInterface::class);
262+
$admMock = $this->createStub(AccessDecisionManagerInterface::class);
274263

275264
$adm = new TraceableAccessDecisionManager($admMock);
276265

src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AuthenticatedVoterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function getCredentials()
105105
}
106106

107107
if ('impersonated' === $authenticated) {
108-
return $this->getMockBuilder(SwitchUserToken::class)->disableOriginalConstructor()->getMock();
108+
return new SwitchUserToken(new InMemoryUser('John', 'password'), 'main', ['ROLE_USER'], new NullToken());
109109
}
110110

111111
return new NullToken();

src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ExpressionVoterTest.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\ExpressionLanguage\Expression;
1616
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
1717
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
18+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
1819
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1920
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
2021
use Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter;
@@ -47,42 +48,44 @@ public static function getVoteTests()
4748

4849
protected function getTokenWithRoleNames(array $roles, $tokenExpectsGetRoles = true)
4950
{
50-
$token = $this->createMock(AbstractToken::class);
51-
5251
if ($tokenExpectsGetRoles) {
53-
$token->expects($this->once())
52+
$mock = $this->createMock(AbstractToken::class);
53+
$mock->expects($this->once())
5454
->method('getRoleNames')
5555
->willReturn($roles);
56+
57+
return $mock;
5658
}
5759

58-
return $token;
60+
return new NullToken();
5961
}
6062

6163
protected function createExpressionLanguage($expressionLanguageExpectsEvaluate = true)
6264
{
63-
$mock = $this->createMock(ExpressionLanguage::class);
64-
6565
if ($expressionLanguageExpectsEvaluate) {
66+
$mock = $this->createMock(ExpressionLanguage::class);
6667
$mock->expects($this->once())
6768
->method('evaluate')
6869
->willReturn(true);
70+
71+
return $mock;
6972
}
7073

71-
return $mock;
74+
return new ExpressionLanguage();
7275
}
7376

7477
protected function createTrustResolver()
7578
{
76-
return $this->createMock(AuthenticationTrustResolverInterface::class);
79+
return $this->createStub(AuthenticationTrustResolverInterface::class);
7780
}
7881

7982
protected function createAuthorizationChecker()
8083
{
81-
return $this->createMock(AuthorizationCheckerInterface::class);
84+
return $this->createStub(AuthorizationCheckerInterface::class);
8285
}
8386

8487
protected static function createExpression()
8588
{
86-
return new Expression('');
89+
return new Expression('is_granted("ROLE_ADMIN") or is_granted("ROLE_MANAGER")');
8790
}
8891
}

src/Symfony/Component/Security/Core/Tests/Authorization/Voter/VoterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Core\Tests\Authorization\Voter;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
1516
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1617
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
1718
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
@@ -22,7 +23,7 @@ class VoterTest extends TestCase
2223

2324
protected function setUp(): void
2425
{
25-
$this->token = $this->createMock(TokenInterface::class);
26+
$this->token = new NullToken();
2627
}
2728

2829
public static function getTests(): array

src/Symfony/Component/Security/Core/Tests/User/ChainUserCheckerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Security\Core\User\ChainUserChecker;
16+
use Symfony\Component\Security\Core\User\InMemoryUser;
1617
use Symfony\Component\Security\Core\User\UserCheckerInterface;
17-
use Symfony\Component\Security\Core\User\UserInterface;
1818

1919
final class ChainUserCheckerTest extends TestCase
2020
{
2121
public function testForwardsPreAuthToAllUserCheckers()
2222
{
23-
$user = $this->createMock(UserInterface::class);
23+
$user = new InMemoryUser('John', 'password');
2424

2525
$checker1 = $this->createMock(UserCheckerInterface::class);
2626
$checker1->expects($this->once())
@@ -42,7 +42,7 @@ public function testForwardsPreAuthToAllUserCheckers()
4242

4343
public function testForwardsPostAuthToAllUserCheckers()
4444
{
45-
$user = $this->createMock(UserInterface::class);
45+
$user = new InMemoryUser('John', 'password');
4646

4747
$checker1 = $this->createMock(UserCheckerInterface::class);
4848
$checker1->expects($this->once())

0 commit comments

Comments
 (0)