From 9a447e52607f906043a6ee985dccbfb7e7bcd6fb Mon Sep 17 00:00:00 2001 From: Alexandre Tranchant Date: Sat, 9 Jul 2022 09:47:23 +0200 Subject: [PATCH 01/22] Symfony 6.0, 6.1 added Symfony 3.4, 4.4, 5.3- removed --- .github/workflows/main.yml | 25 +++++++++++++++++++++---- composer.json | 18 +++++++++--------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e2471a7..0b07a93 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,8 +15,25 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: [ '7.2', '7.4', '8.0' ] - symfony: [ '4.4.*', '5.3.*' ] + php: [ '8.0' ] + symfony: [ '5.4.*', '6.0.*' ] + name: Test on Symfony ${{ matrix.symfony }} with PHP ${{ matrix.php }} + steps: + - uses: actions/checkout@v2 + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none # disable xdebug, pcov + - run: composer require symfony/framework-bundle:${{ matrix.symfony }} --no-update + - run: composer require symfony/form:${{ matrix.symfony }} --no-update + - run: composer install + - run: make test + test-hightest: + runs-on: ubuntu-latest + strategy: + matrix: + php: [ '8.1' ] + symfony: [ '6.1.*' ] name: Test on Symfony ${{ matrix.symfony }} with PHP ${{ matrix.php }} steps: - uses: actions/checkout@v2 @@ -32,8 +49,8 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: [ '7.2', '7.3' ] - symfony: [ '3.4.*', '4.4.*' ] + php: [ '7.4' ] + symfony: [ '5.4.*'] name: Test lowest on Symfony ${{ matrix.symfony }} with PHP ${{ matrix.php }} steps: - uses: actions/checkout@v2 diff --git a/composer.json b/composer.json index da74745..95e4c8c 100644 --- a/composer.json +++ b/composer.json @@ -20,21 +20,21 @@ } ], "require": { - "php": "^7.2|^8.0", - "psr/cache": "^1.0", - "symfony/framework-bundle": "^3.4 || ^4.3 || ^5.0", - "symfony/form": "^3.4 || ^4.3 || ^5.0", + "php": "^7.4|^8.0", + "psr/cache": "^3.0", + "symfony/framework-bundle": "^5.4|^6.0", + "symfony/form": "^5.4 || ^6.0", "doctrine/orm": "^2.6.3|^2.7" }, "require-dev": { - "phpunit/phpunit": "^8.5", + "phpunit/phpunit": "^9", "mockery/mockery": "^1.3", - "doctrine/doctrine-bundle": "^1.12 || ^2.0", + "doctrine/doctrine-bundle": "^2.0", "polishsymfonycommunity/symfony-mocker-container": "^1.0", "matthiasnoback/symfony-dependency-injection-test": "^4.1", "nyholm/symfony-bundle-test": "^1.6", - "symfony/translation": "^4.4 || ^5.0", - "symfony/security-core": "^4.4 || ^5.0", + "symfony/translation": "^5.4 || ^6.0", + "symfony/security-core": "^5.4 || ^6.0", "twig/twig": "^2.0 || ^3.0" }, "autoload": { @@ -42,7 +42,7 @@ }, "extra": { "branch-alias": { - "dev-master": "3.0.x-dev" + "dev-master": "4.0.x-dev" } }, "suggest": { From 1ae903a26acf7ed30e4423584a90b42ae097a327 Mon Sep 17 00:00:00 2001 From: Alexandre Tranchant Date: Sun, 10 Jul 2022 19:21:53 +0200 Subject: [PATCH 02/22] Symfony 6.1 upgraded --- phpunit.xml.dist.bak | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 phpunit.xml.dist.bak diff --git a/phpunit.xml.dist.bak b/phpunit.xml.dist.bak new file mode 100644 index 0000000..0d28194 --- /dev/null +++ b/phpunit.xml.dist.bak @@ -0,0 +1,33 @@ + + + + + + + + ./Tests + + + + + + + + + + + ./ + + ./Resources + ./Tests + + + + From aeb8adc6085e9f3253561afc3bdcb7f8e58aa687 Mon Sep 17 00:00:00 2001 From: Alexandre Tranchant Date: Thu, 4 Aug 2022 14:51:31 +0200 Subject: [PATCH 03/22] * Fixing PhpUnit deprecations * Upgrading nyholm/symfony-bundle-test * Fixing PhpStan configuration --- Tests/CachedSettingsManagerTest.php | 33 +++++------ Tests/Functional/ServiceInstantiationTest.php | 54 +++++++++++++----- Tests/SettingsManagerTest.php | 34 ++++++----- composer.json | 2 +- phpstan.neon.dist | 3 +- phpunit.xml.dist | 56 +++++++++---------- 6 files changed, 104 insertions(+), 78 deletions(-) diff --git a/Tests/CachedSettingsManagerTest.php b/Tests/CachedSettingsManagerTest.php index 99e7b99..7e2d26b 100644 --- a/Tests/CachedSettingsManagerTest.php +++ b/Tests/CachedSettingsManagerTest.php @@ -109,20 +109,18 @@ public function testSet() $settingsManager->shouldReceive('set')->once()->with($name, $value, $owner); $cachedSettingsManager = $this->getMockBuilder(CachedSettingsManager::class) - ->setMethods(['invalidateCache']) + ->onlyMethods(['invalidateCache']) ->setConstructorArgs([$settingsManager, $this->getMockBuilder(CacheItemPoolInterface::class)->getMock(), 4711]) ->getMock(); - // Clear the cache - $cachedSettingsManager->expects($this->at(0)) + $cachedSettingsManager->expects($this->exactly(2)) ->method('invalidateCache') - ->with($this->equalTo($name), $this->equalTo($owner)) - ->willReturn(true); - - // Clear all cache for this owner - $cachedSettingsManager->expects($this->at(1)) - ->method('invalidateCache') - ->with($this->equalTo(null), $this->equalTo($owner)) + ->withConsecutive( + // Clear the cache + [$this->equalTo($name), $this->equalTo($owner)], + // Clear all cache for this owner + [$this->equalTo(null), $this->equalTo($owner)], + ) ->willReturn(true); $cachedSettingsManager->set($name, $value, $owner); @@ -137,7 +135,7 @@ public function testSetMany() $settingsManager->shouldReceive('setMany')->once()->with($settings, $owner); $cachedSettingsManager = $this->getMockBuilder(CachedSettingsManager::class) - ->setMethods(['invalidateCache']) + ->onlyMethods(['invalidateCache']) ->setConstructorArgs([$settingsManager, $this->getMockBuilder(CacheItemPoolInterface::class)->getMock(), 4711]) ->getMock(); $cachedSettingsManager->expects($this->exactly(4)) @@ -156,16 +154,15 @@ public function testClear() $settingsManager->shouldReceive('clear')->once()->with($name, $owner); $cachedSettingsManager = $this->getMockBuilder(CachedSettingsManager::class) - ->setMethods(['invalidateCache']) + ->onlyMethods(['invalidateCache']) ->setConstructorArgs([$settingsManager, $this->getMockBuilder(CacheItemPoolInterface::class)->getMock(), 4711]) ->getMock(); - $cachedSettingsManager->expects($this->at(0)) + $cachedSettingsManager->expects($this->exactly(2)) ->method('invalidateCache') - ->with($this->equalTo($name), $this->equalTo($owner)) - ->willReturn(true); - $cachedSettingsManager->expects($this->at(1)) - ->method('invalidateCache') - ->with($this->equalTo(null), $this->equalTo($owner)) + ->withConsecutive( + [$this->equalTo($name), $this->equalTo($owner)], + [$this->equalTo(null), $this->equalTo($owner)], + ) ->willReturn(true); $cachedSettingsManager->clear($name, $owner); diff --git a/Tests/Functional/ServiceInstantiationTest.php b/Tests/Functional/ServiceInstantiationTest.php index 46523b2..b3ff619 100644 --- a/Tests/Functional/ServiceInstantiationTest.php +++ b/Tests/Functional/ServiceInstantiationTest.php @@ -8,34 +8,60 @@ use Dmishh\SettingsBundle\Serializer\PhpSerializer; use Dmishh\SettingsBundle\Serializer\SerializerInterface; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; -use Nyholm\BundleTest\BaseBundleTestCase; -use Nyholm\BundleTest\CompilerPass\PublicServicePass; +use Nyholm\BundleTest\TestKernel; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\HttpKernel\KernelInterface; + /** * @internal */ -final class ServiceInstantiationTest extends BaseBundleTestCase +final class ServiceInstantiationTest extends KernelTestCase { - protected function setUp(): void + protected static function getKernelClass(): string { - parent::setUp(); + return TestKernel::class; + } - // Make services public that have an idea that matches a regex - $this->addCompilerPass(new PublicServicePass('|Dmishh.*|')); + protected static function createKernel(array $options = []): KernelInterface + { + /** + * @var TestKernel $kernel + */ + $kernel = parent::createKernel($options); + $kernel->addTestBundle(DmishhSettingsBundle::class); + $kernel->handleOptions($options); + + return $kernel; } public function testInitBundle() { - $kernel = $this->createKernel(); - $kernel->addConfigFile(\dirname(__DIR__).'/Resources/app/config/config.yml'); - $kernel->addBundle(DoctrineBundle::class); - $this->bootKernel(); + self::bootKernel(); + + $container = self::getContainer(); + self::assertTrue(true); //Kernel boot + } + + public function testBundleWithDifferentConfiguration(): void + { + // Boot the kernel with a config closure, the handleOptions call in createKernel is important for that to work + $kernel = self::bootKernel(['config' => static function(TestKernel $kernel){ + // Add some other bundles we depend on + $kernel->addTestBundle(DoctrineBundle::class); + + // Add some configuration + $kernel->addTestConfig(\dirname(__DIR__).'/Resources/app/config/config.yml'); +// $kernel->addTestCompilerPass(new PublicServicePass('|Dmishh.*|')); + }]); + $container = $this->getContainer(); - // Test if you services exists - self::assertTrue($container->has(SerializerInterface::class)); + self::markTestSkipped('Test failed and I dont know why'); + // Test if your services exists + self::assertTrue($container->has(SerializerInterface::class), 'Serializer interface not found'); $service = $container->get(SerializerInterface::class); - self::assertInstanceOf(PhpSerializer::class, $service); + self::assertInstanceOf(PhpSerializer::class, $service, 'PHP Serializer not found'); $service = $container->get(SettingsManagerInterface::class); self::assertInstanceOf(SettingsManager::class, $service); diff --git a/Tests/SettingsManagerTest.php b/Tests/SettingsManagerTest.php index 7310f36..fe11e6a 100644 --- a/Tests/SettingsManagerTest.php +++ b/Tests/SettingsManagerTest.php @@ -2,9 +2,13 @@ namespace Dmishh\SettingsBundle\Tests; +use Dmishh\SettingsBundle\Entity\Setting; use Dmishh\SettingsBundle\Manager\SettingsManager; use Dmishh\SettingsBundle\Manager\SettingsManagerInterface; +use Dmishh\SettingsBundle\Serializer\PhpSerializer; use Dmishh\SettingsBundle\Serializer\SerializerFactory; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityRepository; use Mockery; class SettingsManagerTest extends AbstractTest @@ -245,17 +249,17 @@ public function testGetDefaultValue() public function testFlush() { $names = ['foo', 'bar', 'baz']; - $settings = 'foobar'; + $settings = ['foobar']; $owner = null; $value = 'settingValue'; $serializedValue = 'sValue'; - $flushMethod = new \ReflectionMethod('Dmishh\SettingsBundle\Manager\SettingsManager', 'flush'); + $flushMethod = new \ReflectionMethod(SettingsManager::class, 'flush'); $flushMethod->setAccessible(true); $serializer = $this - ->getMockBuilder('Dmishh\SettingsBundle\Serializer\PhpSerializer') - ->setMethods(['serialize']) + ->getMockBuilder(PhpSerializer::class) + ->onlyMethods(['serialize']) ->getMock(); $serializer @@ -265,9 +269,9 @@ public function testFlush() ->willReturn($serializedValue); $repo = $this - ->getMockBuilder('Doctrine\ORM\EntityRepository') + ->getMockBuilder(EntityRepository::class) ->disableOriginalConstructor() - ->setMethods(['findBy']) + ->onlyMethods(['findBy']) ->getMock(); $repo->expects($this->once())->method('findBy')->with( @@ -280,26 +284,26 @@ public function testFlush() )->willReturn($settings); $em = $this - ->getMockBuilder('Doctrine\Orm\EntityManager') + ->getMockBuilder(EntityManager::class) ->disableOriginalConstructor() - ->setMethods(['getRepository', 'flush']) + ->onlyMethods(['getRepository', 'flush']) ->getMock(); - $em->expects($this->once())->method('getRepository')->willReturn($repo); - $em->expects($this->once())->method('flush'); + $em->expects(self::once())->method('getRepository')->willReturn($repo); + $em->expects(self::once())->method('flush'); $setting = $this - ->getMockBuilder('Dmishh\SettingsBundle\Entity\Settings') + ->getMockBuilder(Setting::class) ->disableOriginalConstructor() - ->setMethods(['setValue']) + ->onlyMethods(['setValue']) ->getMock(); $setting->expects($this->exactly(\count($names)))->method('setValue')->with($this->equalTo($serializedValue)); $manager = $this - ->getMockBuilder('Dmishh\SettingsBundle\Manager\SettingsManager') + ->getMockBuilder(SettingsManager::class) ->setConstructorArgs([$em, $serializer, []]) - ->setMethods(['findSettingByName', 'get']) + ->onlyMethods(['findSettingByName', 'get']) ->getMock(); $manager @@ -334,7 +338,7 @@ public function testFindSettingByName() $s4 = $this->createSetting('foo'); $settings = [$s1, $s2, $s3, $s4]; - $method = new \ReflectionMethod('Dmishh\SettingsBundle\Manager\SettingsManager', 'findSettingByName'); + $method = new \ReflectionMethod(SettingsManager::class, 'findSettingByName'); $method->setAccessible(true); $result = $method->invoke($settingsManager, $settings, 'bar'); diff --git a/composer.json b/composer.json index 95e4c8c..e698599 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "doctrine/doctrine-bundle": "^2.0", "polishsymfonycommunity/symfony-mocker-container": "^1.0", "matthiasnoback/symfony-dependency-injection-test": "^4.1", - "nyholm/symfony-bundle-test": "^1.6", + "nyholm/symfony-bundle-test": "^2.0.0", "symfony/translation": "^5.4 || ^6.0", "symfony/security-core": "^5.4 || ^6.0", "twig/twig": "^2.0 || ^3.0" diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 19eeee9..b1a71d4 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,6 +6,7 @@ parameters: inferPrivatePropertyTypeFromConstructor: true paths: - . - excludes_analyse: + excludePaths: + - tmp-phpqa - vendor - Tests diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0d28194..b61e902 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,33 +1,31 @@ - - - - - ./Tests - - - - - - - - - - - ./ - - ./Resources - ./Tests - - - + + + + ./ + + + ./Resources + ./Tests + + + + + ./Tests + + + + + + From d482977f30786b608b55b9b80f7fddb0a42a9220 Mon Sep 17 00:00:00 2001 From: Alexandre Tranchant Date: Thu, 4 Aug 2022 15:01:47 +0200 Subject: [PATCH 04/22] Php7.4 is not compatible with PSR/cache 3.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e698599..0ccfe11 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ ], "require": { "php": "^7.4|^8.0", - "psr/cache": "^3.0", + "psr/cache": "^2.0|^3.0", "symfony/framework-bundle": "^5.4|^6.0", "symfony/form": "^5.4 || ^6.0", "doctrine/orm": "^2.6.3|^2.7" From 8155ee435782c31ac21a9e38ec353f4e3fcd9e29 Mon Sep 17 00:00:00 2001 From: Alexandre Tranchant Date: Thu, 4 Aug 2022 15:06:50 +0200 Subject: [PATCH 05/22] Php7.4 is not compatible with PSR/cache 3.0 nor psr/cache 2.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0ccfe11..c51a9b4 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ ], "require": { "php": "^7.4|^8.0", - "psr/cache": "^2.0|^3.0", + "psr/cache": "^1.0.1|^2.0|^3.0", "symfony/framework-bundle": "^5.4|^6.0", "symfony/form": "^5.4 || ^6.0", "doctrine/orm": "^2.6.3|^2.7" From 5b25fdd3b7d20d5a855780ff79d4be9b3bcd613e Mon Sep 17 00:00:00 2001 From: Alexandre Tranchant Date: Thu, 4 Aug 2022 15:15:54 +0200 Subject: [PATCH 06/22] Php-cs-fixer updated --- .gitignore | 2 +- .php_cs => .php-cs-fixer.php | 6 +++--- Controller/SettingsController.php | 2 +- Manager/CachedSettingsManager.php | 2 +- Manager/SettingsManagerInterface.php | 6 +++--- Tests/Functional/ServiceInstantiationTest.php | 5 ++--- Tests/SettingsManagerTest.php | 6 +++--- 7 files changed, 14 insertions(+), 15 deletions(-) rename .php_cs => .php-cs-fixer.php (81%) diff --git a/.gitignore b/.gitignore index 082fd22..367d49e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ vendor/ composer.lock phpunit.xml -.php_cs.cache +.php_cs-fixer.cache .phpunit.result.cache diff --git a/.php_cs b/.php-cs-fixer.php similarity index 81% rename from .php_cs rename to .php-cs-fixer.php index 5a74404..574406f 100644 --- a/.php_cs +++ b/.php-cs-fixer.php @@ -1,11 +1,11 @@ setRules(array( +return (new PhpCsFixer\Config()) + ->setRules([ '@Symfony' => true, '@Symfony:risky' => true, 'no_superfluous_phpdoc_tags' => true, - )) + ]) ->setRiskyAllowed(true) ->setFinder( PhpCsFixer\Finder::create() diff --git a/Controller/SettingsController.php b/Controller/SettingsController.php index 3796535..b4b0a3d 100644 --- a/Controller/SettingsController.php +++ b/Controller/SettingsController.php @@ -79,7 +79,7 @@ public function manageOwnAction(Request $request): Response $user = $this->get('security.token_storage')->getToken()->getUser(); if (!$user instanceof SettingsOwnerInterface) { - //For this to work the User entity must implement SettingsOwnerInterface + // For this to work the User entity must implement SettingsOwnerInterface throw new AccessDeniedException(); } diff --git a/Manager/CachedSettingsManager.php b/Manager/CachedSettingsManager.php index 06d5258..e7900e4 100644 --- a/Manager/CachedSettingsManager.php +++ b/Manager/CachedSettingsManager.php @@ -10,7 +10,7 @@ */ class CachedSettingsManager implements SettingsManagerInterface { - const PREFIX = 'dmishh_settings_%s_%s'; + public const PREFIX = 'dmishh_settings_%s_%s'; /** * @var CacheItemPoolInterface diff --git a/Manager/SettingsManagerInterface.php b/Manager/SettingsManagerInterface.php index 6dfeba3..9068359 100644 --- a/Manager/SettingsManagerInterface.php +++ b/Manager/SettingsManagerInterface.php @@ -6,9 +6,9 @@ interface SettingsManagerInterface { - const SCOPE_ALL = 'all'; - const SCOPE_GLOBAL = 'global'; - const SCOPE_USER = 'user'; + public const SCOPE_ALL = 'all'; + public const SCOPE_GLOBAL = 'global'; + public const SCOPE_USER = 'user'; /** * Returns setting value by its name. diff --git a/Tests/Functional/ServiceInstantiationTest.php b/Tests/Functional/ServiceInstantiationTest.php index b3ff619..b97f51c 100644 --- a/Tests/Functional/ServiceInstantiationTest.php +++ b/Tests/Functional/ServiceInstantiationTest.php @@ -12,7 +12,6 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\HttpKernel\KernelInterface; - /** * @internal */ @@ -40,13 +39,13 @@ public function testInitBundle() self::bootKernel(); $container = self::getContainer(); - self::assertTrue(true); //Kernel boot + self::assertTrue(true); // Kernel boot } public function testBundleWithDifferentConfiguration(): void { // Boot the kernel with a config closure, the handleOptions call in createKernel is important for that to work - $kernel = self::bootKernel(['config' => static function(TestKernel $kernel){ + $kernel = self::bootKernel(['config' => static function (TestKernel $kernel) { // Add some other bundles we depend on $kernel->addTestBundle(DoctrineBundle::class); diff --git a/Tests/SettingsManagerTest.php b/Tests/SettingsManagerTest.php index fe11e6a..4a343b4 100644 --- a/Tests/SettingsManagerTest.php +++ b/Tests/SettingsManagerTest.php @@ -229,15 +229,15 @@ public function testGetDefaultValue() $user = $this->createOwner(); $settingsManager = $this->createSettingsManager(); - //test default global value + // test default global value $this->assertNull($settingsManager->get('some_setting')); $this->assertEquals('foobar', $settingsManager->get('some_setting', null, 'foobar')); - //test default user value + // test default user value $this->assertNull($settingsManager->get('some_setting')); $this->assertEquals('foobar', $settingsManager->get('some_setting', $user, 'foobar')); - //test when there is an actual value + // test when there is an actual value $settingsManager->set('some_setting', 'value'); $this->assertEquals('value', $settingsManager->get('some_setting', null, 'foobar')); $this->assertEquals('value', $settingsManager->get('some_setting', $user, 'foobar')); From 707518614b42270e35fe3fae69974f6ac87a3fc1 Mon Sep 17 00:00:00 2001 From: Alexandre Tranchant Date: Fri, 5 Aug 2022 12:53:21 +0200 Subject: [PATCH 07/22] Removing PHP7.4 Removing psr/cache:2 and less --- .github/workflows/main.yml | 2 +- .gitignore | 2 +- composer.json | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0b07a93..ad58262 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -49,7 +49,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: [ '7.4' ] + php: [ '8.0' ] symfony: [ '5.4.*'] name: Test lowest on Symfony ${{ matrix.symfony }} with PHP ${{ matrix.php }} steps: diff --git a/.gitignore b/.gitignore index 367d49e..daef913 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ vendor/ composer.lock phpunit.xml -.php_cs-fixer.cache +.php-cs-fixer.cache .phpunit.result.cache diff --git a/composer.json b/composer.json index c51a9b4..15e4b3a 100644 --- a/composer.json +++ b/composer.json @@ -20,8 +20,8 @@ } ], "require": { - "php": "^7.4|^8.0", - "psr/cache": "^1.0.1|^2.0|^3.0", + "php": "^8.0", + "psr/cache": "^3.0", "symfony/framework-bundle": "^5.4|^6.0", "symfony/form": "^5.4 || ^6.0", "doctrine/orm": "^2.6.3|^2.7" From d1e558fa0f3ccd95760f8ecb10cb59192d76c280 Mon Sep 17 00:00:00 2001 From: Alexandre Tranchant Date: Sat, 6 Aug 2022 14:01:57 +0200 Subject: [PATCH 08/22] Issues detected by PhpStan fixed --- Controller/SettingsController.php | 32 +-- DependencyInjection/Configuration.php | 9 +- .../DmishhSettingsExtension.php | 2 +- Entity/Setting.php | 20 +- Exception/InvalidArgumentException.php | 7 + Exception/UnknownConstraintException.php | 11 + Exception/UnknownSerializerException.php | 2 +- Exception/UnknownSettingException.php | 2 +- Exception/WrongScopeException.php | 2 +- Form/Type/SettingsType.php | 12 +- Manager/CachedSettingsManager.php | 34 ++- Manager/SettingsManager.php | 60 +++--- Manager/SettingsManagerInterface.php | 8 +- Serializer/JsonSerializer.php | 14 +- Serializer/PhpSerializer.php | 4 +- Serializer/SerializerFactory.php | 29 +-- Serializer/SerializerInterface.php | 10 +- Tests/Serializer/CustomSerializer.php | 4 +- Tests/SerializerTest.php | 11 +- phpstan-baseline.neon | 202 ++---------------- phpunit.xml.dist | 14 +- 21 files changed, 154 insertions(+), 335 deletions(-) create mode 100644 Exception/InvalidArgumentException.php create mode 100644 Exception/UnknownConstraintException.php diff --git a/Controller/SettingsController.php b/Controller/SettingsController.php index b4b0a3d..3da85b8 100644 --- a/Controller/SettingsController.php +++ b/Controller/SettingsController.php @@ -13,30 +13,15 @@ class SettingsController extends AbstractController { - /** - * @var string|null - */ - private $securityRole; + private ?string $securityRole; - /** - * @var bool - */ - private $securityManageOwnSettings; + private bool $securityManageOwnSettings; - /** - * @var TranslatorInterface - */ - private $translator; + private TranslatorInterface $translator; - /** - * @var SettingsManagerInterface - */ - private $settingsManager; + private SettingsManagerInterface $settingsManager; - /** - * @var string - */ - private $template; + private string $template; public function __construct( TranslatorInterface $translator, @@ -57,7 +42,7 @@ public function __construct( */ public function manageGlobalAction(Request $request): Response { - if (null !== $this->securityRole && !$this->get('security.authorization_checker')->isGranted($this->securityRole)) { + if (null !== $this->securityRole && !$this->isGranted($this->securityRole)) { throw new AccessDeniedException($this->translator->trans('not_allowed_to_edit_global_settings', [], 'settings')); } @@ -69,7 +54,9 @@ public function manageGlobalAction(Request $request): Response */ public function manageOwnAction(Request $request): Response { - if (null === $this->get('security.token_storage')->getToken()) { + $user = $this->getUser(); + + if (null === $user) { throw new AccessDeniedException($this->translator->trans('must_be_logged_in_to_edit_own_settings', [], 'settings')); } @@ -77,7 +64,6 @@ public function manageOwnAction(Request $request): Response throw new AccessDeniedException($this->translator->trans('not_allowed_to_edit_own_settings', [], 'settings')); } - $user = $this->get('security.token_storage')->getToken()->getUser(); if (!$user instanceof SettingsOwnerInterface) { // For this to work the User entity must implement SettingsOwnerInterface throw new AccessDeniedException(); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index f803293..db2f4ad 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -16,12 +16,7 @@ class Configuration implements ConfigurationInterface public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder('dmishh_settings'); - // Keep compatibility with symfony/config < 4.2 - if (!method_exists($treeBuilder, 'getRootNode')) { - $rootNode = $treeBuilder->root('dmishh_settings'); - } else { - $rootNode = $treeBuilder->getRootNode(); - } + $rootNode = $treeBuilder->getRootNode(); $scopes = [ SettingsManagerInterface::SCOPE_ALL, @@ -74,7 +69,7 @@ public function getConfigTreeBuilder() ->end() ->end() ->variableNode('constraints') - ->info('The constraints on this option. Example, use constraits found in Symfony\Component\Validator\Constraints') + ->info('The constraints on this option. Example, use constraints found in Symfony\Component\Validator\Constraints') ->defaultValue([]) ->validate() ->always(function ($v) { diff --git a/DependencyInjection/DmishhSettingsExtension.php b/DependencyInjection/DmishhSettingsExtension.php index 3d858c9..ebf4018 100644 --- a/DependencyInjection/DmishhSettingsExtension.php +++ b/DependencyInjection/DmishhSettingsExtension.php @@ -23,7 +23,7 @@ class DmishhSettingsExtension extends Extension /** * {@inheritdoc} */ - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); diff --git a/Entity/Setting.php b/Entity/Setting.php index b0bd747..2781991 100644 --- a/Entity/Setting.php +++ b/Entity/Setting.php @@ -11,41 +11,33 @@ class Setting { /** - * @var int - * * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ - private $id; + private ?int $id; /** - * @var string|null - * * @ORM\Column(type="string", length=255) */ - private $name; + private ?string $name; /** - * @var string|null - * * @ORM\Column(type="text", nullable=true) */ - private $value; + private ?string $value; /** - * @var string|null - * * @ORM\Column(name="owner_id", type="string", length=255, nullable=true) */ - private $ownerId; + private ?string $ownerId; public function getId(): ?int { return $this->id; } - public function setName(?string $name) + public function setName(?string $name): void { $this->name = $name; } @@ -70,7 +62,7 @@ public function getOwnerId(): ?string return $this->ownerId; } - public function setOwnerId(?string $ownerId) + public function setOwnerId(?string $ownerId): void { $this->ownerId = $ownerId; } diff --git a/Exception/InvalidArgumentException.php b/Exception/InvalidArgumentException.php new file mode 100644 index 0000000..83dad5b --- /dev/null +++ b/Exception/InvalidArgumentException.php @@ -0,0 +1,7 @@ +settingsConfiguration as $name => $configuration) { // If setting's value exists in data and setting isn't disabled @@ -41,7 +41,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) if (class_exists($class)) { $constraints[] = new $class($constraintOptions); } else { - throw new SettingsException(sprintf('Constraint class "%s" not found', $class)); + throw new UnknownConstraintException($class); } } @@ -68,7 +68,7 @@ function ($label) use ($fieldOptions) { } } - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults( [ @@ -77,7 +77,7 @@ public function configureOptions(OptionsResolver $resolver) ); } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'settings_management'; } diff --git a/Manager/CachedSettingsManager.php b/Manager/CachedSettingsManager.php index e7900e4..8201472 100644 --- a/Manager/CachedSettingsManager.php +++ b/Manager/CachedSettingsManager.php @@ -2,6 +2,7 @@ namespace Dmishh\SettingsBundle\Manager; +use DateInterval; use Dmishh\SettingsBundle\Entity\SettingsOwnerInterface; use Psr\Cache\CacheItemPoolInterface; @@ -12,22 +13,13 @@ class CachedSettingsManager implements SettingsManagerInterface { public const PREFIX = 'dmishh_settings_%s_%s'; - /** - * @var CacheItemPoolInterface - */ - private $storage; + private CacheItemPoolInterface $storage; - /** - * @var SettingsManagerInterface - */ - private $settingsManager; + private SettingsManagerInterface $settingsManager; - /** - * @var int - */ - private $cacheLifeTime; + private int|DateInterval|null $cacheLifeTime; - public function __construct(SettingsManagerInterface $settingsManager, CacheItemPoolInterface $storage, $cacheLifeTime) + public function __construct(SettingsManagerInterface $settingsManager, CacheItemPoolInterface $storage, int|DateInterval|null $cacheLifeTime) { $this->settingsManager = $settingsManager; $this->storage = $storage; @@ -37,7 +29,7 @@ public function __construct(SettingsManagerInterface $settingsManager, CacheItem /** * {@inheritdoc} */ - public function get(string $name, ?SettingsOwnerInterface $owner = null, $default = null) + public function get(string $name, ?SettingsOwnerInterface $owner = null, mixed $default = null): mixed { if (null !== $cached = $this->fetchFromCache($name, $owner)) { return $cached; @@ -50,7 +42,10 @@ public function get(string $name, ?SettingsOwnerInterface $owner = null, $defaul } /** - * {@inheritdoc} + * Check cache and populate it if necessary. + * Returns all settings as associative name-value array. + * + * @return array */ public function all(?SettingsOwnerInterface $owner = null): array { @@ -67,7 +62,7 @@ public function all(?SettingsOwnerInterface $owner = null): array /** * {@inheritdoc} */ - public function set(string $name, $value, ?SettingsOwnerInterface $owner = null): void + public function set(string $name, mixed $value, ?SettingsOwnerInterface $owner = null): void { $this->invalidateCache($name, $owner); $this->invalidateCache(null, $owner); @@ -122,11 +117,9 @@ protected function fetchFromCache(?string $name, ?SettingsOwnerInterface $owner /** * Store in cache. * - * @param SettingsOwnerInterface $owner - * * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise */ - protected function storeInCache(?string $name, $value, ?SettingsOwnerInterface $owner = null): bool + protected function storeInCache(?string $name, mixed $value, ?SettingsOwnerInterface $owner = null): bool { $item = $this->storage->getItem($this->getCacheKey($name, $owner)) ->set($value) @@ -135,9 +128,6 @@ protected function storeInCache(?string $name, $value, ?SettingsOwnerInterface $ return $this->storage->save($item); } - /** - * @param SettingsOwnerInterface $owner - */ protected function getCacheKey(?string $key, ?SettingsOwnerInterface $owner = null): string { return sprintf(self::PREFIX, $owner ? $owner->getSettingIdentifier() : '', $key); diff --git a/Manager/SettingsManager.php b/Manager/SettingsManager.php index 4e815ad..08b8c11 100644 --- a/Manager/SettingsManager.php +++ b/Manager/SettingsManager.php @@ -10,7 +10,6 @@ use Dmishh\SettingsBundle\Serializer\SerializerInterface; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; -use Doctrine\Persistence\ObjectManager; /** * Settings Manager provides settings management and persistence using Doctrine's Object Manager. @@ -21,34 +20,30 @@ class SettingsManager implements SettingsManagerInterface { /** - * @var array + * @var ?array */ - private $globalSettings; + private ?array $globalSettings = null; /** - * @var array + * @var ?array> */ - private $ownerSettings; + private ?array $ownerSettings = null; - /** - * @var ObjectManager - */ - private $em; + private EntityManagerInterface $em; /** * @var EntityRepository */ - private $repository; + private EntityRepository $repository; - /** - * @var SerializerInterface - */ - private $serializer; + private SerializerInterface $serializer; /** - * @var array + * $settingConfiguration['foo']['scope']. + * + * @var array> */ - private $settingsConfiguration; + private array $settingsConfiguration; public function __construct( EntityManagerInterface $em, @@ -64,7 +59,7 @@ public function __construct( /** * {@inheritdoc} */ - public function get(string $name, ?SettingsOwnerInterface $owner = null, $default = null) + public function get(string $name, ?SettingsOwnerInterface $owner = null, mixed $default = null): mixed { $this->validateSetting($name, $owner); $this->loadSettings($owner); @@ -90,7 +85,9 @@ public function get(string $name, ?SettingsOwnerInterface $owner = null, $defaul } /** - * {@inheritdoc} + * Returns all settings as associative name-value array. + * + * @return array */ public function all(?SettingsOwnerInterface $owner = null): array { @@ -115,7 +112,7 @@ public function all(?SettingsOwnerInterface $owner = null): array /** * {@inheritdoc} */ - public function set(string $name, $value, ?SettingsOwnerInterface $owner = null): void + public function set(string $name, mixed $value, ?SettingsOwnerInterface $owner = null): void { $this->setWithoutFlush($name, $value, $owner); $this->flush([$name], $owner); @@ -160,7 +157,7 @@ protected function findSettingByName(array $haystack, string $needle): ?Setting /** * Sets setting value to private array. Used for settings' batch saving. */ - private function setWithoutFlush(string $name, $value, ?SettingsOwnerInterface $owner = null): void + private function setWithoutFlush(string $name, mixed $value, ?SettingsOwnerInterface $owner = null): void { $this->validateSetting($name, $owner); $this->loadSettings($owner); @@ -175,29 +172,30 @@ private function setWithoutFlush(string $name, $value, ?SettingsOwnerInterface $ /** * Flushes settings defined by $names to database. * + * @param string[] $names + * * @throws UnknownSerializerException */ private function flush(array $names, ?SettingsOwnerInterface $owner = null): void { $settings = $this->repository->findBy([ 'name' => $names, - 'ownerId' => null === $owner ? null : $owner->getSettingIdentifier(), + 'ownerId' => $owner?->getSettingIdentifier(), ]); // Assert: $settings might be a smaller set than $names - // For each settings that you are trying to save + // For each setting that you are trying to save foreach ($names as $name) { try { $value = $this->get($name, $owner); - } catch (WrongScopeException $e) { + } catch (WrongScopeException) { continue; } - /** @var Setting $setting */ $setting = $this->findSettingByName($settings, $name); - if (!$setting) { + if (null === $setting) { // if the setting does not exist in DB, create it $setting = new Setting(); $setting->setName($name); @@ -216,17 +214,13 @@ private function flush(array $names, ?SettingsOwnerInterface $owner = null): voi /** * Checks that $name is valid setting and it's scope is also valid. * - * @param SettingsOwnerInterface $owner - * - * @return SettingsManager - * * @throws UnknownSettingException * @throws WrongScopeException */ private function validateSetting(string $name, ?SettingsOwnerInterface $owner = null): void { // Name validation - if (!\is_string($name) || !\array_key_exists($name, $this->settingsConfiguration)) { + if (!\array_key_exists($name, $this->settingsConfiguration)) { throw new UnknownSettingException($name); } @@ -256,7 +250,9 @@ private function loadSettings(SettingsOwnerInterface $owner = null): void } /** - * Retreives settings from repository. + * Retrieves settings from repository. + * + * @return array * * @throws UnknownSerializerException */ @@ -268,7 +264,7 @@ private function getSettingsFromRepository(?SettingsOwnerInterface $owner = null try { $this->validateSetting($name, $owner); $settings[$name] = null; - } catch (WrongScopeException $e) { + } catch (WrongScopeException) { continue; } } diff --git a/Manager/SettingsManagerInterface.php b/Manager/SettingsManagerInterface.php index 9068359..3877455 100644 --- a/Manager/SettingsManagerInterface.php +++ b/Manager/SettingsManagerInterface.php @@ -15,20 +15,24 @@ interface SettingsManagerInterface * * @param mixed|null $default value to return if the setting is not set */ - public function get(string $name, ?SettingsOwnerInterface $owner = null, $default = null); + public function get(string $name, ?SettingsOwnerInterface $owner = null, mixed $default = null): mixed; /** * Returns all settings as associative name-value array. + * + * @return array */ public function all(?SettingsOwnerInterface $owner = null): array; /** * Sets setting value by its name. */ - public function set(string $name, $value, ?SettingsOwnerInterface $owner = null): void; + public function set(string $name, mixed $value, ?SettingsOwnerInterface $owner = null): void; /** * Sets settings' values from associative name-value array. + * + * @param array $settings */ public function setMany(array $settings, ?SettingsOwnerInterface $owner = null): void; diff --git a/Serializer/JsonSerializer.php b/Serializer/JsonSerializer.php index 7687b20..053078b 100644 --- a/Serializer/JsonSerializer.php +++ b/Serializer/JsonSerializer.php @@ -2,14 +2,22 @@ namespace Dmishh\SettingsBundle\Serializer; +use Dmishh\SettingsBundle\Exception\InvalidArgumentException; + class JsonSerializer implements SerializerInterface { - public function serialize($data) + public function serialize(mixed $data): string { - return json_encode($data); + $serialized = json_encode($data); + + if (false === $serialized) { + throw new InvalidArgumentException('Invalid argument: this argument cannot be serialized with this serializer'); + } + + return $serialized; } - public function unserialize($serialized) + public function unserialize(string $serialized): mixed { return json_decode($serialized, true); } diff --git a/Serializer/PhpSerializer.php b/Serializer/PhpSerializer.php index f0c44aa..34f4005 100644 --- a/Serializer/PhpSerializer.php +++ b/Serializer/PhpSerializer.php @@ -4,12 +4,12 @@ class PhpSerializer implements SerializerInterface { - public function serialize($data) + public function serialize(mixed $data): string { return serialize($data); } - public function unserialize($serialized) + public function unserialize(string $serialized): mixed { return unserialize($serialized); } diff --git a/Serializer/SerializerFactory.php b/Serializer/SerializerFactory.php index 1857e3f..f173bb5 100644 --- a/Serializer/SerializerFactory.php +++ b/Serializer/SerializerFactory.php @@ -10,24 +10,27 @@ class SerializerFactory /** * @param string $name short name of serializer (ex.: php) or full class name * - * @throws \Dmishh\SettingsBundle\Exception\UnknownSerializerException - * - * @return SerializerInterface + * @throws UnknownSerializerException */ - public static function create($name) + public static function create(string $name): SerializerInterface { $serializerClass = 'Dmishh\\SettingsBundle\\Serializer\\'.Container::camelize($name).'Serializer'; if (class_exists($serializerClass)) { - return new $serializerClass(); - } else { - $serializerClass = $name; - - if (class_exists($serializerClass)) { - $serializer = new $serializerClass(); - if ($serializer instanceof SerializerInterface) { - return $serializer; - } + $serializer = new $serializerClass(); + if ($serializer instanceof SerializerInterface) { + return $serializer; + } + + throw new UnknownSerializerException($serializerClass); + } + + $serializerClass = $name; + + if (class_exists($serializerClass)) { + $serializer = new $serializerClass(); + if ($serializer instanceof SerializerInterface) { + return $serializer; } } diff --git a/Serializer/SerializerInterface.php b/Serializer/SerializerInterface.php index 713a3b8..6ab514d 100644 --- a/Serializer/SerializerInterface.php +++ b/Serializer/SerializerInterface.php @@ -4,13 +4,7 @@ interface SerializerInterface { - /** - * @return string - */ - public function serialize($data); + public function serialize(mixed $data): string; - /** - * @param string $serialized - */ - public function unserialize($serialized); + public function unserialize(string $serialized): mixed; } diff --git a/Tests/Serializer/CustomSerializer.php b/Tests/Serializer/CustomSerializer.php index b1b5f4d..dcabde6 100644 --- a/Tests/Serializer/CustomSerializer.php +++ b/Tests/Serializer/CustomSerializer.php @@ -6,12 +6,12 @@ class CustomSerializer implements SerializerInterface { - public function serialize($data) + public function serialize(mixed $data): string { return serialize(json_encode($data)); } - public function unserialize($serialized) + public function unserialize(string $serialized): mixed { return json_decode(unserialize($serialized), true); } diff --git a/Tests/SerializerTest.php b/Tests/SerializerTest.php index 4bdce52..d5c537a 100644 --- a/Tests/SerializerTest.php +++ b/Tests/SerializerTest.php @@ -2,7 +2,9 @@ namespace Dmishh\SettingsBundle\Tests; +use Dmishh\SettingsBundle\Exception\UnknownSerializerException; use Dmishh\SettingsBundle\Serializer\SerializerFactory; +use Dmishh\SettingsBundle\Tests\Serializer\CustomSerializer; class SerializerTest extends AbstractTest { @@ -11,6 +13,7 @@ class SerializerTest extends AbstractTest public function testPhpSerializer() { $serializer = SerializerFactory::create('php'); + $this->assertEquals(serialize(null), $serializer->serialize(null)); $this->assertEquals(serialize(self::$testData), $serializer->serialize(self::$testData)); $this->assertEquals(self::$testData, $serializer->unserialize($serializer->serialize(self::$testData))); } @@ -18,19 +21,21 @@ public function testPhpSerializer() public function testJsonSerializer() { $serializer = SerializerFactory::create('json'); + $this->assertEquals(json_encode(null), $serializer->serialize(null)); $this->assertEquals(json_encode(self::$testData), $serializer->serialize(self::$testData)); $this->assertEquals(self::$testData, $serializer->unserialize($serializer->serialize(self::$testData))); } public function testCustomSerializer() { - $serializer = SerializerFactory::create('Dmishh\SettingsBundle\Tests\Serializer\CustomSerializer'); + $serializer = SerializerFactory::create(CustomSerializer::class); + $this->assertNull($serializer->unserialize($serializer->serialize(null))); $this->assertEquals(self::$testData, $serializer->unserialize($serializer->serialize(self::$testData))); } public function testUnknownSerializer() { - $this->expectException('\Dmishh\SettingsBundle\Exception\UnknownSerializerException'); - $serializer = SerializerFactory::create('unknown_serializer'); + $this->expectException(UnknownSerializerException::class); + SerializerFactory::create('unknown_serializer'); } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4f684a8..8ec02cc 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,192 +1,67 @@ parameters: ignoreErrors: - - message: "#^Call to an undefined method object\\:\\:isGranted\\(\\)\\.$#" + message: "#^Parameter \\#1 \\$settings of method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManagerInterface\\:\\:setMany\\(\\) expects array\\, mixed given\\.$#" count: 1 path: Controller/SettingsController.php - - message: "#^Call to an undefined method object\\:\\:getToken\\(\\)\\.$#" - count: 2 - path: Controller/SettingsController.php - - - - message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\TreeBuilder\\:\\:root\\(\\)\\.$#" + message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#" count: 1 path: DependencyInjection/Configuration.php - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\DependencyInjection\\\\DmishhSettingsExtension\\:\\:load\\(\\) has no return typehint specified\\.$#" - count: 1 - path: DependencyInjection/DmishhSettingsExtension.php - - message: "#^Method Dmishh\\\\SettingsBundle\\\\DependencyInjection\\\\DmishhSettingsExtension\\:\\:load\\(\\) has parameter \\$configs with no value type specified in iterable type array\\.$#" count: 1 path: DependencyInjection/DmishhSettingsExtension.php - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Entity\\\\Setting\\:\\:setName\\(\\) has no return typehint specified\\.$#" + message: "#^Property Dmishh\\\\SettingsBundle\\\\Entity\\\\Setting\\:\\:\\$id is never written, only read\\.$#" count: 1 path: Entity/Setting.php - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Entity\\\\Setting\\:\\:setOwnerId\\(\\) has no return typehint specified\\.$#" - count: 1 - path: Entity/Setting.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Entity\\\\SettingsOwnerInterface\\:\\:getSettingIdentifier\\(\\) has no return typehint specified\\.$#" + message: "#^Method Dmishh\\\\SettingsBundle\\\\Entity\\\\SettingsOwnerInterface\\:\\:getSettingIdentifier\\(\\) has no return type specified\\.$#" count: 1 path: Entity/SettingsOwnerInterface.php - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Exception\\\\UnknownSerializerException\\:\\:__construct\\(\\) has parameter \\$serializerClass with no typehint specified\\.$#" - count: 1 - path: Exception/UnknownSerializerException.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Exception\\\\UnknownSettingException\\:\\:__construct\\(\\) has parameter \\$settingName with no typehint specified\\.$#" - count: 1 - path: Exception/UnknownSettingException.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Exception\\\\WrongScopeException\\:\\:__construct\\(\\) has parameter \\$scope with no typehint specified\\.$#" - count: 1 - path: Exception/WrongScopeException.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Exception\\\\WrongScopeException\\:\\:__construct\\(\\) has parameter \\$settingName with no typehint specified\\.$#" - count: 1 - path: Exception/WrongScopeException.php - - - - message: "#^Property Dmishh\\\\SettingsBundle\\\\Form\\\\Type\\\\SettingsType\\:\\:\\$settingsConfiguration has no typehint specified\\.$#" - count: 1 - path: Form/Type/SettingsType.php - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Form\\\\Type\\\\SettingsType\\:\\:__construct\\(\\) has parameter \\$settingsConfiguration with no value type specified in iterable type array\\.$#" count: 1 path: Form/Type/SettingsType.php - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Form\\\\Type\\\\SettingsType\\:\\:buildForm\\(\\) has no return typehint specified\\.$#" - count: 1 - path: Form/Type/SettingsType.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Form\\\\Type\\\\SettingsType\\:\\:buildForm\\(\\) has parameter \\$builder with no value type specified in iterable type Symfony\\\\Component\\\\Form\\\\FormBuilderInterface\\.$#" + message: "#^Parameter \\#2 \\$array of function array_key_exists expects array, mixed given\\.$#" count: 1 path: Form/Type/SettingsType.php - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Form\\\\Type\\\\SettingsType\\:\\:buildForm\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#" + message: "#^Parameter \\#2 \\$haystack of function in_array expects array, mixed given\\.$#" count: 1 path: Form/Type/SettingsType.php - - message: "#^Cannot instantiate interface Dmishh\\\\SettingsBundle\\\\Exception\\\\SettingsException\\.$#" + message: "#^Property Dmishh\\\\SettingsBundle\\\\Form\\\\Type\\\\SettingsType\\:\\:\\$settingsConfiguration type has no value type specified in iterable type array\\.$#" count: 1 path: Form/Type/SettingsType.php - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Form\\\\Type\\\\SettingsType\\:\\:configureOptions\\(\\) has no return typehint specified\\.$#" - count: 1 - path: Form/Type/SettingsType.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\CachedSettingsManager\\:\\:__construct\\(\\) has parameter \\$cacheLifeTime with no typehint specified\\.$#" - count: 1 - path: Manager/CachedSettingsManager.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\CachedSettingsManager\\:\\:get\\(\\) has no return typehint specified\\.$#" - count: 1 - path: Manager/CachedSettingsManager.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\CachedSettingsManager\\:\\:all\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: Manager/CachedSettingsManager.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\CachedSettingsManager\\:\\:set\\(\\) has parameter \\$value with no typehint specified\\.$#" - count: 1 - path: Manager/CachedSettingsManager.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\CachedSettingsManager\\:\\:setMany\\(\\) has parameter \\$settings with no value type specified in iterable type array\\.$#" - count: 1 - path: Manager/CachedSettingsManager.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\CachedSettingsManager\\:\\:storeInCache\\(\\) has parameter \\$value with no typehint specified\\.$#" + message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\CachedSettingsManager\\:\\:all\\(\\) should return array\\ but returns mixed\\.$#" count: 1 path: Manager/CachedSettingsManager.php - - - message: "#^Property Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:\\$globalSettings type has no value type specified in iterable type array\\.$#" - count: 1 - path: Manager/SettingsManager.php - - - - message: "#^Property Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:\\$ownerSettings type has no value type specified in iterable type array\\.$#" - count: 1 - path: Manager/SettingsManager.php - - - - message: "#^Property Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:\\$settingsConfiguration type has no value type specified in iterable type array\\.$#" - count: 1 - path: Manager/SettingsManager.php - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:__construct\\(\\) has parameter \\$settingsConfiguration with no value type specified in iterable type array\\.$#" count: 1 path: Manager/SettingsManager.php - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:get\\(\\) has no return typehint specified\\.$#" + message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:all\\(\\) should return array\\ but returns array\\\\|null\\.$#" count: 1 path: Manager/SettingsManager.php - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:all\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: Manager/SettingsManager.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:set\\(\\) has parameter \\$value with no typehint specified\\.$#" - count: 1 - path: Manager/SettingsManager.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:setMany\\(\\) has parameter \\$settings with no value type specified in iterable type array\\.$#" - count: 1 - path: Manager/SettingsManager.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:setWithoutFlush\\(\\) has parameter \\$value with no typehint specified\\.$#" - count: 1 - path: Manager/SettingsManager.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:flush\\(\\) has parameter \\$names with no value type specified in iterable type array\\.$#" - count: 1 - path: Manager/SettingsManager.php - - - - message: "#^Negated boolean expression is always false\\.$#" - count: 1 - path: Manager/SettingsManager.php - - - - message: "#^PHPDoc tag @return with type Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager is incompatible with native type void\\.$#" - count: 1 - path: Manager/SettingsManager.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:getSettingsFromRepository\\(\\) return type has no value type specified in iterable type array\\.$#" + message: "#^Offset mixed does not exist on array\\\\>\\|null\\.$#" count: 1 path: Manager/SettingsManager.php @@ -201,57 +76,6 @@ parameters: path: Manager/SettingsManager.php - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManagerInterface\\:\\:get\\(\\) has no return typehint specified\\.$#" - count: 1 - path: Manager/SettingsManagerInterface.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManagerInterface\\:\\:all\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: Manager/SettingsManagerInterface.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManagerInterface\\:\\:set\\(\\) has parameter \\$value with no typehint specified\\.$#" - count: 1 - path: Manager/SettingsManagerInterface.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManagerInterface\\:\\:setMany\\(\\) has parameter \\$settings with no value type specified in iterable type array\\.$#" - count: 1 - path: Manager/SettingsManagerInterface.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Serializer\\\\JsonSerializer\\:\\:serialize\\(\\) has parameter \\$data with no typehint specified\\.$#" - count: 1 - path: Serializer/JsonSerializer.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Serializer\\\\JsonSerializer\\:\\:serialize\\(\\) should return string but returns string\\|false\\.$#" - count: 1 - path: Serializer/JsonSerializer.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Serializer\\\\JsonSerializer\\:\\:unserialize\\(\\) has no return typehint specified\\.$#" - count: 1 - path: Serializer/JsonSerializer.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Serializer\\\\PhpSerializer\\:\\:serialize\\(\\) has parameter \\$data with no typehint specified\\.$#" - count: 1 - path: Serializer/PhpSerializer.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Serializer\\\\PhpSerializer\\:\\:unserialize\\(\\) has no return typehint specified\\.$#" - count: 1 - path: Serializer/PhpSerializer.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Serializer\\\\SerializerInterface\\:\\:serialize\\(\\) has parameter \\$data with no typehint specified\\.$#" - count: 1 - path: Serializer/SerializerInterface.php - - - - message: "#^Method Dmishh\\\\SettingsBundle\\\\Serializer\\\\SerializerInterface\\:\\:unserialize\\(\\) has no return typehint specified\\.$#" - count: 1 - path: Serializer/SerializerInterface.php - + message: "#^Property Dmishh\\\\SettingsBundle\\\\Manager\\\\SettingsManager\\:\\:\\$ownerSettings \\(array\\\\>\\|null\\) does not accept array\\\\>\\.$#" + count: 2 + path: Manager/SettingsManager.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b61e902..30eb315 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -12,12 +12,16 @@ xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> - ./ - - + ./Controller + ./DependencyInjection + ./Entity + ./Exception + ./Form + ./Manager ./Resources - ./Tests - + ./Serializer + ./Twig + From 149eeeb5f3570052fbcfa63754d988936b01d28d Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 10:22:04 +0100 Subject: [PATCH 09/22] feat: changes for symfony 6 --- Tests/AbstractTest.php | 11 +++-- Tests/CompilerPass/PublicServicePass.php | 42 +++++++++++++++++++ Tests/Functional/ServiceInstantiationTest.php | 31 ++++---------- composer.json | 3 +- 4 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 Tests/CompilerPass/PublicServicePass.php diff --git a/Tests/AbstractTest.php b/Tests/AbstractTest.php index 7c96f5a..ec5e4ff 100644 --- a/Tests/AbstractTest.php +++ b/Tests/AbstractTest.php @@ -37,10 +37,13 @@ protected function createEntityManager() $config->setProxyNamespace('EntityProxy'); $config->setAutoGenerateProxyClasses(true); - AnnotationRegistry::registerFile( - __DIR__. - '/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php' - ); + if (method_exists(AnnotationRegistry::class, 'registerFile')) { + AnnotationRegistry::registerFile( + __DIR__ . + '/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php' + ); + } + $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver( new \Doctrine\Common\Annotations\AnnotationReader(), [__DIR__.'/../Entity'] diff --git a/Tests/CompilerPass/PublicServicePass.php b/Tests/CompilerPass/PublicServicePass.php new file mode 100644 index 0000000..b6b0e8e --- /dev/null +++ b/Tests/CompilerPass/PublicServicePass.php @@ -0,0 +1,42 @@ + + */ +class PublicServicePass implements CompilerPassInterface +{ + /** + * A regex to match the services that should be public. + * + * @var string + */ + private $regex; + + /** + * @param string $regex + */ + public function __construct($regex = '|.*|') + { + $this->regex = $regex; + } + + public function process(ContainerBuilder $container) + { + foreach ($container->getDefinitions() as $id => $definition) { + if (preg_match($this->regex, $id)) { + $definition->setPublic(true); + } + } + + foreach ($container->getAliases() as $id => $alias) { + if (preg_match($this->regex, $id)) { + $alias->setPublic(true); + } + } + } +} diff --git a/Tests/Functional/ServiceInstantiationTest.php b/Tests/Functional/ServiceInstantiationTest.php index b97f51c..66a471b 100644 --- a/Tests/Functional/ServiceInstantiationTest.php +++ b/Tests/Functional/ServiceInstantiationTest.php @@ -7,6 +7,7 @@ use Dmishh\SettingsBundle\Manager\SettingsManagerInterface; use Dmishh\SettingsBundle\Serializer\PhpSerializer; use Dmishh\SettingsBundle\Serializer\SerializerInterface; +use Dmishh\SettingsBundle\Tests\CompilerPass\PublicServicePass; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Nyholm\BundleTest\TestKernel; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -28,36 +29,22 @@ protected static function createKernel(array $options = []): KernelInterface * @var TestKernel $kernel */ $kernel = parent::createKernel($options); + $kernel->addTestCompilerPass(new PublicServicePass('|Dmishh.*|')); + $kernel->addTestConfig(\dirname(__DIR__).'/Resources/app/config/config.yml'); + $kernel->addTestBundle(DoctrineBundle::class); $kernel->addTestBundle(DmishhSettingsBundle::class); $kernel->handleOptions($options); return $kernel; } - public function testInitBundle() + public function testInitBundle(): void { - self::bootKernel(); + $kernel = self::bootKernel(); - $container = self::getContainer(); - self::assertTrue(true); // Kernel boot - } - - public function testBundleWithDifferentConfiguration(): void - { - // Boot the kernel with a config closure, the handleOptions call in createKernel is important for that to work - $kernel = self::bootKernel(['config' => static function (TestKernel $kernel) { - // Add some other bundles we depend on - $kernel->addTestBundle(DoctrineBundle::class); - - // Add some configuration - $kernel->addTestConfig(\dirname(__DIR__).'/Resources/app/config/config.yml'); -// $kernel->addTestCompilerPass(new PublicServicePass('|Dmishh.*|')); - }]); - - $container = $this->getContainer(); + // Get the container + $container = $kernel->getContainer(); - self::markTestSkipped('Test failed and I dont know why'); - // Test if your services exists self::assertTrue($container->has(SerializerInterface::class), 'Serializer interface not found'); $service = $container->get(SerializerInterface::class); self::assertInstanceOf(PhpSerializer::class, $service, 'PHP Serializer not found'); @@ -66,7 +53,7 @@ public function testBundleWithDifferentConfiguration(): void self::assertInstanceOf(SettingsManager::class, $service); } - protected function getBundleClass() + protected function getBundleClass(): string { return DmishhSettingsBundle::class; } diff --git a/composer.json b/composer.json index 15e4b3a..9fcdaff 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ "psr/cache": "^3.0", "symfony/framework-bundle": "^5.4|^6.0", "symfony/form": "^5.4 || ^6.0", - "doctrine/orm": "^2.6.3|^2.7" + "doctrine/orm": "^2.6.3|^2.7", + "doctrine/annotations": "^1.14|^2.0" }, "require-dev": { "phpunit/phpunit": "^9", From 1149aae2eeeab8dc48fa3955a4a2edbd9ead0f8c Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 14:57:07 +0100 Subject: [PATCH 10/22] feat: changes for symfony 6 --- .github/workflows/main.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ad58262..a4df580 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,11 +15,11 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: [ '8.0' ] + php: [ '8.0', '8.1' ] symfony: [ '5.4.*', '6.0.*' ] name: Test on Symfony ${{ matrix.symfony }} with PHP ${{ matrix.php }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} @@ -28,15 +28,15 @@ jobs: - run: composer require symfony/form:${{ matrix.symfony }} --no-update - run: composer install - run: make test - test-hightest: + test-highest: runs-on: ubuntu-latest strategy: matrix: - php: [ '8.1' ] - symfony: [ '6.1.*' ] + php: [ '8.2' ] + symfony: [ '6.2.*' ] name: Test on Symfony ${{ matrix.symfony }} with PHP ${{ matrix.php }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} @@ -53,7 +53,7 @@ jobs: symfony: [ '5.4.*'] name: Test lowest on Symfony ${{ matrix.symfony }} with PHP ${{ matrix.php }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} @@ -65,7 +65,7 @@ jobs: php-cs-fixer: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v3 - name: Run PHP-CS-Fixer uses: docker://jakzal/phpqa with: From 00c0799f3df18f96e465976d7e29f30eac9227e1 Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 14:57:33 +0100 Subject: [PATCH 11/22] feat: changes for symfony 6 --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a4df580..b0b25a7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,7 +6,7 @@ jobs: static-code-analysis: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v3 - name: Run PHPStan uses: docker://jakzal/phpqa with: From 126e036c5bb821285ee4b6b75720efab2d772e67 Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 15:02:43 +0100 Subject: [PATCH 12/22] feat: changes for symfony 6 --- composer.json | 10 +++++----- phpunit.xml.dist | 9 ++------- phpunit.xml.dist.bak | 33 --------------------------------- 3 files changed, 7 insertions(+), 45 deletions(-) delete mode 100644 phpunit.xml.dist.bak diff --git a/composer.json b/composer.json index 9fcdaff..3ed64e1 100644 --- a/composer.json +++ b/composer.json @@ -24,15 +24,15 @@ "psr/cache": "^3.0", "symfony/framework-bundle": "^5.4|^6.0", "symfony/form": "^5.4 || ^6.0", - "doctrine/orm": "^2.6.3|^2.7", - "doctrine/annotations": "^1.14|^2.0" + "doctrine/orm": "^2.7", + "doctrine/annotations": "^1.14 || ^2.0" }, "require-dev": { - "phpunit/phpunit": "^9", - "mockery/mockery": "^1.3", + "phpunit/phpunit": "^9.5", + "mockery/mockery": "^1.5", "doctrine/doctrine-bundle": "^2.0", "polishsymfonycommunity/symfony-mocker-container": "^1.0", - "matthiasnoback/symfony-dependency-injection-test": "^4.1", + "matthiasnoback/symfony-dependency-injection-test": "^4.3", "nyholm/symfony-bundle-test": "^2.0.0", "symfony/translation": "^5.4 || ^6.0", "symfony/security-core": "^5.4 || ^6.0", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 30eb315..b9812a8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,15 +1,10 @@ - + + xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"> ./Controller diff --git a/phpunit.xml.dist.bak b/phpunit.xml.dist.bak deleted file mode 100644 index 0d28194..0000000 --- a/phpunit.xml.dist.bak +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - - ./Tests - - - - - - - - - - - ./ - - ./Resources - ./Tests - - - - From 630dfb2813eef4aae7f5533852bc5d87f5ce019b Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 15:27:39 +0100 Subject: [PATCH 13/22] feat: changes for symfony 6 --- Controller/SettingsController.php | 25 +++------------ DependencyInjection/Configuration.php | 2 +- Form/Type/SettingsType.php | 5 +-- Manager/CachedSettingsManager.php | 13 ++------ Manager/SettingsManager.php | 24 +++------------ README.md | 24 +++++++-------- Serializer/JsonSerializer.php | 8 ++++- Tests/AbstractTest.php | 14 +++------ Tests/CachedSettingsManagerTest.php | 39 ++++++++++++------------ Tests/CompilerPass/PublicServicePass.php | 15 ++------- Tests/Serializer/CustomSerializer.php | 4 +-- Tests/SerializerTest.php | 14 ++++----- Tests/ServiceTest.php | 4 +-- Tests/SettingsManagerTest.php | 2 +- Twig/SettingsExtension.php | 10 +++--- composer.json | 4 +-- phpunit.xml.dist | 9 ++++-- 17 files changed, 87 insertions(+), 129 deletions(-) diff --git a/Controller/SettingsController.php b/Controller/SettingsController.php index 3da85b8..7b73be8 100644 --- a/Controller/SettingsController.php +++ b/Controller/SettingsController.php @@ -13,28 +13,13 @@ class SettingsController extends AbstractController { - private ?string $securityRole; - - private bool $securityManageOwnSettings; - - private TranslatorInterface $translator; - - private SettingsManagerInterface $settingsManager; - - private string $template; - public function __construct( - TranslatorInterface $translator, - SettingsManagerInterface $settingsManager, - string $template, - bool $securityManageOwnSettings, - ?string $securityRole + private TranslatorInterface $translator, + private SettingsManagerInterface $settingsManager, + private string $template, + private bool $securityManageOwnSettings, + private ?string $securityRole ) { - $this->translator = $translator; - $this->settingsManager = $settingsManager; - $this->template = $template; - $this->securityManageOwnSettings = $securityManageOwnSettings; - $this->securityRole = $securityRole; } /** diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index db2f4ad..290fc8f 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -13,7 +13,7 @@ class Configuration implements ConfigurationInterface /** * {@inheritdoc} */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('dmishh_settings'); $rootNode = $treeBuilder->getRootNode(); diff --git a/Form/Type/SettingsType.php b/Form/Type/SettingsType.php index 65faf86..4f0a041 100644 --- a/Form/Type/SettingsType.php +++ b/Form/Type/SettingsType.php @@ -15,11 +15,8 @@ */ class SettingsType extends AbstractType { - protected array $settingsConfiguration; - - public function __construct(array $settingsConfiguration) + public function __construct(protected array $settingsConfiguration) { - $this->settingsConfiguration = $settingsConfiguration; } /** diff --git a/Manager/CachedSettingsManager.php b/Manager/CachedSettingsManager.php index 8201472..ee5b69e 100644 --- a/Manager/CachedSettingsManager.php +++ b/Manager/CachedSettingsManager.php @@ -13,17 +13,10 @@ class CachedSettingsManager implements SettingsManagerInterface { public const PREFIX = 'dmishh_settings_%s_%s'; - private CacheItemPoolInterface $storage; - - private SettingsManagerInterface $settingsManager; - - private int|DateInterval|null $cacheLifeTime; - - public function __construct(SettingsManagerInterface $settingsManager, CacheItemPoolInterface $storage, int|DateInterval|null $cacheLifeTime) + public function __construct(private SettingsManagerInterface $settingsManager, + private CacheItemPoolInterface $storage, + private int|DateInterval|null $cacheLifeTime) { - $this->settingsManager = $settingsManager; - $this->storage = $storage; - $this->cacheLifeTime = $cacheLifeTime; } /** diff --git a/Manager/SettingsManager.php b/Manager/SettingsManager.php index 08b8c11..1211d69 100644 --- a/Manager/SettingsManager.php +++ b/Manager/SettingsManager.php @@ -29,31 +29,17 @@ class SettingsManager implements SettingsManagerInterface */ private ?array $ownerSettings = null; - private EntityManagerInterface $em; - /** * @var EntityRepository */ private EntityRepository $repository; - private SerializerInterface $serializer; - - /** - * $settingConfiguration['foo']['scope']. - * - * @var array> - */ - private array $settingsConfiguration; - public function __construct( - EntityManagerInterface $em, - SerializerInterface $serializer, - array $settingsConfiguration = [] + private EntityManagerInterface $em, + private SerializerInterface $serializer, + private array $settingsConfiguration = [] ) { - $this->em = $em; $this->repository = $em->getRepository(Setting::class); - $this->serializer = $serializer; - $this->settingsConfiguration = $settingsConfiguration; } /** @@ -81,7 +67,7 @@ public function get(string $name, ?SettingsOwnerInterface $owner = null, mixed $ break; } - return null === $value ? $default : $value; + return $value ?? $default; } /** @@ -270,7 +256,7 @@ private function getSettingsFromRepository(?SettingsOwnerInterface $owner = null } /** @var Setting $setting */ - foreach ($this->repository->findBy(['ownerId' => null === $owner ? null : $owner->getSettingIdentifier()]) as $setting) { + foreach ($this->repository->findBy(['ownerId' => $owner?->getSettingIdentifier()]) as $setting) { if (\array_key_exists($setting->getName(), $settings)) { $settings[$setting->getName()] = $this->serializer->unserialize($setting->getValue()); } diff --git a/README.md b/README.md index e4afb5b..434e52d 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ SettingsBundle Bundle for storing configuration with Symfony in database using Doctrine2 ORM. -[![Build Status](https://travis-ci.org/dmishh/SettingsBundle.png?branch=master)](https://travis-ci.org/dmishh/SettingsBundle) +[![Build Status](https://github.com/rvanlaak/SettingsBundle/actions/workflows/main.yml/badge.svg)](https://github.com/rvanlaak/SettingsBundle/actions/workflows/main.yml) ## Features @@ -39,22 +39,22 @@ Twig template: {{ get_setting('some_user_setting', app.user) }} {# => 'value' #} ``` -See the [general usage](/Resources/doc/general-usage.md) documentation for more examples. +See the [general usage](./Resources/doc/general-usage.md) documentation for more examples. ## Documentation -* [Installation](/Resources/doc/installation.md) -* [General usage](/Resources/doc/general-usage.md) -* [Scopes](/Resources/doc/scopes.md) -* [Advanced configuration](/Resources/doc/advanced-configuration.md) -* [I18n](/Resources/doc/i18n.md) -* [Customization](/Resources/doc/customization.md) -* [FAQ](/Resources/doc/faq.md) +* [Installation](./Resources/doc/installation.md) +* [General usage](./Resources/doc/general-usage.md) +* [Scopes](./Resources/doc/scopes.md) +* [Advanced configuration](./Resources/doc/advanced-configuration.md) +* [I18n](./Resources/doc/i18n.md) +* [Customization](./Resources/doc/customization.md) +* [FAQ](./Resources/doc/faq.md) ## Changelog, Roadmap and contribution -Please, do not hesitate to [report bugs](https://github.com/dmishh/SettingsBundle/issues) or send -[pull requests](https://github.com/dmishh/SettingsBundle/pulls). It will help to motivate me to support +Please, do not hesitate to [report bugs](https://github.com/rvanlaak/SettingsBundle/issues) or send +[pull requests](https://github.com/rvanlaak/SettingsBundle/pulls). It will help to motivate me to support library better than anything else :) See [CHANGELOG.md](CHANGELOG.md) for all major changes. @@ -65,4 +65,4 @@ Make sure to read the [UPGRADE.md](UPGRADE.md) to successfully migrate your appl ## License -The MIT License. For the full text of license, please, see [LICENSE](/LICENSE) +The MIT License. For the full text of license, please, see [LICENSE](LICENSE) diff --git a/Serializer/JsonSerializer.php b/Serializer/JsonSerializer.php index 053078b..2526d86 100644 --- a/Serializer/JsonSerializer.php +++ b/Serializer/JsonSerializer.php @@ -6,9 +6,12 @@ class JsonSerializer implements SerializerInterface { + /** + * @throws \JsonException + */ public function serialize(mixed $data): string { - $serialized = json_encode($data); + $serialized = json_encode($data, JSON_THROW_ON_ERROR); if (false === $serialized) { throw new InvalidArgumentException('Invalid argument: this argument cannot be serialized with this serializer'); @@ -17,6 +20,9 @@ public function serialize(mixed $data): string return $serialized; } + /** + * @throws \JsonException + */ public function unserialize(string $serialized): mixed { return json_decode($serialized, true); diff --git a/Tests/AbstractTest.php b/Tests/AbstractTest.php index ec5e4ff..3b1d153 100644 --- a/Tests/AbstractTest.php +++ b/Tests/AbstractTest.php @@ -4,14 +4,12 @@ use Doctrine\Common\Annotations\AnnotationRegistry; use Doctrine\ORM\Configuration; +use Doctrine\ORM\EntityManager; use PHPUnit\Framework\TestCase; abstract class AbstractTest extends TestCase { - /** - * @var \Doctrine\ORM\EntityManager - */ - protected $em; + protected EntityManager $em; /** * {@inheritdoc} @@ -30,7 +28,7 @@ protected function tearDown(): void $this->em->close(); } - protected function createEntityManager() + protected function createEntityManager(): EntityManager { $config = new Configuration(); $config->setProxyDir(sys_get_temp_dir()); @@ -55,12 +53,10 @@ protected function createEntityManager() 'memory' => true, ]; - $em = \Doctrine\ORM\EntityManager::create($conn, $config); - - return $em; + return EntityManager::create($conn, $config); } - protected function generateSchema() + protected function generateSchema(): void { $metadatas = $this->em->getMetadataFactory()->getAllMetadata(); diff --git a/Tests/CachedSettingsManagerTest.php b/Tests/CachedSettingsManagerTest.php index 7e2d26b..35fa183 100644 --- a/Tests/CachedSettingsManagerTest.php +++ b/Tests/CachedSettingsManagerTest.php @@ -2,6 +2,7 @@ namespace Dmishh\SettingsBundle\Tests; +use Dmishh\SettingsBundle\Entity\SettingsOwnerInterface; use Dmishh\SettingsBundle\Manager\CachedSettingsManager; use Dmishh\SettingsBundle\Manager\SettingsManager; use PHPUnit\Framework\TestCase; @@ -9,7 +10,7 @@ class CachedSettingsManagerTest extends TestCase { - public function testGet() + public function testGet(): void { $owner = $this->createOwner(); $name = 'name'; @@ -20,7 +21,7 @@ public function testGet() $settingsManager->shouldReceive('get')->once()->with($name, $owner, $defaultValue)->andReturn($value); $cachedSettingsManager = $this->getMockBuilder(CachedSettingsManager::class) - ->setMethods(['fetchFromCache', 'storeInCache']) + ->onlyMethods(['fetchFromCache', 'storeInCache']) ->setConstructorArgs([$settingsManager, $this->getMockBuilder(CacheItemPoolInterface::class)->getMock(), 4711]) ->getMock(); $cachedSettingsManager->expects($this->once()) @@ -35,7 +36,7 @@ public function testGet() $this->assertEquals($value, $cachedSettingsManager->get($name, $owner, $defaultValue)); } - public function testGetHit() + public function testGetHit(): void { $owner = $this->createOwner(); $name = 'name'; @@ -45,7 +46,7 @@ public function testGetHit() $settingsManager = \Mockery::mock(SettingsManager::class); $cachedSettingsManager = $this->getMockBuilder(CachedSettingsManager::class) - ->setMethods(['fetchFromCache', 'storeInCache']) + ->onlyMethods(['fetchFromCache', 'storeInCache']) ->setConstructorArgs([$settingsManager, $this->getMockBuilder(CacheItemPoolInterface::class)->getMock(), 4711]) ->getMock(); $cachedSettingsManager->expects($this->once()) @@ -56,7 +57,7 @@ public function testGetHit() $this->assertEquals($value, $cachedSettingsManager->get($name, $owner, $defaultValue)); } - public function testAll() + public function testAll(): void { $owner = $this->createOwner(); $value = ['foo' => 'bar']; @@ -65,13 +66,15 @@ public function testAll() $settingsManager->shouldReceive('all')->once()->with($owner)->andReturn($value); $cachedSettingsManager = $this->getMockBuilder(CachedSettingsManager::class) - ->setMethods(['fetchFromCache', 'storeInCache']) + ->onlyMethods(['fetchFromCache', 'storeInCache']) ->setConstructorArgs([$settingsManager, $this->getMockBuilder(CacheItemPoolInterface::class)->getMock(), 4711]) ->getMock(); + $cachedSettingsManager->expects($this->once()) ->method('fetchFromCache') ->with($this->equalTo(null), $this->equalTo($owner)) ->willReturn(null); + $cachedSettingsManager->expects($this->once()) ->method('storeInCache') ->with($this->equalTo(null), $this->equalTo($value), $this->equalTo($owner)) @@ -80,7 +83,7 @@ public function testAll() $this->assertEquals($value, $cachedSettingsManager->all($owner)); } - public function testAllHit() + public function testAllHit(): void { $owner = $this->createOwner(); $value = ['foo' => 'bar']; @@ -88,9 +91,10 @@ public function testAllHit() $settingsManager = \Mockery::mock(SettingsManager::class); $cachedSettingsManager = $this->getMockBuilder(CachedSettingsManager::class) - ->setMethods(['fetchFromCache', 'storeInCache']) + ->onlyMethods(['fetchFromCache', 'storeInCache']) ->setConstructorArgs([$settingsManager, $this->getMockBuilder(CacheItemPoolInterface::class)->getMock(), 4711]) ->getMock(); + $cachedSettingsManager->expects($this->once()) ->method('fetchFromCache') ->with($this->equalTo(null), $this->equalTo($owner)) @@ -99,7 +103,7 @@ public function testAllHit() $this->assertEquals($value, $cachedSettingsManager->all($owner)); } - public function testSet() + public function testSet(): void { $owner = $this->createOwner(); $name = 'name'; @@ -126,7 +130,7 @@ public function testSet() $cachedSettingsManager->set($name, $value, $owner); } - public function testSetMany() + public function testSetMany(): void { $owner = $this->createOwner(); $settings = ['name0' => 'value0', 'name1' => 'value1', 'name2' => 'value2']; @@ -138,6 +142,7 @@ public function testSetMany() ->onlyMethods(['invalidateCache']) ->setConstructorArgs([$settingsManager, $this->getMockBuilder(CacheItemPoolInterface::class)->getMock(), 4711]) ->getMock(); + $cachedSettingsManager->expects($this->exactly(4)) ->method('invalidateCache') ->with($this->logicalOr('name0', 'name1', 'name2', null), $owner); @@ -145,7 +150,7 @@ public function testSetMany() $cachedSettingsManager->setMany($settings, $owner); } - public function testClear() + public function testClear(): void { $owner = $this->createOwner(); $name = 'name'; @@ -157,6 +162,7 @@ public function testClear() ->onlyMethods(['invalidateCache']) ->setConstructorArgs([$settingsManager, $this->getMockBuilder(CacheItemPoolInterface::class)->getMock(), 4711]) ->getMock(); + $cachedSettingsManager->expects($this->exactly(2)) ->method('invalidateCache') ->withConsecutive( @@ -171,7 +177,7 @@ public function testClear() /** * Make sure we do always return a string, no matter input. */ - public function testGetCacheKey() + public function testGetCacheKey(): void { $name = 'name'; $owner = $this->createOwner(); @@ -189,15 +195,10 @@ public function testGetCacheKey() $this->assertStringContainsString('dmishh_settings', $getCacheKey->invoke($cachedSettingsManager, null, null)); } - /** - * @param string $ownerId - * - * @return \Dmishh\SettingsBundle\Entity\SettingsOwnerInterface - */ - protected function createOwner($ownerId = 'user1') + protected function createOwner(string $ownerId = 'user1'): SettingsOwnerInterface { return \Mockery::mock( - 'Dmishh\SettingsBundle\Entity\SettingsOwnerInterface', + SettingsOwnerInterface::class, ['getSettingIdentifier' => $ownerId] ); } diff --git a/Tests/CompilerPass/PublicServicePass.php b/Tests/CompilerPass/PublicServicePass.php index b6b0e8e..dd550c5 100644 --- a/Tests/CompilerPass/PublicServicePass.php +++ b/Tests/CompilerPass/PublicServicePass.php @@ -10,22 +10,11 @@ */ class PublicServicePass implements CompilerPassInterface { - /** - * A regex to match the services that should be public. - * - * @var string - */ - private $regex; - - /** - * @param string $regex - */ - public function __construct($regex = '|.*|') + public function __construct(private string $regex = '|.*|') { - $this->regex = $regex; } - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { foreach ($container->getDefinitions() as $id => $definition) { if (preg_match($this->regex, $id)) { diff --git a/Tests/Serializer/CustomSerializer.php b/Tests/Serializer/CustomSerializer.php index dcabde6..2d2f19a 100644 --- a/Tests/Serializer/CustomSerializer.php +++ b/Tests/Serializer/CustomSerializer.php @@ -8,11 +8,11 @@ class CustomSerializer implements SerializerInterface { public function serialize(mixed $data): string { - return serialize(json_encode($data)); + return serialize(json_encode($data, JSON_THROW_ON_ERROR)); } public function unserialize(string $serialized): mixed { - return json_decode(unserialize($serialized), true); + return json_decode(unserialize($serialized), true, 512, JSON_THROW_ON_ERROR); } } diff --git a/Tests/SerializerTest.php b/Tests/SerializerTest.php index d5c537a..af9f6c4 100644 --- a/Tests/SerializerTest.php +++ b/Tests/SerializerTest.php @@ -8,9 +8,9 @@ class SerializerTest extends AbstractTest { - public static $testData = ['abc' => '123', 123, 5.0]; + public static array $testData = ['abc' => '123', 123, 5.0]; - public function testPhpSerializer() + public function testPhpSerializer(): void { $serializer = SerializerFactory::create('php'); $this->assertEquals(serialize(null), $serializer->serialize(null)); @@ -18,22 +18,22 @@ public function testPhpSerializer() $this->assertEquals(self::$testData, $serializer->unserialize($serializer->serialize(self::$testData))); } - public function testJsonSerializer() + public function testJsonSerializer(): void { $serializer = SerializerFactory::create('json'); - $this->assertEquals(json_encode(null), $serializer->serialize(null)); - $this->assertEquals(json_encode(self::$testData), $serializer->serialize(self::$testData)); + $this->assertEquals(json_encode(null, JSON_THROW_ON_ERROR), $serializer->serialize(null)); + $this->assertEquals(json_encode(self::$testData, JSON_THROW_ON_ERROR), $serializer->serialize(self::$testData)); $this->assertEquals(self::$testData, $serializer->unserialize($serializer->serialize(self::$testData))); } - public function testCustomSerializer() + public function testCustomSerializer(): void { $serializer = SerializerFactory::create(CustomSerializer::class); $this->assertNull($serializer->unserialize($serializer->serialize(null))); $this->assertEquals(self::$testData, $serializer->unserialize($serializer->serialize(self::$testData))); } - public function testUnknownSerializer() + public function testUnknownSerializer(): void { $this->expectException(UnknownSerializerException::class); SerializerFactory::create('unknown_serializer'); diff --git a/Tests/ServiceTest.php b/Tests/ServiceTest.php index 1bb2b89..3ae3f20 100644 --- a/Tests/ServiceTest.php +++ b/Tests/ServiceTest.php @@ -20,7 +20,7 @@ protected function getContainerExtensions(): array ]; } - public function testAlias() + public function testAlias(): void { $this->load(); $this->assertContainerBuilderHasAlias(SettingsManagerInterface::class, SettingsManager::class); @@ -29,7 +29,7 @@ public function testAlias() /** * If we provide a cache_service we should use the CachedSettingsManager as default. */ - public function testCacheServiceAlias() + public function testCacheServiceAlias(): void { $this->load(['cache_service' => 'cache']); $this->assertContainerBuilderHasAlias(SettingsManagerInterface::class, CachedSettingsManager::class); diff --git a/Tests/SettingsManagerTest.php b/Tests/SettingsManagerTest.php index 4a343b4..e0e4e48 100644 --- a/Tests/SettingsManagerTest.php +++ b/Tests/SettingsManagerTest.php @@ -354,7 +354,7 @@ public function testFindSettingByName() protected function createSetting($name) { $s = $this->getMockBuilder('Dmishh\SettingsBundle\Entity\Setting') - ->setMethods(['getName']) + ->onlyMethods(['getName']) ->getMock(); $s->expects($this->any()) diff --git a/Twig/SettingsExtension.php b/Twig/SettingsExtension.php index f368899..aae41b4 100644 --- a/Twig/SettingsExtension.php +++ b/Twig/SettingsExtension.php @@ -13,14 +13,14 @@ */ class SettingsExtension extends AbstractExtension { - private $settingsManager; - - public function __construct(SettingsManagerInterface $settingsManager) + public function __construct(private SettingsManagerInterface $settingsManager) { - $this->settingsManager = $settingsManager; } - public function getFunctions() + /** + * @return TwigFunction[] + */ + public function getFunctions(): array { return [ new TwigFunction('get_setting', [$this->settingsManager, 'get']), diff --git a/composer.json b/composer.json index 3ed64e1..c3bca4a 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "authors": [ { "name": "Dmitriy Scherbina", - "homepage": "http://dmishh.com" + "homepage": "https://dmishh.com/" }, { "name": "Tobias Nyholm", @@ -24,7 +24,7 @@ "psr/cache": "^3.0", "symfony/framework-bundle": "^5.4|^6.0", "symfony/form": "^5.4 || ^6.0", - "doctrine/orm": "^2.7", + "doctrine/orm": "^2.8", "doctrine/annotations": "^1.14 || ^2.0" }, "require-dev": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b9812a8..c8fe8d8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,10 +1,12 @@ - + + xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" +> + ./Controller @@ -18,13 +20,16 @@ ./Twig + ./Tests + + From dbef7262317e2dfcb42c8f8c705001581be500e0 Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 15:39:10 +0100 Subject: [PATCH 14/22] feat: changes for symfony 6 --- .github/workflows/main.yml | 8 +++++++- DependencyInjection/DmishhSettingsExtension.php | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b0b25a7..b0fe17c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,6 +28,7 @@ jobs: - run: composer require symfony/form:${{ matrix.symfony }} --no-update - run: composer install - run: make test + test-highest: runs-on: ubuntu-latest strategy: @@ -45,6 +46,7 @@ jobs: - run: composer require symfony/form:${{ matrix.symfony }} --no-update - run: composer install - run: make test + test-lowest: runs-on: ubuntu-latest strategy: @@ -62,8 +64,12 @@ jobs: - run: composer require symfony/form:${{ matrix.symfony }} --no-update - run: composer install - run: make test-lowest + php-cs-fixer: - runs-on: ubuntu-latest + # https://github.com/jakzal/phpqa php-cs-fixer is not supported for php 8.2 (checked on 07-02-2023) + # https://github.com/jakzal/phpqa/issues/398 + runs-on: ubuntu-20.04 + #runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Run PHP-CS-Fixer diff --git a/DependencyInjection/DmishhSettingsExtension.php b/DependencyInjection/DmishhSettingsExtension.php index ebf4018..89e0423 100644 --- a/DependencyInjection/DmishhSettingsExtension.php +++ b/DependencyInjection/DmishhSettingsExtension.php @@ -22,6 +22,8 @@ class DmishhSettingsExtension extends Extension { /** * {@inheritdoc} + * + * @param array> $configs */ public function load(array $configs, ContainerBuilder $container): void { From 05eb00d2e140d11d126e1b22112d9cd51bb9c3af Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 16:10:00 +0100 Subject: [PATCH 15/22] feat: changes for symfony 6 --- Controller/SettingsController.php | 3 +- DependencyInjection/Configuration.php | 2 +- Entity/Setting.php | 26 ++++--------- Tests/AbstractTest.php | 11 +----- Tests/SettingsManagerTest.php | 55 +++++++++++++-------------- composer.json | 3 +- 6 files changed, 39 insertions(+), 61 deletions(-) diff --git a/Controller/SettingsController.php b/Controller/SettingsController.php index 7b73be8..2b8c5b1 100644 --- a/Controller/SettingsController.php +++ b/Controller/SettingsController.php @@ -8,6 +8,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Contracts\Translation\TranslatorInterface; @@ -18,7 +19,7 @@ public function __construct( private SettingsManagerInterface $settingsManager, private string $template, private bool $securityManageOwnSettings, - private ?string $securityRole + private ?string $securityRole, ) { } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 290fc8f..f73c13e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -27,7 +27,7 @@ public function getConfigTreeBuilder(): TreeBuilder $rootNode ->children() ->scalarNode('template') - ->defaultValue('DmishhSettingsBundle:Settings:manage.html.twig') + ->defaultValue('@DmishhSettings/Settings/manage.html.twig') ->end() ->scalarNode('cache_service')->defaultNull()->info('A service implementing Psr\Cache\CacheItemPoolInterface')->end() ->integerNode('cache_lifetime')->defaultValue(3600)->end() diff --git a/Entity/Setting.php b/Entity/Setting.php index 2781991..4b50ecc 100644 --- a/Entity/Setting.php +++ b/Entity/Setting.php @@ -4,32 +4,22 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Table(name="dmishh_settings", indexes={@ORM\Index(name="name_owner_id_idx", columns={"name", "owner_id"})}) - * @ORM\Entity - */ +#[ORM\Table(name: 'dmishh_settings', indexes: [new ORM\Index(name: 'name_owner_id_idx', columns: ['name', 'owner_id'])])] +#[ORM\Entity] class Setting { - /** - * @ORM\Column(type="integer") - * @ORM\Id - * @ORM\GeneratedValue(strategy="AUTO") - */ + #[ORM\Column(type: 'integer')] + #[ORM\Id] + #[ORM\GeneratedValue(strategy: 'AUTO')] private ?int $id; - /** - * @ORM\Column(type="string", length=255) - */ + #[ORM\Column(type: 'string', length: 255)] private ?string $name; - /** - * @ORM\Column(type="text", nullable=true) - */ + #[ORM\Column(type: 'text', nullable: true)] private ?string $value; - /** - * @ORM\Column(name="owner_id", type="string", length=255, nullable=true) - */ + #[ORM\Column(name: 'owner_id', type: 'string', length: 255, nullable: true)] private ?string $ownerId; public function getId(): ?int diff --git a/Tests/AbstractTest.php b/Tests/AbstractTest.php index 3b1d153..dbdd5ad 100644 --- a/Tests/AbstractTest.php +++ b/Tests/AbstractTest.php @@ -2,7 +2,6 @@ namespace Dmishh\SettingsBundle\Tests; -use Doctrine\Common\Annotations\AnnotationRegistry; use Doctrine\ORM\Configuration; use Doctrine\ORM\EntityManager; use PHPUnit\Framework\TestCase; @@ -35,15 +34,7 @@ protected function createEntityManager(): EntityManager $config->setProxyNamespace('EntityProxy'); $config->setAutoGenerateProxyClasses(true); - if (method_exists(AnnotationRegistry::class, 'registerFile')) { - AnnotationRegistry::registerFile( - __DIR__ . - '/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php' - ); - } - - $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver( - new \Doctrine\Common\Annotations\AnnotationReader(), + $driver = new \Doctrine\ORM\Mapping\Driver\AttributeDriver( [__DIR__.'/../Entity'] ); $config->setMetadataDriverImpl($driver); diff --git a/Tests/SettingsManagerTest.php b/Tests/SettingsManagerTest.php index e0e4e48..f786418 100644 --- a/Tests/SettingsManagerTest.php +++ b/Tests/SettingsManagerTest.php @@ -3,6 +3,7 @@ namespace Dmishh\SettingsBundle\Tests; use Dmishh\SettingsBundle\Entity\Setting; +use Dmishh\SettingsBundle\Entity\SettingsOwnerInterface; use Dmishh\SettingsBundle\Manager\SettingsManager; use Dmishh\SettingsBundle\Manager\SettingsManagerInterface; use Dmishh\SettingsBundle\Serializer\PhpSerializer; @@ -10,6 +11,7 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; use Mockery; +use Dmishh\SettingsBundle\Exception\WrongScopeException; class SettingsManagerTest extends AbstractTest { @@ -35,7 +37,7 @@ public function testGlobalSettingsClear() $this->assertNull($settingsManager->get('some_setting')); } - public function testUserSettingsAccessor() + public function testUserSettingsAccessor(): void { $owner = $this->createOwner(); $settingsManager = $this->createSettingsManager(); @@ -43,7 +45,7 @@ public function testUserSettingsAccessor() $this->assertEquals('VALUE_USER', $settingsManager->get('some_setting', $owner)); } - public function testUserSettingsClear() + public function testUserSettingsClear(): void { $owner = $this->createOwner(); $settingsManager = $this->createSettingsManager(); @@ -52,7 +54,7 @@ public function testUserSettingsClear() $this->assertNull($settingsManager->get('some_setting', $owner)); } - public function testGlobalAndUserSettingsArentIntersect() + public function testGlobalAndUserSettingsArentIntersect(): void { $owner = $this->createOwner(); $settingsManager = $this->createSettingsManager(); @@ -68,7 +70,7 @@ public function testGlobalAndUserSettingsArentIntersect() $this->assertEquals('VALUE_USER_2', $settingsManager->get('some_setting', $owner)); } - public function testUsersSettingsArentIntersect() + public function testUsersSettingsArentIntersect(): void { $owner1 = $this->createOwner(1); $owner2 = $this->createOwner(2); @@ -85,7 +87,7 @@ public function testUsersSettingsArentIntersect() $this->assertEquals('VALUE_USER_2', $settingsManager->get('some_setting', $owner2)); } - public function testPersistence() + public function testPersistence(): void { $settingsManager = $this->createSettingsManager(); $settingsManager->set('some_setting', 'VALUE_GLOBAL'); @@ -100,9 +102,9 @@ public function testPersistence() $this->assertNull($settingsManager->get('some_setting')); } - public function testSetUserSettingInGlobalScopeRaisesException() + public function testSetUserSettingInGlobalScopeRaisesException(): void { - $this->expectException('\Dmishh\SettingsBundle\Exception\WrongScopeException'); + $this->expectException(WrongScopeException::class); $owner = $this->createOwner(); $settingsManager = $this->createSettingsManager(); @@ -112,18 +114,18 @@ public function testSetUserSettingInGlobalScopeRaisesException() $settingsManager->set('some_global_setting', 'VALUE_GLOBAL', $owner); } - public function testGetUserSettingInGlobalScopeRaisesException() + public function testGetUserSettingInGlobalScopeRaisesException(): void { - $this->expectException('\Dmishh\SettingsBundle\Exception\WrongScopeException'); + $this->expectException(WrongScopeException::class); $owner = $this->createOwner(); $settingsManager = $this->createSettingsManager(); $settingsManager->get('some_global_setting', $owner); } - public function testSetGlobalSettingInUserScopeRaisesException() + public function testSetGlobalSettingInUserScopeRaisesException(): void { - $this->expectException('\Dmishh\SettingsBundle\Exception\WrongScopeException'); + $this->expectException(WrongScopeException::class); $owner = $this->createOwner(); $settingsManager = $this->createSettingsManager(); @@ -133,15 +135,15 @@ public function testSetGlobalSettingInUserScopeRaisesException() $settingsManager->set('some_user_setting', 'VALUE_USER'); } - public function testGetGlobalSettingInUserScopeRaisesException() + public function testGetGlobalSettingInUserScopeRaisesException(): void { - $this->expectException('\Dmishh\SettingsBundle\Exception\WrongScopeException'); + $this->expectException(WrongScopeException::class); $settingsManager = $this->createSettingsManager(); $settingsManager->get('some_user_setting'); } - public function testGetAllGlobalSettings() + public function testGetAllGlobalSettings(): void { $settingsManager = $this->createSettingsManager(); $this->assertEquals( @@ -150,7 +152,7 @@ public function testGetAllGlobalSettings() ); } - public function testGetAllUserSettings() + public function testGetAllUserSettings(): void { $owner = $this->createOwner(); $settingsManager = $this->createSettingsManager(); @@ -160,7 +162,7 @@ public function testGetAllUserSettings() ); } - public function testScopeAll() + public function testScopeAll(): void { $owner = $this->createOwner(); $settingsManager = $this->createSettingsManager(); @@ -194,7 +196,7 @@ public function testScopeAll() ); } - public function testValidSerizalizationTypes() + public function testValidSerizalizationTypes(): void { $settingsManager = $this->createSettingsManager([], 'php'); $settingsManager->set('some_setting', 123); @@ -224,7 +226,7 @@ public function testGetSettingWithInvalidSerizalizationType() $settingsManager->get('some_setting'); } - public function testGetDefaultValue() + public function testGetDefaultValue(): void { $user = $this->createOwner(); $settingsManager = $this->createSettingsManager(); @@ -246,7 +248,7 @@ public function testGetDefaultValue() /** * @see https://github.com/dmishh/SettingsBundle/issues/28 */ - public function testFlush() + public function testFlush(): void { $names = ['foo', 'bar', 'baz']; $settings = ['foobar']; @@ -328,7 +330,7 @@ public function testFlush() $flushMethod->invoke($manager, $names, $owner); } - public function testFindSettingByName() + public function testFindSettingByName(): void { $settingsManager = $this->createSettingsManager(); @@ -353,7 +355,7 @@ public function testFindSettingByName() protected function createSetting($name) { - $s = $this->getMockBuilder('Dmishh\SettingsBundle\Entity\Setting') + $s = $this->getMockBuilder(Setting::class) ->onlyMethods(['getName']) ->getMock(); @@ -364,20 +366,15 @@ protected function createSetting($name) return $s; } - /** - * @param string $ownerId - * - * @return \Dmishh\SettingsBundle\Entity\SettingsOwnerInterface - */ - protected function createOwner($ownerId = 'user1') + protected function createOwner(string $ownerId = 'user1'): SettingsOwnerInterface { return Mockery::mock( - 'Dmishh\SettingsBundle\Entity\SettingsOwnerInterface', + SettingsOwnerInterface::class, ['getSettingIdentifier' => $ownerId] ); } - protected function createSettingsManager(array $configuration = [], $serialization = 'php') + protected function createSettingsManager(array $configuration = [], $serialization = 'php'): SettingsManager { if (empty($configuration)) { $configuration = [ diff --git a/composer.json b/composer.json index c3bca4a..38a6e43 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,7 @@ "psr/cache": "^3.0", "symfony/framework-bundle": "^5.4|^6.0", "symfony/form": "^5.4 || ^6.0", - "doctrine/orm": "^2.8", - "doctrine/annotations": "^1.14 || ^2.0" + "doctrine/orm": "^2.8" }, "require-dev": { "phpunit/phpunit": "^9.5", From f0a47aa63f13aa8841a9012df35b796ce5f2ef5e Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 16:13:22 +0100 Subject: [PATCH 16/22] feat: changes for symfony 6 --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b0fe17c..dc15652 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,6 +1,6 @@ name: Tests -on: [ pull_request ] +on: ["push", "pull_request"] jobs: static-code-analysis: From 73cf8cec8bb271d31e5a034118ebf47197b411de Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 16:19:22 +0100 Subject: [PATCH 17/22] feat: changes for symfony 6 --- Entity/Setting.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Entity/Setting.php b/Entity/Setting.php index 4b50ecc..af42572 100644 --- a/Entity/Setting.php +++ b/Entity/Setting.php @@ -4,7 +4,8 @@ use Doctrine\ORM\Mapping as ORM; -#[ORM\Table(name: 'dmishh_settings', indexes: [new ORM\Index(name: 'name_owner_id_idx', columns: ['name', 'owner_id'])])] +#[ORM\Table(name: 'dmishh_settings')] +#[ORM\Index(name: 'name_owner_id_idx', columns: ['name', 'owner_id'])] #[ORM\Entity] class Setting { From 2c7a672e106304433af2e35781477593f3de6c6b Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 16:21:11 +0100 Subject: [PATCH 18/22] feat: changes for symfony 6 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 38a6e43..6791795 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "psr/cache": "^3.0", "symfony/framework-bundle": "^5.4|^6.0", "symfony/form": "^5.4 || ^6.0", - "doctrine/orm": "^2.8" + "doctrine/orm": "^2.11" }, "require-dev": { "phpunit/phpunit": "^9.5", From 9c6da913d0b4698fe8d27e409f82d80b84c6d9fe Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 16:25:46 +0100 Subject: [PATCH 19/22] feat: changes for symfony 6 --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 6791795..d9be0fc 100644 --- a/composer.json +++ b/composer.json @@ -24,12 +24,12 @@ "psr/cache": "^3.0", "symfony/framework-bundle": "^5.4|^6.0", "symfony/form": "^5.4 || ^6.0", - "doctrine/orm": "^2.11" + "doctrine/orm": "^2.14" }, "require-dev": { "phpunit/phpunit": "^9.5", "mockery/mockery": "^1.5", - "doctrine/doctrine-bundle": "^2.0", + "doctrine/doctrine-bundle": "^2.8", "polishsymfonycommunity/symfony-mocker-container": "^1.0", "matthiasnoback/symfony-dependency-injection-test": "^4.3", "nyholm/symfony-bundle-test": "^2.0.0", From 0b534cd7a948230539b02d22d70b95a8a5f424a1 Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 16:33:21 +0100 Subject: [PATCH 20/22] feat: changes for symfony 6 --- Form/Type/SettingsType.php | 2 ++ Serializer/JsonSerializer.php | 1 + phpstan.neon.dist | 2 ++ 3 files changed, 5 insertions(+) diff --git a/Form/Type/SettingsType.php b/Form/Type/SettingsType.php index 4f0a041..26de79e 100644 --- a/Form/Type/SettingsType.php +++ b/Form/Type/SettingsType.php @@ -21,6 +21,8 @@ public function __construct(protected array $settingsConfiguration) /** * {@inheritdoc} + * + * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options): void { diff --git a/Serializer/JsonSerializer.php b/Serializer/JsonSerializer.php index 2526d86..7354845 100644 --- a/Serializer/JsonSerializer.php +++ b/Serializer/JsonSerializer.php @@ -13,6 +13,7 @@ public function serialize(mixed $data): string { $serialized = json_encode($data, JSON_THROW_ON_ERROR); + /** @phpstan-ignore-next-line */ if (false === $serialized) { throw new InvalidArgumentException('Invalid argument: this argument cannot be serialized with this serializer'); } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index b1a71d4..5937773 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -4,9 +4,11 @@ includes: parameters: level: max inferPrivatePropertyTypeFromConstructor: true + reportUnmatchedIgnoredErrors: false paths: - . excludePaths: - tmp-phpqa - vendor - Tests + - .github From 99a3e07231bd093406238f15161ce585b69f4605 Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 16:37:21 +0100 Subject: [PATCH 21/22] feat: changes for symfony 6 --- .github/workflows/main.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index dc15652..4ae6e25 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -68,11 +68,11 @@ jobs: php-cs-fixer: # https://github.com/jakzal/phpqa php-cs-fixer is not supported for php 8.2 (checked on 07-02-2023) # https://github.com/jakzal/phpqa/issues/398 - runs-on: ubuntu-20.04 - #runs-on: ubuntu-latest + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Run PHP-CS-Fixer - uses: docker://jakzal/phpqa + #uses: docker://jakzal/phpqa + uses: docker://jakzal/phpqa:php8.1-alpine with: args: php-cs-fixer fix --dry-run From f1324797dadc70e0e6e5c4b061d52e23b4f69850 Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Tue, 7 Feb 2023 16:41:04 +0100 Subject: [PATCH 22/22] feat: changes for symfony 6 --- Controller/SettingsController.php | 9 ++++----- Manager/CachedSettingsManager.php | 5 ++--- Manager/SettingsManager.php | 6 +++--- Serializer/JsonSerializer.php | 4 ++-- Tests/Serializer/CustomSerializer.php | 4 ++-- Tests/SerializerTest.php | 4 ++-- Tests/SettingsManagerTest.php | 5 ++--- 7 files changed, 17 insertions(+), 20 deletions(-) diff --git a/Controller/SettingsController.php b/Controller/SettingsController.php index 2b8c5b1..7c317db 100644 --- a/Controller/SettingsController.php +++ b/Controller/SettingsController.php @@ -8,18 +8,17 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Contracts\Translation\TranslatorInterface; class SettingsController extends AbstractController { public function __construct( - private TranslatorInterface $translator, + private TranslatorInterface $translator, private SettingsManagerInterface $settingsManager, - private string $template, - private bool $securityManageOwnSettings, - private ?string $securityRole, + private string $template, + private bool $securityManageOwnSettings, + private ?string $securityRole, ) { } diff --git a/Manager/CachedSettingsManager.php b/Manager/CachedSettingsManager.php index ee5b69e..bc9efc9 100644 --- a/Manager/CachedSettingsManager.php +++ b/Manager/CachedSettingsManager.php @@ -2,7 +2,6 @@ namespace Dmishh\SettingsBundle\Manager; -use DateInterval; use Dmishh\SettingsBundle\Entity\SettingsOwnerInterface; use Psr\Cache\CacheItemPoolInterface; @@ -14,8 +13,8 @@ class CachedSettingsManager implements SettingsManagerInterface public const PREFIX = 'dmishh_settings_%s_%s'; public function __construct(private SettingsManagerInterface $settingsManager, - private CacheItemPoolInterface $storage, - private int|DateInterval|null $cacheLifeTime) + private CacheItemPoolInterface $storage, + private int|\DateInterval|null $cacheLifeTime) { } diff --git a/Manager/SettingsManager.php b/Manager/SettingsManager.php index 1211d69..43d6a4e 100644 --- a/Manager/SettingsManager.php +++ b/Manager/SettingsManager.php @@ -36,7 +36,7 @@ class SettingsManager implements SettingsManagerInterface public function __construct( private EntityManagerInterface $em, - private SerializerInterface $serializer, + private SerializerInterface $serializer, private array $settingsConfiguration = [] ) { $this->repository = $em->getRepository(Setting::class); @@ -58,8 +58,8 @@ public function get(string $name, ?SettingsOwnerInterface $owner = null, mixed $ break; case SettingsManagerInterface::SCOPE_ALL: $value = $this->globalSettings[$name] ?? null; - // Do not break here. Try to fetch the users settings - // no break + // Do not break here. Try to fetch the users settings + // no break case SettingsManagerInterface::SCOPE_USER: if (null !== $owner) { $value = $this->ownerSettings[$owner->getSettingIdentifier()][$name] ?? $value; diff --git a/Serializer/JsonSerializer.php b/Serializer/JsonSerializer.php index 7354845..984a30a 100644 --- a/Serializer/JsonSerializer.php +++ b/Serializer/JsonSerializer.php @@ -11,9 +11,9 @@ class JsonSerializer implements SerializerInterface */ public function serialize(mixed $data): string { - $serialized = json_encode($data, JSON_THROW_ON_ERROR); + $serialized = json_encode($data, \JSON_THROW_ON_ERROR); - /** @phpstan-ignore-next-line */ + /* @phpstan-ignore-next-line */ if (false === $serialized) { throw new InvalidArgumentException('Invalid argument: this argument cannot be serialized with this serializer'); } diff --git a/Tests/Serializer/CustomSerializer.php b/Tests/Serializer/CustomSerializer.php index 2d2f19a..880cccc 100644 --- a/Tests/Serializer/CustomSerializer.php +++ b/Tests/Serializer/CustomSerializer.php @@ -8,11 +8,11 @@ class CustomSerializer implements SerializerInterface { public function serialize(mixed $data): string { - return serialize(json_encode($data, JSON_THROW_ON_ERROR)); + return serialize(json_encode($data, \JSON_THROW_ON_ERROR)); } public function unserialize(string $serialized): mixed { - return json_decode(unserialize($serialized), true, 512, JSON_THROW_ON_ERROR); + return json_decode(unserialize($serialized), true, 512, \JSON_THROW_ON_ERROR); } } diff --git a/Tests/SerializerTest.php b/Tests/SerializerTest.php index af9f6c4..079d195 100644 --- a/Tests/SerializerTest.php +++ b/Tests/SerializerTest.php @@ -21,8 +21,8 @@ public function testPhpSerializer(): void public function testJsonSerializer(): void { $serializer = SerializerFactory::create('json'); - $this->assertEquals(json_encode(null, JSON_THROW_ON_ERROR), $serializer->serialize(null)); - $this->assertEquals(json_encode(self::$testData, JSON_THROW_ON_ERROR), $serializer->serialize(self::$testData)); + $this->assertEquals(json_encode(null, \JSON_THROW_ON_ERROR), $serializer->serialize(null)); + $this->assertEquals(json_encode(self::$testData, \JSON_THROW_ON_ERROR), $serializer->serialize(self::$testData)); $this->assertEquals(self::$testData, $serializer->unserialize($serializer->serialize(self::$testData))); } diff --git a/Tests/SettingsManagerTest.php b/Tests/SettingsManagerTest.php index f786418..fab338c 100644 --- a/Tests/SettingsManagerTest.php +++ b/Tests/SettingsManagerTest.php @@ -4,14 +4,13 @@ use Dmishh\SettingsBundle\Entity\Setting; use Dmishh\SettingsBundle\Entity\SettingsOwnerInterface; +use Dmishh\SettingsBundle\Exception\WrongScopeException; use Dmishh\SettingsBundle\Manager\SettingsManager; use Dmishh\SettingsBundle\Manager\SettingsManagerInterface; use Dmishh\SettingsBundle\Serializer\PhpSerializer; use Dmishh\SettingsBundle\Serializer\SerializerFactory; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; -use Mockery; -use Dmishh\SettingsBundle\Exception\WrongScopeException; class SettingsManagerTest extends AbstractTest { @@ -368,7 +367,7 @@ protected function createSetting($name) protected function createOwner(string $ownerId = 'user1'): SettingsOwnerInterface { - return Mockery::mock( + return \Mockery::mock( SettingsOwnerInterface::class, ['getSettingIdentifier' => $ownerId] );