Skip to content

Commit

Permalink
test: add orphan removal premutation
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Jan 4, 2025
1 parent 482fcdd commit adcb4b1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
foreach ($this->metadata as $metadatum) {
if ($classMetadata->getName() === $metadatum->class) {
$classMetadata->getAssociationMapping($metadatum->field)['cascade'] = $metadatum->cascade ? ['persist'] : [];

if ($metadatum->orphanRemoval) {
$classMetadata->getAssociationMapping($metadatum->field)['orphanRemoval'] = true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ public static function provideCascadeRelationshipsCombinations(): iterable
throw new \LogicException(\sprintf("Wrong parameters for attribute \"%s\". Association \"{$class}::\${$field}\" does not exist.", UsingRelationships::class));
}

$relationshipFields[] = ['class' => $association['sourceEntity'], 'field' => $association['fieldName']];
$relationshipFields[] = ['class' => $association['sourceEntity'], 'field' => $association['fieldName'], 'isOneToMany' => $association['type'] === ClassMetadata::ONE_TO_MANY];
if ($association['inversedBy'] ?? $association['mappedBy'] ?? null) {
$relationshipFields[] = ['class' => $association['targetEntity'], 'field' => $association['inversedBy'] ?? $association['mappedBy']];
/** @var ClassMetadata<object> $metadataTargetEntity */
$metadataTargetEntity = $persistenceManager->metadataFor($association['targetEntity']); // @phpstan-ignore argument.templateType
$associationTargetEntity = $metadataTargetEntity->getAssociationMapping($association['inversedBy'] ?? $association['mappedBy']);
$relationshipFields[] = ['class' => $associationTargetEntity['sourceEntity'], 'field' => $associationTargetEntity['fieldName'], 'isOneToMany' => $associationTargetEntity['type'] === ClassMetadata::ONE_TO_MANY];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,31 @@ private function __construct(
public readonly string $class,
public readonly string $field,
public readonly bool $cascade,
public readonly bool $orphanRemoval,
) {
}

public function __toString(): string
{
return \sprintf('%s::$%s - %s', $this->class, $this->field, $this->cascade ? 'cascade' : 'no cascade');
$name = \sprintf('%s::$%s - %s', $this->class, $this->field, $this->cascade ? 'cascade' : 'no cascade');

if ($this->orphanRemoval) {
$name = "{$name} - (orphan removal)";
}

return $name;
}

/**
* @param array{class: class-string, field: string} $source
*/
public static function fromArray(array $source, bool $cascade = false): self
public static function fromArray(array $source, bool $cascade = false, bool $orphanRemoval = false): self
{
return new self(class: $source['class'], field: $source['field'], cascade: $cascade);
return new self(class: $source['class'], field: $source['field'], cascade: $cascade, orphanRemoval: $orphanRemoval);
}

/**
* @param list<array{class: class-string, field: string}> $relationshipFields
* @param list<array{class: class-string, field: string, isOneToMany: bool}> $relationshipFields
* @return \Generator<list<static>>
*/
public static function allCombinations(array $relationshipFields): iterable
Expand All @@ -55,6 +62,8 @@ public static function allCombinations(array $relationshipFields): iterable

$total = 2 ** \count($relationshipFields);

$hasOneToMany = false;

for ($i = 0; $i < $total; ++$i) {
$temp = [];

Expand All @@ -64,9 +73,28 @@ public static function allCombinations(array $relationshipFields): iterable

$temp[] = $metadata;
$permutationName = "{$permutationName}$metadata\n";

if ($relationshipFields[$j]['isOneToMany']) {
$hasOneToMany = true;
}
}

yield $permutationName => $temp;
}

if (!$hasOneToMany) {
return;
}

// if we have at least one OneToMany relationship, we need to test with orphan removal
// let's add only one permutation with orphan removal (and all cascade to true)
$temp = [];
$permutationName = "\n";
foreach ($relationshipFields as $relationshipField) {
$metadata = self::fromArray($relationshipField, cascade: true, orphanRemoval: true);
$temp[] = $metadata;
$permutationName = "{$permutationName}$metadata\n";
}
yield $permutationName => $temp;
}
}

0 comments on commit adcb4b1

Please sign in to comment.