Skip to content

Commit

Permalink
Merge pull request #70 from loevgaard/fixtures-improvements
Browse files Browse the repository at this point in the history
Fixtures improvements
  • Loading branch information
igormukhingmailcom authored May 19, 2020
2 parents 926d4dd + c86d1f6 commit 6771bc7
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 48 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ If you want to add a brand to your products this is the plugin to use. Use cases

## Screenshots

<details><summary>CLICK TO SEE</summary>

Menu:

![Screenshot showing admin menu](docs/images/admin-menu-with-brand.png)
Expand All @@ -33,6 +35,8 @@ Products admin pages:

![Screenshot showing brand tab at product admin update page](docs/images/admin-product-update-tab-brand.png)

</details>

## Installation

### Step 1: Download the plugin
Expand Down Expand Up @@ -228,8 +232,12 @@ $ php bin/console doctrine:migrations:migrate
name: 'My brand'
code: 'my-brand'
images:
- type: logo
path: logos/my-brand.jpg
local_image:
type: logo
path: images/my-brand/logo.jpg
3rd_party_plugin_image:
type: black-and-white
path: '@SomePlugin/Resources/images/my-brand/black-and-white.jpg'
products:
- product_code_1
- product_code_2
Expand Down
18 changes: 18 additions & 0 deletions src/Fixture/BrandAwareFixtureTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Loevgaard\SyliusBrandPlugin\Fixture;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

trait BrandAwareFixtureTrait
{
protected function configureBrandResourceNode(ArrayNodeDefinition $resourceNode): void
{
$resourceNode
->children()
->scalarNode('brand')
;
}
}
6 changes: 0 additions & 6 deletions src/Fixture/BrandFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@

class BrandFixture extends AbstractResourceFixture
{
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'loevgaard_sylius_brand_plugin_brand';
}

