Skip to content
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

[DDC-3300] Added resolve entities support in discrim. map #1130

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2725,7 +2725,7 @@ public function addDiscriminatorMapClass($name, $className)
if ($this->name == $className) {
$this->discriminatorValue = $name;
} else {
if ( ! class_exists($className)) {
if ( ! class_exists($className) && ! interface_exists($className)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be covered in ClassMetadataTest

throw MappingException::invalidClassInDiscriminatorMap($className, $this->name);
}
if (is_subclass_of($className, $this->name) && ! in_array($className, $this->subClasses)) {
Expand Down
82 changes: 82 additions & 0 deletions lib/Doctrine/ORM/Tools/ResolveDiscriminatorMapListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ORM\Tools;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Mapping\ClassMetadataInfo;

/**
* ResolveDiscriminatorMapListener
*
* Mechanism to overwrite interfaces or classes specified in discrimination map
*
* @author Benjamin Eberlei <[email protected]>
* @since 2.2
*/
class ResolveDiscriminatorMapListener
{
/**
* @var array
*/
private $resolveTargetEntities = array();

/**
* Construct
*
* @param array $resolveTargetEntities
*/
public function __construct(array $resolveTargetEntities)
{
$this->resolveTargetEntities = $resolveTargetEntities;
}

/**
* Processes event and resolves new target entity names.
*
* @param LoadClassMetadataEventArgs $args
*
* @return void
*/
public function loadClassMetadata(LoadClassMetadataEventArgs $args)
{
$classMetadata = $args->getClassMetadata();

if (!empty($classMetadata->discriminatorMap)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the map is still empty after remap?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmoreram can you clarify on this point? Is it covered by tests?


$this->remapDiscriminatorMap($classMetadata);
}
}

/**
* Replaces all Interfaces in discriminator map
*
* @param \Doctrine\ORM\Mapping\ClassMetadataInfo $classMetadata
*/
private function remapDiscriminatorMap(ClassMetadataInfo $classMetadata)
{
foreach ($classMetadata->discriminatorMap as $name => $interface) {

if (isset($this->resolveTargetEntities[$interface])) {

$classMetadata->discriminatorMap[$name] = $this->resolveTargetEntities[$interface];
}
}
}
}
82 changes: 82 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3300Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Events;
use Doctrine\ORM\Tools\ResolveDiscriminatorMapListener;

/**
* @group DDC-3300
*/
class DDC3300Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function testIssue()
{
$this
->_em
->getEventManager()
->addEventListener(
Events::loadClassMetadata,
new ResolveDiscriminatorMapListener(array(
'Doctrine\Tests\ORM\Functional\Ticket\DDC3300BossInterface' => 'Doctrine\Tests\ORM\Functional\Ticket\DDC3300Boss',
'Doctrine\Tests\ORM\Functional\Ticket\DDC3300EmployeeInterface' => 'Doctrine\Tests\ORM\Functional\Ticket\DDC3300Employee',
))
);

$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC3300Person'),
));

$boss = new DDC3300Boss();
$this->_em->persist($boss);

$employee = new DDC3300Employee();
$this->_em->persist($employee);

$this->_em->flush();
}
}

/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DdiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({
* "boss" = "Doctrine\Tests\ORM\Functional\Ticket\DDC3300BossInterface",
* "employee" = "Doctrine\Tests\ORM\Functional\Ticket\DDC3300EmployeeInterface"
* })
*/
abstract class DDC3300Person
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
}

interface DDC3300BossInterface
{

}

/**
* @Entity
*/
class DDC3300Boss extends DDC3300Person implements DDC3300BossInterface
{
}

interface DDC3300EmployeeInterface
{

}

/**
* @Entity
*/
class DDC3300Employee extends DDC3300Person implements DDC3300EmployeeInterface
{
}

112 changes: 112 additions & 0 deletions tests/Doctrine/Tests/ORM/Tools/ResolveDiscriminatorMapListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Doctrine\Tests\ORM\Tools;

use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\MappedSuperclass;
use Doctrine\ORM\Tools\ResolveDiscriminatorMapListener;

class ResolveDiscriminatorMapListenerTest extends \Doctrine\Tests\OrmTestCase
{
/**
* @var EntityManager
*/
private $em = null;

/**
* @var ClassMetadataFactory
*/
private $factory = null;

public function setUp()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still an integration test: could you also write a unit test that does not interact with the ORM?

{
$annotationDriver = $this->createAnnotationDriver();

$this->em = $this->_getTestEntityManager();
$this->em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
$this->factory = new ClassMetadataFactory;
$this->factory->setEntityManager($this->em);
}

/**
* @group DDC-3300
*/
public function testResolveDiscriminatorMapListenerTestCanResolveDiscriminatorMap()
{
$evm = $this->em->getEventManager();
$listener = new ResolveDiscriminatorMapListener(array(
'Doctrine\Tests\ORM\Tools\BossInterface' => 'Doctrine\Tests\ORM\Tools\Boss',
'Doctrine\Tests\ORM\Tools\EmployeeInterface' => 'Doctrine\Tests\ORM\Tools\Employee',
));

$evm->addEventListener(Events::loadClassMetadata, $listener);
$cm = $this->factory->getMetadataFor('Doctrine\Tests\ORM\Tools\Person');
$meta = $cm->discriminatorMap;
$this->assertSame('Doctrine\Tests\ORM\Tools\Boss', $meta['boss']);
$this->assertSame('Doctrine\Tests\ORM\Tools\Employee', $meta['employee']);
}

/**
* @group DDC-3300
*/
public function testResolveDiscriminatorMapListenerTestCannotResolveWrongDiscriminatorMap()
{
$evm = $this->em->getEventManager();
$listener = new ResolveDiscriminatorMapListener(array(
'Doctrine\Tests\ORM\Tools\EmployeeInterface' => 'Doctrine\Tests\ORM\Tools\Employee',
));

$evm->addEventListener(Events::loadClassMetadata, $listener);
$cm = $this->factory->getMetadataFor('Doctrine\Tests\ORM\Tools\Person');
$meta = $cm->discriminatorMap;
$this->assertSame('Doctrine\Tests\ORM\Tools\BossInterface', $meta['boss']);
$this->assertSame('Doctrine\Tests\ORM\Tools\Employee', $meta['employee']);
}
}

/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({
* "boss" = "\Doctrine\Tests\ORM\Tools\BossInterface",
* "employee" = "\Doctrine\Tests\ORM\Tools\EmployeeInterface"
* })
*/
abstract class Person
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
}

interface BossInterface
{

}

class Boss extends Person implements BossInterface
{
/**
* @Column(type="integer")
*/
private $earnedMoneyInEuros;
}

interface EmployeeInterface
{

}

class Employee extends Person implements EmployeeInterface
{
/**
* @Column(type="integer")
*/
private $daysOfLifeReducedInDays;
}