• Stars
    star
    634
  • Rank 70,315 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 4 years ago
  • Updated 14 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.

Foundry

CI Status Code Coverage Latest Version Downloads

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

How to contribute

The test suite of this library needs one or more database, and static analysis needs to be ran on the smaller PHP version supported (currently PHP 7.2), then it comes with a full docker stack.

Install docker

You must install docker and install docker-compose at first before running the tests.

Run tests

The library is shipped with a Makefile to run tests. Each target will build and start the docker stack and install composer only if needed.

$ make help
validate                       Run sca, full test suite and validate migrations
test                           Run PHPUnit tests suite
sca                            Run static analysis
docs                           Generate documentation to docs/output
database-generate-migration    Generate new migration based on mapping in Zenstruck\Foundry\Tests\Fixtures\Entity
database-validate-mapping      Validate mapping in Zenstruck\Foundry\Tests\Fixtures\Entity
database-drop-schema           Drop database schema
composer                       Run composer command
docker-start                   Build and run containers
docker-stop                    Stop containers
docker-purge                   Purge containers
clear                          Start from a fresh install (use it for troubleshooting)

Use double-dash to pass any PHPUnit options or arguments with make:

$ make test -- --stop-on-failure
$ make test -- --filter FactoryTest
# don't use "=" options value. ie: don't do this:
$ make test -- --filter=FactoryTest

Same syntax is available for composer:

$ make composer -- info symfony/*

Run tests in different environments

You can create a .env file to change the context in which tests will execute:

USE_ORM=1
USE_ODM=1
USE_DAMA_DOCTRINE_TEST_BUNDLE=1
SYMFONY_REQUIRE=5.4.* # allowed values: 5.4.* | 6.0.* | 6.1.* | 6.2.*
PHP_VERSION=8.0 # allowed values: 8.0 | 8.1 | 8.2
PREFER_LOWEST=1 # force composer to request lowest dependencies

Change docker's ports

You can also add these variables to the .env file to change the ports used by docker:

PGSQL_PORT=5434
MONGO_PORT=27018

Execute commands in php container

You can execute any command into the php container using docker compose:

$ docker-compose exec php [your commmand] # or "docker compose" depending on your compose version

Using xdebug with PhpStorm

The php container is shipped with xdebug activated. You can use step by step debugging session with PhpStorm: you should create a server called FOUNDRY in your PHP Remote Debug, with the IDE key xdebug_foundry

PhpStorm with xdebug

Troubleshooting

IF any problem occurs with the docker stack or a make target, try to run make clear.

Migrations

Whenever an entity in the fixtures is added or updated a migration must be generated with make migrations-generate

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.

More Repositories

1

schedule-bundle

Schedule Cron jobs (commands/callbacks/bash scripts) within your Symfony application.
PHP
383
star
2

messenger-test

Assertions and helpers for testing your symfony/messenger queues.
PHP
222
star
3

browser

A fluent interface for your Symfony functional tests.
PHP
185
star
4

messenger-monitor-bundle

Batteries included UI to monitor your Messenger workers, transports, schedules, and messages.
PHP
149
star
5

assert

Standalone, lightweight, framework agnostic, test assertion library.
PHP
56
star
6

console-extra

A modular set of features to reduce configuration boilerplate for your Symfony commands.
PHP
47
star
7

console-test

Alternative, opinionated helper for testing Symfony console commands.
PHP
34
star
8

callback

Callable wrapper to validate and inject arguments.
PHP
34
star
9

image

Image file wrapper with generic transformation support.
PHP
33
star
10

mailer-test

Alternative, opinionated helpers for testing emails sent with symfony/mailer.
PHP
33
star
11

redirect-bundle

Store redirects for your site and keeps statistics on redirects and 404 errors.
PHP
25
star
12

filesystem

Wrapper for league/flysystem with alternate API and added functionality.
PHP
17
star
13

uri

Object-oriented wrapper/manipulator for parse_url with additional features.
PHP
14
star
14

twig-service-bundle

Make functions, static methods, Symfony service methods available in your twig templates.
PHP
9
star
15

changelog

Generate pretty release changelogs using the commit log and Github API.
PHP
8
star
16

collection

Helpers for iterating/paginating/filtering collections (with Doctrine ORM/DBAL implementations and batch processing utilities).
PHP
8
star
17

signed-url-bundle

Helpers for signing and verifying urls with support for temporary and single-use urls.
PHP
6
star
18

class-metadata

Add human readable class aliases & metadata with efficient lookups.
PHP
5
star
19

dimension

Wrap quantity and unit of measure with conversions/humanizers.
PHP
4
star
20

redis

Lazy proxy for php-redis with DX helpers and utilities.
PHP
4
star
21

commonmark-extensions

A collection of CommonMark extensions.
PHP
4
star
22

phpmyadmin-server

Run phpMyAdmin in the background with a PHP webserver
PHP
3
star
23

dsn

DSN parsing library with support for complex expressions.
PHP
3
star
24

memoize

Helper trait to efficiently cache expensive methods in memory.
PHP
2
star
25

bytes

Parse, manipulate, humanize, and format bytes.
PHP
1
star
26

assert-html

Fluent html assertions plugin for zenstruck/assert.
PHP
1
star
27

temp-file

Temporary file wrapper.
PHP
1
star
28

stream

Object wrapper for PHP resources.
PHP
1
star
29

dom

DOM crawler with advanced selector API and assertions.
PHP
1
star