Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DoctrineBridge] Fix automapping #44987

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
Jan 26, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,15 @@ protected function getMappingDriverBundleConfigDefaults(array $bundleConfig, \Re
$bundleDir = func_get_arg(3);
}

$bundleDir ?? $bundleDir = \dirname($bundle->getFileName());
$bundleClassDir = \dirname($bundle->getFileName());
$bundleDir ?? $bundleDir = $bundleClassDir;

if (!$bundleConfig['type']) {
$bundleConfig['type'] = $this->detectMetadataDriver($bundleDir, $container);

if (!$bundleConfig['type'] && $bundleDir !== $bundleClassDir) {
chalasr marked this conversation as resolved.
Show resolved Hide resolved
$bundleConfig['type'] = $this->detectMetadataDriver($bundleClassDir, $container);
}
}

if (!$bundleConfig['type']) {
Expand All @@ -161,7 +166,7 @@ protected function getMappingDriverBundleConfigDefaults(array $bundleConfig, \Re

if (!$bundleConfig['dir']) {
if (\in_array($bundleConfig['type'], ['annotation', 'staticphp', 'attribute'])) {
$bundleConfig['dir'] = $bundleDir.'/'.$this->getMappingObjectDefaultName();
$bundleConfig['dir'] = $bundleClassDir.'/'.$this->getMappingObjectDefaultName();
} else {
$bundleConfig['dir'] = $bundleDir.'/'.$this->getMappingResourceConfigDirectory($bundleDir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;

/**
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
Expand Down Expand Up @@ -53,6 +54,10 @@ protected function setUp(): void
$this->extension
->method('getMappingObjectDefaultName')
->willReturn('Entity');

$this->extension
->method('getMappingResourceExtension')
->willReturn('orm');
}

public function testFixManagersAutoMappingsWithTwoAutomappings()
Expand Down Expand Up @@ -271,6 +276,75 @@ public function testUnrecognizedCacheDriverException()
$this->invokeLoadCacheDriver($objectManager, $container, $cacheName);
}

public function providerBundles()
{
yield ['AnnotationsBundle', 'annotation', '/Entity'];
if (\PHP_VERSION_ID >= 80000) {
yield ['AttributesBundle', 'attribute', '/Entity'];
}
yield ['XmlBundle', 'xml', '/Resources/config/doctrine'];
yield ['PhpBundle', 'php', '/Resources/config/doctrine'];
yield ['YamlBundle', 'yml', '/Resources/config/doctrine'];

yield ['SrcXmlBundle', 'xml', '/Resources/config/doctrine'];

yield ['NewAnnotationsBundle', 'annotation', '/src/Entity'];
yield ['NewXmlBundle', 'xml', '/config/doctrine'];
}

/**
* @dataProvider providerBundles
*/
public function testBundleAutoMapping(string $bundle, string $expectedType, string $dirSuffix)
{
$bundleDir = __DIR__.'/../Fixtures/Bundles/'.$bundle;
$bundleClassName = 'Fixtures\\Bundles\\'.$bundle.'\\'.$bundle;

if (is_dir($bundleDir.'/src')) {
require_once $bundleDir.'/src/'.$bundle.'.php';
} else {
require_once $bundleDir.'/'.$bundle.'.php';
}

/** @var BundleInterface $bundleClass */
$bundleClass = new $bundleClassName();

$mappingConfig = [
'dir' => false,
'type' => false,
'prefix' => false,
'mapping' => true,
'is_bundle' => true,
];

$this->extension
->method('getMappingResourceConfigDirectory')
->willReturnCallback(function ($bundleDir) {
if (null !== $bundleDir && is_dir($bundleDir.'/config/doctrine')) {
return 'config/doctrine';
}

return 'Resources/config/doctrine';
});

$container = $this->createContainer([], [$bundle => $bundleClassName]);

$reflection = new \ReflectionClass(\get_class($this->extension));
$method = $reflection->getMethod('getMappingDriverBundleConfigDefaults');
$method->setAccessible(true);

$this->assertSame(
[
'dir' => $bundleClass->getPath().$dirSuffix,
'type' => $expectedType,
'prefix' => $bundleClass->getNamespace().'\\Entity',
'mapping' => true,
'is_bundle' => true,
],
$method->invoke($this->extension, $mappingConfig, new \ReflectionClass($bundleClass), $container, $bundleClass->getPath())
);
}

protected function invokeLoadCacheDriver(array $objectManager, ContainerBuilder $container, $cacheName)
{
$method = new \ReflectionMethod($this->extension, 'loadObjectManagerCacheDriver');
Expand All @@ -280,10 +354,10 @@ protected function invokeLoadCacheDriver(array $objectManager, ContainerBuilder
$method->invokeArgs($this->extension, [$objectManager, $container, $cacheName]);
}

protected function createContainer(array $data = []): ContainerBuilder
protected function createContainer(array $data = [], array $extraBundles = []): ContainerBuilder
{
return new ContainerBuilder(new ParameterBag(array_merge([
'kernel.bundles' => ['FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'],
'kernel.bundles' => array_merge(['FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'], $extraBundles),
'kernel.cache_dir' => __DIR__,
'kernel.build_dir' => __DIR__,
'kernel.container_class' => 'kernel',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixtures\Bundles\AnnotationsBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AnnotationsBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixtures\Bundles\AnnotationsBundle\Entity;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;

/**
* @Entity
*/
class Person
{
/** @Id @Column(type="integer") */
protected $id;

/** @Column(type="string") */
public $name;

public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}

public function __toString(): string
{
return (string) $this->name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixtures\Bundles\AttributesBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AttributesBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixtures\Bundles\AttributesBundle\Entity;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;

#[Entity]
class Person
{
#[Id, Column(type: 'integer')]
protected $id;

#[Column(type: 'string')]
public $name;

public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}

public function __toString(): string
{
return (string) $this->name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixtures\Bundles\NewAnnotationsBundle\Entity;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;

/**
* @Entity
*/
class Person
{
/** @Id @Column(type="integer") */
protected $id;

/** @Column(type="string") */
public $name;

public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}

public function __toString(): string
{
return (string) $this->name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixtures\Bundles\NewAnnotationsBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class NewAnnotationsBundle extends Bundle
{
public function getPath(): string
{
return \dirname(__DIR__);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixtures\Bundles\NewXmlBundle\Entity;

class Person
{
protected $id;

public $name;

public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}

public function __toString(): string
{
return (string) $this->name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixtures\Bundles\NewXmlBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class NewXmlBundle extends Bundle
{
public function getPath(): string
{
return \dirname(__DIR__);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixtures\Bundles\PhpBundle\Entity;

class Person
{
protected $id;

public $name;

public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}

public function __toString(): string
{
return (string) $this->name;
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.