-
Notifications
You must be signed in to change notification settings - Fork 0
Provide mechanism to generate IDs outside of flush, "import" entities #57
Description
For a large number of tests that Mocktrine simplifies, there's setup code that's basically "create an entity in some state and pretend it's already in the database". When doing this for entities that generate or otherwise manage their own IDs this largely isn't a problem, although it's a touch clumsy. For entities that rely on #[GeneratedValue] (auto_increment ids, etc), the id won't get initialized until after a persist+flush (unless you roll your own, negating some of the benefit of the library) which frequently leads to all sorts of annoyance.
Historically you could sort of use merge() (part of EntityManager but not EntityManagerInterface) but that's dropped in 3.0 without replacement. Fair enough since it has no real runtime utility.
I think the approach here is function import(object $entity, bool $generateIdIfNeeded = true): object - it'd be the 2.x equivalent of merge() plus generating the id, though it exists outside of the main interface. It may be worthwhile to make a MocktrineInterface extends EntityManagerInterface that declares it, so that tests don't have to do anything too spooky to integrate it.