Skip to content

Commit 9124743

Browse files
committed
OP-289: Add fixtures
1 parent 09dc0e8 commit 9124743

5 files changed

Lines changed: 170 additions & 1 deletion

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusProductBundlePlugin\Fixture\Factory;
13+
14+
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleInterface;
15+
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface;
16+
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
17+
use Sylius\Component\Core\Model\ProductVariantInterface;
18+
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
19+
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
20+
use Sylius\Component\Resource\Factory\FactoryInterface;
21+
use Symfony\Component\OptionsResolver\OptionsResolver;
22+
23+
final class ProductBundleFixtureFactory implements ExampleFactoryInterface
24+
{
25+
private readonly OptionsResolver $optionsResolver;
26+
27+
public function __construct(
28+
private FactoryInterface $productBundleFactory,
29+
private FactoryInterface $productBundleItemFactory,
30+
private ProductRepositoryInterface $productRepository,
31+
private ProductVariantRepositoryInterface $productVariantRepository,
32+
) {
33+
$this->optionsResolver = new OptionsResolver();
34+
$this->configureOptions($this->optionsResolver);
35+
}
36+
37+
private function configureOptions(OptionsResolver $resolver): void
38+
{
39+
$resolver
40+
->setDefault('bundle', '')
41+
->setAllowedTypes('bundle', 'string')
42+
->setDefault('items', [])
43+
->setAllowedTypes('items', 'array')
44+
->setDefault('is_packed', '')
45+
->setAllowedTypes('is_packed', 'bool')
46+
;
47+
}
48+
49+
public function create(array $options = []): ProductBundleInterface
50+
{
51+
$options = $this->optionsResolver->resolve($options);
52+
53+
$bundleProduct = $this->productRepository->findOneByCode($options['bundle']);
54+
/** @var ProductBundleInterface $productBundle */
55+
$productBundle = $this->productBundleFactory->createNew();
56+
$productBundle->setProduct($bundleProduct);
57+
$productBundle->setIsPackedProduct($options['is_packed']);
58+
59+
foreach ($options['items'] ?? [] as $item) {
60+
/** @var ProductVariantInterface $productVariant */
61+
$productVariant = $this->productVariantRepository->findOneBy(['code' => $item]);
62+
/** @var ProductBundleItemInterface $bundleItem */
63+
$bundleItem = $this->productBundleItemFactory->createNew();
64+
$bundleItem->setProductVariant($productVariant);
65+
$bundleItem->setQuantity(1);
66+
$bundleItem->setProductBundle($productBundle);
67+
$productBundle->addProductBundleItem($bundleItem);
68+
}
69+
70+
return $productBundle;
71+
}
72+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusProductBundlePlugin\Fixture;
13+
14+
use Sylius\Bundle\CoreBundle\Fixture\AbstractResourceFixture;
15+
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
16+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
17+
18+
final class ProductBundleFixture extends AbstractResourceFixture implements FixtureInterface
19+
{
20+
protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void
21+
{
22+
$resourceNodeChildren = $resourceNode->children();
23+
$resourceNodeChildren->scalarNode('bundle')->end();
24+
$resourceNodeChildren->arrayNode('items')->scalarPrototype()->end();
25+
$resourceNodeChildren->booleanNode('is_packed')->end();
26+
}
27+
28+
public function getName(): string
29+
{
30+
return 'product_bundle';
31+
}
32+
}

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<import resource="services/menu.xml"/>
77
<import resource="services/factory.xml"/>
88
<import resource="services/filter.xml"/>
9+
<import resource="services/fixture.xml"/>
910
<import resource="services/controller.xml"/>
1011
<import resource="services/handler.xml"/>
1112
<import resource="services/processor.xml"/>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
<services>
6+
<service id="bitbag_sylius_product_bundle.fixture.product_bundle"
7+
class="BitBag\SyliusProductBundlePlugin\Fixture\ProductBundleFixture">
8+
<argument type="service" id="bitbag_sylius_product_bundle.manager.product_bundle"/>
9+
<argument type="service" id="bitbag_sylius_product_bundle.fixture.factory.product_bundle"/>
10+
<tag name="sylius_fixtures.fixture"/>
11+
</service>
12+
<service id="bitbag_sylius_product_bundle.fixture.factory.product_bundle"
13+
class="BitBag\SyliusProductBundlePlugin\Fixture\Factory\ProductBundleFixtureFactory">
14+
<argument type="service" id="bitbag_sylius_product_bundle.factory.product_bundle"/>
15+
<argument type="service" id="bitbag_sylius_product_bundle.factory.product_bundle_item"/>
16+
<argument type="service" id="sylius.repository.product"/>
17+
<argument type="service" id="sylius.repository.product_variant"/>
18+
</service>
19+
</services>
20+
</container>
Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
11
imports:
2-
- { resource: "@BitBagSyliusProductBundlePlugin/Resources/config/config.yml" }
2+
- { resource: "@BitBagSyliusProductBundlePlugin/Resources/config/config.yml" }
3+
4+
sylius_fixtures:
5+
suites:
6+
default:
7+
fixtures:
8+
tshirt_bundle_products:
9+
name: product
10+
options:
11+
custom:
12+
- name: 'Packed T-Shirt bundle'
13+
tax_category: 'clothing'
14+
channels:
15+
- 'FASHION_WEB'
16+
main_taxon: 'mens_t_shirts'
17+
taxons:
18+
- 't_shirts'
19+
- 'mens_t_shirts'
20+
21+
- name: 'Not packed T-Shirt bundle'
22+
tax_category: 'clothing'
23+
channels:
24+
- 'FASHION_WEB'
25+
main_taxon: 'womens_t_shirts'
26+
taxons:
27+
- 't_shirts'
28+
- 'womens_t_shirts'
29+
30+
tshirt_bundles:
31+
name: product_bundle
32+
options:
33+
custom:
34+
- bundle: 'Packed_T_Shirt_bundle'
35+
items:
36+
- 'Sport_basic_white_T_Shirt-variant-0'
37+
- 'Raglan_grey_&_black_Tee-variant-0'
38+
- 'Oversize_white_cotton_T_Shirt-variant-0'
39+
is_packed: true
40+
41+
- bundle: 'Not_packed_T_Shirt_bundle'
42+
items:
43+
- 'Everyday_white_basic_T_Shirt-variant-0'
44+
- 'Loose_white_designer_T_Shirt-variant-0'
45+
- 'Ribbed_copper_slim_fit_Tee-variant-0'
46+
is_packed: false

0 commit comments

Comments
 (0)