Skip to content

Commit 24c7334

Browse files
committed
minor #801 [make:auth] handle session deprecations in 5.3 (jrushlow)
This PR was squashed before being merged into the 1.0-dev branch. Discussion ---------- [make:auth] handle session deprecations in 5.3 - fixed deprecated ReflectionType::__toString() (MakeAuthenticatorTest) Deprecated in PHP 7.1 - removes unneeded optional param - triggers required param order deprecation in PHP8 (GeneratorTest) - handles Session deprecation's introduced in Symfony 5.3 in test suites Commits ------- cf0a27d handle session deprecations in 5.3 for make:auth tests b275bae fixed required parameter order 3a0a2ac fixed deprecated ReflectionType::__toString()
2 parents 486a4e8 + cf0a27d commit 24c7334

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

src/Maker/MakeAuthenticator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,12 @@ public function configureDependencies(DependencyBuilder $dependencies, InputInte
403403
private function providerKeyTypeHint(): string
404404
{
405405
$reflectionMethod = new \ReflectionMethod(AbstractFormLoginAuthenticator::class, 'onAuthenticationSuccess');
406-
$typeHint = (string) $reflectionMethod->getParameters()[2]->getType();
407-
if ($typeHint) {
408-
$typeHint .= ' ';
406+
$type = $reflectionMethod->getParameters()[2]->getType();
407+
408+
if (!$type instanceof \ReflectionNamedType) {
409+
return '';
409410
}
410411

411-
return $typeHint;
412+
return sprintf('%s ', $type->getName());
412413
}
413414
}

tests/GeneratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class GeneratorTest extends TestCase
2121
/**
2222
* @dataProvider getClassNameDetailsTests
2323
*/
24-
public function testCreateClassNameDetails(string $name, string $prefix, string $suffix = '', string $expectedFullClassName, string $expectedRelativeClassName)
24+
public function testCreateClassNameDetails(string $name, string $prefix, string $suffix, string $expectedFullClassName, string $expectedRelativeClassName): void
2525
{
2626
$fileManager = $this->createMock(FileManager::class);
2727
$fileManager->expects($this->any())
@@ -38,7 +38,7 @@ public function testCreateClassNameDetails(string $name, string $prefix, string
3838
$this->assertSame($expectedRelativeClassName, $classNameDetails->getRelativeName());
3939
}
4040

41-
public function getClassNameDetailsTests()
41+
public function getClassNameDetailsTests(): \Generator
4242
{
4343
yield 'simple_class' => [
4444
'foo',

tests/fixtures/MakeAuthenticatorLoginFormUserEntity/tests/SecurityControllerTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Security\AppCustomAuthenticator;
77
use Doctrine\ORM\EntityManagerInterface;
88
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
9+
use Symfony\Component\HttpKernel\Kernel;
910

1011
class SecurityControllerTest extends WebTestCase
1112
{
@@ -60,7 +61,17 @@ public function testCommand()
6061
$client->submit($form);
6162

6263
$this->assertStringContainsString('TODO: provide a valid redirect', $client->getResponse()->getContent());
63-
$this->assertNotNull($token = $client->getContainer()->get('security.token_storage')->getToken());
64+
65+
$tokenStorage = static::$container->get('security.token_storage');
66+
67+
// Handle Session deprecations in Symfony 5.3+
68+
if (Kernel::VERSION_ID >= 50300) {
69+
$tokenStorage->disableUsageTracking();
70+
}
71+
72+
$token = $tokenStorage->getToken();
73+
74+
self::assertNotNull($token);
6475
$this->assertInstanceOf(User::class, $token->getUser());
6576
}
6677
}

tests/fixtures/MakeAuthenticatorLoginFormUserEntityLogout/tests/SecurityControllerTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use App\Security\AppCustomAuthenticator;
77
use Doctrine\ORM\EntityManagerInterface;
88
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
9+
use Symfony\Component\HttpKernel\Kernel;
10+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
911

1012
class SecurityControllerTest extends WebTestCase
1113
{
@@ -60,10 +62,23 @@ public function testCommand()
6062
$client->submit($form);
6163

6264
$this->assertStringContainsString('TODO: provide a valid redirect', $client->getResponse()->getContent());
63-
$this->assertNotNull($token = $client->getContainer()->get('security.token_storage')->getToken());
64-
$this->assertInstanceOf(User::class, $token->getUser());
65+
$this->assertInstanceOf(User::class, $this->getToken()->getUser());
6566

6667
$client->request('GET', '/logout');
67-
$this->assertNull($client->getContainer()->get('security.token_storage')->getToken());
68+
$this->assertNull($this->getToken());
69+
}
70+
71+
/**
72+
* Handle Session deprecations in Symfony 5.3+
73+
*/
74+
private function getToken(): ?TokenInterface
75+
{
76+
$tokenStorage = static::$container->get('security.token_storage');
77+
78+
if (Kernel::VERSION_ID >= 50300) {
79+
$tokenStorage->disableUsageTracking();
80+
}
81+
82+
return $tokenStorage->getToken();
6883
}
6984
}

tests/fixtures/MakeAuthenticatorSecurity52LoginForm/tests/SecurityControllerTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Security\AppTestSecurity52LoginFormAuthenticator;
77
use Doctrine\ORM\EntityManagerInterface;
88
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
9+
use Symfony\Component\HttpKernel\Kernel;
910
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1011

1112
/**
@@ -68,7 +69,17 @@ public function testLoginFormAuthenticatorUsingSecurity51(): void
6869
$client->submit($form);
6970

7071
self::assertStringContainsString('TODO: provide a valid redirect', $client->getResponse()->getContent());
71-
self::assertNotNull($token = $client->getContainer()->get('security.token_storage')->getToken());
72+
73+
$tokenStorage = static::$container->get('security.token_storage');
74+
75+
// Handle Session deprecations in Symfony 5.3+
76+
if (Kernel::VERSION_ID >= 50300) {
77+
$tokenStorage->disableUsageTracking();
78+
}
79+
80+
$token = $tokenStorage->getToken();
81+
82+
self::assertNotNull($token);
7283
self::assertInstanceOf(User::class, $token->getUser());
7384
}
7485
}

0 commit comments

Comments
 (0)