Skip to content

Commit

Permalink
Refactor on how to create translations
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed May 26, 2021
1 parent 445fa5f commit 97a9fad
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 58 deletions.
11 changes: 10 additions & 1 deletion packages/basic-page-bundle/src/Factory/BasicPageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,20 @@ protected function getDefaults(): array
BasicPage::LOCATION_NONE,
]),
'publish' => self::faker()->boolean(),
'translations' => BasicPageTranslationFactory::createMany(2),
'metaInformation' => EntityMetaInformationFactory::createOne(),
];
}

/** @param string[] $locales */
public function withTranslations(array $locales, array $defaultAttributes = []): self
{
return $this->addState([
'translations' => BasicPageTranslationFactory::createMany(count($locales), \array_merge($defaultAttributes, [
'locale' => self::faker()->unique()->randomElement($locales),
])),
]);
}

protected static function getClass(): string
{
return BasicPage::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Doctrine\ORM\NoResultException;
use Runroom\BasicPageBundle\Factory\BasicPageFactory;
use Runroom\BasicPageBundle\Factory\BasicPageTranslationFactory;
use Runroom\BasicPageBundle\Repository\BasicPageRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Foundry\Test\Factories;
Expand All @@ -39,13 +38,9 @@ protected function setUp(): void
/** @test */
public function itFindsBasicPageGivenItsSlug(): void
{
BasicPageFactory::createOne([
'translations' => BasicPageTranslationFactory::createMany(1, [
'locale' => 'en',
'slug' => 'slug',
]),
'publish' => true,
]);
BasicPageFactory::new(['publish' => true])->withTranslations(['en'], [
'slug' => 'slug',
])->create();

$basicPage = $this->repository->findBySlug('slug');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public function itReturnsAvailableLocales(): void
/** @test */
public function itReturnsRouteParameters(): void
{
$basicPage = BasicPageFactory::createOne()->object();
$basicPage = BasicPageFactory::new()->withTranslations(['en'])->create()->object();
$model = new BasicPageViewModel();
$model->setBasicPage($basicPage);

foreach ($basicPage->getTranslations()->getKeys() as $locale) {
foreach (['en'] as $locale) {
self::assertSame(
['slug' => $basicPage->translate($locale)->getSlug()],
$this->provider->getParameters($model, $locale)
Expand Down
14 changes: 11 additions & 3 deletions packages/cookies-bundle/src/Factory/CookiesPageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ final class CookiesPageFactory extends ModelFactory
/** @return array<string, mixed> */
protected function getDefaults(): array
{
return [
'translations' => CookiesPageTranslationFactory::createMany(2),
];
return [];
}

/** @param string[] $locales */
public function withTranslations(array $locales, array $defaultAttributes = []): self
{
return $this->addState([
'translations' => CookiesPageTranslationFactory::createMany(count($locales), \array_merge($defaultAttributes, [
'locale' => self::faker()->unique()->randomElement($locales),
])),
]);
}

protected static function getClass(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Runroom\CookiesBundle\Tests\Integration;

use Runroom\CookiesBundle\Factory\CookiesPageFactory;
use Runroom\CookiesBundle\Factory\CookiesPageTranslationFactory;
use Runroom\CookiesBundle\Repository\CookiesPageRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Foundry\Test\Factories;
Expand All @@ -38,11 +37,7 @@ protected function setUp(): void
/** @test */
public function ifFindsCookiesPageById(): void
{
CookiesPageFactory::createOne([
'translations' => CookiesPageTranslationFactory::createMany(1, [
'locale' => 'en',
]),
]);
CookiesPageFactory::new()->withTranslations(['en'])->create();

$cookiesPage = $this->repository->find(1);

Expand Down
14 changes: 11 additions & 3 deletions packages/seo-bundle/src/Factory/EntityMetaInformationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ final class EntityMetaInformationFactory extends ModelFactory
/** @return array<string, mixed> */
protected function getDefaults(): array
{
return [
'translations' => EntityMetaInformationTranslationFactory::createMany(2),
];
return [];
}

/** @param string[] $locales */
public function withTranslations(array $locales, array $defaultAttributes = []): self
{
return $this->addState([
'translations' => EntityMetaInformationTranslationFactory::createMany(count($locales), \array_merge($defaultAttributes, [
'locale' => self::faker()->unique()->randomElement($locales),
])),
]);
}

protected static function getClass(): string
Expand Down
11 changes: 10 additions & 1 deletion packages/seo-bundle/src/Factory/MetaInformationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ protected function getDefaults(): array
return [
'route' => self::faker()->unique()->word(),
'routeName' => self::faker()->words(3, true),
'translations' => MetaInformationTranslationFactory::createMany(2),
];
}

/** @param string[] $locales */
public function withTranslations(array $locales, array $defaultAttributes = []): self
{
return $this->addState([
'translations' => MetaInformationTranslationFactory::createMany(count($locales), \array_merge($defaultAttributes, [
'locale' => self::faker()->unique()->randomElement($locales),
])),
]);
}

protected static function getClass(): string
{
return MetaInformation::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Runroom\SeoBundle\Tests\Integration;

use Runroom\SeoBundle\Factory\MetaInformationFactory;
use Runroom\SeoBundle\Factory\MetaInformationTranslationFactory;
use Runroom\SeoBundle\MetaInformation\MetaInformationBuilder;
use Runroom\SeoBundle\Repository\MetaInformationRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
Expand All @@ -39,12 +38,7 @@ protected function setUp(): void
/** @test */
public function itFindsDefaultMetaInformation(): void
{
MetaInformationFactory::createOne([
'route' => MetaInformationBuilder::DEFAULT_ROUTE,
'translations' => MetaInformationTranslationFactory::createMany(1, [
'locale' => 'en',
]),
]);
MetaInformationFactory::new(['route' => MetaInformationBuilder::DEFAULT_ROUTE])->withTranslations(['en'])->create();

$metaInformation = $this->repository->findOneBy(['route' => MetaInformationBuilder::DEFAULT_ROUTE]);

Expand Down
7 changes: 1 addition & 6 deletions packages/seo-bundle/tests/Unit/EntityMetaInformationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use PHPUnit\Framework\TestCase;
use Runroom\SeoBundle\Factory\EntityMetaInformationFactory;
use Runroom\SeoBundle\Factory\MetaInformationTranslationFactory;
use Zenstruck\Foundry\Test\Factories;

class EntityMetaInformationTest extends TestCase
Expand All @@ -25,11 +24,7 @@ class EntityMetaInformationTest extends TestCase
/** @test */
public function itGetsProperties(): void
{
$metaInformation = EntityMetaInformationFactory::createOne([
'translations' => MetaInformationTranslationFactory::createMany(1, [
'locale' => 'en',
]),
]);
$metaInformation = EntityMetaInformationFactory::new()->withTranslations(['en'])->create();

self::assertNotEmpty((string) $metaInformation);
self::assertNull($metaInformation->getId());
Expand Down
12 changes: 4 additions & 8 deletions packages/seo-bundle/tests/Unit/MetaInformationBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Runroom\SeoBundle\Factory\MetaInformationFactory;
use Runroom\SeoBundle\Factory\MetaInformationTranslationFactory;
use Runroom\SeoBundle\MetaInformation\AbstractMetaInformationProvider;
use Runroom\SeoBundle\MetaInformation\MetaInformationBuilder;
use Runroom\SeoBundle\Repository\MetaInformationRepository;
Expand Down Expand Up @@ -49,13 +48,10 @@ public function itBuildsMetaInformationViewModel(): void
$model = new \stdClass();
$model->placeholder = 'test';

$metaInformation = MetaInformationFactory::createOne([
'translations' => MetaInformationTranslationFactory::createMany(1, [
'title' => '[placeholder] title',
'description' => '[missing] description',
'locale' => 'en',
]),
])->object();
$metaInformation = MetaInformationFactory::new()->withTranslations(['en'], [
'title' => '[placeholder] title',
'description' => '[missing] description',
])->create()->object();

$this->repository->method('findOneBy')->willReturn($metaInformation);

Expand Down
11 changes: 10 additions & 1 deletion packages/translation-bundle/src/Factory/TranslationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ protected function getDefaults(): array
{
return [
'key' => self::faker()->unique()->word(),
'translations' => TranslationTranslationFactory::createMany(2),
];
}

/** @param string[] $locales */
public function withTranslations(array $locales, array $defaultAttributes = []): self
{
return $this->addState([
'translations' => TranslationTranslationFactory::createMany(count($locales), \array_merge($defaultAttributes, [
'locale' => self::faker()->unique()->randomElement($locales),
])),
]);
}

protected static function getClass(): string
{
return Translation::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Runroom\TranslationBundle\Tests\Integration;

use Runroom\TranslationBundle\Factory\TranslationFactory;
use Runroom\TranslationBundle\Factory\TranslationTranslationFactory;
use Runroom\TranslationBundle\Repository\TranslationRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Foundry\Test\Factories;
Expand All @@ -38,12 +37,7 @@ protected function setUp(): void
/** @test */
public function itFindsTranslationsByKey(): void
{
TranslationFactory::createOne([
'key' => 'test',
'translations' => TranslationTranslationFactory::createMany(1, [
'locale' => 'en',
]),
]);
TranslationFactory::new(['key' => 'test'])->withTranslations(['en'])->create();

$translation = $this->repository->findOneBy(['key' => 'test']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ protected function setUp(): void
/** @test */
public function itReturnsAStringTranslatedByTheRepository(): void
{
$translation = TranslationFactory::createOne([
'translations' => TranslationTranslationFactory::createMany(1, [
'locale' => 'en',
]),
]);
$translation = TranslationFactory::new(['key' => 'key'])->withTranslations(['en'])->create();

$this->repository->method('findOneBy')->with(['key' => 'key'])->willReturn($translation);
$this->translator->expects(self::never())->method('trans')->with('key', [], null, 'en');
Expand Down

0 comments on commit 97a9fad

Please sign in to comment.