Skip to content

Commit

Permalink
OP-525: PHPSpec fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jkindly committed Sep 18, 2024
1 parent fbc2c2e commit 7a972e0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 71 deletions.
4 changes: 0 additions & 4 deletions spec/Importer/BlockImporterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function let(
ResourceResolverInterface $blockResourceResolver,
ImporterCollectionsResolverInterface $importerCollectionsResolver,
ImporterChannelsResolverInterface $importerChannelsResolver,
ImporterLocalesResolverInterface $importerLocalesResolver,
ImporterProductsResolverInterface $importerProductsResolver,
ImporterTaxonsResolverInterface $importerTaxonsResolver,
ImporterProductsInTaxonsResolverInterface $importerProductsInTaxonsResolver,
Expand All @@ -36,7 +35,6 @@ public function let(
$blockResourceResolver,
$importerCollectionsResolver,
$importerChannelsResolver,
$importerLocalesResolver,
$importerProductsResolver,
$importerTaxonsResolver,
$importerProductsInTaxonsResolver,
Expand All @@ -55,7 +53,6 @@ public function it_imports_block(
ResourceResolverInterface $blockResourceResolver,
ImporterCollectionsResolverInterface $importerCollectionsResolver,
ImporterChannelsResolverInterface $importerChannelsResolver,
ImporterLocalesResolverInterface $importerLocalesResolver,
ImporterProductsResolverInterface $importerProductsResolver,
ImporterTaxonsResolverInterface $importerTaxonsResolver,
ImporterProductsInTaxonsResolverInterface $importerProductsInTaxonsResolver,
Expand All @@ -73,7 +70,6 @@ public function it_imports_block(

$importerCollectionsResolver->resolve($block, null)->shouldBeCalled();
$importerChannelsResolver->resolve($block, null)->shouldBeCalled();
$importerLocalesResolver->resolve($block, null)->shouldBeCalled();
$importerProductsResolver->resolve($block, null)->shouldBeCalled();
$importerTaxonsResolver->resolve($block, null)->shouldBeCalled();
$importerProductsInTaxonsResolver->resolve($block, null)->shouldBeCalled();
Expand Down
61 changes: 0 additions & 61 deletions spec/Resolver/BlockResourceResolverSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace spec\Sylius\CmsPlugin\Resolver;

use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Psr\Log\LoggerInterface;
use Sylius\CmsPlugin\Entity\BlockInterface;
Expand All @@ -14,7 +13,6 @@
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class BlockResourceResolverSpec extends ObjectBehavior
Expand All @@ -39,74 +37,15 @@ public function it_implements_block_resource_resolver_interface(): void
$this->shouldHaveType(BlockResourceResolverInterface::class);
}

public function it_logs_warning_if_block_was_not_found(
BlockRepositoryInterface $blockRepository,
LoggerInterface $logger,
ChannelContextInterface $channelContext,
ChannelInterface $channel,
) {
$channel->getCode()->willReturn('WEB');
$channelContext->getChannel()->willReturn($channel);
$blockRepository->findEnabledByCode('homepage_banner', 'WEB')->willReturn(null);

$logger
->warning(sprintf(
'Block with "%s" code was not found in the database.',
'homepage_banner',
))
->shouldBeCalled()
;

$this->findOrLog('homepage_banner');
}

public function it_logs_warning_if_block_was_found_but_it_does_not_have_locale(
BlockRepositoryInterface $blockRepository,
LoggerInterface $logger,
ChannelContextInterface $channelContext,
ChannelInterface $channel,
LocaleContextInterface $localeContext,
LocaleInterface $locale,
RepositoryInterface $localeRepository,
BlockInterface $block,
) {
$channel->getCode()->willReturn('WEB');
$channelContext->getChannel()->willReturn($channel);
$blockRepository->findEnabledByCode('homepage_banner', 'WEB')->willReturn($block);
$localeContext->getLocaleCode()->willReturn('en_US');
$locale->getCode()->willReturn('en_US');
$localeRepository->findOneBy(['code' => 'en_US'])->willReturn($locale);
$block->hasLocale($locale)->willReturn(false);
$block->getLocales()->willReturn(new ArrayCollection(['pl_PL']));

$logger
->warning(sprintf(
'Block with "%s" code was found in the database, but it does not have "%s" locale.',
'homepage_banner',
'en_US',
))
->shouldBeCalled()
;

$this->findOrLog('homepage_banner');
}

public function it_returns_block_if_found_in_database(
BlockRepositoryInterface $blockRepository,
BlockInterface $block,
ChannelContextInterface $channelContext,
ChannelInterface $channel,
LocaleContextInterface $localeContext,
LocaleInterface $locale,
RepositoryInterface $localeRepository,
) {
$channel->getCode()->willReturn('WEB');
$channelContext->getChannel()->willReturn($channel);
$blockRepository->findEnabledByCode('homepage_banner', 'WEB')->willReturn($block);
$localeContext->getLocaleCode()->willReturn('en_US');
$locale->getCode()->willReturn('en_US');
$localeRepository->findOneBy(['code' => 'en_US'])->willReturn($locale);
$block->hasLocale($locale)->willReturn(true);

$this->findOrLog('homepage_banner')->shouldReturn($block);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Importer/AbstractImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ protected function getTranslatableColumnValue(
protected function getAvailableLocales(array $translatableColumns, array $columns): array
{
$locales = [];

foreach ($translatableColumns as $translatableColumn) {
$translatableColumn = str_replace('__locale__', '_', $translatableColumn);
foreach ($columns as $column) {
if (
str_starts_with($column, $translatableColumn) &&
preg_match('/^' . preg_quote($translatableColumn, '/') . '([a-z]{2}_[A-Z]{2})$/', $column, $matches)
) {
$locales[] = $matches[1];
if (str_starts_with($column, $translatableColumn)) {
$localePart = substr($column, strlen($translatableColumn));

if (preg_match('/^[a-z]{2}(_[A-Z]{2})?$/', $localePart)) {
$locales[] = $localePart;
}
}
}
}
Expand Down

0 comments on commit 7a972e0

Please sign in to comment.