Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

DDC-1544 Unit test / assertions for ResolveTargetEntityListener #222

Merged
merged 1 commit into from over 2 years ago

2 participants

Evan Coury Benjamin Eberlei
Evan Coury

Also fixed a couple of small things in the listener itself.

Benjamin Eberlei beberlei merged commit 20c8596 into from December 17, 2011
Benjamin Eberlei beberlei closed this December 17, 2011
elHornair elHornair referenced this pull request from a commit February 22, 2013
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
16  lib/Doctrine/ORM/Tools/ResolveTargetEntityListener.php
@@ -19,7 +19,8 @@
19 19
 
20 20
 namespace Doctrine\ORM\Tools;
21 21
 
22  
-use Doctrine\ORM\Events\LoadClassMetadataEventArgs;
  22
+use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
  23
+use Doctrine\ORM\Mapping\ClassMetadata;
23 24
 
24 25
 /**
25 26
  * ResolveTargetEntityListener
@@ -67,24 +68,25 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $args)
67 68
         }
68 69
     }
69 70
 
70  
-    private function remapAssocation($classMetadata, $mapping)
  71
+    private function remapAssociation($classMetadata, $mapping)
71 72
     {
72 73
         $newMapping = $this->resolveTargetEntities[$mapping['targetEntity']];
  74
+        $newMapping = array_replace_recursive($mapping, $newMapping);
73 75
         $newMapping['fieldName'] = $mapping['fieldName'];
74  
-        unset($cm->associationMappings[$mapping['fieldName']]);
  76
+        unset($classMetadata->associationMappings[$mapping['fieldName']]);
75 77
 
76 78
         switch ($mapping['type']) {
77 79
             case ClassMetadata::MANY_TO_MANY:
78  
-                $cm->mapManyToMany($newMapping);
  80
+                $classMetadata->mapManyToMany($newMapping);
79 81
                 break;
80 82
             case ClassMetadata::MANY_TO_ONE:
81  
-                $cm->mapManyToOne($newMapping);
  83
+                $classMetadata->mapManyToOne($newMapping);
82 84
                 break;
83 85
             case ClassMetadata::ONE_TO_MANY:
84  
-                $cm->mapOneToMany($newMapping);
  86
+                $classMetadata->mapOneToMany($newMapping);
85 87
                 break;
86 88
             case ClassMetadata::ONE_TO_ONE:
87  
-                $cm->mapOneToOne($newMapping);
  89
+                $classMetadata->mapOneToOne($newMapping);
88 90
                 break;
89 91
         }
90 92
     }
129  tests/Doctrine/Tests/ORM/Tools/ResolveTargetEntityListenerTest.php
... ...
@@ -0,0 +1,129 @@
  1
+<?php
  2
+
  3
+namespace Doctrine\Tests\ORM\Tools;
  4
+
  5
+use Doctrine\ORM\Events;
  6
+use Doctrine\ORM\Mapping\ClassMetadataFactory;
  7
+use Doctrine\ORM\Tools\ResolveTargetEntityListener;
  8
+
  9
+require_once __DIR__ . '/../../TestInit.php';
  10
+
  11
+class ResolveTargetEntityListenerTest extends \Doctrine\Tests\OrmTestCase
  12
+{
  13
+    /**
  14
+     * @var EntityManager
  15
+     */
  16
+    private $em = null;
  17
+
  18
+    /**
  19
+     * @var ResolveTargetEntityListener
  20
+     */
  21
+    private $listener = null;
  22
+
  23
+    /**
  24
+     * @var ClassMetadataFactory
  25
+     */
  26
+    private $factory = null;
  27
+
  28
+    public function setUp()
  29
+    {
  30
+        $annotationDriver = $this->createAnnotationDriver();
  31
+
  32
+        $this->em = $this->_getTestEntityManager();
  33
+        $this->em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
  34
+        $this->factory = new ClassMetadataFactory;
  35
+        $this->factory->setEntityManager($this->em);
  36
+        $this->listener = new ResolveTargetEntityListener;
  37
+    }
  38
