kununu / data-fixtures
Load data fixtures in your application for any storage
Installs: 25 997
Dependents: 1
Suggesters: 0
Security: 0
Stars: 7
Watchers: 13
Forks: 0
Open Issues: 0
Requires
- php: >=8.3
- ext-json: *
Requires (Dev)
- doctrine/dbal: ^3.9
- elasticsearch/elasticsearch: ^7.1
- kununu/scripts: >=5.1
- phpunit/phpunit: ^11.3
- psr/cache: ^2.0
- symfony/http-client: ^6.4
- symfony/http-foundation: ^6.4
Suggests
- doctrine/dbal: Load fixtures using Doctrine DBAL
- elasticsearch/elasticsearch: Load fixtures with Elasticsearch
- kununu/testing-bundle: Use this package in a Symfony application
- psr/cache: Load fixtures for implementation of the PSR6 standard
- symfony/http-client: Load fixtures with mocked data for Symfony Http client
- symfony/http-foundation: Load fixtures with mocked data for Symfony Http client
- dev-master
- v12.0.0
- v11.0.2
- v11.0.1
- v11.0.0
- v10.2.1
- v10.2.0
- v10.1.0
- v10.0.0
- v9.4.0
- v9.3.0
- v9.2.0
- v9.1.0
- v9.0.0
- v8.1.0
- v8.0.1
- 8.0.0
- v7.0.0
- 6.0.0
- v5.0.0
- 4.0.0
- 3.0.0
- 2.0.0
- 1.0.1
- 1.0.0
- dev-dependabot/composer/symfony/http-client-tw-6.4or-tw-7.0
- dev-dependabot/composer/symfony/http-foundation-tw-6.4or-tw-7.0
This package is auto-updated.
Last update: 2024-11-18 12:08:39 UTC
README
At kununu we rely on data fixtures in our tests as well in our development and testing environments. A good definition of what fixtures are is the one from the documentation of DoctrineFixturesBundle in which the design and implementation of this package was heavily based on.
Fixtures are used to load a “fake” set of data into a database that can then be used for testing or to help give you some interesting data while you’re developing your application.
What is kununu/data-fixtures?
This package provides a simple way to manage and execute the loading of data fixtures for any storage mechanism. It's design and implementation was heavily based on the Doctrine data-fixtures package. If you are interested in why we created this package check out Why kununu/data-fixtures?.
Fixtures types
Currently, this package supports the following types of fixtures:
- Doctrine DBAL Connection Fixtures which relies on Doctrine DBAL by using the Connection implementation
- Cache Pool Fixtures which relies on implementations of the PSR-6 standard
- Elasticsearch Fixtures which relies on the Elasticsearch-PHP client
- Symfony Http Client Fixtures which relies on the Symfony Http Client and Symfony Http Foundation.
Also check Directory Loader to check how to load fixtures from files in a directory.
If you are interested in knowing more about the concepts of the package, or you need to create a new fixture type check out How to create a new Fixture Type.
Install
1. Add kununu/data-fixtures to your project
Before installing this package be aware:
- You own the fixtures you load
- This package should not be used in production mode!
composer require --dev kununu/data-fixtures
2. Enable any fixture type
In order to enable the fixture types that you are interested, check out their documentation:
- Doctrine DBAL Connection Fixtures
- Cache Pool Fixtures
- Elasticsearch Fixtures
- Symfony Http Client Fixtures
Append Fixtures
By default, when loading fixtures the data storage is purged. If you want to change this behavior and instead append the fixtures you can pass false as second argument to any executor.
// By default, the data storage is purged $executor->execute($loader->getFixtures()); // If you want you can `append` the fixtures instead of purging the database $executor->execute($loader->getFixtures(), true);
Load Fixtures
In order to load fixtures the default Loader provides a couple of options:
loadFromDirectory(string $dir)
loadFromFile(string $fileName)
loadFromClassName(string $className)
addFixture(FixtureInterface $fixture)
<?php declare(strict_types=1); use Kununu\DataFixtures\Loader\ConnectionFixturesLoader; $loader = new ConnectionFixturesLoader(); $loader->loadFromDirectory('/your/directory/'); $loader->loadFromFile('/your/file.php'); $loader->loadFromClassName(MyFixtureSql::class); $loader->addFixture(new MyFixtureSql());
Initializable Fixtures
If you want your Fixture classes to be initialized you can implement the InitializableFixtureInterface
public function initializeFixture(mixed ...$args): void;
Then before loading the fixtures you need to register them in the Loader:
<?php declare(strict_types=1); use Kununu\DataFixtures\Loader\ConnectionFixturesLoader; $loader = new ConnectionFixturesLoader(); $this->loader->registerInitializableFixture( YourFixtureClass::class, // 1st argument 1, // 2nd argument 'This is an argument that will be passed to initializeFixture of YourFixtureClass', // 3rd argument [ 'field' => 'field-name', 'value' => 10, ], // 4th argument $anInstanceOfOneOfYourOtherClasses // Pass as many arguments as you like... ); $loader->addFixture(new YourFixtureClass());
Contribute
If you are interested in contributing read our contributing guidelines.
Tests
If not yet, first install composer dependencies:
composer install
Run the tests by doing:
vendor/bin/phpunit
To run tests without coverage report:
composer install
composer test
To run tests with coverage report:
composer install composer test-coverage