Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dea43a6

Browse files
alexandre-dauboisnicolas-grekas
authored andcommitted
[Tests] Migrate tests to static data providers
1 parent 0725a16 commit dea43a6

18 files changed

+595
-203
lines changed

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,14 @@ public function testGetUserIdentifier($user, string $username)
7373
$this->assertEquals($username, $token->getUserIdentifier());
7474
}
7575

76-
public function provideUsers()
76+
public static function provideUsers()
7777
{
7878
yield [new InMemoryUser('fabien', null), 'fabien'];
7979
}
8080

8181
/**
8282
* @dataProvider provideLegacyUsers
83+
*
8384
* @group legacy
8485
*/
8586
public function testLegacyGetUserIdentifier($user, string $username)
@@ -89,7 +90,7 @@ public function testLegacyGetUserIdentifier($user, string $username)
8990
$this->assertEquals($username, $token->getUserIdentifier());
9091
}
9192

92-
public function provideLegacyUsers()
93+
public static function provideLegacyUsers()
9394
{
9495
return [
9596
[new TestUser('fabien'), 'fabien'],
@@ -174,6 +175,7 @@ public function testSetUser($user)
174175

175176
/**
176177
* @group legacy
178+
*
177179
* @dataProvider getUserChanges
178180
*/
179181
public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $secondUser)
@@ -189,9 +191,9 @@ public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $
189191
$this->assertFalse($token->isAuthenticated());
190192
}
191193

192-
public function getUserChanges()
194+
public static function getUserChanges()
193195
{
194-
$user = $this->createMock(UserInterface::class);
196+
$user = new DummyUser();
195197

196198
return [
197199
['foo', 'bar'],
@@ -207,6 +209,7 @@ public function getUserChanges()
207209

208210
/**
209211
* @group legacy
212+
*
210213
* @dataProvider provideUsers
211214
* @dataProvider provideLegacyUsers
212215
*/
@@ -322,6 +325,36 @@ public function __unserialize(array $data): void
322325
}
323326
}
324327

328+
class DummyUser implements UserInterface
329+
{
330+
public function getRoles(): array
331+
{
332+
return [];
333+
}
334+
335+
public function getPassword(): ?string
336+
{
337+
return null;
338+
}
339+
340+
public function getSalt(): ?string
341+
{
342+
return null;
343+
}
344+
345+
public function eraseCredentials(): void
346+
{
347+
}
348+
349+
public function getUsername(): string
350+
{
351+
}
352+
353+
public function getUserIdentifier(): string
354+
{
355+
}
356+
}
357+
325358
class ConcreteToken extends AbstractToken
326359
{
327360
private $credentials = 'credentials_value';

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

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -116,62 +116,62 @@ public function decide(\Traversable $results): bool
116116
$this->assertTrue($manager->decide($token, ['ROLE_FOO']));
117117
}
118118

119-
public function getStrategyTests()
119+
public static function getStrategyTests(): array
120120
{
121121
return [
122122
// affirmative
123-
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, $this->getVoters(1, 0, 0), false, true, true],
124-
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, $this->getVoters(1, 2, 0), false, true, true],
125-
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, $this->getVoters(0, 1, 0), false, true, false],
126-
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, $this->getVoters(0, 0, 1), false, true, false],
127-
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, $this->getVoters(0, 0, 1), true, true, true],
123+
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, self::getVoters(1, 0, 0), false, true, true],
124+
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, self::getVoters(1, 2, 0), false, true, true],
125+
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, self::getVoters(0, 1, 0), false, true, false],
126+
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, self::getVoters(0, 0, 1), false, true, false],
127+
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, self::getVoters(0, 0, 1), true, true, true],
128128

129129
// consensus
130-
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(1, 0, 0), false, true, true],
131-
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(1, 2, 0), false, true, false],
132-
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(2, 1, 0), false, true, true],
130+
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(1, 0, 0), false, true, true],
131+
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(1, 2, 0), false, true, false],
132+
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(2, 1, 0), false, true, true],
133133

134-
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(0, 0, 1), false, true, false],
134+
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(0, 0, 1), false, true, false],
135135

136-
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(0, 0, 1), true, true, true],
136+
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(0, 0, 1), true, true, true],
137137

138-
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(2, 2, 0), false, true, true],
139-
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(2, 2, 1), false, true, true],
138+
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(2, 2, 0), false, true, true],
139+
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(2, 2, 1), false, true, true],
140140

