Skip to content

Commit

Permalink
OP-344: Fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
jkindly committed Aug 12, 2024
1 parent 4ae4c02 commit 8d7e4ad
Show file tree
Hide file tree
Showing 40 changed files with 573 additions and 124 deletions.
8 changes: 4 additions & 4 deletions src/Entity/Trait/CollectibleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public function addCollection(CollectionInterface $collection): void
/** @phpstan-var Block|Page|Media $this */
if ($this instanceof PageInterface) {
$collection->addPage($this);
} else if ($this instanceof BlockInterface) {
} elseif ($this instanceof BlockInterface) {
$collection->addBlock($this);
} else if ($this instanceof MediaInterface) {
} elseif ($this instanceof MediaInterface) {
$collection->addMedium($this);
}
}
Expand All @@ -62,9 +62,9 @@ public function removeCollection(CollectionInterface $collection): void
/** @phpstan-var Block|Page|Media $this */
if ($this instanceof PageInterface) {
$collection->removePage($this);
} else if ($this instanceof BlockInterface) {
} elseif ($this instanceof BlockInterface) {
$collection->removeBlock($this);
} else if ($this instanceof MediaInterface) {
} elseif ($this instanceof MediaInterface) {
$collection->removeMedium($this);
}
}
Expand Down
54 changes: 32 additions & 22 deletions src/Fixture/BlockFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,38 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->arrayNode('taxons')->scalarPrototype()->end()->end()
->arrayNode('products_in_taxons')->scalarPrototype()->end()->end()
->arrayNode('content_elements')
->prototype('array')
->children()
->scalarNode('heading_type')->end()
->scalarNode('heading')->end()
->scalarNode('textarea')->end()
->scalarNode('single_media')->end()
->scalarNode('products_carousel_by_taxon')->end()
->scalarNode('products_grid_by_taxon')->end()
->arrayNode('multiple_media')->scalarPrototype()->end()->end()
->arrayNode('products_grid')
->children()
->arrayNode('products')->scalarPrototype()->end()->end()
->end()
->end()
->arrayNode('products_carousel')
->children()
->arrayNode('products')->scalarPrototype()->end()->end()
->end()
->end()
->arrayNode('taxons_list')
->children()
->arrayNode('taxons')->scalarPrototype()->end()->end()
->useAttributeAsKey('key')
->arrayPrototype()
->children()
->scalarNode('type')->end()
->arrayNode('data')
->children()
->scalarNode('heading_type')->end()
->scalarNode('heading')->end()
->scalarNode('textarea')->end()
->scalarNode('single_media')->end()
->scalarNode('products_carousel_by_taxon')->end()
->scalarNode('products_grid_by_taxon')->end()
->scalarNode('pages_collection')->end()
->scalarNode('pages_collection')->end()
->scalarNode('spacer')->end()
->arrayNode('multiple_media')->scalarPrototype()->end()->end()
->arrayNode('products_grid')
->children()
->arrayNode('products')->scalarPrototype()->end()->end()
->end()
->end()
->arrayNode('products_carousel')
->children()
->arrayNode('products')->scalarPrototype()->end()->end()
->end()
->end()
->arrayNode('taxons_list')
->children()
->arrayNode('taxons')->scalarPrototype()->end()->end()
->end()
->end()
->end()
->end()
->end()
->end()
Expand Down
10 changes: 7 additions & 3 deletions src/Fixture/Factory/BlockFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ private function createBlock(string $code, array $blockData): void
$this->taxonsAssigner->assign($block, $blockData['taxons']);
$this->productsInTaxonsAssigner->assign($block, $blockData['products_in_taxons']);

