Skip to content

[Autocomplete] Fix tests for ORM 3.0 #1638

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

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions src/Autocomplete/src/Doctrine/EntityMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\UX\Autocomplete\Doctrine;

use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\Persistence\Mapping\ClassMetadata;

/**
Expand Down Expand Up @@ -75,8 +76,14 @@ public function getFieldMetadata(string $propertyName): array
public function getAssociationMetadata(string $propertyName): array
{
if (\array_key_exists($propertyName, $this->metadata->associationMappings)) {
// Cast to array, because in doctrine/orm:^3.0; $metadata will be an AssociationMapping object
return (array) $this->metadata->associationMappings[$propertyName];
$associationMapping = $this->metadata->associationMappings[$propertyName];

// Doctrine ORM 3.0
if (class_exists(AssociationMapping::class) && $associationMapping instanceof AssociationMapping) {
return $associationMapping->toArray();
}

return $associationMapping;
}

throw new \InvalidArgumentException(sprintf('The "%s" field does not exist in the "%s" entity.', $propertyName, $this->metadata->getName()));
Expand Down
12 changes: 10 additions & 2 deletions src/Autocomplete/tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\UX\Autocomplete\Tests\Fixtures;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\ORM\Mapping\AssociationMapping;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
Expand Down Expand Up @@ -100,7 +101,7 @@ protected function configureContainer(ContainerConfigurator $c): void
'auto_refresh_proxies' => false,
]);

$c->extension('doctrine', [
$config = [
'dbal' => ['url' => '%env(resolve:DATABASE_URL)%'],
'orm' => [
'auto_generate_proxy_classes' => true,
Expand All @@ -115,7 +116,14 @@ protected function configureContainer(ContainerConfigurator $c): void
],
],
],
]);
];
if (class_exists(AssociationMapping::class)) {
// Doctrine ORM >= 3.0
$config['orm']['controller_resolver'] = [
'auto_mapping' => true,
];
}
$c->extension('doctrine', $config);

$c->extension('security', [
'password_hashers' => [
Expand Down
76 changes: 49 additions & 27 deletions src/Autocomplete/tests/Integration/Doctrine/EntityMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\UX\Autocomplete\Tests\Integration\Doctrine;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\Autocomplete\Doctrine\EntityMetadata;
use Symfony\UX\Autocomplete\Doctrine\EntityMetadataFactory;
Expand Down Expand Up @@ -51,13 +50,16 @@ public function testGetPropertyDataType(): void
{
$metadata = $this->getMetadata();
$this->assertSame(Types::STRING, $metadata->getPropertyDataType('name'));
$this->assertEquals(ClassMetadataInfo::MANY_TO_ONE, $metadata->getPropertyDataType('category'));
// ORM 2.* ClassMetadataInfo::MANY_TO_ONE
// ORM 3.* ClassMetadata::MANY_TO_ONE
$this->assertEquals(2, $metadata->getPropertyDataType('category'));
}

public function testGetFieldMetadata(): void
{
$metadata = $this->getMetadata();
$this->assertSame([
$nameMetadata = $metadata->getFieldMetadata('name');
$expected = [
'fieldName' => 'name',
'type' => 'string',
'scale' => null,
Expand All @@ -66,48 +68,68 @@ public function testGetFieldMetadata(): void
'nullable' => false,
'precision' => null,
'columnName' => 'name',
], $metadata->getFieldMetadata('name'));
];
foreach ($expected as $key => $value) {
$this->assertArrayHasKey($key, $nameMetadata);
$this->assertSame($value, $nameMetadata[$key]);
}
}

public function testGetAssociationMetadata(): void
{
$metadata = $this->getMetadata();
$this->assertSame([
$expected = [
'fieldName' => 'category',
'joinColumns' => [
[
'name' => 'category_id',
'unique' => false,
'nullable' => false,
'onDelete' => null,
'columnDefinition' => null,
'referencedColumnName' => 'id',
],
],
'cascade' => [],
'inversedBy' => 'products',
'targetEntity' => 'Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Category',
'fetch' => 2,
'type' => 2,
'mappedBy' => null,
'isOwningSide' => true,
'sourceEntity' => 'Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Product',
'isCascadeRemove' => false,
'isCascadePersist' => false,
'isCascadeRefresh' => false,
'isCascadeMerge' => false,
'isCascadeDetach' => false,
'sourceToTargetKeyColumns' => [
'category_id' => 'id',
],
'joinColumnFieldNames' => [
'category_id' => 'category_id',
],
'targetToSourceKeyColumns' => [
'id' => 'category_id',
],
'orphanRemoval' => false,
], $metadata->getAssociationMetadata('category'));
];

$metadata = $metadata->getAssociationMetadata('category');

foreach ($expected as $key => $val) {
$this->assertArrayHasKey($key, $metadata);
if (!\is_array($val)) {
$this->assertEquals($val, $metadata[$key]);
continue;
}
foreach ($val as $k => $v) {
$this->assertArrayHasKey($k, $metadata[$key]);
$this->assertEquals($v, $metadata[$key][$k]);
}
}

$this->assertArrayHasKey('joinColumns', $metadata);
$this->assertCount(1, $metadata['joinColumns']);
$expectedJoinColumn = [
'name' => 'category_id',
'columnDefinition' => null,
// Doctrine 3.0 removed
// 'fieldName' => 'category_id',
'unique' => false,
'nullable' => true,
'referencedColumnName' => 'id',
];
$this->assertArrayHasKey(0, $metadata['joinColumns']);
$columnMetadata = $metadata['joinColumns'][0];
foreach ($expectedJoinColumn as $key => $val) {
$this->assertArrayHasKey($key, $columnMetadata);
// Doctrine 3.0 changed the way it determines 'nullable' for join columns
if ('nullable' === $key) {
$this->assertIsBool($columnMetadata[$key]);
continue;
}
$this->assertSame($val, $columnMetadata[$key]);
}
}

public function testIsEmbeddedClassProperty(): void
Expand Down