Shopware 6 Fixtures
Did you ever want to create and load Doctrine-like fixtures in your Shopware 6 plugin?
Look no further!
This plugin provides an ultra-lightweight approach to fixture loading in your Shopware 6 plugin.
Usage requirements
Development requirements
Usage
Install and activate the plugin in your Shopware instance.
composer require family-office/fixtures
bin/console plugin:refresh && bin/console plugin:install FamilyOfficeFixtures --activate
We start by creating a fixture class which is implementing the FixtureInterface
.
In this example, we're creating demo data for fictional entity named Elephant
.
<?php // <plugin root>/src/Fixtures/ElephantFixtures.php
namespace PluginThatNeedsFixtures\Fixtures;
use FamilyOffice\Fixtures\Component\FixtureInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Uuid\Uuid;
class ElephantFixtures implements FixtureInterface
{
private EntityRepositoryInterface $elephantRepository;
public function __construct(EntityRepositoryInterface $elephantRepository)
{
$this->elephantRepository = $elephantRepository;
}
public function getDependencies(): array
{
return [];
}
public function load(): void
{
$this->elephantRepository->create([
'id' => Uuid::randomHex(),
'name' => 'PHP Elephant',
'color' => 'blue'
], Context::createDefaultContext());
}
}
After creating our fixture class, we will have to register it as a service.
It's automatically tagged as a fixture (fo.entity.fixture
) if you use auto configuration.
<!-- <plugin root>/src/Resources/services.xml -->
<service id="PluginThatNeedsFixtures\Fixtures\ElephantFixtures"/>
Sometimes, a fixture depends on another fixture.
Maybe our ElephantFixtures
entity require the BigEarsFixtures
to be loaded before them.
If both fixtures are defined within the same services.xml
file, the loading order can be changed by arranging the service definitions accordingly.
<!-- <plugin root>/src/Resources/services.xml -->
<!-- first loaded -->
<service id="PluginThatNeedsFixtures\Fixture\BigEarsFixtures"/>
<!-- second loaded -->
<service id="PluginThatNeedsFixtures\Fixture\ElephantFixture"/>
A perhaps easier way to depend on another fixture, which also works if the dependet on fixture is defined in a different services.xml
file, is to return all fixtures classes the current fixture depends on in the getDependencies
method.
<?php // <plugin root>/src/Fixture/ElephantFixtures.php
namespace PluginThatNeedsFixtures\Fixtures;
// ...
class ElephantFixtures implements FixtureInterface
{
private EntityRepositoryInterface $elephantRepository;
public function __construct(EntityRepositoryInterface $elephantRepository)
{
$this->elephantRepository = $elephantRepository;
}
public function getDependencies(): array
{
return [
BigEarsFixtures::class
];
}
public function load(): void
{
// ...
}
}
Circular dependencies and loading a fixture more than once are prohibited by default.
Finally, make sure the plugin you are trying to load the fixtures for is installed and activated.
If that is the case, go ahead and load the fixtures.
bin/console fo:fixtures:load
The command loads all fixtures from all installed and activated plugins.
Development
Clone the project into your Shopware installation
git clone [email protected]:Family-Office-Company/FamilyOfficeFixtures.git
cd FamilyOfficeFixtures
Install the development dependencies
make install
..and start developing!
Acknowledgements
Thanks to Freepik from www.flaticon.com for providing the plugin icon.
License
This project is licensed under the MIT license.
Feel free to do whatever you want with the code!