foreach ($blockData['content_elements'] as $type => $data) {
foreach ($blockData['content_elements'] as $data) {
$data['data'] = array_filter($data['data'], static function ($value) {
return !empty($value);
});

$contentConfiguration = new ContentConfiguration();
$contentConfiguration->setType($type);
$contentConfiguration->setConfiguration($data);
$contentConfiguration->setType($data['type']);
$contentConfiguration->setConfiguration($data['data']);
$contentConfiguration->setBlock($block);
$block->addContentElement($contentConfiguration);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fixture/Factory/CollectionFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function load(array $data): void
$collection->setType($fields['type']);

foreach ($fields['page_codes'] as $pageCode) {
/** @var PageInterface $page */
/** @var PageInterface|null $page */
$page = $this->pageRepository->findOneBy(['code' => $pageCode]);
if ($page) {
$collection->addPage($page);
Expand Down
19 changes: 16 additions & 3 deletions src/Fixture/Factory/PageFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
use BitBag\SyliusCmsPlugin\Assigner\ChannelsAssignerInterface;
use BitBag\SyliusCmsPlugin\Assigner\CollectionsAssignerInterface;
use BitBag\SyliusCmsPlugin\Entity\ContentConfiguration;
use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface;
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface;
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class PageFixtureFactory implements FixtureFactoryInterface
{
public const CHANNEL_WITH_CODE_NOT_FOUND_MESSAGE = 'Channel with code "%s" not found';

public function __construct(
private FactoryInterface $pageFactory,
private FactoryInterface $pageTranslationFactory,
private PageRepositoryInterface $pageRepository,
private MediaRepositoryInterface $mediaRepository,
private CollectionsAssignerInterface $collectionsAssigner,
private ChannelsAssignerInterface $channelAssigner,
) {
Expand Down Expand Up @@ -61,6 +62,15 @@ private function createPage(
$page->setName($pageData['name']);
$page->setEnabled($pageData['enabled']);

/** @var MediaInterface|null $mediaImage */
$mediaImage = $this->mediaRepository->findOneBy(['code' => $pageData['teaser_image']]);
if ($mediaImage) {
$page->setTeaserImage($mediaImage);
}

$page->setTeaserTitle($pageData['teaser_title']);
$page->setTeaserContent($pageData['teaser_content']);

foreach ($pageData['translations'] as $localeCode => $translation) {
/** @var PageTranslationInterface $pageTranslation */
$pageTranslation = $this->pageTranslationFactory->createNew();
Expand All @@ -76,7 +86,10 @@ private function createPage(
}

foreach ($pageData['content_elements'] as $data) {
dd($data);
$data['data'] = array_filter($data['data'], static function ($value) {
return !empty($value);
});

$contentConfiguration = new ContentConfiguration();
$contentConfiguration->setType($data['type']);
$contentConfiguration->setConfiguration($data['data']);
Expand Down
3 changes: 2 additions & 1 deletion src/Fixture/Factory/TemplateFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function load(array $data): void
}
}

private function createPage(array $pageData): void {
private function createPage(array $pageData): void
{
/** @var TemplateInterface $template */
$template = $this->templateFactory->createNew();

Expand Down
18 changes: 12 additions & 6 deletions src/Fixture/PageFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->scalarNode('name')->end()
->arrayNode('collections')->scalarPrototype()->end()->end()
->arrayNode('channels')->scalarPrototype()->end()->end()
->scalarNode('teaser_title')->defaultNull()->end()
->scalarNode('teaser_content')->defaultNull()->end()
->scalarNode('teaser_image')->defaultNull()->end()
->arrayNode('translations')
->prototype('array')
->children()
Expand All @@ -53,18 +56,20 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->end()
->end()
->arrayNode('content_elements')
->useAttributeAsKey('key') // Use keys from YAML as node names
->useAttributeAsKey('key')
->arrayPrototype()
->children()
->scalarNode('type')->end() // "type" field at the top level of each content element
->arrayNode('data') // "data" field containing the actual data
->scalarNode('type')->end()
->arrayNode('data')
->children()
->scalarNode('heading_type')->end()
->scalarNode('heading')->end()
->scalarNode('textarea')->end()
->scalarNode('single_media')->end()
->scalarNode('products_carousel_by_taxon')->end()
->scalarNode('products_grid_by_taxon')->end()
->scalarNode('pages_collection')->end()
->scalarNode('spacer')->end()
->arrayNode('multiple_media')->scalarPrototype()->end()->end()
->arrayNode('products_grid')
->children()
Expand All @@ -79,13 +84,14 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->arrayNode('taxons_list')
->children()
->arrayNode('taxons')->scalarPrototype()->end()->end()
->end()
->end()
->end()
->end()
->end() // End of data
->end()
->end()
->end() // End of each content element
->end() // End of content_elements array
->end()
->end()
->end()
->end()
->end()
Expand Down
13 changes: 5 additions & 8 deletions src/Form/Type/MediaAutocompleteChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ public function configureOptions(OptionsResolver $resolver): void
'resource' => 'bitbag_sylius_cms_plugin.media',
'choice_name' => 'name',
'choice_value' => 'code',
'media_type' => null,
]);

$resolver->setAllowedValues('media_type', [
MediaInterface::IMAGE_TYPE,
MediaInterface::FILE_TYPE,
MediaInterface::VIDEO_TYPE,
null,
'media_type' => [
MediaInterface::IMAGE_TYPE,
MediaInterface::FILE_TYPE,
MediaInterface::VIDEO_TYPE,
],
]);
}

Expand Down
7 changes: 1 addition & 6 deletions src/Form/Type/MediaImageAutocompleteChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ public function configureOptions(OptionsResolver $resolver): void
'resource' => 'bitbag_sylius_cms_plugin.media',
'choice_name' => 'name',
'choice_value' => 'code',
'media_type' => MediaInterface::IMAGE_TYPE,
]);

$resolver->setAllowedValues('media_type', [
MediaInterface::IMAGE_TYPE,
null,
'media_type' => [MediaInterface::IMAGE_TYPE],
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Migrations/Version20240808102216.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getDescription(): string

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE bitbag_cms_page ADD teaser_image_id INT DEFAULT NULL, ADD teaser_title VARCHAR(255) DEFAULT NULL, ADD teaser_content VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE bitbag_cms_page ADD teaser_image_id INT DEFAULT NULL, ADD teaser_title VARCHAR(255) DEFAULT NULL, ADD teaser_content LONGTEXT DEFAULT NULL');
$this->addSql('ALTER TABLE bitbag_cms_page ADD CONSTRAINT FK_18F07F1BF56F16CF FOREIGN KEY (teaser_image_id) REFERENCES bitbag_cms_media (id)');
$this->addSql('CREATE INDEX IDX_18F07F1BF56F16CF ON bitbag_cms_page (teaser_image_id)');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Repository/MediaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ public function findByProductCode(
;
}

public function findByNamePart(string $phrase, string $mediaType): array
public function findByNamePart(string $phrase, array $mediaType): array
{
return $this->createQueryBuilder('o')
->andWhere('o.name LIKE :name')
->andWhere('o.type = :mediaType')
->andWhere('o.type IN (:mediaType)')
->setParameter('name', '%' . $phrase . '%')
->setParameter('mediaType', $mediaType)
->getQuery()
Expand Down
2 changes: 2 additions & 0 deletions src/Repository/MediaRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ public function findByProductCode(
string $localeCode,
string $channelCode,
): array;

public function findByNamePart(string $phrase, array $mediaType): array;
}
9 changes: 7 additions & 2 deletions src/Resources/assets/shop/scss/block/show.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.bitbag-block img {
max-width: 100%;
.bitbag-block {
margin-bottom: 20px;

img {
max-width: 100%;
}
}

Empty file.
5 changes: 5 additions & 0 deletions src/Resources/assets/shop/scss/content_element/main.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import "pages_collection";

[class^="cms_content_element__"]:not(:last-child) {
margin-bottom: 1rem;
}
2 changes: 2 additions & 0 deletions src/Resources/assets/shop/scss/main.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
@import './block/main.scss';
@import 'carousel';
@import 'page/teaser';
@import './content_element/main.scss';
14 changes: 14 additions & 0 deletions src/Resources/assets/shop/scss/page/_teaser.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.teaser {
&__content {
margin-top: 10px;
}

&__description {
margin-top: 5px;
}

&__read-more {
display: block;
margin-top: 20px;
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/doctrine/Page.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

<field name="teaserTitle" column="teaser_title" type="string" nullable="true"/>

<field name="teaserContent" column="teaser_content" type="string" nullable="true"/>
<field name="teaserContent" column="teaser_content" type="text" nullable="true"/>

<many-to-one field="teaserImage" target-entity="BitBag\SyliusCmsPlugin\Entity\MediaInterface" inversed-by="pages">
<join-column name="teaser_image_id" referenced-column-name="id" nullable="true" />
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/routing/shop/page.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bitbag_sylius_cms_plugin_shop_page_show_link_by_code:
- $code

bitbag_sylius_cms_plugin_shop_page_index_by_collection_code:
path: /pages/{collectionCode}
path: /page/{collectionCode}
methods: [GET]
defaults:
_controller: bitbag_sylius_cms_plugin.controller.page.overriden::indexAction
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/fixture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<argument type="service" id="bitbag_sylius_cms_plugin.factory.page" />
<argument type="service" id="bitbag_sylius_cms_plugin.factory.page_translation" />
<argument type="service" id="bitbag_sylius_cms_plugin.repository.page" />
<argument type="service" id="bitbag_sylius_cms_plugin.repository.media" />
<argument type="service" id="bitbag_sylius_cms_plugin.assigner.collections" />
<argument type="service" id="bitbag_sylius_cms_plugin.assigner.channels" />
</service>
Expand Down
Loading

0 comments on commit 8d7e4ad

Please sign in to comment.