zenstruck / foundry
A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
Fund package maintenance!
kbond
Installs: 5 576 260
Dependents: 64
Suggesters: 2
Security: 0
Stars: 658
Watchers: 9
Forks: 75
Open Issues: 32
Requires
- php: >=8.1
- doctrine/persistence: ^2.0|^3.0
- fakerphp/faker: ^1.23
- symfony/deprecation-contracts: ^2.2|^3.0
- symfony/framework-bundle: ^6.4|^7.0
- symfony/property-access: ^6.4|^7.0
- symfony/var-exporter: ^6.4.9|~7.0.9|^7.1.2
- zenstruck/assert: ^1.4
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8
- dama/doctrine-test-bundle: ^7.0|^8.0
- doctrine/collections: ^1.7|^2.0
- doctrine/common: ^3.2
- doctrine/doctrine-bundle: ^2.10
- doctrine/doctrine-migrations-bundle: ^2.2|^3.0
- doctrine/mongodb-odm-bundle: ^4.6|^5.0
- doctrine/orm: ^2.16|^3.0
- phpunit/phpunit: ^9.5.0 || ^10.0 || ^11.0
- symfony/console: ^6.4|^7.0
- symfony/dotenv: ^6.4|^7.0
- symfony/maker-bundle: ^1.55
- symfony/phpunit-bridge: ^6.4|^7.0
- symfony/translation-contracts: ^3.4
- symfony/var-dumper: ^6.4|^7.0
- symfony/yaml: ^6.4|^7.0
- 2.x-dev
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- 1.x-dev
- v1.38.3
- v1.38.2
- v1.38.1
- v1.38.0
- v1.37.0
- v1.36.1
- v1.36.0
- v1.35.0
- v1.34.1
- v1.34.0
- v1.33.0
- v1.32.0
- v1.31.0
- v1.30.3
- v1.30.2
- v1.30.1
- v1.30.0
- v1.29.0
- v1.28.0
- v1.27.0
- v1.26.0
- v1.25.0
- v1.24.1
- v1.24.0
- 1.23.x-dev
- v1.23.0
- v1.22.1
- v1.22.0
- v1.21.1
- v1.21.0
- v1.20.0
- v1.19.0
- v1.18.2
- v1.18.1
- v1.18.0
- v1.17.0
- v1.16.0
- v1.15.0
- v1.14.1
- v1.14.0
- v1.13.4
- v1.13.3
- v1.13.2
- v1.13.1
- v1.13.0
- v1.12.0
- v1.11.1
- v1.11.0
- v1.10.0
- v1.9.1
- v1.9.0
- v1.8.0
- v1.7.1
- v1.7.0
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
This package is auto-updated.
Last update: 2024-11-06 14:37:20 UTC
README
Foundry makes creating fixtures data fun again, via an expressive, auto-completable, on-demand fixtures system with Symfony and Doctrine:
$post = PostFactory::new() // Create the factory for Post objects ->published() // Make the post in a "published" state ->create([ // create & persist the Post object 'slug' => 'post-a' // This Post object only requires the slug field - all other fields are random data ]) ;
The factories can be used inside DoctrineFixturesBundle to load fixtures or inside your tests, where it has even more features.
Foundry supports doctrine/orm
(with doctrine/doctrine-bundle),
doctrine/mongodb-odm
(with doctrine/mongodb-odm-bundle)
or a combination of these.
Want to watch a screencast 🎥 about it? Check out https://symfonycasts.com/foundry
Read the Documentation Upgrade guide to v2
How to contribute
Running the Test Suite
The test suite of this library needs one or more databases, then it comes with a docker compose configuration.
Note
Docker and PHP installed locally (with mysql
, pgsql
& mongodb
extensions) is required.
You can start the containers and run the test suite:
# start the container $ docker compose up --detach # install dependencies $ composer update # run test suite with all available permutations $ composer test # run only one permutation $ ./phpunit # run test suite with dama/doctrine-test-bundle $ USE_DAMA_DOCTRINE_TEST_BUNDLE=1 vendor/bin/phpunit # run test suite with postgreSQL instead of MySQL $ DATABASE_URL="postgresql://zenstruck:zenstruck@127.0.0.1:5433/zenstruck_foundry?serverVersion=15" vendor/bin/phpunit # run test suite with another PHPUnit version $ PHPUNIT_VERSION=10 vendor/bin/phpunit
Overriding the default configuration
You can override default environment variables by creating a .env.local
file, to easily enable permutations:
# .env.local DATABASE_URL="postgresql://zenstruck:zenstruck@127.0.0.1:5433/zenstruck_foundry?serverVersion=15" # enables postgreSQL instead of MySQL MONGO_URL="" # disables Mongo USE_DAMA_DOCTRINE_TEST_BUNDLE="1" # enables dama/doctrine-test-bundle PHPUNIT_VERSION="11" # possible values: 9, 10, 11, 11.4 # run test suite with postgreSQL $ vendor/bin/phpunit
The .env.local
file can also be used to override the port of the database containers,
if it does not meet your local requirements. You'll also need to override docker compose configuration:
Here is an example to use MySQL on port 3308
:
# docker-compose.override.yml version: '3.9' services: mysql: ports: - "3308:3306"
# .env.local DATABASE_URL="mysql://root:1234@127.0.0.1:3308/foundry_test?serverVersion=5.7.42"
Running the documentation linter locally
This section provides guidance on how to run the documentation linter, contained within a Docker image, locally on your machine. This linter is designed to verify and ensure that the document syntax adheres to the required standards.
The following command runs the linter in the local directory:
docker run --rm -it -e DOCS_DIR='/docs' -v ${PWD}/docs:/docs oskarstark/doctor-rst:latest
Credit
The AAA style of testing was first introduced to me by Adam Wathan's excellent Test Driven Laravel Course. The inspiration for this libraries API comes from Laravel factories and christophrumpel/laravel-factories-reloaded.