/**
* {@inheritdoc}
*/
protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void
{
$node = $resourceNode->children();
Expand Down
18 changes: 18 additions & 0 deletions src/Fixture/BrandsAwareFixtureTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Loevgaard\SyliusBrandPlugin\Fixture;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

trait BrandsAwareFixtureTrait
{
protected function configureBrandsResourceNode(ArrayNodeDefinition $resourceNode): void
{
$resourceNode
->children()
->arrayNode('brands')->scalarPrototype()
;
}
}
36 changes: 36 additions & 0 deletions src/Fixture/Factory/BrandAwareExampleFactoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Loevgaard\SyliusBrandPlugin\Fixture\Factory;

use Loevgaard\SyliusBrandPlugin\Doctrine\ORM\BrandRepositoryInterface;
use Loevgaard\SyliusBrandPlugin\Model\BrandAwareInterface;
use Loevgaard\SyliusBrandPlugin\Model\BrandInterface;
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
use Symfony\Component\OptionsResolver\OptionsResolver;

trait BrandAwareExampleFactoryTrait
{
/** @var BrandRepositoryInterface */
protected $brandRepository;

public function __construct(BrandRepositoryInterface $brandRepository)
{
$this->brandRepository = $brandRepository;
}

protected function configureBrandOptions(OptionsResolver $resolver, int $chanceOfRandomBrand = 90): void
{
$resolver
->setDefault('brand', LazyOption::randomOneOrNull($this->brandRepository, $chanceOfRandomBrand))
->setAllowedTypes('brand', ['null', 'string', BrandInterface::class])
->setNormalizer('brand', LazyOption::findOneBy($this->brandRepository, 'code'))
;
}

protected function setBrandField(BrandAwareInterface $brandAware, array $resolvedOptions = []): void
{
$brandAware->setBrand($resolvedOptions['brand']);
}
}
41 changes: 17 additions & 24 deletions src/Fixture/Factory/BrandExampleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BrandExampleFactory extends AbstractExampleFactory
{
/** @var OptionsResolver */
private $optionsResolver;
protected $optionsResolver;

/** @var ProductRepositoryInterface */
private $productRepository;
protected $productRepository;

/** @var ProductsAssignerInterface */
private $productAssigner;
protected $productAssigner;

/** @var FactoryInterface */
protected $brandFactory;
Expand All @@ -33,37 +34,32 @@ class BrandExampleFactory extends AbstractExampleFactory
protected $productImageFactory;

/** @var ImageUploaderInterface */
private $imageUploader;

/**
* @param ProductRepositoryInterface $productRepository
* @param ProductsAssignerInterface $productAssigner
* @param FactoryInterface $brandFactory
* @param FactoryInterface $productImageFactory
* @param ImageUploaderInterface $imageUploader
*/
protected $imageUploader;

/** @var FileLocatorInterface */
protected $fileLocator;

public function __construct(
ProductRepositoryInterface $productRepository,
ProductsAssignerInterface $productAssigner,
FactoryInterface $brandFactory,
FactoryInterface $productImageFactory,
ImageUploaderInterface $imageUploader
ImageUploaderInterface $imageUploader,
FileLocatorInterface $fileLocator
) {
$this->productRepository = $productRepository;
$this->productAssigner = $productAssigner;
$this->brandFactory = $brandFactory;

$this->productImageFactory = $productImageFactory;
$this->imageUploader = $imageUploader;
$this->fileLocator = $fileLocator;

$this->optionsResolver = new OptionsResolver();

$this->configureOptions($this->optionsResolver);
}

/**
* {@inheritdoc}
*/
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver
Expand All @@ -81,9 +77,6 @@ protected function configureOptions(OptionsResolver $resolver): void
;
}

/**
* {@inheritdoc}
*/
public function create(array $options = []): BrandInterface
{
$options = $this->optionsResolver->resolve($options);
Expand All @@ -100,16 +93,16 @@ public function create(array $options = []): BrandInterface
return $brand;
}

/**
* @param BrandInterface $brand
* @param array $options
*/
private function createImages(BrandInterface $brand, array $options): void
protected function createImages(BrandInterface $brand, array $options): void
{
foreach ($options['images'] as $image) {
$imagePath = $image['path'];
$imageType = $image['type'] ?? null;

$imagePath = $this->fileLocator->locate($imagePath);
if (is_array($imagePath)) {
$imagePath = $imagePath[array_key_first($imagePath)];
}
$uploadedImage = new UploadedFile($imagePath, basename($imagePath));

/** @var BrandImageInterface $brandImage */
Expand Down
46 changes: 46 additions & 0 deletions src/Fixture/Factory/BrandsAwareExampleFactoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Loevgaard\SyliusBrandPlugin\Fixture\Factory;

use Loevgaard\SyliusBrandPlugin\Doctrine\ORM\BrandRepositoryInterface;
use Loevgaard\SyliusBrandPlugin\Model\BrandAwareInterface;
use Loevgaard\SyliusBrandPlugin\Model\BrandInterface;
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Webmozart\Assert\Assert;

trait BrandsAwareExampleFactoryTrait
{
/** @var BrandRepositoryInterface */
protected $brandRepository;

/** @var \Faker\Generator */
protected $faker;

public function __construct(BrandRepositoryInterface $brandRepository)
{
$this->brandRepository = $brandRepository;
if (null === $this->faker) {
$this->faker = \Faker\Factory::create();
}
}

protected function configureBrandsOptions(OptionsResolver $resolver, int $amount = 10): void
{
$resolver
->setDefault('brands', LazyOption::randomOnes($this->brandRepository, $amount))
->setAllowedTypes('brands', ['array'])
->setNormalizer('brands', LazyOption::findBy($this->brandRepository, 'code'))
;
}

protected function setBrandField(BrandAwareInterface $brandAware, array $resolvedOptions = []): void
{
$brand = $this->faker->randomElement($resolvedOptions['brands']);
Assert::isInstanceOf($brand, BrandInterface::class);

$brandAware->setBrand($brand);
}
}
32 changes: 16 additions & 16 deletions src/Resources/config/app/fixtures.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ sylius_fixtures:
suites:
default:
fixtures:

product:
options:
custom:
product_setono_mug:
name: Setono Mug
code: setono-mug
main_taxon: mugs
taxons: [mugs]
product_setono_cap:
name: Setono Cap
code: setono-cap
main_taxon: caps
taxons: [caps]
product_setono_macbook_sticker:
name: Setono Macbook Sticker
code: setono-macbook-sticker
main_taxon: stickers
taxons: [stickers]
name: Setono T-Shirt
code: setono-t_shirt
main_taxon: t_shirts
taxons: [t_shirts]

loevgaard_sylius_brand_plugin_brand:
options:
custom:
Expand All @@ -23,16 +25,14 @@ sylius_fixtures:
code: setono
images:
- type: logo
path: fixtures/images/logos/setono.jpg
- path: fixtures/images/logos/setono.jpg
- type: logo
path: fixtures/images/logos/setono.jpg
path: '@LoevgaardSyliusBrandPlugin/Resources/fixtures/images/logos/setono.jpg'
- path: '@LoevgaardSyliusBrandPlugin/Resources/fixtures/images/logos/setono-borderless.png'
products:
- setono-mug
- setono-macbook-sticker
- setono-cap
- setono-t_shirt
brand_sylius:
name: Sylius
code: sylius
images:
- type: logo
path: fixtures/images/logos/sylius.png
path: '@LoevgaardSyliusBrandPlugin/Resources/fixtures/images/logos/sylius.png'
1 change: 1 addition & 0 deletions src/Resources/config/services/fixture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<argument type="service" id="loevgaard_sylius_brand.factory.brand"/>
<argument type="service" id="loevgaard_sylius_brand.factory.brand_image"/>
<argument type="service" id="sylius.image_uploader"/>
<argument type="service" id="file_locator" />
</service>

</services>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6771bc7

Please sign in to comment.