Skip to content

Replicate behavior of implicit entity-to-id casting #41

@Firehed

Description

@Firehed

If an entity has a property referencing the id of another entity (but isn't defined as a relation; e.g. a Column rather than ManyToOne), and you pass that entity to a find*By* method, Doctrine appears to detect the entity and automatically use its primary key in the actual query. Mocktrine does not have this same behavior, resulting in either misleading test results or forcing specific repository interactions.

Instead, Mocktrine should aim to detect that the query parameter is an entity and use its id to perform lookups.

// Most details elided
#[Entity]
class User
{
  #[Column]
  #[Id]
  public string $id;
}
#[Entity]
class Membership
{
  #[Column]
  public string $userId;
}

// These should produce the same results in Mocktrine, and do in Doctrine
$repo = $this->em->getRepository(Membership::class);
$byId = $repo->findOneBy(['userId' => $user->id]);
$byEntity = $repo->findOneBy(['userId' => $user]);
assert($byId === $byEntity); // fails only under mocktrine

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions