Skip to content

Commit

Permalink
Merge pull request #512 from BitBagCommerce/feature/OP-441
Browse files Browse the repository at this point in the history
OP-441: Twig function to render Collection
  • Loading branch information
senghe authored Jul 24, 2024
2 parents 12664cf + 56ccb60 commit ecbdd33
Show file tree
Hide file tree
Showing 33 changed files with 944 additions and 236 deletions.
100 changes: 100 additions & 0 deletions spec/Renderer/Collection/CollectionBlocksRendererSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusCmsPlugin\Renderer\Collection;

use BitBag\SyliusCmsPlugin\Entity\BlockInterface;
use BitBag\SyliusCmsPlugin\Entity\CollectionInterface;
use BitBag\SyliusCmsPlugin\Renderer\Collection\CollectionBlocksRenderer;
use BitBag\SyliusCmsPlugin\Renderer\Collection\CollectionRendererInterface;
use BitBag\SyliusCmsPlugin\Renderer\ContentElementRendererStrategyInterface;
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;

final class CollectionBlocksRendererSpec extends ObjectBehavior
{
public function let(ContentElementRendererStrategyInterface $contentElementRendererStrategy): void
{
$this->beConstructedWith($contentElementRendererStrategy);
}

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

public function it_implements_collection_renderer_interface(): void
{
$this->shouldImplement(CollectionRendererInterface::class);
}

public function it_renders_blocks_from_collection(
ContentElementRendererStrategyInterface $contentElementRendererStrategy,
CollectionInterface $collection,
BlockInterface $block1,
BlockInterface $block2
): void
{
$blocks = new ArrayCollection([$block1->getWrappedObject(), $block2->getWrappedObject()]);
$collection->getBlocks()->willReturn($blocks);

$contentElementRendererStrategy->render($block1)->willReturn('block1_content');
$contentElementRendererStrategy->render($block2)->willReturn('block2_content');

$this->render($collection)->shouldReturn('block1_contentblock2_content');
}

public function it_limits_number_of_rendered_blocks(
ContentElementRendererStrategyInterface $contentElementRendererStrategy,
CollectionInterface $collection,
BlockInterface $block1,
BlockInterface $block2
): void
{
$blocks = new ArrayCollection([$block1->getWrappedObject(), $block2->getWrappedObject()]);
$collection->getBlocks()->willReturn($blocks);

$contentElementRendererStrategy->render($block1)->willReturn('block1_content');
$contentElementRendererStrategy->render($block2)->willReturn('block2_content');

$this->render($collection, 1)->shouldReturn('block1_content');
}

public function it_supports_collections_with_blocks(
CollectionInterface $collection,
BlockInterface $block
): void
{
$blocks = new ArrayCollection([$block]);
$collection->getBlocks()->willReturn($blocks);

$this->supports($collection)->shouldReturn(true);
}

public function it_does_not_support_empty_collections(
CollectionInterface $collection
): void
{
$collection->getBlocks()->willReturn(new ArrayCollection());

$this->supports($collection)->shouldReturn(false);
}

public function it_throws_exception_when_blocks_are_null(
ContentElementRendererStrategyInterface $contentElementRendererStrategy,
CollectionInterface $collection
): void
{
$collection->getBlocks()->willReturn(null);

$this->shouldThrow(\InvalidArgumentException::class)
->during('render', [$collection]);
}
}
78 changes: 78 additions & 0 deletions spec/Renderer/Collection/CollectionMediaRendererSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusCmsPlugin\Renderer\Collection;

use BitBag\SyliusCmsPlugin\Entity\CollectionInterface;
use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
use BitBag\SyliusCmsPlugin\Renderer\Collection\CollectionMediaRenderer;
use BitBag\SyliusCmsPlugin\Renderer\Collection\CollectionRendererInterface;
use BitBag\SyliusCmsPlugin\Twig\Runtime\RenderMediaRuntimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;

final class CollectionMediaRendererSpec extends ObjectBehavior
{
function let(RenderMediaRuntimeInterface $renderMediaRuntime)
{
$this->beConstructedWith($renderMediaRuntime);
}

function it_is_initializable()
{
$this->shouldHaveType(CollectionMediaRenderer::class);
$this->shouldImplement(CollectionRendererInterface::class);
}

function it_renders_media_collection(RenderMediaRuntimeInterface $renderMediaRuntime, CollectionInterface $collection, MediaInterface $media1, MediaInterface $media2)
{
$media1->getId()->willReturn(1);
$media2->getId()->willReturn(2);

$media1->getCode()->willReturn('media_code_1');
$media2->getCode()->willReturn('media_code_2');

$collection->getMedia()->willReturn(new ArrayCollection([$media1->getWrappedObject(), $media2->getWrappedObject()]));

$renderMediaRuntime->renderMedia('media_code_1')->willReturn('media1');
$renderMediaRuntime->renderMedia('media_code_2')->willReturn('media2');

$this->render($collection)->shouldReturn('media1media2');
}

function it_renders_limited_number_of_media(RenderMediaRuntimeInterface $renderMediaRuntime, CollectionInterface $collection, MediaInterface $media1, MediaInterface $media2)
{
$media1->getId()->willReturn(1);
$media2->getId()->willReturn(2);

$media1->getCode()->willReturn('media_code_1');
$media2->getCode()->willReturn('media_code_2');

$collection->getMedia()->willReturn(new ArrayCollection([$media1->getWrappedObject(), $media2->getWrappedObject()]));

$renderMediaRuntime->renderMedia('media_code_1')->willReturn('media1');

$this->render($collection, 1)->shouldReturn('media1');
}

function it_supports_collections_with_media(CollectionInterface $collection, MediaInterface $media1)
{
$collection->getMedia()->willReturn(new ArrayCollection([$media1]));

$this->supports($collection)->shouldReturn(true);
}

function it_does_not_support_collections_without_media(CollectionInterface $collection)
{
$collection->getMedia()->willReturn(new ArrayCollection());

$this->supports($collection)->shouldReturn(false);
}
}
92 changes: 92 additions & 0 deletions spec/Renderer/Collection/CollectionPagesRendererSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusCmsPlugin\Renderer\Collection;

use BitBag\SyliusCmsPlugin\Entity\CollectionInterface;
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
use BitBag\SyliusCmsPlugin\Renderer\Collection\CollectionPagesRenderer;
use BitBag\SyliusCmsPlugin\Renderer\Collection\CollectionRendererInterface;
use BitBag\SyliusCmsPlugin\Renderer\PageLinkRendererInterface;
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;

final class CollectionPagesRendererSpec extends ObjectBehavior
{
public function let(PageLinkRendererInterface $pageLinkRenderer): void
{
$this->beConstructedWith($pageLinkRenderer);
}

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

public function it_implements_collection_renderer_interface(): void
{
$this->shouldImplement(CollectionRendererInterface::class);
}

public function it_renders_pages_from_collection(
PageLinkRendererInterface $pageLinkRenderer,
CollectionInterface $collection,
PageInterface $page1,
PageInterface $page2
): void
{
$page1->getId()->willReturn(2);
$page2->getId()->willReturn(1);

$collection->getPages()->willReturn(new ArrayCollection([$page1->getWrappedObject(), $page2->getWrappedObject()]));

$pageLinkRenderer->render($page1)->willReturn('page1_content');
$pageLinkRenderer->render($page2)->willReturn('page2_content');

$this->render($collection)->shouldReturn('page1_contentpage2_content');
}

public function it_limits_number_of_rendered_pages(
PageLinkRendererInterface $pageLinkRenderer,
CollectionInterface $collection,
PageInterface $page1,
PageInterface $page2
): void
{
$page1->getId()->willReturn(2);
$page2->getId()->willReturn(1);

$collection->getPages()->willReturn(new ArrayCollection([$page1->getWrappedObject(), $page2->getWrappedObject()]));

$pageLinkRenderer->render($page1)->willReturn('page1_content');
$pageLinkRenderer->render($page2)->willReturn('page2_content');

$this->render($collection, 1)->shouldReturn('page1_content');
}

public function it_supports_collections_with_pages(
CollectionInterface $collection,
PageInterface $page
): void
{
$collection->getPages()->willReturn(new ArrayCollection([$page]));

$this->supports($collection)->shouldReturn(true);
}

public function it_does_not_support_empty_collections(
CollectionInterface $collection
): void
{
$collection->getPages()->willReturn(new ArrayCollection());

$this->supports($collection)->shouldReturn(false);
}
}
73 changes: 73 additions & 0 deletions spec/Renderer/CollectionRendererStrategySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusCmsPlugin\Renderer;

use BitBag\SyliusCmsPlugin\Entity\CollectionInterface;
use BitBag\SyliusCmsPlugin\Renderer\Collection\CollectionRendererInterface;
use BitBag\SyliusCmsPlugin\Renderer\CollectionRendererStrategy;
use BitBag\SyliusCmsPlugin\Renderer\CollectionRendererStrategyInterface;
use PhpSpec\ObjectBehavior;

final class CollectionRendererStrategySpec extends ObjectBehavior
{
public function let(CollectionRendererInterface $renderer1, CollectionRendererInterface $renderer2): void
{
$this->beConstructedWith([$renderer1, $renderer2]);
}

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

public function it_implements_collection_renderer_strategy_interface(): void
{
$this->shouldImplement(CollectionRendererStrategyInterface::class);
}

public function it_renders_collection_using_supported_renderer(
CollectionRendererInterface $renderer1,
CollectionRendererInterface $renderer2,
CollectionInterface $collection
): void
{
$renderer1->supports($collection)->willReturn(false);
$renderer2->supports($collection)->willReturn(true);
$renderer2->render($collection, null)->willReturn('rendered content');

$this->render($collection)->shouldReturn('rendered content');
}

public function it_renders_collection_with_count_to_render(
CollectionRendererInterface $renderer1,
CollectionRendererInterface $renderer2,
CollectionInterface $collection
): void
{
$renderer1->supports($collection)->willReturn(false);
$renderer2->supports($collection)->willReturn(true);
$renderer2->render($collection, 5)->willReturn('rendered content with count');

$this->render($collection, 5)->shouldReturn('rendered content with count');
}

public function it_returns_empty_string_when_no_renderer_supports_collection(
CollectionRendererInterface $renderer1,
CollectionRendererInterface $renderer2,
CollectionInterface $collection
): void
{
$renderer1->supports($collection)->willReturn(false);
$renderer2->supports($collection)->willReturn(false);

$this->render($collection)->shouldReturn('');
}
}
Loading

0 comments on commit ecbdd33

Please sign in to comment.