Comments (4)
Ho yes, of course, double @template
, that makes sense. I'm in favor of this idea, even if it would break some CIs... that's not a big deal IMO. What's your opinon on this @kbond?
About $factory->dataThatShouldBeDifferent(faker()->randomElement($someDataToChooseFrom));
, I think you could use the lazy()
helper.
from foundry.
One general problem with the FactoryCollection<T>
template type is that it only references the class of the object to be produced – not the actual factory class to produce the object.
So the factory class is technically known when a factory collection is created (because a factory instance is passed), but the information gets lost in the construction.
Therefore, calls like
$factories = MyFactory::new()->many();
foreach ($factories->all() as $factory) {
$factory->myMethodDefinedInMyFactoryClass();
}
are not possible, because the moment we get the factory in the loop, it is just a general Factory<MyObject>
, not a MyFactory
.
This could be changed if T
on FactoryCollection
was changed to represent the factory class instead of the to produced object class.
from foundry.
Hi @janopae
sorry for the late reply.
One general problem with the FactoryCollection template type is that it only references the class of the object to be produced – not the actual factory class to produce the object.
This is actually true. In foundry v2, this is still the case. We have:
/**
* @template T
* @implements \IteratorAggregate<Factory<T>>
*/
final class FactoryCollection implements \IteratorAggregate
Maybe your solution would as well fix some problem with proxy/not-proxy objects in factory collection.
But this would break some CI in the wild... on the other hand, I think a lot of user still have not migrated to Foundry v2, so maybe this would be acceptable...
I'm just wondering how we should document FactoryCollection::create()
which is now:
/**
* @return T[]
*/
public function create(array|callable $attributes = []): array
Any thoughts, @kbond?
By the way:
Therefore, calls like
$factories = MyFactory::new()->many(); foreach ($factories->all() as $factory) { $factory->myMethodDefinedInMyFactoryClass(); }are not possible, because the moment we get the factory in the loop, it is just a general Factory, not a MyFactory.
genuine question: do you really need to do this? I usually go for MyFactory::new()->myMethodDefinedInMyFactoryClass()->many()->create()
and never need to call FactoryCollection::all()
from foundry.
Thanks for taking the time to reply :)
I'm just wondering how we should document FactoryCollection::create()
I'd write it like this:
/**
* @template TModel
* @template TFactory of Factory<TModel>
* @implements \IteratorAggregate<TFactory>
*/
final class FactoryCollection implements \IteratorAggregate
// ...
/**
* @return TModel[]
*/
public function create(array|callable $attributes = []): array
genuine question: do you really need to do this? I usually go for MyFactory::new()-> myMethodDefinedInMyFactoryClass()->many()->create() and never need to call FactoryCollection::all()
In my case, I needed some data that was all the same for all created objects, and some data that needed to be different.
I wrote something like
$factories = MyFactory::new()->setDataThatShouldBeTheSame()->many();
foreach ($factories->all() as $factory) {
$factory->dataThatShouldBeDifferent(faker()->randomElement($someDataToChooseFrom));
}
If there's a more elegant way to do this, I'd love to switch to that.
from foundry.
Related Issues (20)
- Find functions do not refresh entity HOT 2
- Attribute wrapped in a Foundry Proxy is not compatible with `Symfony\Component\VarExporter\LazyProxyTrait` HOT 6
- Foundry should not remove symfony-s errorHandlers HOT 6
- Global state relationships and flush_after HOT 13
- How to fix deprecation notices HOT 9
- [2.0] Problem with inversed `OneToOne` relationships HOT 4
- [2.0] No autocompletion for Proxy objects HOT 2
- [2.0] ResetDatabase with DamaDoctrineTestBundle and symfony/doctrine-messenger library HOT 1
- [2.0] Re-add `assertPersisted()` & `assertNotPersisted()` HOT 6
- [2.0] Customize Faker's locale HOT 6
- Foundry 2 with PHPUnit changes the error handler of Symfony HOT 4
- createMany does not work with references to third entities HOT 1
- Cannot access actual factory instance in after/before Instantiate hooks HOT 1
- Memory leak during PHPunit test with the ResetDatabase HOT 12
- Using `Zenstruck\Foundry\Test\Factories` triggers deprecations HOT 4
- Database reset randomly fails HOT 12
- DAMA 8 breaks Foundry and blocks upgrading to Symfony 7 HOT 9
- Allow to reset many databases in migrate mode HOT 9
- PHP 8.4 Support HOT 1
- [Tests] provide tests for "migration configurations"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from foundry.