Browse Source

Make target table class name check less strict.

Robert Pustułka 9 years ago
parent
commit
eadae153e4
2 changed files with 29 additions and 2 deletions
  1. 2 2
      src/ORM/Association.php
  2. 27 0
      tests/TestCase/ORM/AssociationTest.php

+ 2 - 2
src/ORM/Association.php

@@ -411,10 +411,10 @@ abstract class Association
             $this->_targetTable = $tableLocator->get($registryAlias, $config);
 
             if ($exists) {
-                $targetClassName = get_class($this->_targetTable);
                 $className = $this->_getClassName($registryAlias, ['className' => $this->_className]);
 
-                if ($targetClassName !== $className) {
+                if (!$this->_targetTable instanceof $className) {
+                    $targetClassName = get_class($this->_targetTable);
                     throw new RuntimeException(sprintf(
                         'Invalid Table retrieved from a registry. Requested: %s, got: %s',
                         $className,

+ 27 - 0
tests/TestCase/ORM/AssociationTest.php

@@ -203,6 +203,33 @@ class AssociationTest extends TestCase
     }
 
     /**
+     * Tests that a descendant table could be fetched from a registry.
+     *
+     * @return void
+     */
+    public function testTargetTableDescendant()
+    {
+        TableRegistry::get('Test', [
+            'className' => '\Cake\Test\TestCase\ORM\TestTable'
+        ]);
+        $className = '\Cake\ORM\Table';
+
+        $config = [
+            'className' => $className,
+        ];
+        $this->association = $this->getMockBuilder('\Cake\ORM\Association')
+            ->setMethods([
+                '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
+                'saveAssociated', 'eagerLoader', 'type', 'requiresKeys'
+            ])
+            ->setConstructorArgs(['Test', $config])
+            ->getMock();
+
+        $target = $this->association->getTarget();
+        $this->assertInstanceOf($className, $target);
+    }
+
+    /**
      * Tests that cascadeCallbacks() returns the correct configured value
      *
      * @return void