Skip to content

Commit

Permalink
Ensure Association::$_className is always set.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jul 24, 2019
1 parent 12c6a3c commit f550426
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/ORM/Association.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function __construct(string $alias, array $options = [])
}
}

if (empty($this->_className) && strpos($alias, '.')) {
if (empty($this->_className)) {
$this->_className = $alias;
}

Expand Down Expand Up @@ -378,7 +378,7 @@ public function setTarget(Table $table)
public function getTarget(): Table
{
if ($this->_targetTable === null) {
if (strpos((string)$this->_className, '.')) {
if (strpos($this->_className, '.')) {
[$plugin] = pluginSplit($this->_className, true);
$registryAlias = $plugin . $this->_name;
} else {
Expand Down
19 changes: 18 additions & 1 deletion tests/TestCase/ORM/AssociationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Cake\Test\TestCase\ORM;

use Cake\Core\Configure;
use Cake\ORM\Association;
use Cake\ORM\Table;
use Cake\TestSuite\TestCase;
use TestApp\Model\Table\AuthorsTable;
Expand Down Expand Up @@ -49,7 +50,7 @@ public function setUp(): void
'sourceTable' => $this->source,
'joinType' => 'INNER',
];
$this->association = $this->getMockBuilder('Cake\ORM\Association')
$this->association = $this->getMockBuilder(Association::class)
->setMethods([
'_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide',
'saveAssociated', 'eagerLoader', 'type', 'requiresKeys',
Expand Down Expand Up @@ -130,6 +131,22 @@ public function testSetNameToTargetAlias()
$this->assertEquals($alias, $this->association->getName());
}

/**
* Test that _className property is set to alias when "className" config
* if not explicitly set.
*
* @return void
*/
public function testSetttingClassNameFromAlias()
{
$association = $this->getMockBuilder(Association::class)
->setMethods(['type', 'eagerLoader', 'cascadeDelete', 'isOwningSide', 'saveAssociated'])
->setConstructorArgs(['Foo'])
->getMock();

$this->assertSame('Foo', $association->getClassName());
}

/**
* Tests that setClassName() succeeds before the target table is resolved.
*
Expand Down

0 comments on commit f550426

Please sign in to comment.