141-
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(2, 2, 0), false, false, false],
142-
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(2, 2, 1), false, false, false],
141+
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(2, 2, 0), false, false, false],
142+
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(2, 2, 1), false, false, false],
143143

144144
// unanimous
145-
[AccessDecisionManager::STRATEGY_UNANIMOUS, $this->getVoters(1, 0, 0), false, true, true],
146-
[AccessDecisionManager::STRATEGY_UNANIMOUS, $this->getVoters(1, 0, 1), false, true, true],
147-
[AccessDecisionManager::STRATEGY_UNANIMOUS, $this->getVoters(1, 1, 0), false, true, false],
145+
[AccessDecisionManager::STRATEGY_UNANIMOUS, self::getVoters(1, 0, 0), false, true, true],
146+
[AccessDecisionManager::STRATEGY_UNANIMOUS, self::getVoters(1, 0, 1), false, true, true],
147+
[AccessDecisionManager::STRATEGY_UNANIMOUS, self::getVoters(1, 1, 0), false, true, false],
148148

149-
[AccessDecisionManager::STRATEGY_UNANIMOUS, $this->getVoters(0, 0, 2), false, true, false],
150-
[AccessDecisionManager::STRATEGY_UNANIMOUS, $this->getVoters(0, 0, 2), true, true, true],
149+
[AccessDecisionManager::STRATEGY_UNANIMOUS, self::getVoters(0, 0, 2), false, true, false],
150+
[AccessDecisionManager::STRATEGY_UNANIMOUS, self::getVoters(0, 0, 2), true, true, true],
151151

152152
// priority
153153
[AccessDecisionManager::STRATEGY_PRIORITY, [
154-
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
155-
$this->getVoter(VoterInterface::ACCESS_GRANTED),
156-
$this->getVoter(VoterInterface::ACCESS_DENIED),
157-
$this->getVoter(VoterInterface::ACCESS_DENIED),
154+
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
155+
self::getVoter(VoterInterface::ACCESS_GRANTED),
156+
self::getVoter(VoterInterface::ACCESS_DENIED),
157+
self::getVoter(VoterInterface::ACCESS_DENIED),
158158
], true, true, true],
159159

160160
[AccessDecisionManager::STRATEGY_PRIORITY, [
161-
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
162-
$this->getVoter(VoterInterface::ACCESS_DENIED),
163-
$this->getVoter(VoterInterface::ACCESS_GRANTED),
164-
$this->getVoter(VoterInterface::ACCESS_GRANTED),
161+
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
162+
self::getVoter(VoterInterface::ACCESS_DENIED),
163+
self::getVoter(VoterInterface::ACCESS_GRANTED),
164+
self::getVoter(VoterInterface::ACCESS_GRANTED),
165165
], true, true, false],
166166

167167
[AccessDecisionManager::STRATEGY_PRIORITY, [
168-
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
169-
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
168+
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
169+
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
170170
], false, true, false],
171171

172172
[AccessDecisionManager::STRATEGY_PRIORITY, [
173-
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
174-
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
173+
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
174+
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
175175
], true, true, true],
176176
];
177177
}
@@ -338,30 +338,37 @@ public function testCacheableVotersWithMultipleAttributesAndNonString()
338338
$this->assertTrue($manager->decide($token, ['foo', 1337], 'bar', true));
339339
}
340340

341-
protected function getVoters($grants, $denies, $abstains)
341+
protected static function getVoters($grants, $denies, $abstains): array
342342
{
343343
$voters = [];
344344
for ($i = 0; $i < $grants; ++$i) {
345-
$voters[] = $this->getVoter(VoterInterface::ACCESS_GRANTED);
345+
$voters[] = self::getVoter(VoterInterface::ACCESS_GRANTED);
346346
}
347347
for ($i = 0; $i < $denies; ++$i) {
348-
$voters[] = $this->getVoter(VoterInterface::ACCESS_DENIED);
348+
$voters[] = self::getVoter(VoterInterface::ACCESS_DENIED);
349349
}
350350
for ($i = 0; $i < $abstains; ++$i) {
351-
$voters[] = $this->getVoter(VoterInterface::ACCESS_ABSTAIN);
351+
$voters[] = self::getVoter(VoterInterface::ACCESS_ABSTAIN);
352352
}
353353

354354
return $voters;
355355
}
356356

