Skip to content

Commit

Permalink
OP-289: Add fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
hmfilar committed Aug 21, 2024
1 parent 09dc0e8 commit 9124743
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
72 changes: 72 additions & 0 deletions src/Fixture/Factory/ProductBundleFixtureFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

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

declare(strict_types=1);

namespace BitBag\SyliusProductBundlePlugin\Fixture\Factory;

use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleInterface;
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface;
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class ProductBundleFixtureFactory implements ExampleFactoryInterface
{
private readonly OptionsResolver $optionsResolver;

public function __construct(
private FactoryInterface $productBundleFactory,
private FactoryInterface $productBundleItemFactory,
private ProductRepositoryInterface $productRepository,
private ProductVariantRepositoryInterface $productVariantRepository,
) {
$this->optionsResolver = new OptionsResolver();
$this->configureOptions($this->optionsResolver);
}

private function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefault('bundle', '')
->setAllowedTypes('bundle', 'string')
->setDefault('items', [])
->setAllowedTypes('items', 'array')
->setDefault('is_packed', '')
->setAllowedTypes('is_packed', 'bool')
;
}

public function create(array $options = []): ProductBundleInterface
{
$options = $this->optionsResolver->resolve($options);

$bundleProduct = $this->productRepository->findOneByCode($options['bundle']);
/** @var ProductBundleInterface $productBundle */
$productBundle = $this->productBundleFactory->createNew();
$productBundle->setProduct($bundleProduct);
$productBundle->setIsPackedProduct($options['is_packed']);

foreach ($options['items'] ?? [] as $item) {
/** @var ProductVariantInterface $productVariant */
$productVariant = $this->productVariantRepository->findOneBy(['code' => $item]);
/** @var ProductBundleItemInterface $bundleItem */
$bundleItem = $this->productBundleItemFactory->createNew();
$bundleItem->setProductVariant($productVariant);
$bundleItem->setQuantity(1);
$bundleItem->setProductBundle($productBundle);
$productBundle->addProductBundleItem($bundleItem);
}

return $productBundle;
}
}
32 changes: 32 additions & 0 deletions src/Fixture/ProductBundleFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

declare(strict_types=1);

namespace BitBag\SyliusProductBundlePlugin\Fixture;

use Sylius\Bundle\CoreBundle\Fixture\AbstractResourceFixture;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

final class ProductBundleFixture extends AbstractResourceFixture implements FixtureInterface
{
protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void
{
$resourceNodeChildren = $resourceNode->children();
$resourceNodeChildren->scalarNode('bundle')->end();
$resourceNodeChildren->arrayNode('items')->scalarPrototype()->end();
$resourceNodeChildren->booleanNode('is_packed')->end();
}

public function getName(): string
{
return 'product_bundle';
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<import resource="services/menu.xml"/>
<import resource="services/factory.xml"/>
<import resource="services/filter.xml"/>
<import resource="services/fixture.xml"/>
<import resource="services/controller.xml"/>
<import resource="services/handler.xml"/>
<import resource="services/processor.xml"/>
Expand Down
20 changes: 20 additions & 0 deletions src/Resources/config/services/fixture.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="bitbag_sylius_product_bundle.fixture.product_bundle"
class="BitBag\SyliusProductBundlePlugin\Fixture\ProductBundleFixture">
<argument type="service" id="bitbag_sylius_product_bundle.manager.product_bundle"/>
<argument type="service" id="bitbag_sylius_product_bundle.fixture.factory.product_bundle"/>
<tag name="sylius_fixtures.fixture"/>
</service>
<service id="bitbag_sylius_product_bundle.fixture.factory.product_bundle"
class="BitBag\SyliusProductBundlePlugin\Fixture\Factory\ProductBundleFixtureFactory">
<argument type="service" id="bitbag_sylius_product_bundle.factory.product_bundle"/>
<argument type="service" id="bitbag_sylius_product_bundle.factory.product_bundle_item"/>
<argument type="service" id="sylius.repository.product"/>
<argument type="service" id="sylius.repository.product_variant"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
imports:
- { resource: "@BitBagSyliusProductBundlePlugin/Resources/config/config.yml" }
- { resource: "@BitBagSyliusProductBundlePlugin/Resources/config/config.yml" }

sylius_fixtures:
suites:
default:
fixtures:
tshirt_bundle_products:
name: product
options:
custom:
- name: 'Packed T-Shirt bundle'
tax_category: 'clothing'
channels:
- 'FASHION_WEB'
main_taxon: 'mens_t_shirts'
taxons:
- 't_shirts'
- 'mens_t_shirts'

- name: 'Not packed T-Shirt bundle'
tax_category: 'clothing'
channels:
- 'FASHION_WEB'
main_taxon: 'womens_t_shirts'
taxons:
- 't_shirts'
- 'womens_t_shirts'

tshirt_bundles:
name: product_bundle
options:
custom:
- bundle: 'Packed_T_Shirt_bundle'
items:
- 'Sport_basic_white_T_Shirt-variant-0'
- 'Raglan_grey_&_black_Tee-variant-0'
- 'Oversize_white_cotton_T_Shirt-variant-0'
is_packed: true

- bundle: 'Not_packed_T_Shirt_bundle'
items:
- 'Everyday_white_basic_T_Shirt-variant-0'
- 'Loose_white_designer_T_Shirt-variant-0'
- 'Ribbed_copper_slim_fit_Tee-variant-0'
is_packed: false

0 comments on commit 9124743

Please sign in to comment.