+
  39
+    /**
  40
+     * @group DDC-1544
  41
+     */
  42
+    public function testResolveTargetEntityListenerCanResolveTargetEntity()
  43
+    {
  44
+        $evm = $this->em->getEventManager();
  45
+        $this->listener->addResolveTargetEntity(
  46
+            'Doctrine\Tests\ORM\Tools\ResolveTargetInterface',
  47
+            'Doctrine\Tests\ORM\Tools\ResolveTargetEntity',
  48
+            array()
  49
+        );
  50
+        $this->listener->addResolveTargetEntity(
  51
+            'Doctrine\Tests\ORM\Tools\TargetInterface',
  52
+            'Doctrine\Tests\ORM\Tools\TargetEntity',
  53
+            array()
  54
+        );
  55
+        $evm->addEventListener(Events::loadClassMetadata, $this->listener);
  56
+        $cm = $this->factory->getMetadataFor('Doctrine\Tests\ORM\Tools\ResolveTargetEntity');
  57
+        $meta = $cm->associationMappings;
  58
+        $this->assertSame('Doctrine\Tests\ORM\Tools\TargetEntity', $meta['manyToMany']['targetEntity']);
  59
+        $this->assertSame('Doctrine\Tests\ORM\Tools\ResolveTargetEntity', $meta['manyToOne']['targetEntity']);
  60
+        $this->assertSame('Doctrine\Tests\ORM\Tools\ResolveTargetEntity', $meta['oneToMany']['targetEntity']);
  61
+        $this->assertSame('Doctrine\Tests\ORM\Tools\TargetEntity', $meta['oneToOne']['targetEntity']);
  62
+    }
  63
+}
  64
+
  65
+interface ResolveTargetInterface
  66
+{
  67
+    public function getId();
  68
+}
  69
+
  70
+interface TargetInterface extends ResolveTargetInterface
  71
+{
  72
+}
  73
+
  74
+/**
  75
+ * @Entity
  76
+ */
  77
+class ResolveTargetEntity implements ResolveTargetInterface
  78
+{
  79
+    /**
  80
+     * @Id
  81
+     * @Column(type="integer")
  82
+     * @GeneratedValue(strategy="AUTO")  
  83
+     */
  84
+    private $id;
  85
+
  86
+    /**
  87
+     * @ManyToMany(targetEntity="Doctrine\Tests\ORM\Tools\TargetInterface")
  88
+     */
  89
+    private $manyToMany;
  90
+
  91
+    /**
  92
+     * @ManyToOne(targetEntity="Doctrine\Tests\ORM\Tools\ResolveTargetInterface", inversedBy="oneToMany")
  93
+     */
  94
+    private $manyToOne;
  95
+
  96
+    /**
  97
+     * @OneToMany(targetEntity="Doctrine\Tests\ORM\Tools\ResolveTargetInterface", mappedBy="manyToOne")
  98
+     */
  99
+    private $oneToMany;
  100
+
  101
+    /**
  102
+     * @OneToOne(targetEntity="Doctrine\Tests\ORM\Tools\TargetInterface")
  103
+     * @JoinColumn(name="target_entity_id", referencedColumnName="id")
  104
+     */
  105
+    private $oneToOne;
  106
+
  107
+    public function getId()
  108
+    {
  109
+        return $this->id;
  110
+    }
  111
+}
  112
+
  113
+/**
  114
+ * @Entity
  115
+ */
  116
+class TargetEntity implements TargetInterface
  117
+{
  118
+    /**
  119
+     * @Id
  120
+     * @Column(type="integer")
  121
+     * @GeneratedValue(strategy="AUTO")  
  122
+     */
  123
+    private $id;
  124
+
  125
+    public function getId()
  126
+    {
  127
+        return $this->id;
  128
+    }
  129
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.