diff --git a/.github/workflows/test_symfony_7.yaml b/.github/workflows/test_symfony_7.yaml new file mode 100644 index 00000000..3e9c9b1a --- /dev/null +++ b/.github/workflows/test_symfony_7.yaml @@ -0,0 +1,74 @@ +# TEMPORARILY USED until knplabs/doctrine-behaviors is compatible with Symfony 7 + +name: Test + +on: + schedule: + - cron: '30 0 * * *' + push: + branches: + - 3.x + - 4.x + pull_request: + +permissions: + contents: read + +jobs: + test: + name: PHP ${{ matrix.php-version }} + ${{ matrix.dependencies }} + ${{ matrix.variant }} (Symfony 7) + + runs-on: ubuntu-latest + + continue-on-error: ${{ matrix.allowed-to-fail }} + + env: + SYMFONY_REQUIRE: ${{matrix.symfony-require}} + + strategy: + matrix: + include: + - php-version: '8.2' + dependencies: highest + allowed-to-fail: false + symfony-require: 7.0.* + variant: symfony/symfony:"7.0.*" + - php-version: '8.3' + dependencies: highest + allowed-to-fail: false + symfony-require: 7.0.* + variant: symfony/symfony:"7.0.*" + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install PHP with extensions + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + coverage: pcov + tools: composer:v2, flex + + - name: Add PHPUnit matcher + run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Remove knplabs/doctrine-behaviors + run: composer remove knplabs/doctrine-behaviors --dev --no-update + + - name: Install variant + if: matrix.variant != 'normal' && !startsWith(matrix.variant, 'symfony/symfony') + run: composer require ${{ matrix.variant }} --no-update + + - name: Install Composer dependencies (${{ matrix.dependencies }}) + uses: ramsey/composer-install@v3 + with: + dependency-versions: ${{ matrix.dependencies }} + + - name: Run Tests with coverage + run: make coverage + + - name: Send coverage to Codecov + uses: codecov/codecov-action@v4 + with: + files: build/logs/clover.xml diff --git a/composer.json b/composer.json index 04062276..132b9877 100644 --- a/composer.json +++ b/composer.json @@ -21,16 +21,16 @@ "homepage": "https://docs.sonata-project.org/projects/SonataTranslationBundle", "require": { "php": "^8.0", - "sonata-project/admin-bundle": "^4.14", + "sonata-project/admin-bundle": "^4.15", "sonata-project/block-bundle": "^4.11 || ^5.0", - "symfony/config": "^5.4 || ^6.2", - "symfony/dependency-injection": "^5.4 || ^6.2", - "symfony/form": "^5.4 || ^6.2", - "symfony/framework-bundle": "^5.4 || ^6.2", - "symfony/http-foundation": "^5.4 || ^6.2", - "symfony/http-kernel": "^5.4 || ^6.2", - "symfony/intl": "^5.4 || ^6.2", - "symfony/options-resolver": "^5.4 || ^6.2", + "symfony/config": "^5.4 || ^6.2 || ^7.0", + "symfony/dependency-injection": "^5.4 || ^6.2 || ^7.0", + "symfony/form": "^5.4 || ^6.2 || ^7.0", + "symfony/framework-bundle": "^5.4 || ^6.2 || ^7.0", + "symfony/http-foundation": "^5.4 || ^6.2 || ^7.0", + "symfony/http-kernel": "^5.4 || ^6.2 || ^7.0", + "symfony/intl": "^5.4 || ^6.2 || ^7.0", + "symfony/options-resolver": "^5.4 || ^6.2 || ^7.0", "twig/twig": "^3.0" }, "require-dev": { @@ -40,9 +40,9 @@ "doctrine/persistence": "^3.0.2", "friendsofphp/php-cs-fixer": "^3.4", "gedmo/doctrine-extensions": "^3.11", - "knplabs/doctrine-behaviors": "^2.2", - "matthiasnoback/symfony-config-test": "^4.2", - "matthiasnoback/symfony-dependency-injection-test": "^4.0", + "knplabs/doctrine-behaviors": "^2.6.2", + "matthiasnoback/symfony-config-test": "^4.2 || ^5.0", + "matthiasnoback/symfony-dependency-injection-test": "^4.0 || ^5.0", "phpstan/extension-installer": "^1.0", "phpstan/phpstan": "^1.0", "phpstan/phpstan-doctrine": "^1.0", @@ -54,18 +54,18 @@ "psalm/plugin-symfony": "^5.0", "rector/rector": "^1.1", "sonata-project/doctrine-orm-admin-bundle": "^4.0", - "symfony/browser-kit": "^5.4 || ^6.2", - "symfony/css-selector": "^5.4 || ^6.2", - "symfony/doctrine-bridge": "^5.4 || ^6.2", - "symfony/filesystem": "^5.4 || ^6.2", - "symfony/phpunit-bridge": "^6.2", + "symfony/browser-kit": "^5.4 || ^6.2 || ^7.0", + "symfony/css-selector": "^5.4 || ^6.2 || ^7.0", + "symfony/doctrine-bridge": "^5.4 || ^6.2 || ^7.0", + "symfony/filesystem": "^5.4 || ^6.2 || ^7.0", + "symfony/phpunit-bridge": "^7.0", "vimeo/psalm": "^5.8", "weirdan/doctrine-psalm-plugin": "^2.0" }, "conflict": { "doctrine/persistence": "<3.0.2", "gedmo/doctrine-extensions": "<3.11", - "knplabs/doctrine-behaviors": "<2.2 || >=3.0" + "knplabs/doctrine-behaviors": "<2.6.2 || >=3.0" }, "suggest": { "gedmo/doctrine-extensions": "if you translate orm entities with the gedmo extensions", diff --git a/src/Admin/Extension/AbstractTranslatableAdminExtension.php b/src/Admin/Extension/AbstractTranslatableAdminExtension.php index 687ed606..3b40c201 100644 --- a/src/Admin/Extension/AbstractTranslatableAdminExtension.php +++ b/src/Admin/Extension/AbstractTranslatableAdminExtension.php @@ -30,6 +30,8 @@ abstract class AbstractTranslatableAdminExtension extends AbstractAdminExtension { /** * Request parameter. + * + * @psalm-suppress MissingClassConstType */ public const TRANSLATABLE_LOCALE_PARAMETER = 'tl'; @@ -37,7 +39,7 @@ abstract class AbstractTranslatableAdminExtension extends AbstractAdminExtension public function __construct( protected TranslatableChecker $translatableChecker, - private LocaleProviderInterface $localeProvider + private LocaleProviderInterface $localeProvider, ) { } diff --git a/src/Admin/Extension/Gedmo/TranslatableAdminExtension.php b/src/Admin/Extension/Gedmo/TranslatableAdminExtension.php index e2b69c47..c8347283 100644 --- a/src/Admin/Extension/Gedmo/TranslatableAdminExtension.php +++ b/src/Admin/Extension/Gedmo/TranslatableAdminExtension.php @@ -37,7 +37,7 @@ public function __construct( TranslatableChecker $translatableChecker, private TranslatableListener $translatableListener, private ManagerRegistry $managerRegistry, - LocaleProviderInterface $localeProvider + LocaleProviderInterface $localeProvider, ) { parent::__construct($translatableChecker, $localeProvider); } @@ -79,9 +79,9 @@ private function setLocale(object $object): void \assert($objectManager instanceof ObjectManager); $configuration = $this->translatableListener->getConfiguration($objectManager, $objectClassName); - /** @psalm-suppress InvalidArrayOffset */ + if (!isset($configuration['locale'])) { - throw new \LogicException(sprintf( + throw new \LogicException(\sprintf( 'There is no locale or language property found on class: "%s"', $object::class )); diff --git a/src/Block/LocaleSwitcherBlockService.php b/src/Block/LocaleSwitcherBlockService.php index c13208b3..9c5d79f3 100644 --- a/src/Block/LocaleSwitcherBlockService.php +++ b/src/Block/LocaleSwitcherBlockService.php @@ -27,7 +27,7 @@ final class LocaleSwitcherBlockService extends AbstractBlockService { public function __construct( Environment $twig, - private LocaleProviderInterface $localeProvider + private LocaleProviderInterface $localeProvider, ) { parent::__construct($twig); } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 3a0d9ba6..6db4fa45 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -23,7 +23,7 @@ final class Configuration implements ConfigurationInterface { /** - * @psalm-suppress PossiblyNullReference, UndefinedInterfaceMethod + * @psalm-suppress UndefinedInterfaceMethod * * @see https://github.com/psalm/psalm-plugin-symfony/issues/174 */ diff --git a/src/Filter/TranslationFieldFilter.php b/src/Filter/TranslationFieldFilter.php index 66474595..93e592bb 100644 --- a/src/Filter/TranslationFieldFilter.php +++ b/src/Filter/TranslationFieldFilter.php @@ -70,7 +70,7 @@ public function filter(ProxyQueryInterface $query, string $alias, string $field, $this->setActive(true); } else { - throw new \LogicException(sprintf('Invalid filter mode given: "%s"', $filterMode)); + throw new \LogicException(\sprintf('Invalid filter mode given: "%s"', $filterMode)); } } @@ -123,7 +123,7 @@ private function applyGedmoFilters(ProxyQueryInterface $query, string $joinAlias ) ), $query->getQueryBuilder()->expr()->like( - sprintf('%s.%s', $alias, $field), + \sprintf('%s.%s', $alias, $field), $query->getQueryBuilder()->expr()->literal('%'.$value.'%') ) )); diff --git a/src/Provider/Knplabs/LocaleProvider.php b/src/Provider/Knplabs/LocaleProvider.php index ae6015c7..95024d81 100644 --- a/src/Provider/Knplabs/LocaleProvider.php +++ b/src/Provider/Knplabs/LocaleProvider.php @@ -22,7 +22,7 @@ final class LocaleProvider implements KnpLocaleProviderInterface public function __construct( private RequestStack $requestStack, private KnpLocaleProviderInterface $localeProvider, - private SonataLocaleProviderInterface $sonataLocaleProvider + private SonataLocaleProviderInterface $sonataLocaleProvider, ) { } diff --git a/src/Provider/RequestLocaleProvider.php b/src/Provider/RequestLocaleProvider.php index 3ca7c751..7c667438 100644 --- a/src/Provider/RequestLocaleProvider.php +++ b/src/Provider/RequestLocaleProvider.php @@ -22,7 +22,7 @@ final class RequestLocaleProvider implements LocaleProviderInterface public function __construct( private RequestStack $requestStack, - private string $defaultTranslationLocale + private string $defaultTranslationLocale, ) { } diff --git a/tests/Admin/Extension/AbstractTranslatableAdminExtensionTest.php b/tests/Admin/Extension/AbstractTranslatableAdminExtensionTest.php index ed6fcbda..176fd738 100644 --- a/tests/Admin/Extension/AbstractTranslatableAdminExtensionTest.php +++ b/tests/Admin/Extension/AbstractTranslatableAdminExtensionTest.php @@ -26,16 +26,11 @@ final class AbstractTranslatableAdminExtensionTest extends TestCase */ private AbstractTranslatableAdminExtension $extension; - private TranslatableChecker $translatableChecker; - - /** - * @psalm-suppress InternalClass https://github.com/vimeo/psalm/issues/6315 - */ protected function setUp(): void { - $this->translatableChecker = new TranslatableChecker(); + $translatableChecker = new TranslatableChecker(); - $localeProvider = new class() implements LocaleProviderInterface { + $localeProvider = new class implements LocaleProviderInterface { public function get(): string { return 'es'; @@ -44,7 +39,7 @@ public function get(): string $this->extension = new /** * @template-extends AbstractTranslatableAdminExtension - */ class($this->translatableChecker, $localeProvider) extends AbstractTranslatableAdminExtension {}; + */ class($translatableChecker, $localeProvider) extends AbstractTranslatableAdminExtension {}; } public function testSetsPersistentParameters(): void diff --git a/tests/Admin/Extension/Gedmo/TranslatableAdminExtensionTest.php b/tests/Admin/Extension/Gedmo/TranslatableAdminExtensionTest.php index f17446fc..a1323efc 100644 --- a/tests/Admin/Extension/Gedmo/TranslatableAdminExtensionTest.php +++ b/tests/Admin/Extension/Gedmo/TranslatableAdminExtensionTest.php @@ -60,7 +60,7 @@ protected function setUp(): void ->method('getManagerForClass') ->willReturn($this->em); - $localeProvider = new class() implements LocaleProviderInterface { + $localeProvider = new class implements LocaleProviderInterface { public function get(): string { return 'es'; diff --git a/tests/Admin/Extension/Knplabs/TranslatableAdminExtensionTest.php b/tests/Admin/Extension/Knplabs/TranslatableAdminExtensionTest.php index c627219e..ac5f1585 100644 --- a/tests/Admin/Extension/Knplabs/TranslatableAdminExtensionTest.php +++ b/tests/Admin/Extension/Knplabs/TranslatableAdminExtensionTest.php @@ -41,13 +41,17 @@ final class TranslatableAdminExtensionTest extends WebTestCase protected function setUp(): void { + if (!interface_exists(KnpTranslatableInterface::class)) { + static::markTestSkipped('The "knplabs/doctrine-behaviors" package is not installed.'); + } + $translatableChecker = new TranslatableChecker(); $translatableChecker->setSupportedInterfaces([ KnpTranslatableInterface::class, ]); - $localeProvider = new class() implements LocaleProviderInterface { + $localeProvider = new class implements LocaleProviderInterface { public function get(): string { return 'es'; diff --git a/tests/App/Admin/KnpCategoryAdmin.php b/tests/App/Admin/KnpCategoryAdmin.php index 398665ff..aa1ceabd 100644 --- a/tests/App/Admin/KnpCategoryAdmin.php +++ b/tests/App/Admin/KnpCategoryAdmin.php @@ -16,7 +16,7 @@ use Sonata\AdminBundle\Admin\AbstractAdmin; use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Form\FormMapper; -use Sonata\TranslationBundle\Tests\App\Entity\KnpCategory; +use Sonata\TranslationBundle\Tests\App\KnpEntity\KnpCategory; use Symfony\Component\Form\Extension\Core\Type\TextType; /** @@ -24,6 +24,16 @@ */ final class KnpCategoryAdmin extends AbstractAdmin { + protected function generateBaseRouteName(bool $isChildAdmin = false): string + { + return 'admin_app_knpcategory'; + } + + protected function generateBaseRoutePattern(bool $isChildAdmin = false): string + { + return 'tests/app/knpcategory'; + } + protected function configureListFields(ListMapper $list): void { $list diff --git a/tests/App/AppKernel.php b/tests/App/AppKernel.php index 61bdee1d..fa5fb334 100644 --- a/tests/App/AppKernel.php +++ b/tests/App/AppKernel.php @@ -41,7 +41,7 @@ final class AppKernel extends Kernel public function registerBundles(): iterable { - return [ + $bundles = [ new FrameworkBundle(), new KnpMenuBundle(), new SecurityBundle(), @@ -55,8 +55,13 @@ public function registerBundles(): iterable new SonataTwigBundle(), new SonataTranslationBundle(), new TwigBundle(), - new DoctrineBehaviorsBundle(), ]; + + if (class_exists(DoctrineBehaviorsBundle::class)) { + $bundles[] = new DoctrineBehaviorsBundle(); + } + + return $bundles; } public function getCacheDir(): string @@ -76,7 +81,7 @@ public function getProjectDir(): string protected function configureRoutes(RoutingConfigurator $routes): void { - $routes->import(sprintf('%s/config/routes.yaml', $this->getProjectDir())); + $routes->import(\sprintf('%s/config/routes.yaml', $this->getProjectDir())); } protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void @@ -94,41 +99,58 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa $loader->load(__DIR__.'/config/config_sonata_block_v4.yaml'); } + $mappings = [ + 'tests' => [ + 'type' => 'attribute', + 'dir' => '%kernel.project_dir%/Entity', + 'is_bundle' => false, + 'prefix' => 'Sonata\TranslationBundle\Tests\App\Entity', + ], + 'gedmo_translatable' => [ + 'type' => 'attribute', + 'prefix' => 'Gedmo\Translatable\Entity', + 'dir' => '%kernel.project_dir%/../../vendor/gedmo/doctrine-extensions/src/Translatable/Entity', + 'is_bundle' => false, + ], + ]; + + if (class_exists(DoctrineBehaviorsBundle::class)) { + $mappings['knp_translatable'] = [ + 'type' => 'attribute', + 'prefix' => 'Sonata\TranslationBundle\Tests\App\KnpEntity', + 'dir' => '%kernel.project_dir%/KnpEntity', + 'is_bundle' => false, + ]; + } + $container ->loadFromExtension('doctrine', [ 'dbal' => ['url' => '%env(resolve:DATABASE_URL)%'], 'orm' => [ 'auto_generate_proxy_classes' => true, 'auto_mapping' => true, - 'mappings' => [ - 'tests' => [ - 'type' => 'attribute', - 'dir' => '%kernel.project_dir%/Entity', - 'is_bundle' => false, - 'prefix' => 'Sonata\TranslationBundle\Tests\App\Entity', - ], - 'gedmo_translatable' => [ - 'type' => 'attribute', - 'prefix' => 'Gedmo\Translatable\Entity', - 'dir' => '%kernel.project_dir%/../../vendor/gedmo/doctrine-extensions/src/Translatable/Entity', - 'is_bundle' => false, - ], - ], + 'mappings' => $mappings, ], ]); + $extensionsConfig = [ + 'gedmo' => [ + 'enabled' => true, + 'translatable_listener_service' => 'app.gedmo.translation_listener', + ], + ]; + + if (class_exists(DoctrineBehaviorsBundle::class)) { + $extensionsConfig['knplabs'] = [ + 'enabled' => true, + ]; + } + $container ->loadFromExtension('sonata_translation', [ 'default_locale' => 'en', 'locales' => ['en', 'es', 'fr'], - 'gedmo' => [ - 'enabled' => true, - 'translatable_listener_service' => 'app.gedmo.translation_listener', - ], - 'knplabs' => [ - 'enabled' => true, - ], - ]); + ] + $extensionsConfig); $loader->load(__DIR__.'/config/services.php'); } diff --git a/tests/App/DataFixtures/KnpCategoryFixtures.php b/tests/App/DataFixtures/KnpCategoryFixtures.php index 179d5d45..8c431cb1 100644 --- a/tests/App/DataFixtures/KnpCategoryFixtures.php +++ b/tests/App/DataFixtures/KnpCategoryFixtures.php @@ -15,7 +15,8 @@ use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Persistence\ObjectManager; -use Sonata\TranslationBundle\Tests\App\Entity\KnpCategory; +use Knp\DoctrineBehaviors\DoctrineBehaviorsBundle; +use Sonata\TranslationBundle\Tests\App\KnpEntity\KnpCategory; final class KnpCategoryFixtures extends Fixture { @@ -23,6 +24,10 @@ final class KnpCategoryFixtures extends Fixture public function load(ObjectManager $manager): void { + if (!class_exists(DoctrineBehaviorsBundle::class)) { + return; + } + $novelCategory = new KnpCategory(self::CATEGORY, 'Novel'); $novelCategory->setCurrentLocale('es'); diff --git a/tests/App/Entity/GedmoCategory.php b/tests/App/Entity/GedmoCategory.php index 2f16aba8..720d77bf 100644 --- a/tests/App/Entity/GedmoCategory.php +++ b/tests/App/Entity/GedmoCategory.php @@ -30,7 +30,7 @@ public function __construct( private string $id = '', #[Gedmo\Translatable(fallback: true)] #[ORM\Column] - private string $name = '' + private string $name = '', ) { } diff --git a/tests/App/Entity/KnpCategory.php b/tests/App/KnpEntity/KnpCategory.php similarity index 94% rename from tests/App/Entity/KnpCategory.php rename to tests/App/KnpEntity/KnpCategory.php index 0dd3726e..dc951693 100644 --- a/tests/App/Entity/KnpCategory.php +++ b/tests/App/KnpEntity/KnpCategory.php @@ -11,7 +11,7 @@ * file that was distributed with this source code. */ -namespace Sonata\TranslationBundle\Tests\App\Entity; +namespace Sonata\TranslationBundle\Tests\App\KnpEntity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; @@ -28,7 +28,7 @@ public function __construct( #[ORM\Column] #[ORM\GeneratedValue(strategy: 'NONE')] private string $id = '', - string $name = '' + string $name = '', ) { $this->translations = new ArrayCollection(); $this->newTranslations = new ArrayCollection(); diff --git a/tests/App/Entity/KnpCategoryTranslation.php b/tests/App/KnpEntity/KnpCategoryTranslation.php similarity index 94% rename from tests/App/Entity/KnpCategoryTranslation.php rename to tests/App/KnpEntity/KnpCategoryTranslation.php index 0b97ddfe..ebb75617 100644 --- a/tests/App/Entity/KnpCategoryTranslation.php +++ b/tests/App/KnpEntity/KnpCategoryTranslation.php @@ -11,7 +11,7 @@ * file that was distributed with this source code. */ -namespace Sonata\TranslationBundle\Tests\App\Entity; +namespace Sonata\TranslationBundle\Tests\App\KnpEntity; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; diff --git a/tests/App/config/services.php b/tests/App/config/services.php index de1b3f3a..11f6f8dd 100644 --- a/tests/App/config/services.php +++ b/tests/App/config/services.php @@ -13,47 +13,63 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; +use Gedmo\Mapping\Driver\AttributeReader; use Gedmo\Translatable\TranslatableListener; +use Knp\DoctrineBehaviors\DoctrineBehaviorsBundle; +use Knp\DoctrineBehaviors\EventSubscriber\TranslatableEventSubscriber; use Knp\DoctrineBehaviors\Provider\UserProvider; use Sonata\TranslationBundle\Tests\App\Admin\GedmoCategoryAdmin; use Sonata\TranslationBundle\Tests\App\Admin\KnpCategoryAdmin; use Sonata\TranslationBundle\Tests\App\Entity\GedmoCategory; -use Sonata\TranslationBundle\Tests\App\Entity\KnpCategory; +use Sonata\TranslationBundle\Tests\App\KnpEntity\KnpCategory; use Sonata\TranslationBundle\Tests\App\Provider\DummyUserProvider; return static function (ContainerConfigurator $containerConfigurator): void { - $containerConfigurator->services() + $services = $containerConfigurator->services() ->defaults() ->autowire() ->autoconfigure() - ->load('Sonata\\TranslationBundle\\Tests\\App\\DataFixtures\\', \dirname(__DIR__).'/DataFixtures') + ->load('Sonata\\TranslationBundle\\Tests\\App\\DataFixtures\\', \dirname(__DIR__).'/DataFixtures'); - ->set(GedmoCategoryAdmin::class) - ->tag('sonata.admin', [ - 'manager_type' => 'orm', - 'model_class' => GedmoCategory::class, - 'label' => 'Gedmo Category', - ]) + $services->set(GedmoCategoryAdmin::class) + ->tag('sonata.admin', [ + 'manager_type' => 'orm', + 'model_class' => GedmoCategory::class, + 'label' => 'Gedmo Category', + ]) + + ->set('gedmo.mapping.driver.attribute', AttributeReader::class) + + ->set('app.gedmo.translation_listener', TranslatableListener::class) + ->call('setAnnotationReader', [service('gedmo.mapping.driver.attribute')]) + ->call('setDefaultLocale', [param('locale')]) + ->call('setTranslationFallback', [false]) + ->tag('doctrine.event_listener', ['event' => 'postLoad']) + ->tag('doctrine.event_listener', ['event' => 'postPersist']) + ->tag('doctrine.event_listener', ['event' => 'preFlush']) + ->tag('doctrine.event_listener', ['event' => 'onFlush']) + ->tag('doctrine.event_listener', ['event' => 'loadClassMetadata']); - ->set(KnpCategoryAdmin::class) + if (class_exists(DoctrineBehaviorsBundle::class)) { + $services->set(KnpCategoryAdmin::class) ->tag('sonata.admin', [ 'manager_type' => 'orm', 'model_class' => KnpCategory::class, 'label' => 'Knp Category', - ]) + ]); - ->set('app.gedmo.translation_listener', TranslatableListener::class) - ->call('setAnnotationReader', [service('annotation_reader')->nullOnInvalid()]) - ->call('setDefaultLocale', [param('locale')]) - ->call('setTranslationFallback', [false]) + // Temporary fix to decorate User Provider from KNP (see https://github.com/KnpLabs/DoctrineBehaviors/pull/727) + $services + ->set(DummyUserProvider::class) + ->decorate(UserProvider::class) + + // Temporary fix to use event listeners instead of subscriber (see https://github.com/KnpLabs/DoctrineBehaviors/pull/738) + ->set('app.knplabs.translation_listener', TranslatableEventSubscriber::class) + ->arg('$translatableFetchMode', 'LAZY') + ->arg('$translationFetchMode', 'LAZY') ->tag('doctrine.event_listener', ['event' => 'postLoad']) - ->tag('doctrine.event_listener', ['event' => 'postPersist']) - ->tag('doctrine.event_listener', ['event' => 'preFlush']) - ->tag('doctrine.event_listener', ['event' => 'onFlush']) ->tag('doctrine.event_listener', ['event' => 'loadClassMetadata']) - - // Temporary fix to decorate User Provider from KNP (see https://github.com/KnpLabs/DoctrineBehaviors/pull/727) - ->set(DummyUserProvider::class) - ->decorate(UserProvider::class); + ->tag('doctrine.event_listener', ['event' => 'prePersist']); + } }; diff --git a/tests/Checker/TranslatableCheckerForKnpTest.php b/tests/Checker/TranslatableCheckerForKnpTest.php index 656569cf..8edb9f70 100644 --- a/tests/Checker/TranslatableCheckerForKnpTest.php +++ b/tests/Checker/TranslatableCheckerForKnpTest.php @@ -23,6 +23,13 @@ */ final class TranslatableCheckerForKnpTest extends TestCase { + protected function setUp(): void + { + if (!interface_exists(KnpTranslatableInterface::class)) { + static::markTestSkipped('The "knplabs/doctrine-behaviors" package is not installed.'); + } + } + public function testIsTranslatableOnInterface(): void { $translatableChecker = new TranslatableChecker(); diff --git a/tests/Functional/GedmoCRUDTest.php b/tests/Functional/GedmoCRUDTest.php index 0980e1fe..68fe9256 100644 --- a/tests/Functional/GedmoCRUDTest.php +++ b/tests/Functional/GedmoCRUDTest.php @@ -71,7 +71,7 @@ public function testCreate(string $locale, string $newName): void self::assertSelectorTextContains( '.alert-success', - sprintf('"%s" has been successfully created.', $newName) + \sprintf('"%s" has been successfully created.', $newName) ); $url = $this->generateUrlWithLocale('/admin/tests/app/gedmocategory/list', $locale); @@ -79,7 +79,7 @@ public function testCreate(string $locale, string $newName): void $this->client->request(Request::METHOD_GET, $url); self::assertSelectorTextContains( - sprintf('.sonata-ba-list-field-string[objectid="%s"] .sonata-link-identifier', $newId), + \sprintf('.sonata-ba-list-field-string[objectid="%s"] .sonata-link-identifier', $newId), $newName ); } @@ -112,7 +112,7 @@ public function testEdit(string $locale, string $editedName): void self::assertSelectorTextContains( '.alert-success', - sprintf('"%s" has been successfully updated.', $editedName) + \sprintf('"%s" has been successfully updated.', $editedName) ); $url = $this->generateUrlWithLocale('/admin/tests/app/gedmocategory/list', $locale); diff --git a/tests/Functional/KnpCRUDTest.php b/tests/Functional/KnpCRUDTest.php index 66833ee1..3794c6a5 100644 --- a/tests/Functional/KnpCRUDTest.php +++ b/tests/Functional/KnpCRUDTest.php @@ -13,6 +13,7 @@ namespace Sonata\TranslationBundle\Tests\Functional; +use Knp\DoctrineBehaviors\DoctrineBehaviorsBundle; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\HttpFoundation\Request; @@ -23,6 +24,10 @@ final class KnpCRUDTest extends WebTestCase protected function setUp(): void { + if (!class_exists(DoctrineBehaviorsBundle::class)) { + static::markTestSkipped('The "knplabs/doctrine-behaviors" package is not installed.'); + } + $this->client = static::createClient(); $this->client->followRedirects(); } @@ -71,7 +76,7 @@ public function testCreate(string $locale, string $newName): void self::assertSelectorTextContains( '.alert-success', - sprintf('"%s" has been successfully created.', $newName) + \sprintf('"%s" has been successfully created.', $newName) ); $url = $this->generateUrlWithLocale('/admin/tests/app/knpcategory/list', $locale); @@ -79,7 +84,7 @@ public function testCreate(string $locale, string $newName): void $this->client->request(Request::METHOD_GET, $url); self::assertSelectorTextContains( - sprintf('.sonata-ba-list-field-string[objectid="%s"] .sonata-link-identifier', $newId), + \sprintf('.sonata-ba-list-field-string[objectid="%s"] .sonata-link-identifier', $newId), $newName ); } @@ -112,7 +117,7 @@ public function testEdit(string $locale, string $editedName): void self::assertSelectorTextContains( '.alert-success', - sprintf('"%s" has been successfully updated.', $editedName) + \sprintf('"%s" has been successfully updated.', $editedName) ); $url = $this->generateUrlWithLocale('/admin/tests/app/knpcategory/list', $locale); diff --git a/tests/Provider/Knplabs/LocaleProviderTest.php b/tests/Provider/Knplabs/LocaleProviderTest.php index 3f96fcdc..e0577350 100644 --- a/tests/Provider/Knplabs/LocaleProviderTest.php +++ b/tests/Provider/Knplabs/LocaleProviderTest.php @@ -14,6 +14,7 @@ namespace Sonata\TranslationBundle\Tests\Provider\Knplabs; use Knp\DoctrineBehaviors\Contract\Provider\LocaleProviderInterface; +use Knp\DoctrineBehaviors\DoctrineBehaviorsBundle; use PHPUnit\Framework\TestCase; use Sonata\TranslationBundle\Provider\Knplabs\LocaleProvider; use Sonata\TranslationBundle\Provider\LocaleProviderInterface as SonataLocaleProviderInterface; @@ -22,6 +23,13 @@ final class LocaleProviderTest extends TestCase { + protected function setUp(): void + { + if (!class_exists(DoctrineBehaviorsBundle::class)) { + static::markTestSkipped('The "knplabs/doctrine-behaviors" package is not installed.'); + } + } + /** * @dataProvider provideUsesTheProperProviderBasedOnRequestCases */ @@ -29,7 +37,7 @@ public function testUsesTheProperProviderBasedOnRequest( string $expectedLocale, string $knpLocale, string $sonataLocale, - bool $isAdminEnabled + bool $isAdminEnabled, ): void { $request = new Request(); diff --git a/tests/Traits/DoctrineOrmTestCase.php b/tests/Traits/DoctrineOrmTestCase.php index b8a7d6e7..76ba8dfb 100644 --- a/tests/Traits/DoctrineOrmTestCase.php +++ b/tests/Traits/DoctrineOrmTestCase.php @@ -38,6 +38,8 @@ abstract class DoctrineOrmTestCase extends TestCase * EntityManager mock object together with * annotation mapping driver and pdo_sqlite * database in memory. + * + * @psalm-suppress ArgumentTypeCoercion */ final protected function getMockSqliteEntityManager(?EventManager $evm = null): EntityManager { diff --git a/tests/Traits/KnplabsOrmTest.php b/tests/Traits/KnplabsOrmTest.php index f40952e8..c2a699a5 100644 --- a/tests/Traits/KnplabsOrmTest.php +++ b/tests/Traits/KnplabsOrmTest.php @@ -16,6 +16,7 @@ use Doctrine\Common\EventManager; use Doctrine\ORM\EntityManager; use Gedmo\Translatable\TranslatableListener; +use Knp\DoctrineBehaviors\DoctrineBehaviorsBundle; use Sonata\AdminBundle\Filter\Model\FilterData; use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; use Sonata\TranslationBundle\Enum\TranslationFilterMode; @@ -31,8 +32,6 @@ final class KnplabsOrmTest extends DoctrineOrmTestCase private const ARTICLE = Article::class; private const TRANSLATION = ArticleTranslation::class; - private TranslatableListener $translatableListener; - private EntityManager $em; protected function setUp(): void @@ -43,11 +42,15 @@ protected function setUp(): void static::markTestSkipped('Doctrine ORM is not available.'); } + if (!class_exists(DoctrineBehaviorsBundle::class)) { + static::markTestSkipped('The "knplabs/doctrine-behaviors" package is not installed.'); + } + $evm = new EventManager(); - $this->translatableListener = new TranslatableListener(); - $this->translatableListener->setTranslatableLocale('en'); - $this->translatableListener->setDefaultLocale('en'); - $evm->addEventSubscriber($this->translatableListener); + $translatableListener = new TranslatableListener(); + $translatableListener->setTranslatableLocale('en'); + $translatableListener->setDefaultLocale('en'); + $evm->addEventSubscriber($translatableListener); $this->em = $this->getMockSqliteEntityManager($evm); }