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

Skip to content

[Tests] Migrate tests to static data providers #49345

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

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
Expand Up @@ -73,13 +73,14 @@ public function testGetUserIdentifier($user, string $username)
$this->assertEquals($username, $token->getUserIdentifier());
}

public function provideUsers()
public static function provideUsers()
{
yield [new InMemoryUser('fabien', null), 'fabien'];
}

/**
* @dataProvider provideLegacyUsers
*
* @group legacy
*/
public function testLegacyGetUserIdentifier($user, string $username)
Expand All @@ -89,7 +90,7 @@ public function testLegacyGetUserIdentifier($user, string $username)
$this->assertEquals($username, $token->getUserIdentifier());
}

public function provideLegacyUsers()
public static function provideLegacyUsers()
{
return [
[new TestUser('fabien'), 'fabien'],
Expand Down Expand Up @@ -174,6 +175,7 @@ public function testSetUser($user)

/**
* @group legacy
*
* @dataProvider getUserChanges
*/
public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $secondUser)
Expand All @@ -189,9 +191,9 @@ public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $
$this->assertFalse($token->isAuthenticated());
}

public function getUserChanges()
public static function getUserChanges()
{
$user = $this->createMock(UserInterface::class);
$user = new DummyUser();

return [
['foo', 'bar'],
Expand All @@ -207,6 +209,7 @@ public function getUserChanges()

/**
* @group legacy
*
* @dataProvider provideUsers
* @dataProvider provideLegacyUsers
*/
Expand Down Expand Up @@ -322,6 +325,36 @@ public function __unserialize(array $data): void
}
}

class DummyUser implements UserInterface
{
public function getRoles(): array
{
return [];
}

public function getPassword(): ?string
{
return null;
}

public function getSalt(): ?string
{
return null;
}

public function eraseCredentials(): void
{
}

public function getUsername(): string
{
}

public function getUserIdentifier(): string
{
}
}

class ConcreteToken extends AbstractToken
{
private $credentials = 'credentials_value';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,62 +116,62 @@ public function decide(\Traversable $results): bool
$this->assertTrue($manager->decide($token, ['ROLE_FOO']));
}

public function getStrategyTests()
public static function getStrategyTests(): array
{
return [
// affirmative
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, $this->getVoters(1, 0, 0), false, true, true],
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, $this->getVoters(1, 2, 0), false, true, true],
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, $this->getVoters(0, 1, 0), false, true, false],
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, $this->getVoters(0, 0, 1), false, true, false],
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, $this->getVoters(0, 0, 1), true, true, true],
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, self::getVoters(1, 0, 0), false, true, true],
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, self::getVoters(1, 2, 0), false, true, true],
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, self::getVoters(0, 1, 0), false, true, false],
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, self::getVoters(0, 0, 1), false, true, false],
[AccessDecisionManager::STRATEGY_AFFIRMATIVE, self::getVoters(0, 0, 1), true, true, true],

// consensus
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(1, 0, 0), false, true, true],
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(1, 2, 0), false, true, false],
[AccessDecisionManager::STRATEGY_CONSENSUS, $this->getVoters(2, 1, 0), false, true, true],
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(1, 0, 0), false, true, true],
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(1, 2, 0), false, true, false],
[AccessDecisionManager::STRATEGY_CONSENSUS, self::getVoters(2, 1, 0), false, true, true],

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

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

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

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

// unanimous
[AccessDecisionManager::STRATEGY_UNANIMOUS, $this->getVoters(1, 0, 0), false, true, true],
[AccessDecisionManager::STRATEGY_UNANIMOUS, $this->getVoters(1, 0, 1), false, true, true],
[AccessDecisionManager::STRATEGY_UNANIMOUS, $this->getVoters(1, 1, 0), false, true, false],
[AccessDecisionManager::STRATEGY_UNANIMOUS, self::getVoters(1, 0, 0), false, true, true],
[AccessDecisionManager::STRATEGY_UNANIMOUS, self::getVoters(1, 0, 1), false, true, true],
[AccessDecisionManager::STRATEGY_UNANIMOUS, self::getVoters(1, 1, 0), false, true, false],

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

// priority
[AccessDecisionManager::STRATEGY_PRIORITY, [
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
$this->getVoter(VoterInterface::ACCESS_GRANTED),
$this->getVoter(VoterInterface::ACCESS_DENIED),
$this->getVoter(VoterInterface::ACCESS_DENIED),
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
self::getVoter(VoterInterface::ACCESS_GRANTED),
self::getVoter(VoterInterface::ACCESS_DENIED),
self::getVoter(VoterInterface::ACCESS_DENIED),
], true, true, true],

[AccessDecisionManager::STRATEGY_PRIORITY, [
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
$this->getVoter(VoterInterface::ACCESS_DENIED),
$this->getVoter(VoterInterface::ACCESS_GRANTED),
$this->getVoter(VoterInterface::ACCESS_GRANTED),
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
self::getVoter(VoterInterface::ACCESS_DENIED),
self::getVoter(VoterInterface::ACCESS_GRANTED),
self::getVoter(VoterInterface::ACCESS_GRANTED),
], true, true, false],

[AccessDecisionManager::STRATEGY_PRIORITY, [
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
], false, true, false],

[AccessDecisionManager::STRATEGY_PRIORITY, [
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
self::getVoter(VoterInterface::ACCESS_ABSTAIN),
], true, true, true],
];
}
Expand Down Expand Up @@ -338,30 +338,37 @@ public function testCacheableVotersWithMultipleAttributesAndNonString()
$this->assertTrue($manager->decide($token, ['foo', 1337], 'bar', true));
}

protected function getVoters($grants, $denies, $abstains)
protected static function getVoters($grants, $denies, $abstains): array
{
$voters = [];
for ($i = 0; $i < $grants; ++$i) {
$voters[] = $this->getVoter(VoterInterface::ACCESS_GRANTED);
$voters[] = self::getVoter(VoterInterface::ACCESS_GRANTED);
}
for ($i = 0; $i < $denies; ++$i) {
$voters[] = $this->getVoter(VoterInterface::ACCESS_DENIED);
$voters[] = self::getVoter(VoterInterface::ACCESS_DENIED);
}
for ($i = 0; $i < $abstains; ++$i) {
$voters[] = $this->getVoter(VoterInterface::ACCESS_ABSTAIN);
$voters[] = self::getVoter(VoterInterface::ACCESS_ABSTAIN);
}

return $voters;
}

protected function getVoter($vote)
protected static function getVoter($vote)
{
$voter = $this->createMock(VoterInterface::class);
$voter->expects($this->any())
->method('vote')
->willReturn($vote);
return new class($vote) implements VoterInterface {
private $vote;

return $voter;
public function __construct(int $vote)
{
$this->vote = $vote;
}

public function vote(TokenInterface $token, $subject, array $attributes)
{
return $this->vote;
}
};
}

private function getExpectedVoter(int $vote): VoterInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Security\Core\Authorization\DebugAccessDecisionManager;
use Symfony\Component\Security\Core\Authorization\TraceableAccessDecisionManager;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Tests\Fixtures\DummyVoter;

class TraceableAccessDecisionManagerTest extends TestCase
{
Expand Down Expand Up @@ -50,10 +51,10 @@ public function testDecideLog(array $expectedLog, array $attributes, $object, ar
$this->assertEquals($expectedLog, $adm->getDecisionLog());
}

public function provideObjectsAndLogs(): \Generator
public static function provideObjectsAndLogs(): \Generator
{
$voter1 = $this->getMockForAbstractClass(VoterInterface::class);
$voter2 = $this->getMockForAbstractClass(VoterInterface::class);
$voter1 = new DummyVoter();
$voter2 = new DummyVoter();

yield [
[[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public function testVoteWithTokenThatReturnsRoleNames($roles, $attributes, $expe
$this->assertSame($expected, $voter->vote($this->getTokenWithRoleNames($roles, $tokenExpectsGetRoles), null, $attributes));
}

public function getVoteTests()
public static function getVoteTests()
{
return [
[[], [], VoterInterface::ACCESS_ABSTAIN, false, false],
[[], ['FOO'], VoterInterface::ACCESS_ABSTAIN, false, false],

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

[['ROLE_FOO'], [$this->createExpression(), $this->createExpression()], VoterInterface::ACCESS_GRANTED],
[['ROLE_BAR', 'ROLE_FOO'], [$this->createExpression()], VoterInterface::ACCESS_GRANTED],
[['ROLE_FOO'], [self::createExpression(), self::createExpression()], VoterInterface::ACCESS_GRANTED],
[['ROLE_BAR', 'ROLE_FOO'], [self::createExpression()], VoterInterface::ACCESS_GRANTED],
];
}

Expand Down Expand Up @@ -81,8 +81,8 @@ protected function createAuthorizationChecker()
return $this->createMock(AuthorizationCheckerInterface::class);
}

protected function createExpression()
protected static function createExpression()
{
return $this->createMock(Expression::class);
return new Expression('');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function setUp(): void
$this->token = $this->createMock(TokenInterface::class);
}

public function getTests()
public static function getTests(): array
{
$voter = new VoterTest_Voter();
$integerVoter = new IntegerVoterTest_Voter();
Expand All @@ -41,7 +41,7 @@ public function getTests()

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Security\Core\Tests\Fixtures;

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

final class DummyVoter implements VoterInterface
{
public function vote(TokenInterface $token, $subject, array $attributes): int
{
}
}
Loading