From 9c5ff8a66b9a28f052a1bcf1d73eed5a4d698fe6 Mon Sep 17 00:00:00 2001 From: Michel Roca Date: Fri, 14 Jun 2024 20:07:25 +0200 Subject: [PATCH] chore: add Symfony 7 support --- README.md | 4 +- composer.json | 35 +++++++-------- doc/02 - The UserSubscription Manager.md | 4 +- phpunit.xml.dist | 31 ++++++------- src/DependencyInjection/Configuration.php | 10 +---- tests/BundleTest.php | 18 ++++---- tests/Classes/TestKernel.php | 29 ++++-------- tests/Classes/TestUser.php | 23 +++------- tests/Classes/TestUserSubscription.php | 45 +++---------------- tests/Classes/TestUserSubscriptionManager.php | 14 +----- tests/RegistrationTest.php | 7 +-- tests/Resources/framework.yaml | 1 + tests/bootstrap.php | 25 +++++++++++ 13 files changed, 96 insertions(+), 150 deletions(-) create mode 100644 tests/bootstrap.php diff --git a/README.md b/README.md index e03de99..f38fdb4 100644 --- a/README.md +++ b/README.md @@ -39,14 +39,12 @@ This bundle is just the back-end part of the subscription process. For the front ### Composer is your friend: -PHP7.1+ is required. +PHP8.1+ is required. ```bash composer require bentools/webpush-bundle ``` -If you're using Symfony 3, add the bundle to your kernel. With Symfony Flex, this should be done automatically. - ⚠️ _We aren't on stable version yet - expect some changes._ diff --git a/composer.json b/composer.json index 1e772c3..a9581a2 100644 --- a/composer.json +++ b/composer.json @@ -15,25 +15,23 @@ "ext-json": "*", "ext-mbstring": "*", "ext-openssl": "*", - "guzzlehttp/guzzle": "^6.5.8|^7.4", - "minishlink/web-push": "~6.0|~7.0|~8.0", - "symfony/http-kernel": "^4.4.50|^5.4.20|^6.0" + "guzzlehttp/guzzle": "^6.5.8 || ^7.4", + "minishlink/web-push": "^6.0.7 || ^7.0 || ^8.0", + "symfony/http-kernel": "^5.4.20 || ^6.0 || ^7.0" }, "require-dev": { "bentools/doctrine-static": "1.0.x-dev", - "doctrine/dbal": "~2.5 <=2.9", - "nyholm/symfony-bundle-test": "~1.8", - "phpunit/phpunit": "^8.5.38|~9.0", - "symfony/config": "~4.0|~5.0|~6.0", - "symfony/dependency-injection": "~3.0|~4.0|~5.0|~6.0", - "symfony/framework-bundle": "~3.0|~4.0|~5.0|~6.0", - "symfony/http-foundation": "~3.0|~4.0|~5.0|~6.0", - "symfony/routing": "~3.0|~4.0|~5.0|~6.0", - "symfony/security": "~3.0|~4.0|~5.0|~6.0", - "symfony/var-dumper": "~3.0|~4.0|~5.0|~6.0", - "symfony/yaml": "~3.0|~4.0|~5.0|~6.0", - "twig/twig": "~1.0|~2.0", - "web-token/jwt-util-ecc": ">=2.1" + "doctrine/dbal": "^2.9 || ^3.0", + "phpunit/phpunit": "^9.0", + "symfony/config": "^5.4 || ^6.0 || ^7.0", + "symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0", + "symfony/framework-bundle": "^5.4 || ^6.0 || ^7.0", + "symfony/http-foundation": "^5.4 || ^6.0 || ^7.0", + "symfony/routing": "^5.4 || ^6.0 || ^7.0", + "symfony/security-core": "^5.4 || ^6.0 || ^7.0", + "symfony/var-dumper": "^5.4 || ^6.0 || ^7.0", + "symfony/yaml": "^5.4 || ^6.0 || ^7.0", + "twig/twig": "^2.0 || ^3.0" }, "autoload": { "psr-4": { @@ -43,10 +41,7 @@ "autoload-dev": { "psr-4": { "BenTools\\WebPushBundle\\Tests\\": "tests" - }, - "files": [ - "vendor/symfony/var-dumper/Resources/functions/dump.php" - ] + } }, "config": { "sort-packages": true diff --git a/doc/02 - The UserSubscription Manager.md b/doc/02 - The UserSubscription Manager.md index f36d7ae..6622bec 100644 --- a/doc/02 - The UserSubscription Manager.md +++ b/doc/02 - The UserSubscription Manager.md @@ -15,7 +15,7 @@ namespace App\Services; use App\Entity\UserSubscription; use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionInterface; use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionManagerInterface; -use Doctrine\Common\Persistence\ManagerRegistry; +use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\Security\Core\User\UserInterface; class UserSubscriptionManager implements UserSubscriptionManagerInterface @@ -120,4 +120,4 @@ services: Previous: [The UserSubscription Class](01%20-%20The%20UserSubscription%20Class.md) -Next: [Configuration](03%20-%20Configuration.md) \ No newline at end of file +Next: [Configuration](03%20-%20Configuration.md) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4bd0fd2..26c90bd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,31 +1,28 @@ - + + + src + + - - - - - - + + + + + + - tests - - - - src - - diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 5d9d45d..04259a4 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -4,19 +4,13 @@ use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; -use Symfony\Component\HttpKernel\Kernel; class Configuration implements ConfigurationInterface { public function getConfigTreeBuilder(): TreeBuilder { - if (Kernel::MAJOR_VERSION < 4) { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('bentools_webpush'); - } else { - $treeBuilder = new TreeBuilder('bentools_webpush'); - $rootNode = $treeBuilder->getRootNode(); - } + $treeBuilder = new TreeBuilder('bentools_webpush'); + $rootNode = $treeBuilder->getRootNode(); $rootNode ->children() diff --git a/tests/BundleTest.php b/tests/BundleTest.php index fff0412..7c623be 100644 --- a/tests/BundleTest.php +++ b/tests/BundleTest.php @@ -17,31 +17,31 @@ protected function setUp(): void /** * @test */ - public function parameters_are_set() + public function parameters_are_set(): void { - $this->assertEquals('this_is_a_private_key', self::$kernel->getContainer()->getParameter('bentools_webpush.vapid_private_key')); - $this->assertEquals('this_is_a_public_key', self::$kernel->getContainer()->getParameter('bentools_webpush.vapid_public_key')); - $this->assertTrue(self::$kernel->getContainer()->has(UserSubscriptionManagerRegistry::class)); + $this->assertEquals('this_is_a_private_key', self::getContainer()->getParameter('bentools_webpush.vapid_private_key')); + $this->assertEquals('this_is_a_public_key', self::getContainer()->getParameter('bentools_webpush.vapid_public_key')); + $this->assertTrue(self::getContainer()->has(UserSubscriptionManagerRegistry::class)); } /** * @test */ - public function manager_is_found() + public function manager_is_found(): void { // Find by class name - $this->assertInstanceOf(TestUserSubscriptionManager::class, self::$kernel->getContainer()->get(UserSubscriptionManagerRegistry::class)->getManager(TestUser::class)); + $this->assertInstanceOf(TestUserSubscriptionManager::class, self::getContainer()->get(UserSubscriptionManagerRegistry::class)->getManager(TestUser::class)); // Find by object - $this->assertInstanceOf(TestUserSubscriptionManager::class, self::$kernel->getContainer()->get(UserSubscriptionManagerRegistry::class)->getManager(new TestUser('foo'))); + $this->assertInstanceOf(TestUserSubscriptionManager::class, self::getContainer()->get(UserSubscriptionManagerRegistry::class)->getManager(new TestUser('foo'))); } /** * @test */ - public function unknown_manager_raises_exception() + public function unknown_manager_raises_exception(): void { $this->expectException(\InvalidArgumentException::class); - self::$kernel->getContainer()->get(UserSubscriptionManagerRegistry::class)->getManager(Foo::class); + self::getContainer()->get(UserSubscriptionManagerRegistry::class)->getManager(Foo::class); } } diff --git a/tests/Classes/TestKernel.php b/tests/Classes/TestKernel.php index 7f59d78..0b363ab 100644 --- a/tests/Classes/TestKernel.php +++ b/tests/Classes/TestKernel.php @@ -3,20 +3,18 @@ namespace BenTools\WebPushBundle\Tests\Classes; use BenTools\WebPushBundle\WebPushBundle; -use Nyholm\BundleTest\CompilerPass\PublicServicePass; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Kernel; -use Symfony\Component\Routing\RouteCollectionBuilder; final class TestKernel extends Kernel { use MicroKernelTrait; - protected $cacheDir; - protected $logDir; + protected string $cacheDir; + protected string $logDir; public function __construct(string $environment, bool $debug) { @@ -26,7 +24,7 @@ public function __construct(string $environment, bool $debug) $this->logDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $uniqid . DIRECTORY_SEPARATOR . 'logs'; } - public function registerBundles() + public function registerBundles(): iterable { return [ new FrameworkBundle(), @@ -34,36 +32,27 @@ public function registerBundles() ]; } - protected function configureRoutes(RouteCollectionBuilder $routes) + protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void { - } - - protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) - { - $c->loadFromExtension('framework', [ + $container->loadFromExtension('framework', [ 'secret' => getenv('APP_SECRET'), ]); - $c->loadFromExtension('bentools_webpush', [ + $container->loadFromExtension('bentools_webpush', [ 'settings' => [ 'private_key' => 'this_is_a_private_key', 'public_key' => 'this_is_a_public_key', ] ]); $loader->load(dirname(__DIR__) . '/Resources/services.yaml'); - - if (1 === version_compare(self::VERSION, '4.0')) { - $loader->load(dirname(__DIR__) . '/Resources/framework.yaml'); - } - - $c->addCompilerPass(new PublicServicePass()); + $loader->load(dirname(__DIR__) . '/Resources/framework.yaml'); } - public function getCacheDir() + public function getCacheDir(): string { return $this->cacheDir; } - public function getLogDir() + public function getLogDir(): string { return $this->logDir; } diff --git a/tests/Classes/TestUser.php b/tests/Classes/TestUser.php index 8419013..3605812 100644 --- a/tests/Classes/TestUser.php +++ b/tests/Classes/TestUser.php @@ -6,34 +6,21 @@ final class TestUser implements UserInterface { - /** - * @var string - */ - private $userName; - - public function __construct(string $userName) + public function __construct(private string $userName) { - $this->userName = $userName; } - public function getUsername() + public function getUserIdentifier(): string { return $this->userName; } - public function getRoles() - { - } - - public function getPassword() - { - } - - public function getSalt() + public function getRoles(): array { + return []; } - public function eraseCredentials() + public function eraseCredentials(): void { } } diff --git a/tests/Classes/TestUserSubscription.php b/tests/Classes/TestUserSubscription.php index b85e56b..ddc0a52 100644 --- a/tests/Classes/TestUserSubscription.php +++ b/tests/Classes/TestUserSubscription.php @@ -7,47 +7,16 @@ final class TestUserSubscription implements UserSubscriptionInterface { - - private $id; - - /** - * @var UserInterface - */ - private $user; - - /** - * @var string - */ - private $endpoint; - - /** - * @var string - */ - private $publicKey; - - /** - * @var string - */ - private $authtoken; - - /** - * @var string - */ - private $subscriptionHash; + private string $id; public function __construct( - UserInterface $user, - string $endpoint, - string $publicKey, - string $authtoken, - string $subscriptionHash + private UserInterface $user, + private string $endpoint, + private string $publicKey, + private string $authtoken, + private string $subscriptionHash ) { - $this->id = $user->getUsername(); - $this->user = $user; - $this->endpoint = $endpoint; - $this->publicKey = $publicKey; - $this->authtoken = $authtoken; - $this->subscriptionHash = $subscriptionHash; + $this->id = $user->getUserIdentifier(); } public function getUser(): UserInterface diff --git a/tests/Classes/TestUserSubscriptionManager.php b/tests/Classes/TestUserSubscriptionManager.php index f53f520..25beb72 100644 --- a/tests/Classes/TestUserSubscriptionManager.php +++ b/tests/Classes/TestUserSubscriptionManager.php @@ -4,23 +4,13 @@ use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionInterface; use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionManagerInterface; -use Doctrine\Common\Persistence\ManagerRegistry; +use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\Security\Core\User\UserInterface; final class TestUserSubscriptionManager implements UserSubscriptionManagerInterface { - /** - * @var ManagerRegistry - */ - private $doctrine; - - /** - * UserSubscriptionManager constructor. - * @param ManagerRegistry $doctrine - */ - public function __construct(ManagerRegistry $doctrine) + public function __construct(private readonly ManagerRegistry $doctrine) { - $this->doctrine = $doctrine; } /** diff --git a/tests/RegistrationTest.php b/tests/RegistrationTest.php index b18911c..cb9f968 100644 --- a/tests/RegistrationTest.php +++ b/tests/RegistrationTest.php @@ -22,15 +22,16 @@ protected function setUp(): void public function registration_works() { /** @var ManagerRegistry $persistence */ - $persistence = self::$kernel->getContainer()->get('doctrine'); - $registry = self::$kernel->getContainer()->get(UserSubscriptionManagerRegistry::class); + $persistence = self::getContainer()->get('doctrine'); + $registry = self::getContainer()->get(UserSubscriptionManagerRegistry::class); $em = $persistence->getManagerForClass(TestUser::class); $bob = new TestUser('bob'); $em->persist($bob); $em->flush(); $this->assertNotNull($em->find(TestUser::class, 'bob')); - $register = self::$kernel->getContainer()->get(RegisterSubscriptionAction::class); + /** @var RegisterSubscriptionAction $register */ + $register = self::getContainer()->get(RegisterSubscriptionAction::class); $rawSubscriptionData = [ 'subscription' => [ diff --git a/tests/Resources/framework.yaml b/tests/Resources/framework.yaml index c60267c..71ee6c4 100644 --- a/tests/Resources/framework.yaml +++ b/tests/Resources/framework.yaml @@ -1,3 +1,4 @@ framework: + test: true cache: default_pdo_provider: ~ diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..1ef4244 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,25 @@ +