composer require --dev akawakaweb/sylius-fixtures-plugin
// config/bundles.php
return [
// [...]
Akawakaweb\SyliusFixturesPlugin\Symfony\SyliusFixturesPlugin::class => ['dev' => true, 'test' => true, 'test_cached' => true],
];
# config/services.yaml
services:
# [...]
when@dev: &fixtures_dev
services:
# You will define here your customization
when@test: *fixtures_dev
As an example, let's customize the default currencies story to only load EUR currency.
// src/Foundry/Story/DefaultCurrenciesStory.php
declare(strict_types=1);
namespace App\Foundry\Story;
use Akawakaweb\SyliusFixturesPlugin\Foundry\Factory\CurrencyFactory;
use Akawakaweb\SyliusFixturesPlugin\Foundry\Story\DefaultCurrenciesStoryInterface;
use Zenstruck\Foundry\Story;
final class DefaultCurrenciesStory extends Story implements DefaultCurrenciesStoryInterface
{
public function build(): void
{
CurrencyFactory::new()->withCode('EUR')->create();
}
}
# config/services.yaml
when@dev: &fixtures_dev
services:
sylius.fixtures_plugin.story.default_currencies: '@App\Foundry\Story\DefaultCurrenciesStory'
As an example, let's add a second phone number to the customer with a new default value.
// src/Entity/Customer/Customer.php
declare(strict_types=1);
namespace App\Entity\Customer;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\Customer as BaseCustomer;
/**
* @ORM\Entity
* @ORM\Table(name="sylius_customer")
*/
#[ORM\Entity]
#[ORM\Table(name: 'sylius_customer')]
class Customer extends BaseCustomer
{
/**
* @ORM\Column
*/
#[ORM\Column]
private ?string $secondPhoneNumber = null;
public function getSecondPhoneNumber(): ?string
{
return $this->secondPhoneNumber;
}
public function setSecondPhoneNumber(?string $secondPhoneNumber): void
{
$this->secondPhoneNumber = $secondPhoneNumber;
}
}
// src/Foundry/DefaultValues/CustomerDefaultValues.php
declare(strict_types=1);
namespace App\Foundry\DefaultValues;
use Akawakaweb\SyliusFixturesPlugin\Foundry\DefaultValues\DefaultValuesInterface;
use Faker\Generator;
final class CustomerDefaultValues implements DefaultValuesInterface
{
public function __construct(
private DefaultValuesInterface $decorated,
) {
}
public function __invoke(Generator $faker): array
{
return array_merge(($this->decorated)($faker), [
'secondPhoneNumber' => $faker->phoneNumber(),
]);
}
}
when@dev: &fixtures_dev
services:
App\Foundry\DefaultValues\CustomerDefaultValues:
decorates: 'sylius.fixtures_plugin.default_values.customer'
arguments:
$decorated: '@.inner'