357-
protected function getVoter($vote)
357+
protected static function getVoter($vote)
358358
{
359-
$voter = $this->createMock(VoterInterface::class);
360-
$voter->expects($this->any())
361-
->method('vote')
362-
->willReturn($vote);
359+
return new class($vote) implements VoterInterface {
360+
private $vote;
363361

364-
return $voter;
362+
public function __construct(int $vote)
363+
{
364+
$this->vote = $vote;
365+
}
366+
367+
public function vote(TokenInterface $token, $subject, array $attributes)
368+
{
369+
return $this->vote;
370+
}
371+
};
365372
}
366373

367374
private function getExpectedVoter(int $vote): VoterInterface

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Security\Core\Authorization\DebugAccessDecisionManager;
1919
use Symfony\Component\Security\Core\Authorization\TraceableAccessDecisionManager;
2020
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
21+
use Symfony\Component\Security\Core\Tests\Fixtures\DummyVoter;
2122

2223
class TraceableAccessDecisionManagerTest extends TestCase
2324
{
@@ -50,10 +51,10 @@ public function testDecideLog(array $expectedLog, array $attributes, $object, ar
5051
$this->assertEquals($expectedLog, $adm->getDecisionLog());
5152
}
5253

53-
public function provideObjectsAndLogs(): \Generator
54+
public static function provideObjectsAndLogs(): \Generator
5455
{
55-
$voter1 = $this->getMockForAbstractClass(VoterInterface::class);
56-
$voter2 = $this->getMockForAbstractClass(VoterInterface::class);
56+
$voter1 = new DummyVoter();
57+
$voter2 = new DummyVoter();
5758

5859
yield [
5960
[[

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ public function testVoteWithTokenThatReturnsRoleNames($roles, $attributes, $expe
3232
$this->assertSame($expected, $voter->vote($this->getTokenWithRoleNames($roles, $tokenExpectsGetRoles), null, $attributes));
3333
}
3434

35-
public function getVoteTests()
35+
public static function getVoteTests()
3636
{
3737
return [
3838
[[], [], VoterInterface::ACCESS_ABSTAIN, false, false],
3939
[[], ['FOO'], VoterInterface::ACCESS_ABSTAIN, false, false],
4040

41-
[[], [$this->createExpression()], VoterInterface::ACCESS_DENIED, true, false],
41+
[[], [self::createExpression()], VoterInterface::ACCESS_DENIED, true, false],
4242

43-
[['ROLE_FOO'], [$this->createExpression(), $this->createExpression()], VoterInterface::ACCESS_GRANTED],
44-
[['ROLE_BAR', 'ROLE_FOO'], [$this->createExpression()], VoterInterface::ACCESS_GRANTED],
43+
[['ROLE_FOO'], [self::createExpression(), self::createExpression()], VoterInterface::ACCESS_GRANTED],
44+
[['ROLE_BAR', 'ROLE_FOO'], [self::createExpression()], VoterInterface::ACCESS_GRANTED],
4545
];
4646
}
4747

@@ -81,8 +81,8 @@ protected function createAuthorizationChecker()
8181
return $this->createMock(AuthorizationCheckerInterface::class);
8282
}
8383

84-
protected function createExpression()
84+
protected static function createExpression()
8585
{
86-
return $this->createMock(Expression::class);
86+
return new Expression('');
8787
}
8888
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected function setUp(): void
2525
$this->token = $this->createMock(TokenInterface::class);
2626
}
2727

28-
public function getTests()
28+
public static function getTests(): array
2929
{
3030
$voter = new VoterTest_Voter();
3131
$integerVoter = new IntegerVoterTest_Voter();
@@ -41,7 +41,7 @@ public function getTests()
4141

4242
[$voter, ['DELETE'], VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if no attribute is supported'],
4343

44-
[$voter, ['EDIT'], VoterInterface::ACCESS_ABSTAIN, $this, 'ACCESS_ABSTAIN if class is not supported'],
44+
[$voter, ['EDIT'], VoterInterface::ACCESS_ABSTAIN, new class() {}, 'ACCESS_ABSTAIN if class is not supported'],
4545

4646
[$voter, ['EDIT'], VoterInterface::ACCESS_ABSTAIN, null, 'ACCESS_ABSTAIN if object is null'],
4747

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Tests\Fixtures;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
16+
17+
final class DummyVoter implements VoterInterface
18+
{
19+
public function vote(TokenInterface $token, $subject, array $attributes): int
20+
{
21+
}
22+
}

0 commit comments

Comments
 (0)