Skip to content

Commit 9bf2fe9

Browse files
authored
Fix static analysis due to Symfony 5.3 release (#518)
* Fix static analysis due to Symfony 5.3 release * Add changelog entry * Add tests * Amend changelog entry
1 parent d0b85df commit 9bf2fe9

File tree

4 files changed

+71
-43
lines changed

4 files changed

+71
-43
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
## Unreleased
44

5+
- Fix extraction of the username of the logged-in user in Symfony 5.3 (#518)
6+
57
## 4.1.3 (2021-05-31)
68

7-
- Fix missing require of the `symfony/cache-contracts` package (#506)
9+
- Fix missing require of the `symfony/cache-contracts` package (#506)
810

911
## 4.1.2 (2021-05-17)
1012

phpstan.neon

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ parameters:
1010
- tests/End2End/App
1111
dynamicConstantNames:
1212
- Symfony\Component\HttpKernel\Kernel::VERSION
13+
- Symfony\Component\HttpKernel\Kernel::VERSION_ID
1314
- Doctrine\DBAL\Version::VERSION
1415
stubFiles:
1516
- tests/Stubs/Profile.phpstub

src/EventListener/RequestListener.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ public function handleKernelControllerEvent(RequestListenerControllerEvent $even
104104
private function getUsername($user): ?string
105105
{
106106
if ($user instanceof UserInterface) {
107-
return method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
107+
if (method_exists($user, 'getUserIdentifier')) {
108+
return $user->getUserIdentifier();
109+
}
110+
111+
if (method_exists($user, 'getUsername')) {
112+
return $user->getUsername();
113+
}
108114
}
109115

110116
if (\is_string($user)) {

tests/EventListener/RequestListenerTest.php

+60-41
Original file line numberDiff line numberDiff line change
@@ -394,52 +394,28 @@ public function getCredentials()
394394
HttpKernelInterface::MASTER_REQUEST
395395
),
396396
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
397-
new class() extends AbstractToken {
398-
public function __construct()
397+
new MockToken(new class() extends MockUser {
398+
public function getUsername(): string
399399
{
400-
parent::__construct();
401-
402-
$this->setAuthenticated(true);
403-
$this->setUser(new class() implements UserInterface {
404-
public function getRoles()
405-
{
406-
return [];
407-
}
408-
409-
public function getPassword()
410-
{
411-
return null;
412-
}
413-
414-
public function getSalt()
415-
{
416-
return null;
417-
}
418-
419-
public function getUsername(): string
420-
{
421-
return $this->getUserIdentifier();
422-
}
423-
424-
public function getUserIdentifier(): string
425-
{
426-
return 'foo_user';
427-
}
428-
429-
public function eraseCredentials(): void
430-
{
431-
}
432-
});
400+
return $this->getUserIdentifier();
433401
}
434-
435-
public function getCredentials()
436-
{
437-
return null;
438-
}
439-
},
402+
}),
440403
new UserDataBag(null, null, '127.0.0.1', 'foo_user'),
441404
];
442405

406+
if (Kernel::VERSION_ID >= 503000) {
407+
yield 'token.authenticated = TRUE && token.user INSTANCEOF UserInterface WITHOUT getUsername' => [
408+
new RequestEvent(
409+
$this->createMock(HttpKernelInterface::class),
410+
new Request([], [], [], [], [], ['REMOTE_ADDR' => '127.0.0.1']),
411+
HttpKernelInterface::MASTER_REQUEST
412+
),
413+
$this->getMockedClientWithOptions(new Options(['send_default_pii' => true])),
414+
new MockToken(new class() extends MockUser {}),
415+
new UserDataBag(null, null, '127.0.0.1', 'foo_user'),
416+
];
417+
}
418+
443419
yield 'token.authenticated = TRUE && token.user INSTANCEOF object && __toString() method EXISTS' => [
444420
new RequestEvent(
445421
$this->createMock(HttpKernelInterface::class),
@@ -594,3 +570,46 @@ private function getMockedClientWithOptions(Options $options): ClientInterface
594570
return $client;
595571
}
596572
}
573+
574+
class MockToken extends AbstractToken
575+
{
576+
public function __construct(UserInterface $user)
577+
{
578+
parent::__construct();
579+
580+
$this->setAuthenticated(true);
581+
$this->setUser($user);
582+
}
583+
584+
public function getCredentials(): ?string
585+
{
586+
return null;
587+
}
588+
}
589+
590+
abstract class MockUser implements UserInterface
591+
{
592+
public function getUserIdentifier(): string
593+
{
594+
return 'foo_user';
595+
}
596+
597+
public function getRoles()
598+
{
599+
return [];
600+
}
601+
602+
public function getPassword()
603+
{
604+
return 'fake-pw';
605+
}
606+
607+
public function getSalt()
608+
{
609+
return 'fake-salt';
610+
}
611+
612+
public function eraseCredentials(): void
613+
{
614+
}
615+
}

0 commit comments

Comments
 (0)