Skip to content

Commit fc6b897

Browse files
committed
Symfony 5.3 compat fixes
1 parent 7fe7d52 commit fc6b897

File tree

11 files changed

+79
-61
lines changed

11 files changed

+79
-61
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 3.7.2 (????-??-??)
4+
5+
- Symfony 5.3 compatibility fixes
6+
37
## 3.7.1 (2021-03-29)
48

59
- Fix incorrect service reference

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ parameters:
1717
message: '/Else branch is unreachable because previous condition is always true\./'
1818
path: %currentWorkingDirectory%/src/DataCollector/WebsocketCompatibilityDataCollector.php
1919

20+
# Conditionals for interface compatibility, remove when dropping support for Symfony 5.2 and earlier
21+
- '/Call to an undefined method Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface::getUsername\(\)\./'
22+
2023
# Ignore errors for Symfony config builder
2124
-
2225
message: '/Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition::(.*)\(\)\./'

src/Client/Auth/WebsocketAuthenticationProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function authenticate(ConnectionInterface $conn): TokenInterface
5858
$this->logger->info(
5959
sprintf(
6060
'%s connected',
61-
$token->getUsername()
61+
method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername()
6262
),
6363
$loggerContext
6464
);

src/Client/ClientManipulator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public function findAllByUsername(Topic $topic, string $username): array
3737
continue;
3838
}
3939

40-
if ($client->getUsername() === $username) {
40+
$clientUsername = method_exists($client, 'getUserIdentifier') ? $client->getUserIdentifier() : $client->getUsername();
41+
42+
if ($clientUsername === $username) {
4143
$result[] = new ClientConnection($client, $connection);
4244
}
4345
}

src/Client/ClientStorage.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function getClient(string $identifier): TokenInterface
3535
try {
3636
$result = $this->driver->fetch($identifier);
3737
} catch (\Exception $e) {
38-
throw new StorageException(sprintf('Driver %s failed', static::class), $e->getCode(), $e);
38+
throw new StorageException(sprintf('Driver %s failed', self::class), $e->getCode(), $e);
3939
}
4040

4141
if (null !== $this->logger) {
@@ -64,7 +64,7 @@ public function addClient(string $identifier, TokenInterface $token): void
6464
if (null !== $this->logger) {
6565
$context = [
6666
'token' => $token,
67-
'username' => $token->getUsername(),
67+
'username' => method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername(),
6868
];
6969

7070
$this->logger->debug('INSERT CLIENT '.$identifier, $context);
@@ -73,11 +73,13 @@ public function addClient(string $identifier, TokenInterface $token): void
7373
try {
7474
$result = $this->driver->save($identifier, $serializedUser, $this->ttl);
7575
} catch (\Exception $e) {
76-
throw new StorageException(sprintf('Driver %s failed', static::class), $e->getCode(), $e);
76+
throw new StorageException(sprintf('Driver %s failed', self::class), $e->getCode(), $e);
7777
}
7878

7979
if (false === $result) {
80-
throw new StorageException(sprintf('Unable to add client "%s" to storage', $token->getUsername()));
80+
$username = method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername();
81+
82+
throw new StorageException(sprintf('Unable to add client "%s" to storage', $username));
8183
}
8284
}
8385

@@ -89,7 +91,7 @@ public function hasClient(string $identifier): bool
8991
try {
9092
return $this->driver->contains($identifier);
9193
} catch (\Exception $e) {
92-
throw new StorageException(sprintf('Driver %s failed', static::class), $e->getCode(), $e);
94+
throw new StorageException(sprintf('Driver %s failed', self::class), $e->getCode(), $e);
9395
}
9496
}
9597

@@ -105,7 +107,7 @@ public function removeClient(string $identifier): bool
105107
try {
106108
return $this->driver->delete($identifier);
107109
} catch (\Exception $e) {
108-
throw new StorageException(sprintf('Driver %s failed', static::class), $e->getCode(), $e);
110+
throw new StorageException(sprintf('Driver %s failed', self::class), $e->getCode(), $e);
109111
}
110112
}
111113

