Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fixture] Togglable default locale loading #10342

Merged
merged 1 commit into from May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Sylius/Bundle/CoreBundle/Fixture/LocaleFixture.php
Expand Up @@ -45,7 +45,11 @@ public function __construct(FactoryInterface $localeFactory, ObjectManager $loca
*/
public function load(array $options): void
{
$localesCodes = array_merge([$this->baseLocaleCode], $options['locales']);
$localesCodes = $options['locales'];

if ($options['load_default_locale'] && !in_array($this->baseLocaleCode, $localesCodes, true)) {
$localesCodes = array_merge([$this->baseLocaleCode], $localesCodes);
}

foreach ($localesCodes as $localeCode) {
/** @var LocaleInterface $locale */
Expand Down Expand Up @@ -74,8 +78,8 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
{
$optionsNode
->children()
->arrayNode('locales')
->scalarPrototype()
->scalarNode('load_default_locale')->defaultTrue()->end()
->arrayNode('locales')->scalarPrototype()->end()
;
}
}
26 changes: 25 additions & 1 deletion src/Sylius/Bundle/CoreBundle/Tests/Fixture/LocaleFixtureTest.php
Expand Up @@ -36,7 +36,31 @@ public function locales_are_not_required(): void
*/
public function locales_can_be_set(): void
{
$this->assertConfigurationIsValid([['locales' => ['en_US' => true, 'pl_PL' => false, 'es_ES' => true]]], 'locales');
$this->assertConfigurationIsValid([['locales' => ['en_US', 'pl_PL', 'es_ES']]], 'locales');
}

/**
* @test
*/
public function default_locale_may_not_be_loaded(): void
{
$this->assertProcessedConfigurationEquals(
[['load_default_locale' => false]],
['load_default_locale' => false],
'load_default_locale'
);
}

/**
* @test
*/
public function default_locale_is_added_by_default(): void
{
$this->assertProcessedConfigurationEquals(
[[]],
['load_default_locale' => true],
'load_default_locale'
);
}

/**
Expand Down
94 changes: 94 additions & 0 deletions src/Sylius/Bundle/CoreBundle/spec/Fixture/LocaleFixtureSpec.php
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\CoreBundle\Fixture;

use Doctrine\Common\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Sylius\Component\Addressing\Factory\ZoneFactoryInterface;
use Sylius\Component\Addressing\Model\CountryInterface;
use Sylius\Component\Addressing\Model\ProvinceInterface;
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class LocaleFixtureSpec extends ObjectBehavior
{
function let(FactoryInterface $localeFactory, ObjectManager $localeManager): void
{
$this->beConstructedWith($localeFactory, $localeManager, 'default_LOCALE');
}

function it_is_a_fixture(): void
{
$this->shouldImplement(FixtureInterface::class);
}

function it_loads_all_provided_locales(
FactoryInterface $localeFactory,
ObjectManager $localeManager,
LocaleInterface $germanLocale,
LocaleInterface $englishLocale
): void {
$localeFactory->createNew()->willReturn($englishLocale, $germanLocale);

$englishLocale->setCode('en_US')->shouldBeCalled();
$germanLocale->setCode('de_DE')->shouldBeCalled();

$localeManager->persist($englishLocale)->shouldBeCalled();
$localeManager->persist($germanLocale)->shouldBeCalled();

$localeManager->flush()->shouldBeCalledOnce();

$this->load(['locales' => ['en_US', 'de_DE'], 'load_default_locale' => false]);
}

function it_loads_all_provided_locales_and_the_default_one(
FactoryInterface $localeFactory,
ObjectManager $localeManager,
LocaleInterface $defaultLocale,
LocaleInterface $germanLocale,
LocaleInterface $englishLocale
): void {
$localeFactory->createNew()->willReturn($defaultLocale, $englishLocale, $germanLocale);

$defaultLocale->setCode('default_LOCALE')->shouldBeCalled();
$englishLocale->setCode('en_US')->shouldBeCalled();
$germanLocale->setCode('de_DE')->shouldBeCalled();

$localeManager->persist($defaultLocale)->shouldBeCalled();
$localeManager->persist($englishLocale)->shouldBeCalled();
$localeManager->persist($germanLocale)->shouldBeCalled();

$localeManager->flush()->shouldBeCalledOnce();

$this->load(['locales' => ['en_US', 'de_DE'], 'load_default_locale' => true]);
}

function it_allows_to_load_default_locale_and_specify_it_explicitly(
FactoryInterface $localeFactory,
ObjectManager $localeManager,
LocaleInterface $defaultLocale
): void {
$localeFactory->createNew()->willReturn($defaultLocale);

$defaultLocale->setCode('default_LOCALE')->shouldBeCalled();

$localeManager->persist($defaultLocale)->shouldBeCalledOnce();

$localeManager->flush()->shouldBeCalledOnce();

$this->load(['locales' => ['default_LOCALE'], 'load_default_locale' => true]);
}
}