Skip to content

Commit

Permalink
Merge pull request Sylius#84 from bitbager/master
Browse files Browse the repository at this point in the history
[General] Present admin data in the store
  • Loading branch information
bitbager authored Nov 14, 2017
2 parents 36eac1f + b256486 commit d8d0b62
Show file tree
Hide file tree
Showing 32 changed files with 451 additions and 283 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ before_script:
script:
- composer validate --strict

# - bin/phpspec run
- bin/phpspec run
# - bin/phpunit
- bin/behat --strict -vvv --no-interaction || bin/behat --strict -vvv --no-interaction --rerun --tags="~@todo"

Expand Down
63 changes: 63 additions & 0 deletions spec/Resolver/BlockResourceResolverSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* This file was created by the developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace spec\BitBag\CmsPlugin\Resolver;

use BitBag\CmsPlugin\Entity\BlockInterface;
use BitBag\CmsPlugin\Repository\BlockRepositoryInterface;
use BitBag\CmsPlugin\Resolver\BlockResourceResolver;
use BitBag\CmsPlugin\Resolver\BlockResourceResolverInterface;
use PhpSpec\ObjectBehavior;
use Psr\Log\LoggerInterface;

/**
* @author Mikołaj Król <[email protected]>
*/
final class BlockResourceResolverSpec extends ObjectBehavior
{
function let(BlockRepositoryInterface $blockRepository, LoggerInterface $logger)
{
$this->beConstructedWith($blockRepository, $logger);
}

function it_is_initializable(): void
{
$this->shouldHaveType(BlockResourceResolver::class);
}

function it_implements_block_resource_resolver_interface(): void
{
$this->shouldHaveType(BlockResourceResolverInterface::class);
}

function it_logs_warning_if_block_was_not_found(BlockRepositoryInterface $blockRepository, LoggerInterface $logger)
{
$blockRepository->findOneEnabledByCode('homepage_banner')->willReturn(null);

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

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

function it_returns_block_if_found_in_database(BlockRepositoryInterface $blockRepository, BlockInterface $block)
{
$blockRepository->findOneEnabledByCode('homepage_banner')->willReturn($block);

$this->findOrLog('homepage_banner')->shouldReturn($block);
}
}
55 changes: 55 additions & 0 deletions spec/Resolver/BlockTemplateResolverSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* This file was created by the developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace spec\BitBag\CmsPlugin\Resolver;

use BitBag\CmsPlugin\Entity\BlockInterface;
use BitBag\CmsPlugin\Resolver\BlockTemplateResolver;
use BitBag\CmsPlugin\Resolver\BlockTemplateResolverInterface;
use PhpSpec\ObjectBehavior;

/**
* @author Mikołaj Król <[email protected]>
*/
final class BlockTemplateResolverSpec extends ObjectBehavior
{
function it_is_initializable(): void
{
$this->shouldHaveType(BlockTemplateResolver::class);
}

function it_implements_block_template_resolver_interface()
{
$this->shouldHaveType(BlockTemplateResolverInterface::class);
}

function it_returns_text_block_template(BlockInterface $block)
{
$block->getType()->willReturn('text');

$this->resolveTemplate($block)->shouldReturn('@BitBagCmsPlugin/Shop/Block/Show/textBlock.html.twig');
}

function it_returns_html_block_template(BlockInterface $block)
{
$block->getType()->willReturn('html');

$this->resolveTemplate($block)->shouldReturn('@BitBagCmsPlugin/Shop/Block/Show/htmlBlock.html.twig');
}

function it_returns_image_block_template(BlockInterface $block)
{
$block->getType()->willReturn('image');

$this->resolveTemplate($block)->shouldReturn('@BitBagCmsPlugin/Shop/Block/Show/imageBlock.html.twig');
}
}
59 changes: 13 additions & 46 deletions spec/Twig/Extension/RenderBlockExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

use BitBag\CmsPlugin\Entity\BlockInterface;
use BitBag\CmsPlugin\Repository\BlockRepositoryInterface;
use BitBag\CmsPlugin\Resolver\BlockResourceResolverInterface;
use BitBag\CmsPlugin\Resolver\BlockTemplateResolverInterface;
use BitBag\CmsPlugin\Twig\Extension\RenderBlockExtension;
use PhpSpec\ObjectBehavior;
use Psr\Log\LoggerInterface;

/**
* @author Mikołaj Król <[email protected]>
Expand All @@ -25,10 +26,11 @@ final class RenderBlockExtensionSpec extends ObjectBehavior
{
function let(
BlockRepositoryInterface $blockRepository,
LoggerInterface $logger
BlockTemplateResolverInterface $blockTemplateResolver,
BlockResourceResolverInterface $blockResourceResolver
): void
{
$this->beConstructedWith($blockRepository, $logger);
$this->beConstructedWith($blockRepository, $blockTemplateResolver, $blockResourceResolver);
}

function it_is_initializable(): void
Expand All @@ -44,61 +46,26 @@ function it_extends_twig_extension(): void
function it_returns_functions(): void
{
$functions = $this->getFunctions();

$functions->shouldHaveCount(1);

foreach ($functions as $function) {
$function->shouldHaveType(\Twig_SimpleFunction::class);
}
}

function it_adds_warning_for_not_found_block(
BlockRepositoryInterface $blockRepository,
LoggerInterface $logger,
\Twig_Environment $twigEnvironment
): void
{
$blockRepository->findOneEnabledByCode('bitbag')->willReturn(null);
$logger->warning('Block with "bitbag" code was not found in the database.')->shouldBeCalled();

$this->renderBlock($twigEnvironment, 'bitbag')->shouldReturn(null);
}

function it_renders_text_template_for_text_type(
BlockRepositoryInterface $blockRepository,
BlockInterface $block,
\Twig_Environment $twigEnvironment
): void
{
$blockRepository->findOneEnabledByCode('bitbag')->willReturn($block);
$block->getType()->willReturn('text');
$twigEnvironment->render('@BitBagCmsPlugin/Shop/Block/textBlock.html.twig', ['block' => $block])->shouldBeCalled();

$this->renderBlock($twigEnvironment, 'bitbag');
}

function it_renders_html_template_for_html_type(
BlockRepositoryInterface $blockRepository,
BlockInterface $block,
\Twig_Environment $twigEnvironment
): void
{
$blockRepository->findOneEnabledByCode('bitbag')->willReturn($block);
$block->getType()->willReturn('html');
$twigEnvironment->render('@BitBagCmsPlugin/Shop/Block/htmlBlock.html.twig', ['block' => $block])->shouldBeCalled();

$this->renderBlock($twigEnvironment, 'bitbag');
}

function it_renders_image_template_for_image_type(
BlockRepositoryInterface $blockRepository,
function it_renders_block(
BlockResourceResolverInterface $blockResourceResolver,
BlockTemplateResolverInterface $blockTemplateResolver,
BlockInterface $block,
\Twig_Environment $twigEnvironment
): void
{
$blockRepository->findOneEnabledByCode('bitbag')->willReturn($block);
$block->getType()->willReturn('image');
$twigEnvironment->render('@BitBagCmsPlugin/Shop/Block/imageBlock.html.twig', ['block' => $block])->shouldBeCalled();
$blockResourceResolver->findOrLog('bitbag')->willReturn($block);
$blockTemplateResolver->resolveTemplate($block)->willReturn('@BitBagCmsPlugin/Shop/Block/htmlBlock.html.twig');
$twigEnvironment->render('@BitBagCmsPlugin/Shop/Block/htmlBlock.html.twig', ['block' => $block])->willReturn('<div>BitBag</div>');

$this->renderBlock($twigEnvironment, 'bitbag');
}
}

77 changes: 0 additions & 77 deletions spec/Twig/Extension/RenderPageLinkByCodeExtensionSpec.php

This file was deleted.

26 changes: 24 additions & 2 deletions src/Fixture/Factory/BlockFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
use BitBag\CmsPlugin\Factory\BlockFactoryInterface;
use BitBag\CmsPlugin\Repository\BlockRepositoryInterface;
use BitBag\CmsPlugin\Repository\SectionRepositoryInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

Expand Down Expand Up @@ -59,21 +61,35 @@ final class BlockFixtureFactory implements FixtureFactoryInterface
*/
private $sectionRepository;

/**
* @var ChannelContextInterface
*/
private $channelContext;

/**
* @var LocaleContextInterface
*/
private $localeContext;

/**
* @param BlockFactoryInterface $blockFactory
* @param FactoryInterface $blockTranslationFactory
* @param BlockRepositoryInterface $blockRepository
* @param ImageUploaderInterface $imageUploader
* @param ProductRepositoryInterface $productRepository
* @param SectionRepositoryInterface $sectionRepository
* @param ChannelContextInterface $channelContext
* @param LocaleContextInterface $localeContext
*/
public function __construct(
BlockFactoryInterface $blockFactory,
FactoryInterface $blockTranslationFactory,
BlockRepositoryInterface $blockRepository,
ImageUploaderInterface $imageUploader,
ProductRepositoryInterface $productRepository,
SectionRepositoryInterface $sectionRepository
SectionRepositoryInterface $sectionRepository,
ChannelContextInterface $channelContext,
LocaleContextInterface $localeContext
)
{
$this->blockFactory = $blockFactory;
Expand All @@ -82,6 +98,8 @@ public function __construct(
$this->imageUploader = $imageUploader;
$this->productRepository = $productRepository;
$this->sectionRepository = $sectionRepository;
$this->channelContext = $channelContext;
$this->localeContext = $localeContext;
}

/**
Expand Down Expand Up @@ -155,7 +173,11 @@ private function createBlock(string $code, array $blockData): void
*/
private function resolveProducts(BlockInterface $block, int $limit): void
{
$products = $this->productRepository->findBy([], null, $limit);
$products = $this->productRepository->findLatestByChannel(
$this->channelContext->getChannel(),
$this->localeContext->getLocaleCode(),
$limit
);

foreach ($products as $product) {
$block->addProduct($product);
Expand Down
5 changes: 3 additions & 2 deletions src/Fixture/Factory/FrequentlyAskedQuestionFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function load(array $data): void
}

if (null !== $fields['number']) {
for ($i = 0; $i < $fields['number']; $i++) {
$this->createFrequentlyAskedQuestion(md5(uniqid()), $fields, ++$i);
for ($i = 1; $i <= $fields['number']; $i++) {
$this->createFrequentlyAskedQuestion(md5(uniqid()), $fields, $i);
}
} else {
$this->createFrequentlyAskedQuestion($code, $fields, $fields['position']);
Expand All @@ -79,6 +79,7 @@ public function load(array $data): void
/**
* @param string $code
* @param array $frequentlyAskedQuestionData
* @param int $position
*/
private function createFrequentlyAskedQuestion(string $code, array $frequentlyAskedQuestionData, int $position): void
{
Expand Down
Loading

0 comments on commit d8d0b62

Please sign in to comment.