api-skeletons / doctrine-data-fixture
Doctrine Data Fixtures with Grouping for Laminas
Installs: 26 997
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 5
Open Issues: 1
Requires
- php: ^7.2.0 || ~8.0
- doctrine/data-fixtures: ^1.3.0
- doctrine/doctrine-module: ^2.1 || ^4.0 || ^5.0
- laminas/laminas-eventmanager: ^3.0
- laminas/laminas-modulemanager: ^2.7.2 || ^3.0.0
- laminas/laminas-servicemanager: ^3.1
Requires (Dev)
- api-skeletons/coding-standard: ^1.0.0
- doctrine/doctrine-orm-module: ^2.1 || ^3.0 || ^4.0 || ^5.0
- dprevite/lint: ^1.0.0
- laminas/laminas-i18n: ^2.7
- laminas/laminas-test: ^3.0 || ^4.0
- phpstan/phpstan-doctrine: ^1.2
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.2.0
README
This provides command line support for Doctrine Fixtures in Laminas. Often projects will have multiple sets of fixtures for different object managers or modules such as from a 3rd party API. When this is the case a tool which can run fixtures in groups is needed. Additionally dependency injection must be available to the fixtures. To accomplish these needs this module uses a Service Manager configurable on a per-group basis.
Releases
The 6.x release tags support PHP 7.2 and above.
Installation
Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.
$ composer require api-skeletons/doctrine-data-fixture
Add this module to your application's configuration:
'modules' => [ ... 'ApiSkeletons\Doctrine\DataFixture', ],
laminas-component-installer
If you use laminas-component-installer, that plugin will install doctrine-data-fixture as a module for you.
Configuration
This module builds on top of Doctrine configuration. The configuration in a module which implements fixtures is:
return [ 'doctrine' => [ 'fixture' => [ 'group1' => [ 'object_manager' => 'doctrine.entitymanager.orm_default', 'executor' => Executor::class, 'purger' => Purger::class, 'invokables' => [ 'ModuleName\Fixture\FixtureOne' => 'ModuleName\Fixture\FixtureOne', ], 'factories' => [ 'ModuleName\Fixture\FixtureTwo' => 'ModuleName\Fixture\FixtureTwoFactory', ] ], 'group2' => [ 'object_manager' => 'doctrine.entitymanager.orm_zf_doctrine_audit', ... ], ], ], ];
Each group is a Laminas ServiceManager configuration. This allows complete dependency injection control of your fixtures.
The 'executor' and 'purger' keys are optional and allow for custom classes for these functions. If omitted the Doctrine ORM classes will be used.
Listing Fixtures
index.php data-fixture:list [<group>]
List all object managers and their groups, list all groups for a given object manager, or specify object manager and group to list all fixtures for a group.
Executing Fixtures from Command Line
index.php data-fixture:import <group> [--purge-with-truncate] [--do-not-append]
The <group>
is required.
Append is the default option. This is inversed with the new --do-not-append
Options:
--purge-with-truncate
if specified will purge the object manager's tables before running fixtures.
--do-not-append
will delete all data in the database before running fixtures.
Executing Fixtures from Code
For unit testing or other times you must run your fixtures from within code
you must fetch the DataFixtureManager
with build
and pass the group name
to the service manager then load the fixtures into the Loader
manually.
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; use Doctrine\Common\DataFixtures\Purger\ORMPurger; use ApiSkeletons\Doctrine\DataFixture\Loader; // Run audit fixtures $dataFixtureManager = $application->getServiceManager() ->build('ApiSkeletons\Doctrine\DataFixture\DataFixtureManager', ['group' => 'group-name']); $loader = new Loader($dataFixtureManager); $purger = new ORMPurger(); $executor = new ORMExecutor($auditEntityManager, $purger); foreach ($dataFixtureManager->getAll() as $fixture) { $loader->addFixture($fixture); } $executor->execute($loader->getFixtures(), true);
Getting Help
index.php data-fixture:help
Important Notes
- You can only run one group at a time from the command line. If you need to run more create a script to run them in sequence.
- The ServiceManager is injected into each DataFixtureManager at getServiceLocator() so you can use instantiators which run from that level. This makes the DataFixtureManager work like a plugin manager defined with
$serviceListener->addServiceManager()
. - You cannot use abstract factories. Each fixture must be individually configured.