v2.4.0
Main features
feat: introduce distribute()
method by @nikophil in #826
If you have a collection of values that you want to distribute over a collection, you can use the distribute() method:
// let's say we have 2 categories...
$categories = CategoryFactory::createSequence(
[
['name' => 'category 1'],
['name' => 'category 2'],
]
);
// ...that we want to "distribute" over 2 posts
$posts = PostFactory::new()
->sequence(
[
['name' => 'post 1'],
['name' => 'post 2'],
]
)
// "post 1" will have "category 1" and "post 2" will have "category 2"
->distribute('category', $categories)
// you can even chain "distribute()" methods:
// first post is published today, second post is published tomorrow
->distribute('publishedAt', [new \DateTimeImmutable('today'), new \DateTimeImmutable('tomorrow')])
->create();
feat: introduce "reuse()" method by @nikophil in #804
When creating nested objects, sometimes it can be useful to tell Foundry to always use the same object for a given class. It can enforce coherence in your fixtures and avoid creating too many objects.
In order to do this, you can use the reuse() method: it will force Foundry to use the object passed as parameter in all ManyToOne
and OneToOne
relationships using the class of this object:
// let's say both Post and Comment classes have a ManyToOne field "author" of class User
$user = UserFactory::createOne();
PostFactory::new([
'comments' => CommentFactory::new()->many(5),
])
// by calling reuse, the post and all its comments will have the same author
->reuse($user)
->create();
feat(faker): Improve reproducibility with faker by @nikophil in #807
Reproducibility with faker is improved: now, all test run will use a random seed (by default, it used to be passed null
), and if you're using Foundry's extension for PHPUnit, this seed will be displayed at the end of the tests.
You can also freeze the seed by using the environment variable FOUNDRY_FAKER_SEED
➜ FOUNDRY_FAKER_SEED=1234 vendor/bin/phpunit
................................... 35 / 35 (100%)
Faker seed: 1234
Time: 00:00.047, Memory: 48.50 MB
feat(maker): allow no hints by @nikophil in #857
You can now disable the "beginner" hints which are generated by the make:factory
command:
# config/packages/zenstruck_foundry.yaml
zenstruck_foundry:
make_factory:
add_hints: false
feat: add force()
helper (#854) by @nikophil
Foundry already allows you to "force" a property globally to a factory. Now with the force()
helper, you can force it at a single place:
use App\Factory\PostFactory;
use function Zenstruck\Foundry\force;
// in this case, the "body" attribute will be set directly, without using the setter
PostFactory::createOne(['body' => force('some body')]) ;
// in this case, the "title" attribute will still be used in the constructor (otherwise an error would be thrown)
PostFactory::createOne(['title' => force('some title')]) ;
// ...unless we disable the constructor:
PostFactory::new()
->instantiateWith(Instantiator::withoutConstructor())
->create(['title' => force('some title')]) ;
659a7bc minor: use mt_rand
instead of random_int
(#869) by @kbond
348b28d docs: fix default_namespace (#872) by @ebedy
92d9f28 chore(phpbench): actually run phpbench with a baseline (#868) by @nikophil
1a829e5 feat: optimize performance of repository::random() (#867) by @mdeboer
5ccbe51 feat: add support for benchmarks using phpbench (#866) by @mdeboer, @nikophil
2df354c fix: performance problem with reuse (#865) by @nikophil
0747e04 docs: document Faker reproducibility (#860) by @nikophil
f8cc3a0 fix: handle empty constructors (#859) by @nikophil
cb63756 chore: merge 2.3.x (#858) by @nikophil, @mdeboer, @Chris53897
b1e7aec feat(maker): allow no hints (#857) by @nikophil
59d617c fixes typo (#850) by @mvhirsch
5cc8575 feat: introduce "reuse()" method (#804) by @nikophil, @kbond
21f32b8 docs: fix wrong class name (#846) by @nikophil
48d9249 docs: minor fixes (#837) by @nikophil
bdda45c doc: fixes linking to object-proxy (#825) by @mvhirsch
719710a test: ensure Proxy::_real() always return same object (#809) by @nikophil
d15de0e feat: introduce distribute()
method (#826) by @nikophil
5647b5c fix: prevent infinite loop when ->create() is called in after persist callback (#832) by @nikophil
6e1d726 fix: fix failing faker test due to csfix (#829) by @nikophil
7b33216 minor: deprecate auto-persist (#818) by @nikophil
eb6e983 feat(faker): Improve reproducibility with faker (#807) by @nikophil
ae96d19 chore: use PHPUnit 12 (#810) by @nikophil
413bb10 chore: upgrade phpstan (#828) by @nikophil
fbf0981 fix: actually disable persistence cascade (#817) by @nikophil
2426f3e fix: trigger after persist callbacks for entities scheduled for insert (#822) by @nikophil
dea6246 fix(doc): update yml config file for reset keys (#819) by @asalisaf
da1e9db docs: Make sure we add links on separate lines(#823) by @Nyholm
ad8d72c fix: can index one to many relationships based on "indexBy" (#815) by @nikophil
f76cba2 fix: fix deprecation message for Factories trait (#806) by @nikophil
207562f fix: remove APP_ENV from .env (#803) by @nikophil
9032c38 feat: skip readonly properties on entities when generating factories (#798) by @KDederichs, @nikophil