From e8646698ad50fea75551a14a43c0757814e09cea Mon Sep 17 00:00:00 2001 From: Vadim Borodavko Date: Fri, 10 Dec 2021 10:07:10 +0200 Subject: [PATCH] Add Symfony 6.0 support (#1099) --- .github/workflows/ci.yaml | 20 ++++++++++--------- .github/workflows/static-analysis.yaml | 2 +- composer.json | 14 ++++++++----- .../Bridge/Symfony/Application/AppKernel.php | 12 +++++++---- .../PropertyAccess/FakePropertyAccessor.php | 6 +++--- phpunit.xml.dist | 2 +- phpunit_symfony.xml.dist | 2 +- .../DependencyInjection/Configuration.php | 12 +++-------- .../ReflectionPropertyAccessor.php | 6 +++--- src/PropertyAccess/StdPropertyAccessor.php | 6 +++--- .../FixturePropertyReferenceResolverTest.php | 12 +++++++++-- 11 files changed, 53 insertions(+), 41 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 3b935dbd9..8f656ad9e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -12,23 +12,25 @@ jobs: fail-fast: false matrix: php: - - '7.4' - '8.0' - '8.1' symfony-versions: [false] include: - - description: 'Symfony 4.4' - php: '7.4' - symfony-versions: 4.4.* - - description: 'Symfony 5.3' - php: '7.4' - symfony-versions: 5.3.* - description: 'Symfony 4.4' php: '8.0' symfony-versions: 4.4.* - - description: 'Symfony 5.3' + - description: 'Symfony 5.4' + php: '8.0' + symfony-versions: 5.4.* + - description: 'Symfony 6.0' php: '8.0' - symfony-versions: 5.3.* + symfony-versions: 6.0.* + - description: 'Symfony 5.4' + php: '8.1' + symfony-versions: 5.4.* + - description: 'Symfony 6.0' + php: '8.1' + symfony-versions: 6.0.* name: PHP ${{ matrix.php }} ${{ matrix.description }} steps: diff --git a/.github/workflows/static-analysis.yaml b/.github/workflows/static-analysis.yaml index 3e2ad9d74..a10b6f270 100644 --- a/.github/workflows/static-analysis.yaml +++ b/.github/workflows/static-analysis.yaml @@ -16,7 +16,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '7.4' + php-version: '8.0' - name: Install dependencies run: composer install --no-progress --no-interaction --prefer-dist diff --git a/composer.json b/composer.json index 328e8e9b4..53d105f8d 100644 --- a/composer.json +++ b/composer.json @@ -20,20 +20,24 @@ ], "require": { - "php": "^7.4 || ^8.0", + "php": "^8.0", "fakerphp/faker": "^1.10", "myclabs/deep-copy": "^1.10", "sebastian/comparator": "^3.0 || ^4.0", - "symfony/property-access": "^4.4 || ^5.2", - "symfony/yaml": "^4.4 || ^5.2" + "symfony/property-access": "^4.4 || ^5.4 || ^6.0", + "symfony/yaml": "^4.4 || ^5.4 || ^6.0" }, "require-dev": { "bamarni/composer-bin-plugin": "^1.4.1", "phpspec/prophecy": "^1.6", "phpspec/prophecy-phpunit": "^2.0", "phpunit/phpunit": "^9.3", - "symfony/phpunit-bridge": "^5.1.3", - "symfony/var-dumper": "^4.4 || ^5.2" + "symfony/config": "^4.4 || ^5.4 || ^6.0", + "symfony/dependency-injection": "^4.4 || ^5.4 || ^6.0", + "symfony/finder": "^4.4 || ^5.4 || ^6.0", + "symfony/http-kernel": "^4.4 || ^5.4 || ^6.0", + "symfony/phpunit-bridge": "^5.4 || ^6.0", + "symfony/var-dumper": "^4.4 || ^5.4 || ^6.0" }, "conflict": { "symfony/framework-bundle": "<4.4 || >=5.0.0,<5.2.0" diff --git a/fixtures/Bridge/Symfony/Application/AppKernel.php b/fixtures/Bridge/Symfony/Application/AppKernel.php index 25e7548d4..2321f0176 100644 --- a/fixtures/Bridge/Symfony/Application/AppKernel.php +++ b/fixtures/Bridge/Symfony/Application/AppKernel.php @@ -33,7 +33,7 @@ public function __construct($environment, $debug) parent::__construct($environment, $debug); } - public function registerBundles() + public function registerBundles(): array { return [ new FrameworkBundle(), @@ -52,11 +52,15 @@ public function build(ContainerBuilder $container): void public function process(ContainerBuilder $container): void { foreach ($container->getDefinitions() as $id => $definition) { - $definition->setPublic(true); + if (str_starts_with($id, 'nelmio_alice.')) { + $definition->setPublic(true); + } } foreach ($container->getAliases() as $id => $definition) { - $definition->setPublic(true); + if (str_starts_with($id, 'nelmio_alice.')) { + $definition->setPublic(true); + } } } }, PassConfig::TYPE_OPTIMIZE); @@ -67,7 +71,7 @@ public function setConfigurationResource(string $resource): void $this->config = $resource; } - public function getProjectDir() + public function getProjectDir(): string { return __DIR__; } diff --git a/fixtures/Symfony/PropertyAccess/FakePropertyAccessor.php b/fixtures/Symfony/PropertyAccess/FakePropertyAccessor.php index d0114d116..419716fe5 100644 --- a/fixtures/Symfony/PropertyAccess/FakePropertyAccessor.php +++ b/fixtures/Symfony/PropertyAccess/FakePropertyAccessor.php @@ -25,17 +25,17 @@ public function setValue(&$objectOrArray, $propertyPath, $value): void $this->__call(__METHOD__, func_get_args()); } - public function getValue($objectOrArray, $propertyPath): void + public function getValue($objectOrArray, $propertyPath): mixed { $this->__call(__METHOD__, func_get_args()); } - public function isWritable($objectOrArray, $propertyPath): void + public function isWritable($objectOrArray, $propertyPath): bool { $this->__call(__METHOD__, func_get_args()); } - public function isReadable($objectOrArray, $propertyPath): void + public function isReadable($objectOrArray, $propertyPath): bool { $this->__call(__METHOD__, func_get_args()); } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fff65c59a..fd7d69bf0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,7 +18,7 @@ executionOrder="random"> - + diff --git a/phpunit_symfony.xml.dist b/phpunit_symfony.xml.dist index d68829671..488b0a4e4 100644 --- a/phpunit_symfony.xml.dist +++ b/phpunit_symfony.xml.dist @@ -19,7 +19,7 @@ > - + diff --git a/src/Bridge/Symfony/DependencyInjection/Configuration.php b/src/Bridge/Symfony/DependencyInjection/Configuration.php index 7f8c6f799..6beacb98a 100644 --- a/src/Bridge/Symfony/DependencyInjection/Configuration.php +++ b/src/Bridge/Symfony/DependencyInjection/Configuration.php @@ -23,18 +23,12 @@ */ final class Configuration implements ConfigurationInterface { - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('nelmio_alice'); - if (method_exists($treeBuilder, 'getRootNode')) { - $rootNode = $treeBuilder->getRootNode(); - } else { - // BC layer for symfony/config 4.1 and older - $rootNode = $treeBuilder->root('nelmio_alice'); - } - - $rootNode + $treeBuilder + ->getRootNode() ->children() ->scalarNode('locale') ->defaultValue('en_US') diff --git a/src/PropertyAccess/ReflectionPropertyAccessor.php b/src/PropertyAccess/ReflectionPropertyAccessor.php index 82d143144..7f0695b16 100644 --- a/src/PropertyAccess/ReflectionPropertyAccessor.php +++ b/src/PropertyAccess/ReflectionPropertyAccessor.php @@ -66,7 +66,7 @@ function ($object) use ($propertyPath, $value): void { } } - public function getValue($objectOrArray, $propertyPath) + public function getValue($objectOrArray, $propertyPath): mixed { try { return $this->decoratedPropertyAccessor->getValue($objectOrArray, $propertyPath); @@ -94,12 +94,12 @@ function ($object) use ($propertyPath) { } } - public function isWritable($objectOrArray, $propertyPath) + public function isWritable($objectOrArray, $propertyPath): bool { return $this->decoratedPropertyAccessor->isWritable($objectOrArray, $propertyPath) || $this->propertyExists($objectOrArray, $propertyPath); } - public function isReadable($objectOrArray, $propertyPath) + public function isReadable($objectOrArray, $propertyPath): bool { return $this->decoratedPropertyAccessor->isReadable($objectOrArray, $propertyPath) || $this->propertyExists($objectOrArray, $propertyPath); } diff --git a/src/PropertyAccess/StdPropertyAccessor.php b/src/PropertyAccess/StdPropertyAccessor.php index 6ff82da43..b114c0a0d 100644 --- a/src/PropertyAccess/StdPropertyAccessor.php +++ b/src/PropertyAccess/StdPropertyAccessor.php @@ -43,7 +43,7 @@ public function setValue(&$objectOrArray, $propertyPath, $value): void $this->decoratedPropertyAccessor->setValue($objectOrArray, $propertyPath, $value); } - public function getValue($objectOrArray, $propertyPath) + public function getValue($objectOrArray, $propertyPath): mixed { if (false === $objectOrArray instanceof stdClass) { return $this->decoratedPropertyAccessor->getValue($objectOrArray, $propertyPath); @@ -56,7 +56,7 @@ public function getValue($objectOrArray, $propertyPath) return $objectOrArray->$propertyPath; } - public function isWritable($objectOrArray, $propertyPath) + public function isWritable($objectOrArray, $propertyPath): bool { return ($objectOrArray instanceof stdClass) ? true @@ -64,7 +64,7 @@ public function isWritable($objectOrArray, $propertyPath) ; } - public function isReadable($objectOrArray, $propertyPath) + public function isReadable($objectOrArray, $propertyPath): bool { return ($objectOrArray instanceof stdClass) ? isset($objectOrArray->$propertyPath) diff --git a/tests/Generator/Resolver/Value/Chainable/FixturePropertyReferenceResolverTest.php b/tests/Generator/Resolver/Value/Chainable/FixturePropertyReferenceResolverTest.php index 119cb3873..be8674bbc 100644 --- a/tests/Generator/Resolver/Value/Chainable/FixturePropertyReferenceResolverTest.php +++ b/tests/Generator/Resolver/Value/Chainable/FixturePropertyReferenceResolverTest.php @@ -36,9 +36,11 @@ use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use ReflectionClass; +use ReflectionMethod; use stdClass; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; +use TypeError; /** * @covers \Nelmio\Alice\Generator\Resolver\Value\Chainable\FixturePropertyReferenceResolver @@ -225,8 +227,14 @@ public function testThrowsAnExceptionIfReferenceResolvedIsNotAnObject(): void $resolver = new FixturePropertyReferenceResolver(PropertyAccess::createPropertyAccessor(), $valueResolver); - $this->expectException(UnresolvableValueException::class); - $this->expectExceptionMessage('Could not resolve value "dummy->publicProperty": PropertyAccessor requires a graph of objects or arrays to operate on, but it found type "string" while trying to traverse path "publicProperty" at property "publicProperty".'); + if ((new ReflectionMethod(PropertyAccessorInterface::class, 'getValue'))->getParameters()[0]->getType() !== null) { + // Since symfony/property-access 6.0 + $this->expectException(TypeError::class); + $this->expectExceptionMessageMatches(sprintf('/^%s/', preg_quote('Symfony\Component\PropertyAccess\PropertyAccessor::getValue(): Argument #1 ($objectOrArray) must be of type object|array, string given', '/'))); + } else { + $this->expectException(UnresolvableValueException::class); + $this->expectExceptionMessage('Could not resolve value "dummy->publicProperty": PropertyAccessor requires a graph of objects or arrays to operate on, but it found type "string" while trying to traverse path "publicProperty" at property "publicProperty".'); + } $resolver->resolve($value, new FakeFixture(), $set, [], new GenerationContext()); }