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 ed0bcc1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
24 changes: 3 additions & 21 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 All @@ -395,7 +395,7 @@ public function getTarget(): Table
$this->_targetTable = $tableLocator->get($registryAlias, $config);

if ($exists) {
$className = $this->_getClassName($registryAlias, ['className' => $this->_className]);
$className = App::className($this->_className, 'Model/Table', 'Table') ?: Table::class;

if (!$this->_targetTable instanceof $className) {
$errorMessage = '%s association "%s" of type "%s" to "%s" doesn\'t match the expected class "%s". ';
Expand Down Expand Up @@ -1125,24 +1125,6 @@ protected function _extractFinder($finderData): array
return [key($finderData), current($finderData)];
}

/**
* Gets the table class name.
*
* @param string $alias The alias name you want to get.
* @param array $options Table options array.
* @return string
*/
protected function _getClassName(string $alias, array $options = []): string
{
if (empty($options['className'])) {
$options['className'] = Inflector::camelize($alias);
}

$className = App::className($options['className'], 'Model/Table', 'Table') ?: Table::class;

return ltrim($className, '\\');
}

/**
* Proxies property retrieval to the target table. This is handy for getting this
* association's associations
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 ed0bcc1

Please sign in to comment.