-
Notifications
You must be signed in to change notification settings - Fork 30
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 #70 from loevgaard/fixtures-improvements
Fixtures improvements
- Loading branch information
Showing
12 changed files
with
162 additions
and
48 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
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') | ||
; | ||
} | ||
} |
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
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() | ||
; | ||
} | ||
} |
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,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']); | ||
} | ||
} |
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
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); | ||
} | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes