Creating Sylius Fixtures


What are fixtures in Sylius?

Fixtures are plain PHP objects that are designed to change current system state by either persisting some entities to the database, uploading files or doing anything else that you might require.

In this post we will be going through the process of creating a bundle and in it our custom fixture , assigning it to our suite.

Opening a bundle

First thing, we will need to create a bundle. To do so, in  “src/” directory we’ll create our bundle directory called “InchooBundle” and create a php file with the same name in it.

The class will serve as an entry point and bundle declaration.

namespace InchooBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class InchooBundle extends Bundle
    function __construct()

Next thing we need to do is to let autoloader know that he can find our custom bundle. We will do that by modifing composer.json in the root of the project.

"autoload": {
        "psr-4": {
            "AppBundle\\": "src/AppBundle/",
            "InchooBundle\\": "src/InchooBundle/"
        "classmap": ["app/AppKernel.php", "app/AppCache.php"]
You will also need to run “composer dump-autoload” in console.

Now, before we continue with creating our fixture, we need to declare Dependancy injection Extension class.

To do so, we’ll create directory “DependencyInjection” and create php file “InchooExtenison.php”  in it, where we will point to our services configuration file.

There we will be able to declare our fixtures.


namespace InchooBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
class InchooExtension extends Extension
    public function load(array $configs, ContainerBuilder $container)
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        //Note that you can use Loader\YmlFileLoader in case you want to keep your services in .yml format

Also we will need to create “Configuration.php” next to it.

namespace InchooBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
    public function getConfigTreeBuilder()
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('app');
        return $treeBuilder;

This configuration class is required by Sylius to help find configuration files.

Create Fixture class

Now that we have pointed to it, we need to create service configuration. We will do so by creating “Resources/config” directories, and in there we will create “services.xml”.

<?xml version="1.0" encoding="UTF-8"?>
        <!-- it is given a specific id and class path on which it can be found -->
        <service id="inchoo.fixture.products" class="InchooBundle\Fixture\InchooProductFixture">
            <!-- First two arguments are sylius services to be injected , they are used to generate products and products attributes -->
            <argument type="service" id="sylius.fixture.product_attribute" />
            <argument type="service" id="sylius.fixture.product" />
            <!-- To register it among sylius fixtures , service needs to have this tag -->
            <tag name="sylius_fixtures.fixture" />

Now we can go ahead and create our Fixture class. In service, we have declared that class path is  “InchooBundle\Fixture\InchooProductFixture”, this is where we will create our “InchooProductFixture.php” file.

You can access a whole Fixture class on github,  to save space I will go only through main methods.

In Fixture class you have 3 primary methods to keep in mind:

     * As name suggests , this one provides Sylius with name of the fixture
     * This name will be used in later configuration and as its name among listed fixtures
     * @return string
    function getName(): string
        return "inchoo_product";
     * Load is called once fixture is executed
     * @param array $options - Holds app the parameters provided to the fixture
    function load(array $options): void
        //do stuff
     * this method allows us to declare options which fixture will expect upon execution
     * @param ArrayNodeDefinition $optionsNode
     function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
         //optionsNode allows you to define parameters for fixture

One more thing – Suites

Suites are collections of configured fixtures. They allow you to define different sets that can be loaded independently.

To declare a suit, all we need to do is create a directory “app” in “Resources/config” and in there create a “config.yml”

        default: #Suite name
                inchoo_product: # our fixture
                        amount: 100 # applied option value
        inchoo: # our new inchoo suite
                inchoo_product: #our fixture
                        amount: 100

In given example we have added our fixture to default Sylius suite. It will now be triggered with the other fixtures within that suite.

Right below that suite we also declared a new suite name Inchoo, and under it we have our fixture.

You will also notice that we have added an option value that needs to follow the way we declared in our configureOptionsNode() method.


Now if we trigger

php bin/console sylius:fixtures:list

in root of our project, we will see that both our suite and fixture have been listed by Sylius.

Once we have confirmed that our fixture and suite is there, we can load our suite by running:

php bin/console sylius:fixtures:load inchoo

And this wrapps things up for this post, you can find the example on github.

Hope this was helpful and happy coding!

Related Inchoo Services

You made it all the way down here so you must have enjoyed this post! You may also like:

3 best open-source eCommerce platforms in 2021 Zrinka Antolovic
Zrinka Antolovic, | 8

3 best open-source eCommerce platforms in 2021

Sylius custom theme development – Setting up workflow powered with webpack and Symfony 4 best practices Luka Omrcen
Luka Omrcen, | 3

Sylius custom theme development – Setting up workflow powered with webpack and Symfony 4 best practices

Sylius custom theme development – Create a theme Stanislav Mihic
Stanislav Mihic, | 0

Sylius custom theme development – Create a theme

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.