From 35ba29cefdfa18778f5273e1dc7c156f43d92340 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 9 Aug 2021 10:28:16 +0200 Subject: [PATCH] Bump min PHP version to 8.0 and update deps (#646) --- .github/workflows/code_checks.yaml | 2 +- .github/workflows/linter.yaml | 2 +- .github/workflows/tests.yaml | 2 - composer.json | 22 +- ecs.php | 8 +- phpstan.neon | 3 + rector.php | 27 +-- src/Bundle/DoctrineBehaviorsBundle.php | 14 -- src/Model/Blameable/BlameableTrait.php | 1 + src/Model/Sluggable/SluggableTrait.php | 1 + .../SoftDeletable/SoftDeletableTrait.php | 1 + .../Timestampable/TimestampableTrait.php | 1 + .../Translatable/TranslatableMethodsTrait.php | 8 +- src/Model/Translatable/TranslatableTrait.php | 1 + src/Model/Translatable/TranslationTrait.php | 1 + src/Model/Tree/TreeNodeMethodsTrait.php | 10 +- src/Model/Tree/TreeNodeTrait.php | 1 + src/Model/Uuidable/UuidableMethodsTrait.php | 5 + src/Model/Uuidable/UuidableTrait.php | 1 + src/Provider/LocaleProvider.php | 7 +- ...SluggableTranslatableEntityTranslation.php | 1 + .../Source/SomeClassForMetadata.php | 10 + .../TimestampableMakeEntityTest.php | 4 +- tests/ORM/Translatable/TranslatableTest.php | 195 +++++++++--------- 24 files changed, 174 insertions(+), 154 deletions(-) delete mode 100644 src/Bundle/DoctrineBehaviorsBundle.php create mode 100644 tests/ORM/Timestampable/Source/SomeClassForMetadata.php diff --git a/.github/workflows/code_checks.yaml b/.github/workflows/code_checks.yaml index 5bdd43ae..62984c4e 100644 --- a/.github/workflows/code_checks.yaml +++ b/.github/workflows/code_checks.yaml @@ -30,7 +30,7 @@ jobs: - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v1 with: - php-version: 7.3 + php-version: 8.0 extensions: pdo_sqlite, pdo_mysql, pdo_pgsql coverage: none diff --git a/.github/workflows/linter.yaml b/.github/workflows/linter.yaml index fe1d1f6a..f9cbe802 100644 --- a/.github/workflows/linter.yaml +++ b/.github/workflows/linter.yaml @@ -15,7 +15,7 @@ jobs: - uses: shivammathur/setup-php@v1 with: - php-version: 7.3 + php-version: 8.0 extensions: pdo_sqlite, pdo_mysql, pdo_pgsql coverage: none diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 6d736806..b536d238 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -8,8 +8,6 @@ jobs: strategy: matrix: php: - - 7.3 - - 7.4 - 8.0 name: Test ${{ matrix.php }} diff --git a/composer.json b/composer.json index 832c79ac..c07ff7f4 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ { "name": "Knplabs", "homepage": "http://knplabs.com" } ], "require": { - "php": ">=7.3", + "php": ">=8.0", "doctrine/common": "^3.0", "doctrine/persistence": "^2.0", "doctrine/dbal": "^2.9", @@ -35,17 +35,17 @@ "doctrine/annotations": "^1.12", "doctrine/doctrine-bundle": "^2.2", "php-parallel-lint/php-parallel-lint": "^1.3", - "phpstan/phpstan": "^0.12.83", + "phpstan/phpstan": "^0.12.84", "phpunit/phpunit": "^9.5", - "rector/rector-prefixed": "^0.10.4", - "symplify/easy-coding-standard": "^9.2.18", - "symplify/phpstan-extensions": "^9.2.18", - "phpstan/phpstan-doctrine": "^0.12.33", - "phpstan/phpstan-phpunit": "^0.12.18", - "symplify/package-builder": "^9.2.18", - "symplify/phpstan-rules": "^9.2.18", - "symplify/config-transformer": "^9.2", - "symplify/amnesia": "^9.2", + "rector/rector": "^0.11.45", + "symplify/easy-coding-standard": "^9.4", + "symplify/phpstan-extensions": "^9.4", + "phpstan/phpstan-doctrine": "^0.12.42", + "phpstan/phpstan-phpunit": "^0.12", + "symplify/package-builder": "^9.4", + "symplify/phpstan-rules": "^9.4", + "symplify/config-transformer": "^9.4", + "symplify/amnesia": "^9.4", "phpstan/extension-installer": "^1.1" }, "autoload": { diff --git a/ecs.php b/ecs.php index 1312f9eb..0c5a31b8 100644 --- a/ecs.php +++ b/ecs.php @@ -2,7 +2,6 @@ declare(strict_types=1); -use PhpCsFixer\Fixer\ClassNotation\FinalClassFixer; use PhpCsFixer\Fixer\Comment\HeaderCommentFixer; use PhpCsFixer\Fixer\Import\OrderedImportsFixer; use PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixer; @@ -13,6 +12,11 @@ use Symplify\EasyCodingStandard\ValueObject\Set\SetList; return static function (ContainerConfigurator $containerConfigurator): void { + $containerConfigurator->import(SetList::PSR_12); + $containerConfigurator->import(SetList::SYMPLIFY); + $containerConfigurator->import(SetList::COMMON); + $containerConfigurator->import(SetList::CLEAN_CODE); + $parameters = $containerConfigurator->parameters(); $parameters->set(Option::PATHS, [ @@ -24,8 +28,6 @@ __DIR__ . '/rector.php', ]); - $parameters->set(Option::SETS, [SetList::PSR_12, SetList::SYMPLIFY, SetList::COMMON, SetList::CLEAN_CODE]); - $parameters->set(Option::SKIP, [ UnaryOperatorSpacesFixer::class, PhpUnitStrictFixer::class => [__DIR__ . '/tests/ORM/Timestampable/TimestampableTest.php'], diff --git a/phpstan.neon b/phpstan.neon index 821e6218..e3114677 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -101,3 +101,6 @@ parameters: - message: '#Use dependency injection instead of dependency juggling#' path: tests/DatabaseLoader.php + + # known retype + - '#Cannot cast array\|bool\|float\|int\|string\|null to string#' diff --git a/rector.php b/rector.php index 995ad4bf..cad0acd2 100644 --- a/rector.php +++ b/rector.php @@ -3,7 +3,7 @@ declare(strict_types=1); use Rector\Core\Configuration\Option; -use Rector\DeadCode\Rector\Class_\RemoveUnusedDoctrineEntityMethodAndPropertyRector; +use Rector\Nette\Set\NetteSetList; use Rector\Set\ValueObject\SetList; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; @@ -12,20 +12,13 @@ $parameters->set(Option::PATHS, [__DIR__ . '/src', __DIR__ . '/tests', __DIR__ . '/utils']); - $parameters->set(Option::SETS, [ - SetList::DEAD_CODE, - SetList::CODE_QUALITY, - SetList::CODING_STYLE, - SetList::NETTE_UTILS_CODE_QUALITY, - SetList::NAMING, - SetList::PHP_70, - SetList::PHP_71, - SetList::PHP_72, - SetList::PHP_73, - ]); - - $parameters->set(Option::SKIP, [ - RemoveUnusedDoctrineEntityMethodAndPropertyRector::class, - __DIR__ . '/src/Model/Translatable/TranslatableMethodsTrait.php', - ]); + $containerConfigurator->import(SetList::DEAD_CODE); + $containerConfigurator->import(SetList::CODE_QUALITY); + $containerConfigurator->import(SetList::CODING_STYLE); + $containerConfigurator->import(NetteSetList::NETTE_CODE_QUALITY); + $containerConfigurator->import(SetList::NAMING); + $containerConfigurator->import(SetList::PHP_70); + $containerConfigurator->import(SetList::PHP_71); + $containerConfigurator->import(SetList::PHP_72); + $containerConfigurator->import(SetList::PHP_73); }; diff --git a/src/Bundle/DoctrineBehaviorsBundle.php b/src/Bundle/DoctrineBehaviorsBundle.php deleted file mode 100644 index 0cddee0d..00000000 --- a/src/Bundle/DoctrineBehaviorsBundle.php +++ /dev/null @@ -1,14 +0,0 @@ -currentLocale ?: $this->getDefaultLocale(); } - /** - * @param mixed $locale the default locale - */ - public function setDefaultLocale($locale): void + public function setDefaultLocale(string $locale): void { $this->defaultLocale = $locale; } @@ -223,7 +220,8 @@ private function ensureIsIterableOrCollection($translations): void return; } - throw new TranslatableException(sprintf('$translations parameter must be iterable or %s', Collection::class) + throw new TranslatableException( + sprintf('$translations parameter must be iterable or %s', Collection::class) ); } diff --git a/src/Model/Translatable/TranslatableTrait.php b/src/Model/Translatable/TranslatableTrait.php index 83f6d9fb..60d56300 100644 --- a/src/Model/Translatable/TranslatableTrait.php +++ b/src/Model/Translatable/TranslatableTrait.php @@ -7,5 +7,6 @@ trait TranslatableTrait { use TranslatablePropertiesTrait; + use TranslatableMethodsTrait; } diff --git a/src/Model/Translatable/TranslationTrait.php b/src/Model/Translatable/TranslationTrait.php index 765aa519..269cc31c 100644 --- a/src/Model/Translatable/TranslationTrait.php +++ b/src/Model/Translatable/TranslationTrait.php @@ -7,5 +7,6 @@ trait TranslationTrait { use TranslationPropertiesTrait; + use TranslationMethodsTrait; } diff --git a/src/Model/Tree/TreeNodeMethodsTrait.php b/src/Model/Tree/TreeNodeMethodsTrait.php index 2cd1171e..772198c8 100644 --- a/src/Model/Tree/TreeNodeMethodsTrait.php +++ b/src/Model/Tree/TreeNodeMethodsTrait.php @@ -8,6 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Knp\DoctrineBehaviors\Contract\Entity\TreeNodeInterface; +use Knp\DoctrineBehaviors\Exception\ShouldNotHappenException; use Knp\DoctrineBehaviors\Exception\TreeException; use Nette\Utils\Json; @@ -54,7 +55,7 @@ public function getParentMaterializedPath(): string return static::getMaterializedPathSeparator() . implode(static::getMaterializedPathSeparator(), $path); } - public function setParentMaterializedPath($path): void + public function setParentMaterializedPath(string $path): void { $this->parentNodePath = $path; } @@ -273,7 +274,12 @@ public function offsetGet($offset) */ protected function getExplodedPath(): array { - $path = explode(static::getMaterializedPathSeparator(), $this->getRealMaterializedPath()); + $separator = static::getMaterializedPathSeparator(); + if ($separator === '') { + throw new ShouldNotHappenException(); + } + + $path = explode($separator, $this->getRealMaterializedPath()); return array_filter($path, function ($item) { return $item !== ''; diff --git a/src/Model/Tree/TreeNodeTrait.php b/src/Model/Tree/TreeNodeTrait.php index a3b6e935..bf0f0ab1 100644 --- a/src/Model/Tree/TreeNodeTrait.php +++ b/src/Model/Tree/TreeNodeTrait.php @@ -7,5 +7,6 @@ trait TreeNodeTrait { use TreeNodeMethodsTrait; + use TreeNodePropertiesTrait; } diff --git a/src/Model/Uuidable/UuidableMethodsTrait.php b/src/Model/Uuidable/UuidableMethodsTrait.php index b03c0196..7de6cb48 100644 --- a/src/Model/Uuidable/UuidableMethodsTrait.php +++ b/src/Model/Uuidable/UuidableMethodsTrait.php @@ -4,6 +4,7 @@ namespace Knp\DoctrineBehaviors\Model\Uuidable; +use Knp\DoctrineBehaviors\Exception\ShouldNotHappenException; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; @@ -17,6 +18,10 @@ public function setUuid(UuidInterface $uuid): void public function getUuid(): ?UuidInterface { if (is_string($this->uuid)) { + if ($this->uuid === '') { + throw new ShouldNotHappenException(); + } + return Uuid::fromString($this->uuid); } diff --git a/src/Model/Uuidable/UuidableTrait.php b/src/Model/Uuidable/UuidableTrait.php index a2f685a2..809c0d1b 100644 --- a/src/Model/Uuidable/UuidableTrait.php +++ b/src/Model/Uuidable/UuidableTrait.php @@ -7,5 +7,6 @@ trait UuidableTrait { use UuidablePropertiesTrait; + use UuidableMethodsTrait; } diff --git a/src/Provider/LocaleProvider.php b/src/Provider/LocaleProvider.php index e4efe503..6407ce2a 100644 --- a/src/Provider/LocaleProvider.php +++ b/src/Provider/LocaleProvider.php @@ -8,6 +8,7 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Contracts\Translation\LocaleAwareInterface; use Symfony\Contracts\Translation\TranslatorInterface; @@ -42,7 +43,7 @@ public function __construct( public function provideCurrentLocale(): ?string { $currentRequest = $this->requestStack->getCurrentRequest(); - if ($currentRequest === null) { + if (! $currentRequest instanceof Request) { return null; } @@ -67,10 +68,10 @@ public function provideFallbackLocale(): ?string try { if ($this->parameterBag->has('locale')) { - return $this->parameterBag->get('locale'); + return (string) $this->parameterBag->get('locale'); } - return $this->parameterBag->get('kernel.default_locale'); + return (string) $this->parameterBag->get('kernel.default_locale'); } catch (ParameterNotFoundException | InvalidArgumentException $parameterNotFoundException) { return null; } diff --git a/tests/Fixtures/Entity/Sluggable/SluggableTranslatableEntityTranslation.php b/tests/Fixtures/Entity/Sluggable/SluggableTranslatableEntityTranslation.php index 17cd3de6..bdff85d1 100644 --- a/tests/Fixtures/Entity/Sluggable/SluggableTranslatableEntityTranslation.php +++ b/tests/Fixtures/Entity/Sluggable/SluggableTranslatableEntityTranslation.php @@ -16,6 +16,7 @@ class SluggableTranslatableEntityTranslation implements TranslationInterface, SluggableInterface { use SluggableTrait; + use TranslationTrait; /** diff --git a/tests/ORM/Timestampable/Source/SomeClassForMetadata.php b/tests/ORM/Timestampable/Source/SomeClassForMetadata.php new file mode 100644 index 00000000..93db3c52 --- /dev/null +++ b/tests/ORM/Timestampable/Source/SomeClassForMetadata.php @@ -0,0 +1,10 @@ +entityManager); $doctrineEventManager = $this->entityManager->getEventManager(); diff --git a/tests/ORM/Translatable/TranslatableTest.php b/tests/ORM/Translatable/TranslatableTest.php index 594869f4..f75492e1 100644 --- a/tests/ORM/Translatable/TranslatableTest.php +++ b/tests/ORM/Translatable/TranslatableTest.php @@ -31,49 +31,50 @@ protected function setUp(): void public function testShouldPersistTranslations(): void { - $entity = new TranslatableEntity(); - $entity->translate('fr') + $translatableEntity = new TranslatableEntity(); + $translatableEntity->translate('fr') ->setTitle('fabuleux'); - $entity->translate('en') + $translatableEntity->translate('en') ->setTitle('awesome'); - $entity->translate('ru') + $translatableEntity->translate('ru') ->setTitle('удивительный'); - $entity->mergeNewTranslations(); + $translatableEntity->mergeNewTranslations(); - $this->entityManager->persist($entity); + $this->entityManager->persist($translatableEntity); $this->entityManager->flush(); - $id = $entity->getId(); + $id = $translatableEntity->getId(); $this->entityManager->clear(); - /** @var TranslatableEntity $entity */ - $entity = $this->translatableRepository->find($id); + /** @var TranslatableEntity $translatableEntity */ + $translatableEntity = $this->translatableRepository->find($id); - $this->assertSame('fabuleux', $entity->translate('fr')->getTitle()); - $this->assertSame('awesome', $entity->translate('en')->getTitle()); - $this->assertSame('удивительный', $entity->translate('ru')->getTitle()); + $this->assertSame('fabuleux', $translatableEntity->translate('fr')->getTitle()); + $this->assertSame('awesome', $translatableEntity->translate('en')->getTitle()); + $this->assertSame('удивительный', $translatableEntity->translate('ru')->getTitle()); } public function testShouldFallbackCountryLocaleToLanguageOnlyTranslation(): void { - $entity = new TranslatableEntity(); - $entity->translate('en', false) + $translatableEntity = new TranslatableEntity(); + $translatableEntity->translate('en', false) ->setTitle('plastic bag'); - $entity->translate('fr', false) + $translatableEntity->translate('fr', false) ->setTitle('sac plastique'); - $entity->translate('fr_CH', false) + $translatableEntity->translate('fr_CH', false) ->setTitle('cornet'); - $entity->mergeNewTranslations(); + $translatableEntity->mergeNewTranslations(); - $this->entityManager->persist($entity); + $this->entityManager->persist($translatableEntity); $this->entityManager->flush(); - $id = $entity->getId(); + $id = $translatableEntity->getId(); $this->entityManager->clear(); - /** @var TranslatableEntity $entity */ $entity = $this->translatableRepository->find($id); + $this->assertInstanceOf(TranslatableEntity::class, $entity); + /** @var TranslatableEntity $entity */ $this->assertSame('plastic bag', $entity->translate('de')->getTitle()); $this->assertSame('sac plastique', $entity->translate('fr_FR')->getTitle()); $this->assertSame('cornet', $entity->translate('fr_CH')->getTitle()); @@ -81,64 +82,64 @@ public function testShouldFallbackCountryLocaleToLanguageOnlyTranslation(): void public function testShouldFallbackToDefaultLocaleIfNoCountryLocaleTranslation(): void { - $entity = new TranslatableEntity(); - $entity->translate('en', false) + $translatableEntity = new TranslatableEntity(); + $translatableEntity->translate('en', false) ->setTitle('plastic bag'); - $entity->translate('fr_CH', false) + $translatableEntity->translate('fr_CH', false) ->setTitle('cornet'); - $entity->mergeNewTranslations(); + $translatableEntity->mergeNewTranslations(); - $this->entityManager->persist($entity); + $this->entityManager->persist($translatableEntity); $this->entityManager->flush(); - $id = $entity->getId(); + $id = $translatableEntity->getId(); $this->entityManager->clear(); - /** @var TranslatableEntity $entity */ - $entity = $this->translatableRepository->find($id); + /** @var TranslatableEntity $translatableEntity */ + $translatableEntity = $this->translatableRepository->find($id); - $this->assertSame('plastic bag', $entity->translate('de')->getTitle()); - $this->assertSame('plastic bag', $entity->translate('fr_FR')->getTitle()); - $this->assertSame('cornet', $entity->translate('fr_CH')->getTitle()); + $this->assertSame('plastic bag', $translatableEntity->translate('de')->getTitle()); + $this->assertSame('plastic bag', $translatableEntity->translate('fr_FR')->getTitle()); + $this->assertSame('cornet', $translatableEntity->translate('fr_CH')->getTitle()); } public function testShouldUpdateAndAddNewTranslations(): void { - $entity = new TranslatableEntity(); - $entity->translate('en') + $translatableEntity = new TranslatableEntity(); + $translatableEntity->translate('en') ->setTitle('awesome'); - $entity->translate('ru') + $translatableEntity->translate('ru') ->setTitle('удивительный'); - $entity->mergeNewTranslations(); + $translatableEntity->mergeNewTranslations(); - $this->entityManager->persist($entity); + $this->entityManager->persist($translatableEntity); $this->entityManager->flush(); - $id = $entity->getId(); + $id = $translatableEntity->getId(); $this->entityManager->clear(); - /** @var TranslatableEntity $entity */ - $entity = $this->translatableRepository->find($id); + /** @var TranslatableEntity $translatableEntity */ + $translatableEntity = $this->translatableRepository->find($id); - $this->assertSame('awesome', $entity->translate('en')->getTitle()); - $this->assertSame('удивительный', $entity->translate('ru')->getTitle()); + $this->assertSame('awesome', $translatableEntity->translate('en')->getTitle()); + $this->assertSame('удивительный', $translatableEntity->translate('ru')->getTitle()); - $entity->translate('en') + $translatableEntity->translate('en') ->setTitle('great'); - $entity->translate('fr', false) + $translatableEntity->translate('fr', false) ->setTitle('fabuleux'); - $entity->mergeNewTranslations(); + $translatableEntity->mergeNewTranslations(); - $this->entityManager->persist($entity); + $this->entityManager->persist($translatableEntity); $this->entityManager->flush(); $this->entityManager->clear(); - /** @var TranslatableEntity $entity */ - $entity = $this->translatableRepository->find($id); + /** @var TranslatableEntity $translatableEntity */ + $translatableEntity = $this->translatableRepository->find($id); - $this->assertSame('great', $entity->translate('en')->getTitle()); - $this->assertSame('fabuleux', $entity->translate('fr')->getTitle()); - $this->assertSame('удивительный', $entity->translate('ru')->getTitle()); + $this->assertSame('great', $translatableEntity->translate('en')->getTitle()); + $this->assertSame('fabuleux', $translatableEntity->translate('fr')->getTitle()); + $this->assertSame('удивительный', $translatableEntity->translate('ru')->getTitle()); } public function testTranslateMethodShouldAlwaysReturnTranslationObject(): void @@ -150,51 +151,50 @@ public function testTranslateMethodShouldAlwaysReturnTranslationObject(): void public function testSubscriberShouldConfigureEntityWithCurrentLocale(): void { - $entity = new TranslatableEntity(); - + $translatableEntity = new TranslatableEntity(); // magic method - $entity->setTitle('test'); + $translatableEntity->setTitle('test'); - $entity->mergeNewTranslations(); + $translatableEntity->mergeNewTranslations(); - $this->entityManager->persist($entity); + $this->entityManager->persist($translatableEntity); $this->entityManager->flush(); - $id = $entity->getId(); + $id = $translatableEntity->getId(); $this->entityManager->clear(); - /** @var TranslatableEntity $entity */ - $entity = $this->translatableRepository->find($id); + /** @var TranslatableEntity $translatableEntity */ + $translatableEntity = $this->translatableRepository->find($id); - $this->assertSame('en', $entity->getCurrentLocale()); - $this->assertSame('test', $entity->getTitle()); - $this->assertSame('test', $entity->translate($entity->getCurrentLocale())->getTitle()); + $this->assertSame('en', $translatableEntity->getCurrentLocale()); + $this->assertSame('test', $translatableEntity->getTitle()); + $this->assertSame('test', $translatableEntity->translate($translatableEntity->getCurrentLocale())->getTitle()); } public function testSubscriberShouldConfigureEntityWithDefaultLocale(): void { - $entity = new TranslatableEntity(); + $translatableEntity = new TranslatableEntity(); // magic method - $entity->setTitle('test'); - $entity->mergeNewTranslations(); + $translatableEntity->setTitle('test'); + $translatableEntity->mergeNewTranslations(); - $this->entityManager->persist($entity); + $this->entityManager->persist($translatableEntity); $this->entityManager->flush(); - $id = $entity->getId(); + $id = $translatableEntity->getId(); $this->entityManager->clear(); - /** @var TranslatableEntity $entity */ - $entity = $this->translatableRepository->find($id); + /** @var TranslatableEntity $translatableEntity */ + $translatableEntity = $this->translatableRepository->find($id); - $this->assertSame('en', $entity->getDefaultLocale()); + $this->assertSame('en', $translatableEntity->getDefaultLocale()); // magic method - $this->assertSame('test', $entity->getTitle()); + $this->assertSame('test', $translatableEntity->getTitle()); - $this->assertSame('test', $entity->translate($entity->getDefaultLocale())->getTitle()); - $this->assertSame('test', $entity->translate('fr')->getTitle()); + $this->assertSame('test', $translatableEntity->translate($translatableEntity->getDefaultLocale())->getTitle()); + $this->assertSame('test', $translatableEntity->translate('fr')->getTitle()); } public function testShouldHaveOneToManyRelation(): void @@ -258,23 +258,27 @@ public function testSetTranslations(): void public function testShouldNotPersistNewEmptyTranslations(): void { - $entity = new TranslatableEntity(); - $entity->translate('fr') + $translatableEntity = new TranslatableEntity(); + $translatableEntity->translate('fr') ->setTitle('fabuleux'); - $entity->translate('en') + $translatableEntity->translate('en') ->setTitle(''); - $entity->translate('ru') + $translatableEntity->translate('ru') ->setTitle('удивительный'); - $entity->mergeNewTranslations(); - $this->entityManager->persist($entity); + $translatableEntity->mergeNewTranslations(); + + $this->entityManager->persist($translatableEntity); $this->entityManager->flush(); - $id = $entity->getId(); + $id = $translatableEntity->getId(); $this->entityManager->clear(); $entity = $this->translatableRepository->find($id); + $this->assertIsObject($entity); + $this->assertInstanceOf(TranslatableEntity::class, $entity); + /** @var TranslatableEntity $entity */ $this->assertSame('fabuleux', $entity->translate('fr')->getTitle()); // empty English translation @@ -285,33 +289,38 @@ public function testShouldNotPersistNewEmptyTranslations(): void public function testShouldRemoveTranslationsWhichBecomeEmpty(): void { - $entity = new TranslatableEntity(); - $entity->translate('fr') + $translatableEntity = new TranslatableEntity(); + $translatableEntity->translate('fr') ->setTitle('fabuleux'); - $entity->translate('en') + $translatableEntity->translate('en') ->setTitle('awesome'); - $entity->translate('ru') + $translatableEntity->translate('ru') ->setTitle('удивительный'); - $entity->mergeNewTranslations(); - $this->entityManager->persist($entity); + $translatableEntity->mergeNewTranslations(); + + $this->entityManager->persist($translatableEntity); $this->entityManager->flush(); - $entity->translate('en') + $translatableEntity->translate('en') ->setTitle(''); - $entity->mergeNewTranslations(); + $translatableEntity->mergeNewTranslations(); - $this->entityManager->persist($entity); + $this->entityManager->persist($translatableEntity); $this->entityManager->flush(); - $id = $entity->getId(); + $id = $translatableEntity->getId(); $this->entityManager->clear(); - $entity = $this->translatableRepository->find($id); + $translatableEntity = $this->translatableRepository->find($id); - $this->assertSame('fabuleux', $entity->translate('fr')->getTitle()); - $this->assertNull($entity->translate('en')->getTitle()); - $this->assertSame('удивительный', $entity->translate('ru')->getTitle()); + $this->assertIsObject($translatableEntity); + $this->assertInstanceOf(TranslatableEntity::class, $translatableEntity); + + /** @var TranslatableEntity $translatableEntity */ + $this->assertSame('fabuleux', $translatableEntity->translate('fr')->getTitle()); + $this->assertNull($translatableEntity->translate('en')->getTitle()); + $this->assertSame('удивительный', $translatableEntity->translate('ru')->getTitle()); } public function testPhpStanExtensionOnInterfaces(): void