mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 17:10:53 +00:00
[Behat] Replace private convertToCode method in locale setup context
This commit is contained in:
parent
bdd7df7ec3
commit
86b8ce4ee5
3 changed files with 22 additions and 25 deletions
|
|
@ -59,6 +59,7 @@
|
|||
<service id="sylius.behat.context.setup.locale" class="%sylius.behat.context.setup.locale.class%" scope="scenario">
|
||||
<argument type="service" id="sylius.behat.shared_storage" container="symfony" />
|
||||
<argument type="service" id="sylius.factory.locale" container="symfony" />
|
||||
<argument type="service" id="sylius.converter.locale_name" container="symfony" />
|
||||
<argument type="service" id="sylius.repository.locale" container="symfony" />
|
||||
<tag name="sylius.behat.context" />
|
||||
</service>
|
||||
|
|
|
|||
|
|
@ -12,10 +12,10 @@
|
|||
namespace Sylius\Behat\Context\Setup;
|
||||
|
||||
use Behat\Behat\Context\Context;
|
||||
use Sylius\Component\Locale\Converter\LocaleNameConverterInterface;
|
||||
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
|
||||
use Sylius\Component\Resource\Factory\FactoryInterface;
|
||||
use Sylius\Component\Resource\Repository\RepositoryInterface;
|
||||
use Symfony\Component\Intl\Intl;
|
||||
|
||||
/**
|
||||
* @author Arkadiusz Krakowiak <arkadiusz.krakowiak@lakion.com>
|
||||
|
|
@ -32,6 +32,11 @@ final class LocaleContext implements Context
|
|||
*/
|
||||
private $localeFactory;
|
||||
|
||||
/**
|
||||
* @var LocaleNameConverterInterface
|
||||
*/
|
||||
private $localeNameConverter;
|
||||
|
||||
/**
|
||||
* @var RepositoryInterface
|
||||
*/
|
||||
|
|
@ -40,15 +45,18 @@ final class LocaleContext implements Context
|
|||
/**
|
||||
* @param SharedStorageInterface $sharedStorage
|
||||
* @param FactoryInterface $localeFactory
|
||||
* @param LocaleNameConverterInterface $localeNameConverter
|
||||
* @param RepositoryInterface $localeRepository
|
||||
*/
|
||||
public function __construct(
|
||||
SharedStorageInterface $sharedStorage,
|
||||
FactoryInterface $localeFactory,
|
||||
LocaleNameConverterInterface $localeNameConverter,
|
||||
RepositoryInterface $localeRepository
|
||||
) {
|
||||
$this->localeFactory = $localeFactory;
|
||||
$this->localeRepository = $localeRepository;
|
||||
$this->localeNameConverter = $localeNameConverter;
|
||||
$this->sharedStorage = $sharedStorage;
|
||||
}
|
||||
|
||||
|
|
@ -58,7 +66,7 @@ final class LocaleContext implements Context
|
|||
public function theStoreHasLocale($localeName)
|
||||
{
|
||||
$locale = $this->localeFactory->createNew();
|
||||
$locale->setCode($this->convertToCode($localeName));
|
||||
$locale->setCode($this->localeNameConverter->convertToCode($localeName));
|
||||
|
||||
$this->sharedStorage->set('locale', $locale);
|
||||
$this->localeRepository->add($locale);
|
||||
|
|
@ -70,31 +78,10 @@ final class LocaleContext implements Context
|
|||
public function theStoreHasDisabledLocale($localeName)
|
||||
{
|
||||
$locale = $this->localeFactory->createNew();
|
||||
$locale->setCode($this->convertToCode($localeName));
|
||||
$locale->setCode($this->localeNameConverter->convertToCode($localeName));
|
||||
$locale->disable();
|
||||
|
||||
$this->sharedStorage->set('locale', $locale);
|
||||
$this->localeRepository->add($locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $localeName
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
private function convertToCode($localeName)
|
||||
{
|
||||
$localeNames = Intl::getLocaleBundle()->getLocaleNames('en');
|
||||
$localeCode = array_search($localeName, $localeNames, true);
|
||||
|
||||
if (false === $localeCode) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('Cannot find code for %s locale', $localeName)
|
||||
);
|
||||
}
|
||||
|
||||
return $localeCode;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ namespace spec\Sylius\Behat\Context\Setup;
|
|||
use Behat\Behat\Context\Context;
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Sylius\Behat\Context\Setup\LocaleContext;
|
||||
use Sylius\Component\Locale\Converter\LocaleNameConverterInterface;
|
||||
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
|
||||
use Sylius\Component\Locale\Model\LocaleInterface;
|
||||
use Sylius\Component\Resource\Factory\FactoryInterface;
|
||||
|
|
@ -29,9 +30,10 @@ class LocaleContextSpec extends ObjectBehavior
|
|||
function let(
|
||||
SharedStorageInterface $sharedStorage,
|
||||
FactoryInterface $localeFactory,
|
||||
LocaleNameConverterInterface $localeNameConverter,
|
||||
RepositoryInterface $localeRepository
|
||||
) {
|
||||
$this->beConstructedWith($sharedStorage, $localeFactory, $localeRepository);
|
||||
$this->beConstructedWith($sharedStorage, $localeFactory, $localeNameConverter, $localeRepository);
|
||||
}
|
||||
|
||||
function it_is_initializable()
|
||||
|
|
@ -47,10 +49,12 @@ class LocaleContextSpec extends ObjectBehavior
|
|||
function it_creates_locale(
|
||||
FactoryInterface $localeFactory,
|
||||
LocaleInterface $locale,
|
||||
LocaleNameConverterInterface $localeNameConverter,
|
||||
RepositoryInterface $localeRepository,
|
||||
SharedStorageInterface $sharedStorage
|
||||
) {
|
||||
$localeFactory->createNew()->willReturn($locale);
|
||||
$localeNameConverter->convertToCode('Norwegian')->willReturn('no');
|
||||
|
||||
$locale->setCode('no')->shouldBeCalled();
|
||||
$sharedStorage->set('locale', $locale)->shouldBeCalled();
|
||||
|
|
@ -62,10 +66,12 @@ class LocaleContextSpec extends ObjectBehavior
|
|||
function it_creates_disabled_locale(
|
||||
FactoryInterface $localeFactory,
|
||||
LocaleInterface $locale,
|
||||
LocaleNameConverterInterface $localeNameConverter,
|
||||
RepositoryInterface $localeRepository,
|
||||
SharedStorageInterface $sharedStorage
|
||||
) {
|
||||
$localeFactory->createNew()->willReturn($locale);
|
||||
$localeNameConverter->convertToCode('Norwegian')->willReturn('no');
|
||||
|
||||
$locale->setCode('no')->shouldBeCalled();
|
||||
$locale->disable()->shouldBeCalled();
|
||||
|
|
@ -79,10 +85,13 @@ class LocaleContextSpec extends ObjectBehavior
|
|||
function it_throws_invalid_argument_exception_if_cannot_convert_locale_name_to_code(
|
||||
FactoryInterface $localeFactory,
|
||||
LocaleInterface $locale,
|
||||
LocaleNameConverterInterface $localeNameConverter,
|
||||
RepositoryInterface $localeRepository,
|
||||
SharedStorageInterface $sharedStorage
|
||||
) {
|
||||
$localeFactory->createNew()->willReturn($locale);
|
||||
$localeNameConverter->convertToCode('xyz')->willThrow(\InvalidArgumentException::class);
|
||||
|
||||
$locale->setCode('no')->shouldNotBeCalled();
|
||||
$sharedStorage->set('locale', $locale)->shouldNotBeCalled();
|
||||
$localeRepository->add($locale)->shouldNotBeCalled();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue