/** * @return string[] */ private function getLocales() { $locales = $this->localeContext->getLocales(); if (($fallbackLocale = $this->localeContext->getFallbackLocale()) !== null) { $locales[] = $fallbackLocale; } return $locales; }
/** * {@inheritdoc} */ public function create(array $options = []) { $translatable = parent::create($options); $translatable->setLocales($this->localeContext->getLocales()); $translatable->setFallbackLocale($this->localeContext->getFallbackLocale()); $translatable->setTranslationClass($this->resource->getTranslation()->getModel()); return $translatable; }
public function testCreateWithoutOptions() { $this->resource->expects($this->once())->method('getModel')->will($this->returnValue($translatableClass = TranslatableTest::class)); $this->localeContext->expects($this->once())->method('getLocales')->will($this->returnValue($locales = ['fr', 'es'])); $this->localeContext->expects($this->once())->method('getFallbackLocale')->will($this->returnValue($fallbackLocale = 'en')); $translatable = $this->factory->create(); $this->assertInstanceOf($translatableClass, $translatable); $this->assertSame($locales, $translatable->getLocales()); $this->assertSame($fallbackLocale, $translatable->getFallbackLocale()); $this->assertSame($this->translationFactory, $translatable->getTranslationFactory()); }
public function testCreateQueryBuilderForCollection() { $this->classMetadata->expects($this->once())->method('getFieldNames')->will($this->returnValue([])); $this->classMetadata->expects($this->once())->method('getAssociationTargetClass')->with($this->identicalTo('translations'))->will($this->returnValue($translationClass = TranslationTest::class)); $translationClassMetadata = $this->createClassMetadataMock(); $translationClassMetadata->expects($this->once())->method('getFieldNames')->will($this->returnValue(['locale'])); $this->documentManager->expects($this->once())->method('getClassMetadata')->with($this->identicalTo($translationClass))->will($this->returnValue($translationClassMetadata)); $this->documentManager->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock())); $queryBuilder->expects($this->once())->method('field')->with($this->identicalTo('translations.locale'))->will($this->returnSelf()); $this->localeContext->expects($this->once())->method('getLocales')->will($this->returnValue($locales = ['fr'])); $this->localeContext->expects($this->once())->method('getFallbackLocale')->will($this->returnValue($fallbackLocale = 'en')); $queryBuilder->expects($this->once())->method('in')->with($this->identicalTo(array_merge($locales, [$fallbackLocale])))->will($this->returnSelf()); $this->assertSame($queryBuilder, $this->translatableRepository->createQueryBuilderForCollection()); }
public function testPostLoadWithTranslatable() { $event = $this->createLifecycleEventArgsMock(); $event->expects($this->once())->method('getEntity')->will($this->returnValue($translatable = $this->createTranslatableMock())); $this->localeContext->expects($this->once())->method('getLocales')->will($this->returnValue($locales = ['fr'])); $translatable->expects($this->once())->method('setLocales')->with($this->identicalTo($locales)); $this->localeContext->expects($this->once())->method('getFallbackLocale')->will($this->returnValue($fallbackLocale = 'en')); $translatable->expects($this->once())->method('setFallbackLocale')->with($this->identicalTo($fallbackLocale)); $this->resourceRegistry->expects($this->once())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()]))); $resource->expects($this->once())->method('getModel')->will($this->returnValue(get_class($translatable))); $resource->expects($this->once())->method('getTranslation')->will($this->returnValue($translation = $this->createResourceMock())); $translation->expects($this->once())->method('getModel')->will($this->returnValue($model = 'model')); $translatable->expects($this->once())->method('setTranslationClass')->with($this->identicalTo($model)); $this->translatableResourceSubscriber->postLoad($event); }
public function testCreateQueryBuilderForCollection() { $this->classMetadata->expects($this->once())->method('getFieldNames')->will($this->returnValue([])); $this->classMetadata->expects($this->once())->method('getAssociationMapping')->with($this->identicalTo('translations'))->will($this->returnValue(['targetEntity' => $translationClass = TranslationTest::class])); $translationClassMetadata = $this->createClassMetadataMock(); $translationClassMetadata->expects($this->once())->method('getFieldNames')->will($this->returnValue([])); $this->entityManager->expects($this->once())->method('getClassMetadata')->with($this->identicalTo($translationClass))->will($this->returnValue($translationClassMetadata)); $this->resource->expects($this->once())->method('getName')->will($this->returnValue($name = 'resource')); $this->entityManager->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock())); $queryBuilder->expects($this->once())->method('select')->with($this->identicalTo($name))->will($this->returnSelf()); $queryBuilder->expects($this->once())->method('from')->with($this->identicalTo($this->class), $this->identicalTo($name), $this->isNull())->will($this->returnSelf()); $queryBuilder->expects($this->once())->method('addSelect')->with($this->identicalTo('resource_translation'))->will($this->returnSelf()); $queryBuilder->expects($this->exactly(3))->method('getRootAliases')->will($this->returnValue([$name])); $this->localeContext->expects($this->once())->method('getLocales')->will($this->returnValue($locales = ['fr'])); $this->localeContext->expects($this->once())->method('getFallbackLocale')->will($this->returnValue($fallbackLocale = 'en')); $queryBuilder->expects($this->once())->method('expr')->will($this->returnValue($expr = $this->createExprMock())); $expr->expects($this->once())->method('in')->with($this->identicalTo('resource.locale'), $this->identicalTo(array_merge($locales, [$fallbackLocale])))->will($this->returnValue($expression = 'expression')); $queryBuilder->expects($this->once())->method('innerJoin')->with($this->identicalTo($name . '.translations'), $this->identicalTo('resource_translation'), $this->identicalTo(Join::WITH), $this->identicalTo($expression), $this->isNull())->will($this->returnSelf()); $this->assertSame($queryBuilder, $this->translatableRepository->createQueryBuilderForCollection()); }
/** * {@inheritdoc} */ public function create(array $options = []) { return parent::create(array_merge($options, ['locales' => $this->localeContext->getLocales(), 'fallbackLocale' => $this->localeContext->getFallbackLocale(), 'translationFactory' => $this->translationFactory])); }