Skip to content

Commit a906fb4

Browse files
[Tests] Migrate tests to static data providers
1 parent 3b0093b commit a906fb4

File tree

3 files changed

+217
-10
lines changed

3 files changed

+217
-10
lines changed

Tests/EventListener/PasswordMigratingListenerTest.php

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
1717
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
18-
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1918
use Symfony\Component\Security\Core\User\InMemoryUser;
2019
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
2120
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
2221
use Symfony\Component\Security\Core\User\UserInterface;
2322
use Symfony\Component\Security\Core\User\UserProviderInterface;
24-
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
2523
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
2624
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
2725
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
2826
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
2927
use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface;
3028
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
3129
use Symfony\Component\Security\Http\EventListener\PasswordMigratingListener;
30+
use Symfony\Component\Security\Http\Tests\Fixtures\DummyAuthenticator;
31+
use Symfony\Component\Security\Http\Tests\Fixtures\DummyToken;
3232

3333
class PasswordMigratingListenerTest extends TestCase
3434
{
@@ -58,13 +58,13 @@ public function testUnsupportedEvents($event)
5858
$this->listener->onLoginSuccess($event);
5959
}
6060

61-
public function provideUnsupportedEvents()
61+
public static function provideUnsupportedEvents()
6262
{
6363
// no password upgrade badge
64-
yield [$this->createEvent(new SelfValidatingPassport(new UserBadge('test', function () { return $this->createMock(UserInterface::class); })))];
64+
yield [self::createEvent(new SelfValidatingPassport(new UserBadge('test', function () { return $this->createMock(UserInterface::class); })))];
6565

6666
// blank password
67-
yield [$this->createEvent(new SelfValidatingPassport(new UserBadge('test', function () { return $this->createMock(TestPasswordAuthenticatedUser::class); }), [new PasswordUpgradeBadge('', $this->createPasswordUpgrader())]))];
67+
yield [self::createEvent(new SelfValidatingPassport(new UserBadge('test', function () { return new DummyTestPasswordAuthenticatedUser(); }), [new PasswordUpgradeBadge('', self::createPasswordUpgrader())]))];
6868
}
6969

7070
/**
@@ -96,7 +96,7 @@ public function testUnsupportedPassport()
9696

9797
public function testUpgradeWithUpgrader()
9898
{
99-
$passwordUpgrader = $this->createPasswordUpgrader();
99+
$passwordUpgrader = $this->getMockForAbstractClass(TestMigratingUserProvider::class);
100100
$passwordUpgrader->expects($this->once())
101101
->method('upgradePassword')
102102
->with($this->user, 'new-hash')
@@ -133,14 +133,14 @@ public function testUserWithoutPassword()
133133
$this->listener->onLoginSuccess($event);
134134
}
135135

136-
private function createPasswordUpgrader()
136+
private static function createPasswordUpgrader()
137137
{
138-
return $this->getMockForAbstractClass(TestMigratingUserProvider::class);
138+
return new DummyTestMigratingUserProvider();
139139
}
140140

141-
private function createEvent(PassportInterface $passport)
141+
private static function createEvent(PassportInterface $passport)
142142
{
143-
return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class), $passport, $this->createMock(TokenInterface::class), new Request(), null, 'main');
143+
return new LoginSuccessEvent(new DummyAuthenticator(), $passport, new DummyToken(), new Request(), null, 'main');
144144
}
145145
}
146146

@@ -151,9 +151,62 @@ abstract public function upgradePassword(PasswordAuthenticatedUserInterface $use
151151
abstract public function loadUserByIdentifier(string $identifier): UserInterface;
152152
}
153153

154+
class DummyTestMigratingUserProvider extends TestMigratingUserProvider
155+
{
156+
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
157+
{
158+
}
159+
160+
public function loadUserByIdentifier(string $identifier): UserInterface
161+
{
162+
}
163+
164+
public function refreshUser(UserInterface $user)
165+
{
166+
}
167+
168+
public function supportsClass(string $class)
169+
{
170+
}
171+
172+
public function loadUserByUsername(string $username)
173+
{
174+
}
175+
}
176+
154177
abstract class TestPasswordAuthenticatedUser implements UserInterface, PasswordAuthenticatedUserInterface
155178
{
156179
abstract public function getPassword(): ?string;
157180

158181
abstract public function getSalt(): ?string;
159182
}
183+
184+
class DummyTestPasswordAuthenticatedUser extends TestPasswordAuthenticatedUser
185+
{
186+
public function getPassword(): ?string
187+
{
188+
return null;
189+
}
190+
191+
public function getSalt(): ?string
192+
{
193+
return null;
194+
}
195+
196+
public function getRoles(): array
197+
{
198+
return [];
199+
}
200+
201+
public function eraseCredentials()
202+
{
203+
}
204+
205+
public function getUsername()
206+
{
207+
}
208+
209+
public function getUserIdentifier(): string
210+
{
211+
}
212+
}

Tests/Fixtures/DummyAuthenticator.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Http\Tests\Fixtures;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
17+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
18+
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
19+
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
20+
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
21+
22+
/**
23+
* @author Alexandre Daubois <alex.daubois@gmail.com>
24+
*/
25+
class DummyAuthenticator implements AuthenticatorInterface
26+
{
27+
public function supports(Request $request): ?bool
28+
{
29+
return null;
30+
}
31+
32+
public function authenticate(Request $request): Passport
33+
{
34+
}
35+
36+
public function createToken(Passport $passport, string $firewallName): TokenInterface
37+
{
38+
}
39+
40+
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
41+
{
42+
return null;
43+
}
44+
45+
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
46+
{
47+
return null;
48+
}
49+
50+
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
51+
{
52+
}
53+
}

Tests/Fixtures/DummyToken.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Http\Tests\Fixtures;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
use Symfony\Component\Security\Core\User\UserInterface;
16+
17+
/**
18+
* @author Alexandre Daubois <alex.daubois@gmail.com>
19+
*/
20+
class DummyToken implements TokenInterface
21+
{
22+
public function serialize()
23+
{
24+
}
25+
26+
public function unserialize($data)
27+
{
28+
}
29+
30+
public function __toString(): string
31+
{
32+
}
33+
34+
public function getRoleNames(): array
35+
{
36+
}
37+
38+
public function getCredentials(): mixed
39+
{
40+
}
41+
42+
public function getUser(): ?UserInterface
43+
{
44+
}
45+
46+
public function setUser($user)
47+
{
48+
}
49+
50+
public function isAuthenticated(): bool
51+
{
52+
}
53+
54+
public function setAuthenticated(bool $isAuthenticated)
55+
{
56+
}
57+
58+
public function eraseCredentials(): void
59+
{
60+
}
61+
62+
public function getAttributes(): array
63+
{
64+
}
65+
66+
public function setAttributes(array $attributes): void
67+
{
68+
}
69+
70+
public function hasAttribute(string $name): bool
71+
{
72+
}
73+
74+
public function getAttribute(string $name): mixed
75+
{
76+
}
77+
78+
public function setAttribute(string $name, $value): void
79+
{
80+
}
81+
82+
public function getUsername(): string
83+
{
84+
}
85+
86+
public function getUserIdentifier(): string
87+
{
88+
}
89+
90+
public function __serialize(): array
91+
{
92+
}
93+
94+
public function __unserialize(array $data): void
95+
{
96+
}
97+
98+
public function __call(string $name, array $arguments)
99+
{
100+
}
101+
}

0 commit comments

Comments
 (0)