Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit e9d1e19

Browse files
OskarStarkchalasr
authored andcommitted
Use createMock() instead of a getter
1 parent 37b71bc commit e9d1e19

File tree

4 files changed

+32
-52
lines changed

4 files changed

+32
-52
lines changed

Core/Tests/Authentication/AuthenticationTrustResolverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ protected function getRememberMeToken()
140140
protected function getResolver()
141141
{
142142
return new AuthenticationTrustResolver(
143-
'Symfony\\Component\\Security\\Core\\Authentication\\Token\\AnonymousToken',
144-
'Symfony\\Component\\Security\\Core\\Authentication\\Token\\RememberMeToken'
143+
AnonymousToken::class,
144+
RememberMeToken::class
145145
);
146146
}
147147
}

Core/Tests/User/ChainUserProviderTest.php

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ class ChainUserProviderTest extends TestCase
2424
{
2525
public function testLoadUserByUsername()
2626
{
27-
$provider1 = $this->getProvider();
27+
$provider1 = $this->createMock(UserProviderInterface::class);
2828
$provider1
2929
->expects($this->once())
3030
->method('loadUserByUsername')
3131
->with($this->equalTo('foo'))
3232
->willThrowException(new UsernameNotFoundException('not found'))
3333
;
3434

35-
$provider2 = $this->getProvider();
35+
$provider2 = $this->createMock(UserProviderInterface::class);
3636
$provider2
3737
->expects($this->once())
3838
->method('loadUserByUsername')
3939
->with($this->equalTo('foo'))
40-
->willReturn($account = $this->getAccount())
40+
->willReturn($account = $this->createMock(UserInterface::class))
4141
;
4242

4343
$provider = new ChainUserProvider([$provider1, $provider2]);
@@ -47,15 +47,15 @@ public function testLoadUserByUsername()
4747
public function testLoadUserByUsernameThrowsUsernameNotFoundException()
4848
{
4949
$this->expectException(UsernameNotFoundException::class);
50-
$provider1 = $this->getProvider();
50+
$provider1 = $this->createMock(UserProviderInterface::class);
5151
$provider1
5252
->expects($this->once())
5353
->method('loadUserByUsername')
5454
->with($this->equalTo('foo'))
5555
->willThrowException(new UsernameNotFoundException('not found'))
5656
;
5757

58-
$provider2 = $this->getProvider();
58+
$provider2 = $this->createMock(UserProviderInterface::class);
5959
$provider2
6060
->expects($this->once())
6161
->method('loadUserByUsername')
@@ -69,14 +69,14 @@ public function testLoadUserByUsernameThrowsUsernameNotFoundException()
6969

7070
public function testRefreshUser()
7171
{
72-
$provider1 = $this->getProvider();
72+
$provider1 = $this->createMock(UserProviderInterface::class);
7373
$provider1
7474
->expects($this->once())
7575
->method('supportsClass')
7676
->willReturn(false)
7777
;
7878

79-
$provider2 = $this->getProvider();
79+
$provider2 = $this->createMock(UserProviderInterface::class);
8080
$provider2
8181
->expects($this->once())
8282
->method('supportsClass')
@@ -89,7 +89,7 @@ public function testRefreshUser()
8989
->willThrowException(new UnsupportedUserException('unsupported'))
9090
;
9191

92-
$provider3 = $this->getProvider();
92+
$provider3 = $this->createMock(UserProviderInterface::class);
9393
$provider3
9494
->expects($this->once())
9595
->method('supportsClass')
@@ -99,16 +99,16 @@ public function testRefreshUser()
9999
$provider3
100100
->expects($this->once())
101101
->method('refreshUser')
102-
->willReturn($account = $this->getAccount())
102+
->willReturn($account = $this->createMock(UserInterface::class))
103103
;
104104

105105
$provider = new ChainUserProvider([$provider1, $provider2, $provider3]);
106-
$this->assertSame($account, $provider->refreshUser($this->getAccount()));
106+
$this->assertSame($account, $provider->refreshUser($this->createMock(UserInterface::class)));
107107
}
108108

109109
public function testRefreshUserAgain()
110110
{
111-
$provider1 = $this->getProvider();
111+
$provider1 = $this->createMock(UserProviderInterface::class);
112112
$provider1
113113
->expects($this->once())
114114
->method('supportsClass')
@@ -121,7 +121,7 @@ public function testRefreshUserAgain()
121121
->willThrowException(new UsernameNotFoundException('not found'))
122122
;
123123

124-
$provider2 = $this->getProvider();
124+
$provider2 = $this->createMock(UserProviderInterface::class);
125125
$provider2
126126
->expects($this->once())
127127
->method('supportsClass')
@@ -131,17 +131,17 @@ public function testRefreshUserAgain()
131131
$provider2
132132
->expects($this->once())
133133
->method('refreshUser')
134-
->willReturn($account = $this->getAccount())
134+
->willReturn($account = $this->createMock(UserInterface::class))
135135
;
136136

137137
$provider = new ChainUserProvider([$provider1, $provider2]);
138-
$this->assertSame($account, $provider->refreshUser($this->getAccount()));
138+
$this->assertSame($account, $provider->refreshUser($this->createMock(UserInterface::class)));
139139
}
140140

141141
public function testRefreshUserThrowsUnsupportedUserException()
142142
{
143143
$this->expectException(UnsupportedUserException::class);
144-
$provider1 = $this->getProvider();
144+
$provider1 = $this->createMock(UserProviderInterface::class);
145145
$provider1
146146
->expects($this->once())
147147
->method('supportsClass')
@@ -154,7 +154,7 @@ public function testRefreshUserThrowsUnsupportedUserException()
154154
->willThrowException(new UnsupportedUserException('unsupported'))
155155
;
156156

157-
$provider2 = $this->getProvider();
157+
$provider2 = $this->createMock(UserProviderInterface::class);
158158
$provider2
159159
->expects($this->once())
160160
->method('supportsClass')
@@ -168,20 +168,20 @@ public function testRefreshUserThrowsUnsupportedUserException()
168168
;
169169

170170
$provider = new ChainUserProvider([$provider1, $provider2]);
171-
$provider->refreshUser($this->getAccount());
171+
$provider->refreshUser($this->createMock(UserInterface::class));
172172
}
173173

174174
public function testSupportsClass()
175175
{
176-
$provider1 = $this->getProvider();
176+
$provider1 = $this->createMock(UserProviderInterface::class);
177177
$provider1
178178
->expects($this->once())
179179
->method('supportsClass')
180180
->with($this->equalTo('foo'))
181181
->willReturn(false)
182182
;
183183

184-
$provider2 = $this->getProvider();
184+
$provider2 = $this->createMock(UserProviderInterface::class);
185185
$provider2
186186
->expects($this->once())
187187
->method('supportsClass')
@@ -195,15 +195,15 @@ public function testSupportsClass()
195195

196196
public function testSupportsClassWhenNotSupported()
197197
{
198-
$provider1 = $this->getProvider();
198+
$provider1 = $this->createMock(UserProviderInterface::class);
199199
$provider1
200200
->expects($this->once())
201201
->method('supportsClass')
202202
->with($this->equalTo('foo'))
203203
->willReturn(false)
204204
;
205205

206-
$provider2 = $this->getProvider();
206+
$provider2 = $this->createMock(UserProviderInterface::class);
207207
$provider2
208208
->expects($this->once())
209209
->method('supportsClass')
@@ -217,7 +217,7 @@ public function testSupportsClassWhenNotSupported()
217217

218218
public function testAcceptsTraversable()
219219
{
220-
$provider1 = $this->getProvider();
220+
$provider1 = $this->createMock(UserProviderInterface::class);
221221
$provider1
222222
->expects($this->once())
223223
->method('supportsClass')
@@ -230,7 +230,7 @@ public function testAcceptsTraversable()
230230
->willThrowException(new UnsupportedUserException('unsupported'))
231231
;
232232

233-
$provider2 = $this->getProvider();
233+
$provider2 = $this->createMock(UserProviderInterface::class);
234234
$provider2
235235
->expects($this->once())
236236
->method('supportsClass')
@@ -240,11 +240,11 @@ public function testAcceptsTraversable()
240240
$provider2
241241
->expects($this->once())
242242
->method('refreshUser')
243-
->willReturn($account = $this->getAccount())
243+
->willReturn($account = $this->createMock(UserInterface::class))
244244
;
245245

246246
$provider = new ChainUserProvider(new \ArrayObject([$provider1, $provider2]));
247-
$this->assertSame($account, $provider->refreshUser($this->getAccount()));
247+
$this->assertSame($account, $provider->refreshUser($this->createMock(UserInterface::class)));
248248
}
249249

250250
public function testPasswordUpgrades()
@@ -268,14 +268,4 @@ public function testPasswordUpgrades()
268268
$provider = new ChainUserProvider([$provider1, $provider2]);
269269
$provider->upgradePassword($user, 'foobar');
270270
}
271-
272-
protected function getAccount()
273-
{
274-
return $this->createMock(UserInterface::class);
275-
}
276-
277-
protected function getProvider()
278-
{
279-
return $this->createMock(UserProviderInterface::class);
280-
}
281271
}

Core/Tests/Validator/Constraints/UserPasswordValidatorTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function setUp(): void
5454
{
5555
$user = $this->createUser();
5656
$this->tokenStorage = $this->createTokenStorage($user);
57-
$this->encoder = $this->createPasswordEncoder();
57+
$this->encoder = $this->createMock(PasswordEncoderInterface::class);
5858
$this->encoderFactory = $this->createEncoderFactory($this->encoder);
5959

6060
parent::setUp();
@@ -147,11 +147,6 @@ protected function createUser()
147147
return $mock;
148148
}
149149

150-
protected function createPasswordEncoder($isPasswordValid = true)
151-
{
152-
return $this->createMock(PasswordEncoderInterface::class);
153-
}
154-
155150
protected function createEncoderFactory($encoder = null)
156151
{
157152
$mock = $this->createMock(EncoderFactoryInterface::class);

Http/Tests/Session/SessionAuthenticationStrategyTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testSessionIsNotChanged()
2525
$request->expects($this->never())->method('getSession');
2626

2727
$strategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::NONE);
28-
$strategy->onAuthentication($request, $this->getToken());
28+
$strategy->onAuthentication($request, $this->createMock(TokenInterface::class));
2929
}
3030

3131
public function testUnsupportedStrategy()
@@ -36,7 +36,7 @@ public function testUnsupportedStrategy()
3636
$request->expects($this->never())->method('getSession');
3737

3838
$strategy = new SessionAuthenticationStrategy('foo');
39-
$strategy->onAuthentication($request, $this->getToken());
39+
$strategy->onAuthentication($request, $this->createMock(TokenInterface::class));
4040
}
4141

4242
public function testSessionIsMigrated()
@@ -45,7 +45,7 @@ public function testSessionIsMigrated()
4545
$session->expects($this->once())->method('migrate')->with($this->equalTo(true));
4646

4747
$strategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::MIGRATE);
48-
$strategy->onAuthentication($this->getRequest($session), $this->getToken());
48+
$strategy->onAuthentication($this->getRequest($session), $this->createMock(TokenInterface::class));
4949
}
5050

5151
public function testSessionIsInvalidated()
@@ -54,7 +54,7 @@ public function testSessionIsInvalidated()
5454
$session->expects($this->once())->method('invalidate');
5555

5656
$strategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::INVALIDATE);
57-
$strategy->onAuthentication($this->getRequest($session), $this->getToken());
57+
$strategy->onAuthentication($this->getRequest($session), $this->createMock(TokenInterface::class));
5858
}
5959

6060
private function getRequest($session = null)
@@ -67,9 +67,4 @@ private function getRequest($session = null)
6767

6868
return $request;
6969
}
70-
71-
private function getToken()
72-
{
73-
return $this->createMock(TokenInterface::class);
74-
}
7570
}

0 commit comments

Comments
 (0)