From c59cf723901f7df6e7a57f723264cfb6282eadc3 Mon Sep 17 00:00:00 2001 From: Stephan Wentz Date: Wed, 12 Oct 2022 10:53:30 +0200 Subject: [PATCH] feat: Rework for symfony 6, drop PHP 7.4 support --- .github/workflows/test.yml | 1 - composer.json | 15 ++++++++----- src/Exception/NoSessionException.php | 22 +++++++++++++++++++ src/Exception/UnserializeFailedException.php | 5 ++++- src/Owner/SymfonySessionOwnerFactory.php | 21 ++++++++++++++---- src/Owner/SymfonyTokenOwnerFactory.php | 2 +- .../Owner/SymfonySessionOwnerFactoryTest.php | 11 +++++++++- tests/Owner/SymfonyTokenOwnerFactoryTest.php | 2 +- 8 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 src/Exception/NoSessionException.php diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3de4384..24ac927 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,7 +16,6 @@ jobs: matrix: dependencies: ["lowest", "highest"] php-version: - - "7.4" - "8.0" - "8.1" operating-system: ["ubuntu-latest"] diff --git a/composer.json b/composer.json index be0392d..f54a0a8 100644 --- a/composer.json +++ b/composer.json @@ -11,18 +11,18 @@ } ], "require": { - "php": "^7.4|^8.0" + "php": "^8.0" }, "require-dev": { "mikey179/vfsstream": "^1.6.10", "phpunit/phpunit": "^9.5", - "symfony/http-foundation": ">=4.4", - "symfony/security-core": ">=4.4", + "symfony/http-foundation": "^5.4|^6.0", + "symfony/security-core": "^5.4|^6.0", "phpspec/prophecy-phpunit": "^2.0.1", "squizlabs/php_codesniffer": "^3.6", "brainbits/phpcs-standard": "^5.0", - "phpstan/phpstan": "^0.12.99", - "brainbits/phpstan-rules": "^2.0" + "phpstan/phpstan": "^1.0", + "brainbits/phpstan-rules": "^3.0" }, "suggest": { "symfony/http-foundation": "If you want to use the SymfonySessionOwnerFactory", @@ -38,5 +38,10 @@ "branch-alias": { "dev-master": "4.0-dev" } + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/src/Exception/NoSessionException.php b/src/Exception/NoSessionException.php new file mode 100644 index 0000000..6a82b13 --- /dev/null +++ b/src/Exception/NoSessionException.php @@ -0,0 +1,22 @@ +session = $session; + $this->requestStack = $requestStack; } public function createOwner(): OwnerInterface { - return new Owner($this->session->getId()); + $request = $this->requestStack->getCurrentRequest(); + if (!$request instanceof Request) { + throw NoSessionException::create(); + } + + $session = $request->getSession(); + if (!$session instanceof SessionInterface) { + throw NoSessionException::create(); + } + + return new Owner($session->getId()); } } diff --git a/src/Owner/SymfonyTokenOwnerFactory.php b/src/Owner/SymfonyTokenOwnerFactory.php index 3b08612..7457732 100644 --- a/src/Owner/SymfonyTokenOwnerFactory.php +++ b/src/Owner/SymfonyTokenOwnerFactory.php @@ -42,6 +42,6 @@ public function createOwner(): OwnerInterface throw NoUserFoundException::create(); } - return new Owner($user->getUsername()); + return new Owner($user->getUserIdentifier()); } } diff --git a/tests/Owner/SymfonySessionOwnerFactoryTest.php b/tests/Owner/SymfonySessionOwnerFactoryTest.php index cf7cdc2..73f5a87 100644 --- a/tests/Owner/SymfonySessionOwnerFactoryTest.php +++ b/tests/Owner/SymfonySessionOwnerFactoryTest.php @@ -15,6 +15,8 @@ use Brainbits\Blocking\Owner\SymfonySessionOwnerFactory; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** @@ -26,7 +28,14 @@ class SymfonySessionOwnerFactoryTest extends TestCase public function testToString(): void { - $factory = new SymfonySessionOwnerFactory($this->createSession('foo')); + $request = new Request(); + $request->setSession($this->createSession('foo')); + + $requestStack = new RequestStack(); + $requestStack->push($request); + + $factory = new SymfonySessionOwnerFactory($requestStack); + $owner = $factory->createOwner(); $this->assertEquals($owner, new Owner('foo')); diff --git a/tests/Owner/SymfonyTokenOwnerFactoryTest.php b/tests/Owner/SymfonyTokenOwnerFactoryTest.php index 708ba2b..dfe0b48 100644 --- a/tests/Owner/SymfonyTokenOwnerFactoryTest.php +++ b/tests/Owner/SymfonyTokenOwnerFactoryTest.php @@ -60,7 +60,7 @@ public function testE2(): void private function createTokenStorage(string $username, bool $createToken = true, bool $createUser = true) { $user = $this->prophesize(UserInterface::class); - $user->getUsername() + $user->getUserIdentifier() ->willReturn($username); $token = $this->prophesize(TokenInterface::class);