Customizing Fixtures

What are fixtures?

In Sylius, fixtures are plain PHP objects used to initialize or modify your application's state. They're especially useful for:

  • Populating the database with entities (e.g., products, customers)

  • Uploading files

  • Dispatching events

  • Preparing the environment for testing or development

Fixtures can do anything needed to set up your system’s state.

Fixture Basics

A fixture must implement the Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface and be tagged with sylius_fixtures.fixture to be recognized in a fixture suite.

sylius_fixtures:
    suites:
        my_suite_name:
            fixtures:
                my_fixture:  # Fixture key
                    priority: 0  # Lower numbers run later
                    options: ~   # Options passed to the fixture
circle-info

Note: This interface extends the ConfigurationInterface, known from Configuration classes inside DependencyInjection directories in Symfony bundles.

Why Customize Fixtures?

There are two primary reasons to customize fixtures:

  1. Development & Testing – to preload data for QA environments or demo setups.

  2. Production Initialization – to define initial shop configuration (channels, currencies, methods, etc.).

circle-exclamation

How to modify the existing Sylius fixtures?

circle-info

Listing Existing Fixtures

To view available fixtures in your project, run:

php bin/console sylius:fixtures:list

Example: Modifying the Shop Configuration

Create a config/packages/sylius_fixtures.yaml to define your shop setup. Here's an example:

Customizing Fixtures for Extended Models

If you’ve added custom fields to an entity, you'll also need to update the fixture logic. Let's assume that ShippingMethod has been extended with deliveryConditions field.

Scenario: Adding deliveryConditions to ShippingMethod

1. Extend the Example Factory

circle-exclamation
  1. Extend the Fixture Class

3. Register Services

Update your config/services.yaml:

circle-exclamation
  1. Use Your Extended Field

Now you can add the deliveryConditions key to your shipping_method fixture in sylius_fixtures.yaml:

Learn more

Last updated

Was this helpful?