diff --git a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AbstractTokenTest.php b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AbstractTokenTest.php index a52eb4753490d..1c767e1d886f2 100644 --- a/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AbstractTokenTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Authentication/Token/AbstractTokenTest.php @@ -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) @@ -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'], @@ -174,6 +175,7 @@ public function testSetUser($user) /** * @group legacy + * * @dataProvider getUserChanges */ public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $secondUser) @@ -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'], @@ -207,6 +209,7 @@ public function getUserChanges() /** * @group legacy + * * @dataProvider provideUsers * @dataProvider provideLegacyUsers */ @@ -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'; diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/AccessDecisionManagerTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/AccessDecisionManagerTest.php index 2cca853eb4e01..c378b19e38015 100644 --- a/src/Symfony/Component/Security/Core/Tests/Authorization/AccessDecisionManagerTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Authorization/AccessDecisionManagerTest.php @@ -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], ]; } @@ -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 diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/TraceableAccessDecisionManagerTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/TraceableAccessDecisionManagerTest.php index 4b97885eb7ad0..770495137c40e 100644 --- a/src/Symfony/Component/Security/Core/Tests/Authorization/TraceableAccessDecisionManagerTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Authorization/TraceableAccessDecisionManagerTest.php @@ -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 { @@ -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 [ [[ diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ExpressionVoterTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ExpressionVoterTest.php index a3e516950bcc5..369b17f0460ea 100644 --- a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ExpressionVoterTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/ExpressionVoterTest.php @@ -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], ]; } @@ -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(''); } } diff --git a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/VoterTest.php b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/VoterTest.php index 4b729d6a017df..25aff8e1ad841 100644 --- a/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/VoterTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Authorization/Voter/VoterTest.php @@ -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(); @@ -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'], diff --git a/src/Symfony/Component/Security/Core/Tests/Fixtures/DummyVoter.php b/src/Symfony/Component/Security/Core/Tests/Fixtures/DummyVoter.php new file mode 100644 index 0000000000000..1f923423a21ed --- /dev/null +++ b/src/Symfony/Component/Security/Core/Tests/Fixtures/DummyVoter.php @@ -0,0 +1,22 @@ + + * + * 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 + { + } +} diff --git a/src/Symfony/Component/Security/Csrf/Tests/CsrfTokenManagerTest.php b/src/Symfony/Component/Security/Csrf/Tests/CsrfTokenManagerTest.php index bd911987f1f2d..c1bff0522c9f6 100644 --- a/src/Symfony/Component/Security/Csrf/Tests/CsrfTokenManagerTest.php +++ b/src/Symfony/Component/Security/Csrf/Tests/CsrfTokenManagerTest.php @@ -21,13 +21,41 @@ /** * @author Bernhard Schussek + * @author Alexandre Daubois */ class CsrfTokenManagerTest extends TestCase { - /** - * @dataProvider getManagerGeneratorAndStorage - */ - public function testGetNonExistingToken($namespace, $manager, $storage, $generator) + public function testGetNonExistingTokenEmptyNamespace() + { + $this->assertGetNonExistingToken(...$this->getEmptyNamespaceMocks()); + } + + public function testGetNonExistingTokenHttpsNamespace() + { + $this->assertGetNonExistingToken(...$this->getHttpsNamespaceMocks()); + } + + public function testGetNonExistingTokenCustomNamespace() + { + $this->assertGetNonExistingToken(...$this->getCustomNamespaceMocks()); + } + + public function testGetNonExistingTokenRequestStack() + { + $this->assertGetNonExistingToken(...$this->getRequestStackMocks()); + } + + public function testGetNonExistingTokenClosure() + { + $this->assertGetNonExistingToken(...$this->getClosureMocks()); + } + + public function testGetNonExistingTokenRequestStackEmptyNamespace() + { + $this->assertGetNonExistingToken(...$this->getRequestStackWithEmptyNamespaceMocks()); + } + + private function assertGetNonExistingToken($namespace, $manager, $storage, $generator): void { $storage->expects($this->once()) ->method('hasToken') @@ -49,10 +77,37 @@ public function testGetNonExistingToken($namespace, $manager, $storage, $generat $this->assertNotSame('TOKEN', $token->getValue()); } - /** - * @dataProvider getManagerGeneratorAndStorage - */ - public function testUseExistingTokenIfAvailable($namespace, $manager, $storage) + public function testUseExistingTokenIfAvailableEmptyNamespace() + { + $this->assertUseExistingTokenIfAvailable(...$this->getEmptyNamespaceMocks()); + } + + public function testUseExistingTokenIfAvailableHttpsNamespace() + { + $this->assertUseExistingTokenIfAvailable(...$this->getHttpsNamespaceMocks()); + } + + public function testUseExistingTokenIfAvailableCustomNamespace() + { + $this->assertUseExistingTokenIfAvailable(...$this->getCustomNamespaceMocks()); + } + + public function testUseExistingTokenIfAvailableRequestStack() + { + $this->assertUseExistingTokenIfAvailable(...$this->getRequestStackMocks()); + } + + public function testUseExistingTokenIfAvailableClosure() + { + $this->assertUseExistingTokenIfAvailable(...$this->getClosureMocks()); + } + + public function testUseExistingTokenIfAvailableRequestStackEmptyNamespace() + { + $this->assertUseExistingTokenIfAvailable(...$this->getRequestStackWithEmptyNamespaceMocks()); + } + + private function assertUseExistingTokenIfAvailable($namespace, $manager, $storage): void { $storage->expects($this->once()) ->method('hasToken') @@ -71,10 +126,37 @@ public function testUseExistingTokenIfAvailable($namespace, $manager, $storage) $this->assertNotSame('TOKEN', $token->getValue()); } - /** - * @dataProvider getManagerGeneratorAndStorage - */ - public function testRandomizeTheToken($namespace, $manager, $storage) + public function testRandomizeTheTokenEmptyNamespace() + { + $this->assertRandomizeTheToken(...$this->getEmptyNamespaceMocks()); + } + + public function testRandomizeTheTokenHttpsNamespace() + { + $this->assertRandomizeTheToken(...$this->getHttpsNamespaceMocks()); + } + + public function testRandomizeTheTokenCustomNamespace() + { + $this->assertRandomizeTheToken(...$this->getCustomNamespaceMocks()); + } + + public function testRandomizeTheTokenRequestStack() + { + $this->assertRandomizeTheToken(...$this->getRequestStackMocks()); + } + + public function testRandomizeTheTokenClosure() + { + $this->assertRandomizeTheToken(...$this->getClosureMocks()); + } + + public function testRandomizeTheTokenRequestStackEmptyNamespace() + { + $this->assertRandomizeTheToken(...$this->getRequestStackWithEmptyNamespaceMocks()); + } + + private function assertRandomizeTheToken($namespace, $manager, $storage): void { $storage->expects($this->any()) ->method('hasToken') @@ -98,10 +180,37 @@ public function testRandomizeTheToken($namespace, $manager, $storage) $this->assertGreaterThan(2, \count(array_unique($lengths))); } - /** - * @dataProvider getManagerGeneratorAndStorage - */ - public function testRefreshTokenAlwaysReturnsNewToken($namespace, $manager, $storage, $generator) + public function testRefreshTokenAlwaysReturnsNewTokenEmptyNamespace() + { + $this->assertRefreshTokenAlwaysReturnsNewToken(...$this->getEmptyNamespaceMocks()); + } + + public function testRefreshTokenAlwaysReturnsNewTokenHttpsNamespace() + { + $this->assertRefreshTokenAlwaysReturnsNewToken(...$this->getHttpsNamespaceMocks()); + } + + public function testRefreshTokenAlwaysReturnsNewTokenCustomNamespace() + { + $this->assertRefreshTokenAlwaysReturnsNewToken(...$this->getCustomNamespaceMocks()); + } + + public function testRefreshTokenAlwaysReturnsNewTokenRequestStack() + { + $this->assertRefreshTokenAlwaysReturnsNewToken(...$this->getRequestStackMocks()); + } + + public function testRefreshTokenAlwaysReturnsNewTokenClosure() + { + $this->assertRefreshTokenAlwaysReturnsNewToken(...$this->getClosureMocks()); + } + + public function testRefreshTokenAlwaysReturnsNewTokenRequestStackEmptyNamespace() + { + $this->assertRefreshTokenAlwaysReturnsNewToken(...$this->getRequestStackWithEmptyNamespaceMocks()); + } + + private function assertRefreshTokenAlwaysReturnsNewToken($namespace, $manager, $storage, $generator): void { $storage->expects($this->never()) ->method('hasToken'); @@ -121,10 +230,37 @@ public function testRefreshTokenAlwaysReturnsNewToken($namespace, $manager, $sto $this->assertNotSame('TOKEN', $token->getValue()); } - /** - * @dataProvider getManagerGeneratorAndStorage - */ - public function testMatchingTokenIsValid($namespace, $manager, $storage) + public function testMatchingTokenIsValidEmptyNamespace() + { + $this->assertMatchingTokenIsValid(...$this->getEmptyNamespaceMocks()); + } + + public function testMatchingTokenIsValidHttpsNamespace() + { + $this->assertMatchingTokenIsValid(...$this->getHttpsNamespaceMocks()); + } + + public function testMatchingTokenIsValidCustomNamespace() + { + $this->assertMatchingTokenIsValid(...$this->getCustomNamespaceMocks()); + } + + public function testMatchingTokenIsValidRequestStack() + { + $this->assertMatchingTokenIsValid(...$this->getRequestStackMocks()); + } + + public function testMatchingTokenIsValidClosure() + { + $this->assertMatchingTokenIsValid(...$this->getClosureMocks()); + } + + public function testMatchingTokenIsValidRequestStackEmptyNamespace() + { + $this->assertMatchingTokenIsValid(...$this->getRequestStackWithEmptyNamespaceMocks()); + } + + private function assertMatchingTokenIsValid($namespace, $manager, $storage) { $storage->expects($this->exactly(2)) ->method('hasToken') @@ -141,10 +277,37 @@ public function testMatchingTokenIsValid($namespace, $manager, $storage) $this->assertTrue($manager->isTokenValid($token)); } - /** - * @dataProvider getManagerGeneratorAndStorage - */ - public function testMatchingTokenIsValidWithLegacyToken($namespace, $manager, $storage) + public function testMatchingTokenIsValidWithLegacyTokenEmptyNamespace() + { + $this->assertMatchingTokenIsValidWithLegacyToken(...$this->getEmptyNamespaceMocks()); + } + + public function testMatchingTokenIsValidWithLegacyTokenHttpsNamespace() + { + $this->assertMatchingTokenIsValidWithLegacyToken(...$this->getHttpsNamespaceMocks()); + } + + public function testMatchingTokenIsValidWithLegacyTokenCustomNamespace() + { + $this->assertMatchingTokenIsValidWithLegacyToken(...$this->getCustomNamespaceMocks()); + } + + public function testMatchingTokenIsValidWithLegacyTokenRequestStack() + { + $this->assertMatchingTokenIsValidWithLegacyToken(...$this->getRequestStackMocks()); + } + + public function testMatchingTokenIsValidWithLegacyTokenClosure() + { + $this->assertMatchingTokenIsValidWithLegacyToken(...$this->getClosureMocks()); + } + + public function testMatchingTokenIsValidWithLegacyTokenRequestStackEmptyNamespace() + { + $this->assertMatchingTokenIsValidWithLegacyToken(...$this->getRequestStackWithEmptyNamespaceMocks()); + } + + private function assertMatchingTokenIsValidWithLegacyToken($namespace, $manager, $storage): void { $storage->expects($this->once()) ->method('hasToken') @@ -159,10 +322,37 @@ public function testMatchingTokenIsValidWithLegacyToken($namespace, $manager, $s $this->assertTrue($manager->isTokenValid(new CsrfToken('token_id', 'TOKEN'))); } - /** - * @dataProvider getManagerGeneratorAndStorage - */ - public function testNonMatchingTokenIsNotValid($namespace, $manager, $storage) + public function testNonMatchingTokenIsNotValidEmptyNamespace() + { + $this->assertNonMatchingTokenIsNotValid(...$this->getEmptyNamespaceMocks()); + } + + public function testNonMatchingTokenIsNotValidHttpsNamespace() + { + $this->assertNonMatchingTokenIsNotValid(...$this->getHttpsNamespaceMocks()); + } + + public function testNonMatchingTokenIsNotValidCustomNamespace() + { + $this->assertNonMatchingTokenIsNotValid(...$this->getCustomNamespaceMocks()); + } + + public function testNonMatchingTokenIsNotValidRequestStack() + { + $this->assertNonMatchingTokenIsNotValid(...$this->getRequestStackMocks()); + } + + public function testNonMatchingTokenIsNotValidClosure() + { + $this->assertNonMatchingTokenIsNotValid(...$this->getClosureMocks()); + } + + public function testNonMatchingTokenIsNotValidRequestStackEmptyNamespace() + { + $this->assertNonMatchingTokenIsNotValid(...$this->getRequestStackWithEmptyNamespaceMocks()); + } + + private function assertNonMatchingTokenIsNotValid($namespace, $manager, $storage): void { $storage->expects($this->once()) ->method('hasToken') @@ -177,10 +367,37 @@ public function testNonMatchingTokenIsNotValid($namespace, $manager, $storage) $this->assertFalse($manager->isTokenValid(new CsrfToken('token_id', 'FOOBAR'))); } - /** - * @dataProvider getManagerGeneratorAndStorage - */ - public function testNonExistingTokenIsNotValid($namespace, $manager, $storage) + public function testNonExistingTokenIsNotValidEmptyNamespace() + { + $this->assertNonExistingTokenIsNotValid(...$this->getEmptyNamespaceMocks()); + } + + public function testNonExistingTokenIsNotValidHttpsNamespace() + { + $this->assertNonExistingTokenIsNotValid(...$this->getHttpsNamespaceMocks()); + } + + public function testNonExistingTokenIsNotValidCustomNamespace() + { + $this->assertNonExistingTokenIsNotValid(...$this->getCustomNamespaceMocks()); + } + + public function testNonExistingTokenIsNotValidRequestStack() + { + $this->assertNonExistingTokenIsNotValid(...$this->getRequestStackMocks()); + } + + public function testNonExistingTokenIsNotValidClosure() + { + $this->assertNonExistingTokenIsNotValid(...$this->getClosureMocks()); + } + + public function testNonExistingTokenIsNotValidRequestStackEmptyNamespace() + { + $this->assertNonExistingTokenIsNotValid(...$this->getRequestStackWithEmptyNamespaceMocks()); + } + + private function assertNonExistingTokenIsNotValid($namespace, $manager, $storage): void { $storage->expects($this->once()) ->method('hasToken') @@ -213,10 +430,37 @@ public function testTokenShouldNotTriggerDivisionByZero() $this->assertFalse($manager->isTokenValid(new CsrfToken('token_id', 'abc..ghi'))); } - /** - * @dataProvider getManagerGeneratorAndStorage - */ - public function testRemoveToken($namespace, $manager, $storage) + public function testRemoveTokenEmptyNamespace() + { + $this->assertRemoveToken(...$this->getEmptyNamespaceMocks()); + } + + public function testRemoveTokenHttpsNamespace() + { + $this->assertRemoveToken(...$this->getHttpsNamespaceMocks()); + } + + public function testRemoveTokenCustomNamespace() + { + $this->assertRemoveToken(...$this->getCustomNamespaceMocks()); + } + + public function testRemoveTokenRequestStack() + { + $this->assertRemoveToken(...$this->getRequestStackMocks()); + } + + public function testRemoveTokenClosure() + { + $this->assertRemoveToken(...$this->getClosureMocks()); + } + + public function testRemoveTokenRequestStackEmptyNamespace() + { + $this->assertRemoveToken(...$this->getRequestStackWithEmptyNamespaceMocks()); + } + + private function assertRemoveToken($namespace, $manager, $storage): void { $storage->expects($this->once()) ->method('removeToken') @@ -241,35 +485,52 @@ public function testNamespaced() $this->assertSame('foo', $token->getId()); } - public function getManagerGeneratorAndStorage() + private function getEmptyNamespaceMocks(): array { - $data = []; - [$generator, $storage] = $this->getGeneratorAndStorage(); - $data[] = ['', new CsrfTokenManager($generator, $storage, ''), $storage, $generator]; + return ['', new CsrfTokenManager($generator, $storage, ''), $storage, $generator]; + } + + private function getHttpsNamespaceMocks(): array + { [$generator, $storage] = $this->getGeneratorAndStorage(); - $data[] = ['https-', new CsrfTokenManager($generator, $storage), $storage, $generator]; + return ['https-', new CsrfTokenManager($generator, $storage), $storage, $generator]; + } + + private function getCustomNamespaceMocks(): array + { [$generator, $storage] = $this->getGeneratorAndStorage(); - $data[] = ['aNamespace-', new CsrfTokenManager($generator, $storage, 'aNamespace-'), $storage, $generator]; + return ['aNamespace-', new CsrfTokenManager($generator, $storage, 'aNamespace-'), $storage, $generator]; + } + + private function getRequestStackMocks(): array + { $requestStack = new RequestStack(); $requestStack->push(new Request([], [], [], [], [], ['HTTPS' => 'on'])); [$generator, $storage] = $this->getGeneratorAndStorage(); - $data[] = ['https-', new CsrfTokenManager($generator, $storage, $requestStack), $storage, $generator]; + return ['https-', new CsrfTokenManager($generator, $storage, $requestStack), $storage, $generator]; + } + + private function getClosureMocks(): array + { [$generator, $storage] = $this->getGeneratorAndStorage(); - $data[] = ['generated-', new CsrfTokenManager($generator, $storage, function () { + + return ['generated-', new CsrfTokenManager($generator, $storage, function () { return 'generated-'; }), $storage, $generator]; + } + private function getRequestStackWithEmptyNamespaceMocks(): array + { $requestStack = new RequestStack(); $requestStack->push(new Request()); [$generator, $storage] = $this->getGeneratorAndStorage(); - $data[] = ['', new CsrfTokenManager($generator, $storage, $requestStack), $storage, $generator]; - return $data; + return ['', new CsrfTokenManager($generator, $storage, $requestStack), $storage, $generator]; } private function getGeneratorAndStorage(): array diff --git a/src/Symfony/Component/Security/Http/Tests/Authentication/AuthenticatorManagerTest.php b/src/Symfony/Component/Security/Http/Tests/Authentication/AuthenticatorManagerTest.php index 96dea2730aa24..01677154b6482 100644 --- a/src/Symfony/Component/Security/Http/Tests/Authentication/AuthenticatorManagerTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Authentication/AuthenticatorManagerTest.php @@ -33,6 +33,7 @@ use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; use Symfony\Component\Security\Http\Event\AuthenticationTokenCreatedEvent; use Symfony\Component\Security\Http\Event\CheckPassportEvent; +use Symfony\Component\Security\Http\Tests\Fixtures\DummySupportsAuthenticator; class AuthenticatorManagerTest extends TestCase { @@ -64,15 +65,15 @@ public function testSupports($authenticators, $result) $this->assertEquals($result, $manager->supports($this->request)); } - public function provideSupportsData() + public static function provideSupportsData() { - yield [[$this->createAuthenticator(null), $this->createAuthenticator(null)], null]; - yield [[$this->createAuthenticator(null), $this->createAuthenticator(false)], null]; + yield [[self::createDummySupportsAuthenticator(null), self::createDummySupportsAuthenticator(null)], null]; + yield [[self::createDummySupportsAuthenticator(null), self::createDummySupportsAuthenticator(false)], null]; - yield [[$this->createAuthenticator(null), $this->createAuthenticator(true)], true]; - yield [[$this->createAuthenticator(true), $this->createAuthenticator(false)], true]; + yield [[self::createDummySupportsAuthenticator(null), self::createDummySupportsAuthenticator(true)], true]; + yield [[self::createDummySupportsAuthenticator(true), self::createDummySupportsAuthenticator(false)], true]; - yield [[$this->createAuthenticator(false), $this->createAuthenticator(false)], false]; + yield [[self::createDummySupportsAuthenticator(false), self::createDummySupportsAuthenticator(false)], false]; yield [[], false]; } @@ -351,7 +352,7 @@ public function log($level, $message, array $context = []): void $this->assertStringContainsString('Mock_TestInteractiveAuthenticator', $logger->logContexts[0]['authenticator']); } - private function createAuthenticator($supports = true) + private function createAuthenticator(?bool $supports = true) { $authenticator = $this->createMock(TestInteractiveAuthenticator::class); $authenticator->expects($this->any())->method('supports')->willReturn($supports); @@ -359,6 +360,11 @@ private function createAuthenticator($supports = true) return $authenticator; } + private static function createDummySupportsAuthenticator(?bool $supports = true) + { + return new DummySupportsAuthenticator($supports); + } + private function createManager($authenticators, $firewallName = 'main', $eraseCredentials = true, array $requiredBadges = [], LoggerInterface $logger = null) { return new AuthenticatorManager($authenticators, $this->tokenStorage, $this->eventDispatcher, $firewallName, $logger, $eraseCredentials, true, $requiredBadges); diff --git a/src/Symfony/Component/Security/Http/Tests/Fixtures/DummySupportsAuthenticator.php b/src/Symfony/Component/Security/Http/Tests/Fixtures/DummySupportsAuthenticator.php new file mode 100644 index 0000000000000..e2a037cc40614 --- /dev/null +++ b/src/Symfony/Component/Security/Http/Tests/Fixtures/DummySupportsAuthenticator.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Security\Http\Tests\Fixtures; + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; +use Symfony\Component\Security\Core\Exception\AuthenticationException; +use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; +use Symfony\Component\Security\Http\Authenticator\Passport\Passport; +use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; + +class DummySupportsAuthenticator extends DummyAuthenticator +{ + private $supports; + + public function __construct(?bool $supports) + { + $this->supports = $supports; + } + + public function supports(Request $request): ?bool + { + return $this->supports; + } +} diff --git a/src/Symfony/Component/Semaphore/Tests/Store/StoreFactoryTest.php b/src/Symfony/Component/Semaphore/Tests/Store/StoreFactoryTest.php index 8deec34b444b9..2da44ba0a8be8 100644 --- a/src/Symfony/Component/Semaphore/Tests/Store/StoreFactoryTest.php +++ b/src/Symfony/Component/Semaphore/Tests/Store/StoreFactoryTest.php @@ -12,37 +12,43 @@ namespace Symfony\Component\Semaphore\Tests\Store; use PHPUnit\Framework\TestCase; -use Symfony\Component\Cache\Adapter\AbstractAdapter; use Symfony\Component\Cache\Traits\RedisProxy; use Symfony\Component\Semaphore\Store\RedisStore; use Symfony\Component\Semaphore\Store\StoreFactory; /** * @author Jérémy Derussé + * + * @requires extension redis */ class StoreFactoryTest extends TestCase { - /** - * @dataProvider validConnections - */ - public function testCreateStore($connection, string $expectedStoreClass) + public function testCreateRedisStore() { - $store = StoreFactory::createStore($connection); + $store = StoreFactory::createStore($this->createMock(\Redis::class)); - $this->assertInstanceOf($expectedStoreClass, $store); + $this->assertInstanceOf(RedisStore::class, $store); } - public function validConnections() + public function testCreateRedisProxyStore() { - if (class_exists(\Redis::class)) { - yield [$this->createMock(\Redis::class), RedisStore::class]; - } - if (class_exists(RedisProxy::class)) { - yield [$this->createMock(RedisProxy::class), RedisStore::class]; + if (!class_exists(RedisProxy::class)) { + $this->markTestSkipped(); } - yield [new \Predis\Client(), RedisStore::class]; - if (class_exists(\Redis::class) && class_exists(AbstractAdapter::class)) { - yield ['redis://localhost', RedisStore::class]; + + $store = StoreFactory::createStore($this->createMock(RedisProxy::class)); + + $this->assertInstanceOf(RedisStore::class, $store); + } + + public function testCreateRedisAsDsnStore() + { + if (!class_exists(RedisProxy::class)) { + $this->markTestSkipped(); } + + $store = StoreFactory::createStore('redis://localhost'); + + $this->assertInstanceOf(RedisStore::class, $store); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorArrayTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorArrayTest.php index cc258cfca1cff..1f441ea136508 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorArrayTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorArrayTest.php @@ -16,7 +16,7 @@ */ class CountValidatorArrayTest extends CountValidatorTestCase { - protected function createCollection(array $content) + protected static function createCollection(array $content) { return $content; } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorCountableTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorCountableTest.php index 276076e885f6e..48a8063e5fb0a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorCountableTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorCountableTest.php @@ -18,7 +18,7 @@ */ class CountValidatorCountableTest extends CountValidatorTestCase { - protected function createCollection(array $content) + protected static function createCollection(array $content) { return new Countable($content); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php index f011f226c06b2..68b8766c516b8 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CountValidatorTestCase.php @@ -27,7 +27,7 @@ protected function createValidator() return new CountValidator(); } - abstract protected function createCollection(array $content); + abstract protected static function createCollection(array $content); public function testNullIsValid() { @@ -42,30 +42,30 @@ public function testExpectsCountableType() $this->validator->validate(new \stdClass(), new Count(5)); } - public function getThreeOrLessElements() + public static function getThreeOrLessElements() { return [ - [$this->createCollection([1])], - [$this->createCollection([1, 2])], - [$this->createCollection([1, 2, 3])], - [$this->createCollection(['a' => 1, 'b' => 2, 'c' => 3])], + [static::createCollection([1])], + [static::createCollection([1, 2])], + [static::createCollection([1, 2, 3])], + [static::createCollection(['a' => 1, 'b' => 2, 'c' => 3])], ]; } - public function getFourElements() + public static function getFourElements() { return [ - [$this->createCollection([1, 2, 3, 4])], - [$this->createCollection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4])], + [static::createCollection([1, 2, 3, 4])], + [static::createCollection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4])], ]; } - public function getFiveOrMoreElements() + public static function getFiveOrMoreElements() { return [ - [$this->createCollection([1, 2, 3, 4, 5])], - [$this->createCollection([1, 2, 3, 4, 5, 6])], - [$this->createCollection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5])], + [static::createCollection([1, 2, 3, 4, 5])], + [static::createCollection([1, 2, 3, 4, 5, 6])], + [static::createCollection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5])], ]; } @@ -82,6 +82,7 @@ public function testValidValuesMax($value) /** * @requires PHP 8 + * * @dataProvider getThreeOrLessElements */ public function testValidValuesMaxNamed($value) @@ -105,6 +106,7 @@ public function testValidValuesMin($value) /** * @requires PHP 8 + * * @dataProvider getFiveOrMoreElements */ public function testValidValuesMinNamed($value) @@ -128,6 +130,7 @@ public function testValidValuesExact($value) /** * @requires PHP 8 + * * @dataProvider getFourElements */ public function testValidValuesExactNamed($value) @@ -161,6 +164,7 @@ public function testTooManyValues($value) /** * @requires PHP 8 + * * @dataProvider getFiveOrMoreElements */ public function testTooManyValuesNamed($value) @@ -201,6 +205,7 @@ public function testTooFewValues($value) /** * @requires PHP 8 + * * @dataProvider getThreeOrLessElements */ public function testTooFewValuesNamed($value) @@ -242,6 +247,7 @@ public function testTooManyValuesExact($value) /** * @requires PHP 8 + * * @dataProvider getFiveOrMoreElements */ public function testTooManyValuesExactNamed($value) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php index f2cfa0a3f1490..b00f942441f55 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php @@ -64,7 +64,7 @@ public function testValidIpsV4($ip) $this->assertNoViolation(); } - public function getValidIpsV4() + public static function getValidIpsV4() { return [ ['0.0.0.0'], @@ -104,7 +104,7 @@ public function testValidIpV6WithWhitespacesNamed() $this->assertNoViolation(); } - public function getValidIpsV4WithWhitespaces() + public static function getValidIpsV4WithWhitespaces() { return [ ["\x200.0.0.0"], @@ -128,7 +128,7 @@ public function testValidIpsV6($ip) $this->assertNoViolation(); } - public function getValidIpsV6() + public static function getValidIpsV6() { return [ ['2001:0db8:85a3:0000:0000:8a2e:0370:7334'], @@ -165,9 +165,9 @@ public function testValidIpsAll($ip) $this->assertNoViolation(); } - public function getValidIpsAll() + public static function getValidIpsAll() { - return array_merge($this->getValidIpsV4(), $this->getValidIpsV6()); + return array_merge(self::getValidIpsV4(), self::getValidIpsV6()); } /** @@ -188,7 +188,7 @@ public function testInvalidIpsV4($ip) ->assertRaised(); } - public function getInvalidIpsV4() + public static function getInvalidIpsV4() { return [ ['0'], @@ -221,7 +221,7 @@ public function testInvalidPrivateIpsV4($ip) ->assertRaised(); } - public function getInvalidPrivateIpsV4() + public static function getInvalidPrivateIpsV4() { return [ ['10.0.0.0'], @@ -248,7 +248,7 @@ public function testInvalidReservedIpsV4($ip) ->assertRaised(); } - public function getInvalidReservedIpsV4() + public static function getInvalidReservedIpsV4() { return [ ['0.0.0.0'], @@ -275,9 +275,9 @@ public function testInvalidPublicIpsV4($ip) ->assertRaised(); } - public function getInvalidPublicIpsV4() + public static function getInvalidPublicIpsV4() { - return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidReservedIpsV4()); + return array_merge(self::getInvalidPrivateIpsV4(), self::getInvalidReservedIpsV4()); } /** @@ -298,7 +298,7 @@ public function testInvalidIpsV6($ip) ->assertRaised(); } - public function getInvalidIpsV6() + public static function getInvalidIpsV6() { return [ ['z001:0db8:85a3:0000:0000:8a2e:0370:7334'], @@ -335,7 +335,7 @@ public function testInvalidPrivateIpsV6($ip) ->assertRaised(); } - public function getInvalidPrivateIpsV6() + public static function getInvalidPrivateIpsV6() { return [ ['fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c'], @@ -362,12 +362,12 @@ public function testInvalidReservedIpsV6($ip) ->assertRaised(); } - public function getInvalidReservedIpsV6() + public static function getInvalidReservedIpsV6() { // Quoting after official filter documentation: // "FILTER_FLAG_NO_RES_RANGE = This flag does not apply to IPv6 addresses." // Full description: https://php.net/filter.filters.flags - return $this->getInvalidIpsV6(); + return self::getInvalidIpsV6(); } /** @@ -388,9 +388,9 @@ public function testInvalidPublicIpsV6($ip) ->assertRaised(); } - public function getInvalidPublicIpsV6() + public static function getInvalidPublicIpsV6() { - return array_merge($this->getInvalidPrivateIpsV6(), $this->getInvalidReservedIpsV6()); + return array_merge(self::getInvalidPrivateIpsV6(), self::getInvalidReservedIpsV6()); } /** @@ -411,9 +411,9 @@ public function testInvalidIpsAll($ip) ->assertRaised(); } - public function getInvalidIpsAll() + public static function getInvalidIpsAll() { - return array_merge($this->getInvalidIpsV4(), $this->getInvalidIpsV6()); + return array_merge(self::getInvalidIpsV4(), self::getInvalidIpsV6()); } /** @@ -434,9 +434,9 @@ public function testInvalidPrivateIpsAll($ip) ->assertRaised(); } - public function getInvalidPrivateIpsAll() + public static function getInvalidPrivateIpsAll() { - return array_merge($this->getInvalidPrivateIpsV4(), $this->getInvalidPrivateIpsV6()); + return array_merge(self::getInvalidPrivateIpsV4(), self::getInvalidPrivateIpsV6()); } /** @@ -457,9 +457,9 @@ public function testInvalidReservedIpsAll($ip) ->assertRaised(); } - public function getInvalidReservedIpsAll() + public static function getInvalidReservedIpsAll() { - return array_merge($this->getInvalidReservedIpsV4(), $this->getInvalidReservedIpsV6()); + return array_merge(self::getInvalidReservedIpsV4(), self::getInvalidReservedIpsV6()); } /** @@ -480,8 +480,8 @@ public function testInvalidPublicIpsAll($ip) ->assertRaised(); } - public function getInvalidPublicIpsAll() + public static function getInvalidPublicIpsAll() { - return array_merge($this->getInvalidPublicIpsV4(), $this->getInvalidPublicIpsV6()); + return array_merge(self::getInvalidPublicIpsV4(), self::getInvalidPublicIpsV6()); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php index 25889b0778386..fc872ad471e79 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php @@ -26,7 +26,7 @@ protected function createValidator() return new IsbnValidator(); } - public function getValidIsbn10() + public static function getValidIsbn10() { return [ ['2723442284'], @@ -45,7 +45,7 @@ public function getValidIsbn10() ]; } - public function getInvalidIsbn10() + public static function getInvalidIsbn10() { return [ ['27234422841', Isbn::TOO_LONG_ERROR], @@ -65,7 +65,7 @@ public function getInvalidIsbn10() ]; } - public function getValidIsbn13() + public static function getValidIsbn13() { return [ ['978-2723442282'], @@ -83,7 +83,7 @@ public function getValidIsbn13() ]; } - public function getInvalidIsbn13() + public static function getInvalidIsbn13() { return [ ['978-27234422821', Isbn::TOO_LONG_ERROR], @@ -103,19 +103,19 @@ public function getInvalidIsbn13() ]; } - public function getValidIsbn() + public static function getValidIsbn() { return array_merge( - $this->getValidIsbn10(), - $this->getValidIsbn13() + self::getValidIsbn10(), + self::getValidIsbn13() ); } - public function getInvalidIsbn() + public static function getInvalidIsbn() { return array_merge( - $this->getInvalidIsbn10(), - $this->getInvalidIsbn13() + self::getInvalidIsbn10(), + self::getInvalidIsbn13() ); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php index ea2ca4ecd6169..287f8c14477e8 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/IssnValidatorTest.php @@ -26,7 +26,7 @@ protected function createValidator() return new IssnValidator(); } - public function getValidLowerCasedIssn() + public static function getValidLowerCasedIssn() { return [ ['2162-321x'], @@ -39,7 +39,7 @@ public function getValidLowerCasedIssn() ]; } - public function getValidNonHyphenatedIssn() + public static function getValidNonHyphenatedIssn() { return [ ['2162321X'], @@ -52,7 +52,7 @@ public function getValidNonHyphenatedIssn() ]; } - public function getFullValidIssn() + public static function getFullValidIssn() { return [ ['1550-7416'], @@ -66,16 +66,16 @@ public function getFullValidIssn() ]; } - public function getValidIssn() + public static function getValidIssn() { return array_merge( - $this->getValidLowerCasedIssn(), - $this->getValidNonHyphenatedIssn(), - $this->getFullValidIssn() + self::getValidLowerCasedIssn(), + self::getValidNonHyphenatedIssn(), + self::getFullValidIssn() ); } - public function getInvalidIssn() + public static function getInvalidIssn() { return [ [0, Issn::TOO_SHORT_ERROR], diff --git a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php index 7a34810bdfab8..d67bbe526cbaf 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php @@ -78,6 +78,7 @@ public function testValidValuesMin($value) /** * @requires PHP 8 + * * @dataProvider getTenToTwenty */ public function testValidValuesMinNamed($value) @@ -101,6 +102,7 @@ public function testValidValuesMax($value) /** * @requires PHP 8 + * * @dataProvider getTenToTwenty */ public function testValidValuesMaxNamed($value) @@ -124,6 +126,7 @@ public function testValidValuesMinMax($value) /** * @requires PHP 8 + * * @dataProvider getTenToTwenty */ public function testValidValuesMinMaxNamed($value) @@ -155,6 +158,7 @@ public function testInvalidValuesMin($value, $formattedValue) /** * @requires PHP 8 + * * @dataProvider getLessThanTen */ public function testInvalidValuesMinNamed($value, $formattedValue) @@ -191,6 +195,7 @@ public function testInvalidValuesMax($value, $formattedValue) /** * @requires PHP 8 + * * @dataProvider getMoreThanTwenty */ public function testInvalidValuesMaxNamed($value, $formattedValue) @@ -229,6 +234,7 @@ public function testInvalidValuesCombinedMax($value, $formattedValue) /** * @requires PHP 8 + * * @dataProvider getMoreThanTwenty */ public function testInvalidValuesCombinedMaxNamed($value, $formattedValue) @@ -268,6 +274,7 @@ public function testInvalidValuesCombinedMin($value, $formattedValue) /** * @requires PHP 8 + * * @dataProvider getLessThanTen */ public function testInvalidValuesCombinedMinNamed($value, $formattedValue) @@ -284,11 +291,12 @@ public function testInvalidValuesCombinedMinNamed($value, $formattedValue) ->assertRaised(); } - public function getTenthToTwentiethMarch2014() + public static function getTenthToTwentiethMarch2014() { // The provider runs before setUp(), so we need to manually fix // the default timezone - $this->setDefaultTimezone('UTC'); + $timezone = date_default_timezone_get(); + date_default_timezone_set('UTC'); $tests = [ [new \DateTime('March 10, 2014')], @@ -300,16 +308,17 @@ public function getTenthToTwentiethMarch2014() $tests[] = [new \DateTimeImmutable('March 15, 2014')]; $tests[] = [new \DateTimeImmutable('March 20, 2014')]; - $this->restoreDefaultTimezone(); + date_default_timezone_set($timezone); return $tests; } - public function getSoonerThanTenthMarch2014() + public static function getSoonerThanTenthMarch2014() { // The provider runs before setUp(), so we need to manually fix // the default timezone - $this->setDefaultTimezone('UTC'); + $timezone = date_default_timezone_get(); + date_default_timezone_set('UTC'); $tests = [ [new \DateTime('March 20, 2013'), 'Mar 20, 2013, 12:00 AM'], @@ -319,16 +328,17 @@ public function getSoonerThanTenthMarch2014() $tests[] = [new \DateTimeImmutable('March 20, 2013'), 'Mar 20, 2013, 12:00 AM']; $tests[] = [new \DateTimeImmutable('March 9, 2014'), 'Mar 9, 2014, 12:00 AM']; - $this->restoreDefaultTimezone(); + date_default_timezone_set($timezone); return $tests; } - public function getLaterThanTwentiethMarch2014() + public static function getLaterThanTwentiethMarch2014() { // The provider runs before setUp(), so we need to manually fix // the default timezone - $this->setDefaultTimezone('UTC'); + $timezone = date_default_timezone_get(); + date_default_timezone_set('UTC'); $tests = [ [new \DateTime('March 21, 2014'), 'Mar 21, 2014, 12:00 AM'], @@ -338,7 +348,7 @@ public function getLaterThanTwentiethMarch2014() $tests[] = [new \DateTimeImmutable('March 21, 2014'), 'Mar 21, 2014, 12:00 AM']; $tests[] = [new \DateTimeImmutable('March 9, 2015'), 'Mar 9, 2015, 12:00 AM']; - $this->restoreDefaultTimezone(); + date_default_timezone_set($timezone); return $tests; } @@ -639,6 +649,7 @@ public function testValidValuesMinPropertyPath($value) /** * @requires PHP 8 + * * @dataProvider getTenToTwenty */ public function testValidValuesMinPropertyPathNamed($value) @@ -666,6 +677,7 @@ public function testValidValuesMaxPropertyPath($value) /** * @requires PHP 8 + * * @dataProvider getTenToTwenty */ public function testValidValuesMaxPropertyPathNamed($value) @@ -763,6 +775,7 @@ public function testInvalidValuesCombinedMaxPropertyPath($value, $formattedValue /** * @requires PHP 8 + * * @dataProvider getMoreThanTwenty */ public function testInvalidValuesCombinedMaxPropertyPathNamed($value, $formattedValue) @@ -814,6 +827,7 @@ public function testInvalidValuesCombinedMinPropertyPath($value, $formattedValue /** * @requires PHP 8 + * * @dataProvider getLessThanTen */ public function testInvalidValuesCombinedMinPropertyPathNamed($value, $formattedValue) @@ -1074,6 +1088,7 @@ public function provideMessageIfMinAndMaxSet(): array /** * @group legacy + * * @dataProvider provideMessageIfMinAndMaxSet */ public function testMessageIfMinAndMaxSet(array $constraintExtraOptions, int $value, string $expectedMessage, string $expectedCode) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php index 024de4553693b..088bb40c79ede 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TypeValidatorTest.php @@ -70,10 +70,10 @@ public function testValidValues($value, $type) $this->assertNoViolation(); } - public function getValidValues() + public static function getValidValues() { $object = new \stdClass(); - $file = $this->createFile(); + $file = self::createFile(); return [ [true, 'Boolean'], @@ -126,10 +126,10 @@ public function testInvalidValues($value, $type, $valueAsString) ->assertRaised(); } - public function getInvalidValues() + public static function getInvalidValues() { $object = new \stdClass(); - $file = $this->createFile(); + $file = self::createFile(); return [ ['foobar', 'numeric', '"foobar"'], @@ -209,20 +209,20 @@ public function provideConstraintsWithMultipleTypes() } } - protected function createFile() + protected static function createFile() { - if (!static::$file) { - static::$file = fopen(__FILE__, 'r'); + if (!self::$file) { + self::$file = fopen(__FILE__, 'r'); } - return static::$file; + return self::$file; } public static function tearDownAfterClass(): void { - if (static::$file) { - fclose(static::$file); - static::$file = null; + if (self::$file) { + fclose(self::$file); + self::$file = null; } } }