@@ -125,7 +127,7 @@ public function removeAllClients(): void
125127
try {
126128
$this->driver->clear();
127129
} catch (\Exception $e) {
128-
throw new StorageException(sprintf('Driver %s failed', static::class), $e->getCode(), $e);
130+
throw new StorageException(sprintf('Driver %s failed', self::class), $e->getCode(), $e);
129131
}
130132
}
131133
}

src/EventListener/ClientEventListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function onClientDisconnect(ClientDisconnectedEvent $event): void
5353

5454
$this->clientStorage->removeClient($storageId);
5555

56-
$username = $token->getUsername();
56+
$username = method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername();
5757

5858
if (null !== $this->logger) {
5959
$this->logger->info(
@@ -108,7 +108,7 @@ public function onClientError(ClientErrorEvent $event): void
108108
if ($this->clientStorage->hasClient($storageId)) {
109109
$token = $this->clientStorage->getClient($storageId);
110110

111-
$loggerContext['client'] = $token->getUsername();
111+
$loggerContext['client'] = method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername();
112112
}
113113

114114
$this->logger->error(

src/Server/App/WampApplication.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ public function onPublish(ConnectionInterface $conn, $topic, $event, array $excl
5757
if (null !== $this->logger) {
5858
if ($this->clientStorage->hasClient($this->clientStorage->getStorageId($conn))) {
5959
$token = $this->clientStorage->getClient($this->clientStorage->getStorageId($conn));
60-
$username = $token->getUsername();
6160

6261
$this->logger->debug(
6362
sprintf(
6463
'User %s published to %s',
65-
$username,
64+
method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername(),
6665
$topic->getId()
6766
)
6867
);
@@ -123,12 +122,11 @@ public function onSubscribe(ConnectionInterface $conn, $topic): void
123122
if (null !== $this->logger) {
124123
if ($this->clientStorage->hasClient($this->clientStorage->getStorageId($conn))) {
125124
$token = $this->clientStorage->getClient($this->clientStorage->getStorageId($conn));
126-
$username = $token->getUsername();
127125

128126
$this->logger->info(
129127
sprintf(
130128
'User %s subscribed to %s',
131-
$username,
129+
method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername(),
132130
$topic->getId()
133131
)
134132
);
@@ -152,12 +150,11 @@ public function onUnSubscribe(ConnectionInterface $conn, $topic): void
152150
if (null !== $this->logger) {
153151
if ($this->clientStorage->hasClient($this->clientStorage->getStorageId($conn))) {
154152
$token = $this->clientStorage->getClient($this->clientStorage->getStorageId($conn));
155-
$username = $token->getUsername();
156153

157154
$this->logger->info(
158155
sprintf(
159156
'User %s unsubscribed from %s',
160-
$username,
157+
method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername(),
161158
$topic->getId()
162159
)
163160
);

tests/Client/ClientManipulatorTest.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
use PHPUnit\Framework\TestCase;
1212
use Ratchet\ConnectionInterface;
1313
use Ratchet\Wamp\Topic;
14+
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
1415
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
15-
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1616
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1717

1818
class ClientManipulatorTest extends TestCase
1919
{
2020
/**
21-
* @var MockObject|ClientStorageInterface
21+
* @var MockObject&ClientStorageInterface
2222
*/
2323
private $clientStorage;
2424

2525
/**
26-
* @var MockObject|WebsocketAuthenticationProviderInterface
26+
* @var MockObject&WebsocketAuthenticationProviderInterface
2727
*/
2828
private $authenticationProvider;
2929

@@ -46,7 +46,7 @@ public function testGetClientForConnection(): void
4646
{
4747
$connection = $this->createMock(ConnectionInterface::class);
4848
$storageId = 42;
49-
$client = $this->createMock(TokenInterface::class);
49+
$client = $this->createMock(AbstractToken::class);
5050

5151
$this->clientStorage->expects($this->once())
5252
->method('getStorageId')
@@ -65,7 +65,7 @@ public function testGetClientForConnectionAfterReauthenticating(): void
6565
{
6666
$connection = $this->createMock(ConnectionInterface::class);
6767
$storageId = 42;
68-
$client = $this->createMock(TokenInterface::class);
68+
$client = $this->createMock(AbstractToken::class);
6969

7070
$this->clientStorage->expects($this->exactly(2))
7171
->method('getStorageId')
@@ -89,13 +89,15 @@ public function testGetClientForConnectionAfterReauthenticating(): void
8989

9090
public function testAllConnectionsForAUserCanBeFoundByUsername(): void
9191
{
92-
/** @var MockObject|ConnectionInterface $connection1 */
92+
$usernameMethod = method_exists(AbstractToken::class, 'getUserIdentifier') ? 'getUserIdentifier' : 'getUsername';
93+
94+
/** @var MockObject&ConnectionInterface $connection1 */
9395
$connection1 = $this->createMock(ConnectionInterface::class);
9496

95-
/** @var MockObject|ConnectionInterface $connection2 */
97+
/** @var MockObject&ConnectionInterface $connection2 */
9698
$connection2 = $this->createMock(ConnectionInterface::class);
9799

98-
/** @var MockObject|ConnectionInterface $connection3 */
100+
/** @var MockObject&ConnectionInterface $connection3 */
99101
$connection3 = $this->createMock(ConnectionInterface::class);
100102

101103
$storageId1 = 42;
@@ -105,22 +107,22 @@ public function testAllConnectionsForAUserCanBeFoundByUsername(): void
105107
$username1 = 'user';
106108
$username2 = 'guest';
107109

108-
/** @var MockObject|TokenInterface $client1 */
109-
$client1 = $this->createMock(TokenInterface::class);
110+
/** @var MockObject&AbstractToken $client1 */
111+
$client1 = $this->createMock(AbstractToken::class);
110112
$client1->expects($this->once())
111-
->method('getUsername')
113+
->method($usernameMethod)
112114
->willReturn($username1);
113115

114-
/** @var MockObject|TokenInterface $client2 */
115-
$client2 = $this->createMock(TokenInterface::class);
116+
/** @var MockObject&AbstractToken $client2 */
117+
$client2 = $this->createMock(AbstractToken::class);
116118
$client2->expects($this->once())
117-
->method('getUsername')
119+
->method($usernameMethod)
118120
->willReturn($username1);
119121

120-
/** @var MockObject|TokenInterface $client3 */
121-
$client3 = $this->createMock(TokenInterface::class);
122+
/** @var MockObject&AbstractToken $client3 */
123+
$client3 = $this->createMock(AbstractToken::class);
122124
$client3->expects($this->once())
123-
->method('getUsername')
125+
->method($usernameMethod)
124126
->willReturn($username2);
125127

126128
$this->clientStorage->expects($this->exactly(3))
@@ -149,7 +151,7 @@ public function testAllConnectionsForAUserCanBeFoundByUsername(): void
149151
$client3
150152
);
151153

152-
/** @var MockObject|Topic $topic */
154+
/** @var MockObject&Topic $topic */
153155
$topic = $this->createMock(Topic::class);
154156
$topic->expects($this->once())
155157
->method('getIterator')
@@ -172,7 +174,7 @@ public function testFetchingAllConnectionsByDefaultOnlyReturnsAuthenticatedUsers
172174
$storageId1 = 42;
173175
$storageId2 = 84;
174176

175-
$authenticatedClient = $this->createMock(TokenInterface::class);
177+
$authenticatedClient = $this->createMock(AbstractToken::class);
176178
$guestClient = $this->createMock(AnonymousToken::class);
177179

178180
$this->clientStorage->expects($this->exactly(2))
@@ -212,19 +214,19 @@ public function testFetchingAllConnectionsByDefaultOnlyReturnsAuthenticatedUsers
212214

213215
public function testFetchingAllConnectionsWithAnonymousFlagReturnsAllConnectedUsers(): void
214216
{
215-
/** @var MockObject|ConnectionInterface $connection1 */
217+
/** @var MockObject&ConnectionInterface $connection1 */
216218
$connection1 = $this->createMock(ConnectionInterface::class);
217219

218-
/** @var MockObject|ConnectionInterface $connection2 */
220+
/** @var MockObject&ConnectionInterface $connection2 */
219221
$connection2 = $this->createMock(ConnectionInterface::class);
220222

221223
$storageId1 = 42;
222224
$storageId2 = 84;
223225

224-
/** @var MockObject|TokenInterface $authenticatedClient */
225-
$authenticatedClient = $this->createMock(TokenInterface::class);
226+
/** @var MockObject&AbstractToken $authenticatedClient */
227+
$authenticatedClient = $this->createMock(AbstractToken::class);
226228

227-
/** @var MockObject|AnonymousToken $guestClient */
229+
/** @var MockObject&AnonymousToken $guestClient */
228230
$guestClient = $this->createMock(AnonymousToken::class);
229231

230232
$this->clientStorage->expects($this->exactly(2))
@@ -249,7 +251,7 @@ public function testFetchingAllConnectionsWithAnonymousFlagReturnsAllConnectedUs
249251
$guestClient
250252
);
251253

252-
/** @var MockObject|Topic $topic */
254+
/** @var MockObject&Topic $topic */
253255
$topic = $this->createMock(Topic::class);
254256
$topic->expects($this->once())
255257
->method('getIterator')

tests/Client/ClientStorageTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
use PHPUnit\Framework\MockObject\MockObject;
1010
use PHPUnit\Framework\TestCase;
1111
use Ratchet\ConnectionInterface;
12+
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
1213
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
1314
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1415

1516
class ClientStorageTest extends TestCase
1617
{
1718
/**
18-
* @var MockObject|ClearableDriverInterface
19+
* @var MockObject&ClearableDriverInterface
1920
*/
2021
private $driver;
2122

@@ -119,9 +120,11 @@ public function testAnExceptionIsThrownIfTheClientIsNotAddedToStorage(): void
119120
$this->expectExceptionMessage('Unable to add client "user" to storage');
120121

121122
$clientId = '42';
122-
$token = $this->createMock(TokenInterface::class);
123+
124+
/** @var MockObject&AbstractToken $token */
125+
$token = $this->createMock(AbstractToken::class);
123126
$token->expects($this->once())
124-
->method('getUsername')
127+
->method(method_exists(AbstractToken::class, 'getUserIdentifier') ? 'getUserIdentifier' : 'getUsername')
125128
->willReturn('user');
126129

127130
$this->driver->expects($this->once())

tests/EventListener/ClientEventListenerTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Psr\Log\Test\TestLogger;
1717
use Ratchet\ConnectionInterface;
18-
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
18+
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
1919

2020
class ClientEventListenerTest extends TestCase
2121
{
@@ -61,7 +61,7 @@ public function testTheUserIsAuthenticatedWhenTheClientConnectEventIsDispatched(
6161
$this->authenticationProvider->expects($this->once())
6262
->method('authenticate')
6363
->with($connection)
64-
->willReturn($this->createMock(TokenInterface::class));
64+
->willReturn($this->createMock(AbstractToken::class));
6565

6666
$this->listener->onClientConnect($event);
6767
}
@@ -74,9 +74,10 @@ public function testTheUserIsRemovedFromStorageWhenTheClientDisconnectEventIsDis
7474
'sessionId' => 'session',
7575
];
7676

77-
$token = $this->createMock(TokenInterface::class);
77+
/** @var MockObject&AbstractToken $token */
78+
$token = $this->createMock(AbstractToken::class);
7879
$token->expects($this->once())
79-
->method('getUsername')
80+
->method(method_exists(AbstractToken::class, 'getUserIdentifier') ? 'getUserIdentifier' : 'getUsername')
8081
->willReturn('username');
8182

8283
$event = new ClientDisconnectedEvent($connection);
@@ -202,7 +203,7 @@ public function testTheClientErrorIsLogged(): void
202203
$this->clientStorage->expects($this->once())
203204
->method('getClient')
204205
->with($connection->resourceId)
205-
->willReturn($this->createMock(TokenInterface::class));
206+
->willReturn($this->createMock(AbstractToken::class));
206207

207208
$this->clientStorage->expects($this->never())
208209
->method('removeClient');

0 commit comments

Comments
 (0)