Skip to content

Association Mappings: replace assertions by explicit exceptions #11896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: 3.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Mapping/AssociationMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,25 @@ public static function fromMappingArray(array $mappingArray): static

foreach ($mappingArray as $key => $value) {
if ($key === 'joinTable') {
assert($mapping instanceof ManyToManyAssociationMapping);
if (! $mapping instanceof ManyToManyAssociationMapping) {
throw new Exception(
"Mapping error on field '" .
$mapping->fieldName . "' in " . $mapping->sourceEntity .
" : relation is not many-to-many, but 'joinTable' is set.",
);
}

if ($value === [] || $value === null) {
continue;
}

assert($mapping instanceof ManyToManyOwningSideMapping);
if (! $mapping instanceof ManyToManyOwningSideMapping) {
throw new Exception(
"Mapping error on field '" .
$mapping->fieldName . "' in " . $mapping->sourceEntity .
" : 'joinTable' can only be set on many-to-many owning side.",
);
}

$mapping->joinTable = JoinTableMapping::fromMappingArray($value);

Expand Down
33 changes: 33 additions & 0 deletions tests/Tests/ORM/Mapping/AssociationMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ManyToManyInverseSideMapping;
use Doctrine\ORM\Mapping\OneToManyAssociationMapping;
use Exception;
use OutOfRangeException;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -89,6 +92,36 @@ public function testItThrowsWhenUnsettingUnknownProperty(): void

unset($mapping['foo']);
}

public function testItThrowsWhenJoinTableIsDefinedOnNonManyToManyProperty(): void
{
$mapping = [
'fieldName' => 'foo',
'sourceEntity' => self::class,
'targetEntity' => self::class,
'joinTable' => 'bar',
];

$this->expectException(Exception::class);
$this->expectExceptionMessage("Mapping error on field 'foo' in " . self::class . " : relation is not many-to-many, but 'joinTable' is set.");

OneToManyAssociationMapping::fromMappingArray($mapping);
}

public function testItThrowsWhenJoinTableIsDefinedOnManyToManyInverseSideProperty(): void
{
$mapping = [
'fieldName' => 'foo',
'sourceEntity' => self::class,
'targetEntity' => self::class,
'joinTable' => 'bar',
];

$this->expectException(Exception::class);
$this->expectExceptionMessage("Mapping error on field 'foo' in " . self::class . " : 'joinTable' can only be set on many-to-many owning side.");

ManyToManyInverseSideMapping::fromMappingArray($mapping);
}
}

class MyAssociationMapping extends AssociationMapping
Expand Down