-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #510 from BitBagCommerce/feature/OP-439
OP-439: Products grid & Products grid by Taxon content elements
- Loading branch information
Showing
33 changed files
with
570 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
spec/Renderer/ContentElement/ProductsGridByTaxonContentElementRendererSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?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\ContentElement; | ||
|
||
use BitBag\SyliusCmsPlugin\Entity\ContentConfigurationInterface; | ||
use BitBag\SyliusCmsPlugin\Form\Type\ContentElements\ProductsGridByTaxonContentElementType; | ||
use BitBag\SyliusCmsPlugin\Renderer\ContentElement\ContentElementRendererInterface; | ||
use BitBag\SyliusCmsPlugin\Renderer\ContentElement\ProductsGridByTaxonContentElementRenderer; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\Product; | ||
use Sylius\Component\Core\Model\TaxonInterface; | ||
use Sylius\Component\Core\Repository\ProductRepositoryInterface; | ||
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; | ||
use Twig\Environment; | ||
|
||
final class ProductsGridByTaxonContentElementRendererSpec extends ObjectBehavior | ||
{ | ||
public function let(Environment $twig, ProductRepositoryInterface $productRepository, TaxonRepositoryInterface $taxonRepository): void | ||
{ | ||
$this->beConstructedWith($twig, $productRepository, $taxonRepository); | ||
} | ||
|
||
public function it_is_initializable(): void | ||
{ | ||
$this->shouldHaveType(ProductsGridByTaxonContentElementRenderer::class); | ||
} | ||
|
||
public function it_implements_content_element_renderer_interface(): void | ||
{ | ||
$this->shouldImplement(ContentElementRendererInterface::class); | ||
} | ||
|
||
public function it_supports_products_grid_by_taxon_content_element_type(ContentConfigurationInterface $contentConfiguration): void | ||
{ | ||
$contentConfiguration->getType()->willReturn(ProductsGridByTaxonContentElementType::TYPE); | ||
$this->supports($contentConfiguration)->shouldReturn(true); | ||
} | ||
|
||
public function it_does_not_support_other_content_element_types(ContentConfigurationInterface $contentConfiguration): void | ||
{ | ||
$contentConfiguration->getType()->willReturn('other_type'); | ||
$this->supports($contentConfiguration)->shouldReturn(false); | ||
} | ||
|
||
public function it_renders_products_grid_by_taxon_content_element( | ||
Environment $twig, | ||
ProductRepositoryInterface $productRepository, | ||
TaxonRepositoryInterface $taxonRepository, | ||
ContentConfigurationInterface $contentConfiguration, | ||
TaxonInterface $taxon, | ||
Product $product1, | ||
Product $product2 | ||
): void | ||
{ | ||
$contentConfiguration->getConfiguration()->willReturn([ | ||
'products_grid_by_taxon' => 'taxon_code' | ||
]); | ||
|
||
$taxonRepository->findOneBy(['code' => 'taxon_code'])->willReturn($taxon); | ||
$productRepository->findByTaxon($taxon)->willReturn([$product1, $product2]); | ||
|
||
$twig->render('@BitBagSyliusCmsPlugin/Shop/ContentElement/index.html.twig', [ | ||
'content_element' => '@BitBagSyliusCmsPlugin/Shop/ContentElement/_products_grid.html.twig', | ||
'products' => [$product1, $product2], | ||
])->willReturn('rendered template'); | ||
|
||
$this->render($contentConfiguration)->shouldReturn('rendered template'); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
spec/Renderer/ContentElement/ProductsGridContentElementRendererSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?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\ContentElement; | ||
|
||
use BitBag\SyliusCmsPlugin\Entity\ContentConfigurationInterface; | ||
use BitBag\SyliusCmsPlugin\Form\Type\ContentElements\ProductsGridContentElementType; | ||
use BitBag\SyliusCmsPlugin\Renderer\ContentElement\ContentElementRendererInterface; | ||
use BitBag\SyliusCmsPlugin\Renderer\ContentElement\ProductsGridContentElementRenderer; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\Product; | ||
use Sylius\Component\Core\Repository\ProductRepositoryInterface; | ||
use Twig\Environment; | ||
|
||
final class ProductsGridContentElementRendererSpec extends ObjectBehavior | ||
{ | ||
public function let(Environment $twig, ProductRepositoryInterface $productRepository): void | ||
{ | ||
$this->beConstructedWith($twig, $productRepository); | ||
} | ||
|
||
public function it_is_initializable(): void | ||
{ | ||
$this->shouldHaveType(ProductsGridContentElementRenderer::class); | ||
} | ||
|
||
public function it_implements_content_element_renderer_interface(): void | ||
{ | ||
$this->shouldImplement(ContentElementRendererInterface::class); | ||
} | ||
|
||
public function it_supports_products_grid_content_element_type(ContentConfigurationInterface $contentConfiguration): void | ||
{ | ||
$contentConfiguration->getType()->willReturn(ProductsGridContentElementType::TYPE); | ||
$this->supports($contentConfiguration)->shouldReturn(true); | ||
} | ||
|
||
public function it_does_not_support_other_content_element_types(ContentConfigurationInterface $contentConfiguration): void | ||
{ | ||
$contentConfiguration->getType()->willReturn('other_type'); | ||
$this->supports($contentConfiguration)->shouldReturn(false); | ||
} | ||
|
||
public function it_renders_products_grid_content_element( | ||
Environment $twig, | ||
ProductRepositoryInterface $productRepository, | ||
ContentConfigurationInterface $contentConfiguration, | ||
Product $product1, | ||
Product $product2 | ||
): void | ||
{ | ||
$contentConfiguration->getConfiguration()->willReturn([ | ||
'products_grid' => ['products' => ['code1', 'code2']] | ||
]); | ||
|
||
$productRepository->findBy(['code' => ['code1', 'code2']])->willReturn([$product1, $product2]); | ||
|
||
$twig->render('@BitBagSyliusCmsPlugin/Shop/ContentElement/index.html.twig', [ | ||
'content_element' => '@BitBagSyliusCmsPlugin/Shop/ContentElement/_products_grid.html.twig', | ||
'products' => [$product1, $product2], | ||
])->willReturn('rendered template'); | ||
|
||
$this->render($contentConfiguration)->shouldReturn('rendered template'); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Form/Type/ContentElements/ProductsGridByTaxonContentElementType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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 BitBag\SyliusCmsPlugin\Form\Type\ContentElements; | ||
|
||
use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer; | ||
use Sylius\Bundle\TaxonomyBundle\Form\Type\TaxonAutocompleteChoiceType; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\ReversedTransformer; | ||
|
||
final class ProductsGridByTaxonContentElementType extends AbstractType | ||
{ | ||
public const TYPE = 'products_grid_by_taxon'; | ||
|
||
public function __construct(private RepositoryInterface $taxonRepository) | ||
{ | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add(self::TYPE, TaxonAutocompleteChoiceType::class, [ | ||
'label' => 'bitbag_sylius_cms_plugin.ui.taxon', | ||
'choice_value' => 'code', | ||
'resource' => 'sylius.taxon', | ||
]) | ||
; | ||
|
||
$builder->get(self::TYPE)->addModelTransformer( | ||
new ReversedTransformer(new ResourceToIdentifierTransformer($this->taxonRepository, 'code')), | ||
); | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return 'bitbag_sylius_cms_plugin_content_elements_' . self::TYPE; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Form/Type/ContentElements/ProductsGridContentElementType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?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 BitBag\SyliusCmsPlugin\Form\Type\ContentElements; | ||
|
||
use Sylius\Bundle\CoreBundle\Form\Type\CatalogPromotionScope\ForProductsScopeConfigurationType; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
final class ProductsGridContentElementType extends AbstractType | ||
{ | ||
public const TYPE = 'products_grid'; | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add(self::TYPE, ForProductsScopeConfigurationType::class, [ | ||
'label' => false, | ||
]) | ||
; | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return 'bitbag_sylius_cms_plugin_content_elements_' . self::TYPE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Renderer/ContentElement/ProductsGridByTaxonContentElementRenderer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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 BitBag\SyliusCmsPlugin\Renderer\ContentElement; | ||
|
||
use BitBag\SyliusCmsPlugin\Entity\ContentConfigurationInterface; | ||
use BitBag\SyliusCmsPlugin\Form\Type\ContentElements\ProductsGridByTaxonContentElementType; | ||
use Sylius\Component\Core\Model\TaxonInterface; | ||
use Sylius\Component\Core\Repository\ProductRepositoryInterface; | ||
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; | ||
use Twig\Environment; | ||
|
||
final class ProductsGridByTaxonContentElementRenderer implements ContentElementRendererInterface | ||
{ | ||
public function __construct( | ||
private Environment $twig, | ||
private ProductRepositoryInterface $productRepository, | ||
private TaxonRepositoryInterface $taxonRepository, | ||
) { | ||
} | ||
|
||
public function supports(ContentConfigurationInterface $contentConfiguration): bool | ||
{ | ||
return ProductsGridByTaxonContentElementType::TYPE === $contentConfiguration->getType(); | ||
} | ||
|
||
public function render(ContentConfigurationInterface $contentConfiguration): string | ||
{ | ||
$taxonCode = $contentConfiguration->getConfiguration()['products_grid_by_taxon']; | ||
|
||
/** @var TaxonInterface $taxon */ | ||
$taxon = $this->taxonRepository->findOneBy(['code' => $taxonCode]); | ||
$products = $this->productRepository->findByTaxon($taxon); | ||
|
||
return $this->twig->render('@BitBagSyliusCmsPlugin/Shop/ContentElement/index.html.twig', [ | ||
'content_element' => '@BitBagSyliusCmsPlugin/Shop/ContentElement/_products_grid.html.twig', | ||
'products' => $products, | ||
]); | ||
} | ||
} |
Oops, something went wrong.