Skip to content

Commit

Permalink
Update deprecated method usage in ORM\Association.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Nov 16, 2017
1 parent b0ebd29 commit 1a98f56
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 53 deletions.
48 changes: 48 additions & 0 deletions src/ORM/Association.php
Expand Up @@ -279,6 +279,10 @@ public function getName()
*/
public function name($name = null)
{
deprecationWarning(
get_called_class() . '::name() is deprecated. ' .
'Use setName()/getName() instead.'
);
if ($name !== null) {
$this->setName($name);
}
Expand Down Expand Up @@ -319,6 +323,10 @@ public function getCascadeCallbacks()
*/
public function cascadeCallbacks($cascadeCallbacks = null)
{
deprecationWarning(
get_called_class() . '::cascadeCallbacks() is deprecated. ' .
'Use setCascadeCallbacks()/getCascadeCallbacks() instead.'
);
if ($cascadeCallbacks !== null) {
$this->setCascadeCallbacks($cascadeCallbacks);
}
Expand Down Expand Up @@ -369,6 +377,10 @@ public function getSource()
*/
public function source(Table $table = null)
{
deprecationWarning(
get_called_class() . '::source() is deprecated. ' .
'Use setSource()/getSource() instead.'
);
if ($table === null) {
return $this->_sourceTable;
}
Expand Down Expand Up @@ -445,6 +457,10 @@ public function getTarget()
*/
public function target(Table $table = null)
{
deprecationWarning(
get_called_class() . '::target() is deprecated. ' .
'Use setTarget()/getTarget() instead.'
);
if ($table !== null) {
$this->setTarget($table);
}
Expand Down Expand Up @@ -490,6 +506,10 @@ public function getConditions()
*/
public function conditions($conditions = null)
{
deprecationWarning(
get_called_class() . '::conditions() is deprecated. ' .
'Use setConditions()/getConditions() instead.'
);
if ($conditions !== null) {
$this->setConditions($conditions);
}
Expand Down Expand Up @@ -540,6 +560,10 @@ public function getBindingKey()
*/
public function bindingKey($key = null)
{
deprecationWarning(
get_called_class() . '::bindingKey() is deprecated. ' .
'Use setBindingKey()/getBindingKey() instead.'
);
if ($key !== null) {
$this->setBindingKey($key);
}
Expand Down Expand Up @@ -580,6 +604,10 @@ public function setForeignKey($key)
*/
public function foreignKey($key = null)
{
deprecationWarning(
get_called_class() . '::foreignKey() is deprecated. ' .
'Use setForeignKey()/getForeignKey() instead.'
);
if ($key !== null) {
$this->setForeignKey($key);
}
Expand Down Expand Up @@ -632,6 +660,10 @@ public function getDependent()
*/
public function dependent($dependent = null)
{
deprecationWarning(
get_called_class() . '::dependent() is deprecated. ' .
'Use setDependent()/getDependent() instead.'
);
if ($dependent !== null) {
$this->setDependent($dependent);
}
Expand Down Expand Up @@ -685,6 +717,10 @@ public function getJoinType()
*/
public function joinType($type = null)
{
deprecationWarning(
get_called_class() . '::joinType() is deprecated. ' .
'Use setJoinType()/getJoinType() instead.'
);
if ($type !== null) {
$this->setJoinType($type);
}
Expand Down Expand Up @@ -740,6 +776,10 @@ public function getProperty()
*/
public function property($name = null)
{
deprecationWarning(
get_called_class() . '::property() is deprecated. ' .
'Use setProperty()/getProperty() instead.'
);
if ($name !== null) {
$this->setProperty($name);
}
Expand Down Expand Up @@ -805,6 +845,10 @@ public function getStrategy()
*/
public function strategy($name = null)
{
deprecationWarning(
get_called_class() . '::strategy() is deprecated. ' .
'Use setStrategy()/getStrategy() instead.'
);
if ($name !== null) {
$this->setStrategy($name);
}
Expand Down Expand Up @@ -846,6 +890,10 @@ public function setFinder($finder)
*/
public function finder($finder = null)
{
deprecationWarning(
get_called_class() . '::finder() is deprecated. ' .
'Use setFinder()/getFinder() instead.'
);
if ($finder !== null) {
$this->setFinder($finder);
}
Expand Down
79 changes: 50 additions & 29 deletions tests/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -17,8 +17,11 @@
use Cake\Database\Expression\QueryExpression;
use Cake\Datasource\ConnectionManager;
use Cake\Event\Event;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Association\HasMany;
use Cake\ORM\Entity;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -67,19 +70,38 @@ public function setUp()
}

/**
* Tests that foreignKey() returns the correct configured value
* Tests setForeignKey()
*
* @return void
*/
public function testForeignKey()
public function testSetForeignKey()
{
$assoc = new BelongsToMany('Test', [
'sourceTable' => $this->article,
'targetTable' => $this->tag
]);
$this->assertEquals('article_id', $assoc->foreignKey());
$this->assertEquals('another_key', $assoc->foreignKey('another_key'));
$this->assertEquals('another_key', $assoc->foreignKey());
$this->assertEquals('article_id', $assoc->getForeignKey());
$this->assertSame($assoc, $assoc->setForeignKey('another_key'));
$this->assertEquals('another_key', $assoc->getForeignKey());
}

/**
* Tests that foreignKey() returns the correct configured value
*
* @group deprecated
* @return void
*/
public function testForeignKey()
{
$this->deprecated(function () {
$assoc = new BelongsToMany('Test', [
'sourceTable' => $this->article,
'targetTable' => $this->tag
]);
$this->assertEquals('article_id', $assoc->foreignKey());
$this->assertEquals('another_key', $assoc->foreignKey('another_key'));
$this->assertEquals('another_key', $assoc->foreignKey());
});
}

/**
Expand Down Expand Up @@ -131,9 +153,11 @@ public function testRequiresKeys()
{
$assoc = new BelongsToMany('Test');
$this->assertTrue($assoc->requiresKeys());
$assoc->strategy(BelongsToMany::STRATEGY_SUBQUERY);

$assoc->setStrategy(BelongsToMany::STRATEGY_SUBQUERY);
$this->assertFalse($assoc->requiresKeys());
$assoc->strategy(BelongsToMany::STRATEGY_SELECT);

$assoc->setStrategy(BelongsToMany::STRATEGY_SELECT);
$this->assertTrue($assoc->requiresKeys());
}

Expand All @@ -147,7 +171,7 @@ public function testStrategyFailure()
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid strategy "join" was provided');
$assoc = new BelongsToMany('Test');
$assoc->strategy(BelongsToMany::STRATEGY_JOIN);
$assoc->setStrategy(BelongsToMany::STRATEGY_JOIN);
}

/**
Expand All @@ -163,23 +187,20 @@ public function testJunction()
'strategy' => 'subquery'
]);
$junction = $assoc->junction();
$this->assertInstanceOf('Cake\ORM\Table', $junction);
$this->assertInstanceOf(Table::class, $junction);
$this->assertEquals('ArticlesTags', $junction->alias());
$this->assertEquals('articles_tags', $junction->table());
$this->assertSame($this->article, $junction->getAssociation('Articles')->target());
$this->assertSame($this->tag, $junction->getAssociation('Tags')->target());
$this->assertSame($this->article, $junction->getAssociation('Articles')->getTarget());
$this->assertSame($this->tag, $junction->getAssociation('Tags')->getTarget());

$belongsTo = '\Cake\ORM\Association\BelongsTo';
$this->assertInstanceOf($belongsTo, $junction->getAssociation('Articles'));
$this->assertInstanceOf($belongsTo, $junction->getAssociation('Tags'));
$this->assertInstanceOf(BelongsTo::class, $junction->getAssociation('Articles'));
$this->assertInstanceOf(BelongsTo::class, $junction->getAssociation('Tags'));

$this->assertSame($junction, $this->tag->getAssociation('ArticlesTags')->target());
$this->assertSame($this->article, $this->tag->getAssociation('Articles')->target());
$this->assertSame($junction, $this->tag->getAssociation('ArticlesTags')->getTarget());
$this->assertSame($this->article, $this->tag->getAssociation('Articles')->getTarget());

$hasMany = '\Cake\ORM\Association\HasMany';
$belongsToMany = '\Cake\ORM\Association\BelongsToMany';
$this->assertInstanceOf($belongsToMany, $this->tag->getAssociation('Articles'));
$this->assertInstanceOf($hasMany, $this->tag->getAssociation('ArticlesTags'));
$this->assertInstanceOf(BelongsToMany::class, $this->tag->getAssociation('Articles'));
$this->assertInstanceOf(HasMany::class, $this->tag->getAssociation('ArticlesTags'));

$this->assertSame($junction, $assoc->junction());
$junction2 = TableRegistry::get('Foos');
Expand All @@ -189,9 +210,9 @@ public function testJunction()
$assoc->junction('ArticlesTags');
$this->assertSame($junction, $assoc->junction());

$this->assertSame($assoc->strategy(), $this->tag->getAssociation('Articles')->strategy());
$this->assertSame($assoc->strategy(), $this->tag->getAssociation('ArticlesTags')->strategy());
$this->assertSame($assoc->strategy(), $this->article->getAssociation('ArticlesTags')->strategy());
$this->assertSame($assoc->getStrategy(), $this->tag->getAssociation('Articles')->getStrategy());
$this->assertSame($assoc->getStrategy(), $this->tag->getAssociation('ArticlesTags')->getStrategy());
$this->assertSame($assoc->getStrategy(), $this->article->getAssociation('ArticlesTags')->getStrategy());
}

/**
Expand Down Expand Up @@ -235,12 +256,12 @@ public function testJunctionCustomKeys()
'targetForeignKey' => 'article'
]);
$junction = $this->article->getAssociation('Tags')->junction();
$this->assertEquals('article', $junction->getAssociation('Articles')->foreignKey());
$this->assertEquals('article', $this->article->getAssociation('ArticlesTags')->foreignKey());
$this->assertEquals('article', $junction->getAssociation('Articles')->getForeignKey());
$this->assertEquals('article', $this->article->getAssociation('ArticlesTags')->getForeignKey());

$junction = $this->tag->getAssociation('Articles')->junction();
$this->assertEquals('tag', $junction->getAssociation('Tags')->foreignKey());
$this->assertEquals('tag', $this->tag->getAssociation('ArticlesTags')->foreignKey());
$this->assertEquals('tag', $junction->getAssociation('Tags')->getForeignKey());
$this->assertEquals('tag', $this->tag->getAssociation('ArticlesTags')->getForeignKey());
}

/**
Expand Down Expand Up @@ -337,7 +358,7 @@ public function testCascadeDelete()
$association->junction($articleTag);
$this->article
->getAssociation($articleTag->alias())
->conditions(['click_count' => 3]);
->setConditions(['click_count' => 3]);

$articleTag->expects($this->once())
->method('deleteAll')
Expand Down Expand Up @@ -370,7 +391,7 @@ public function testCascadeDeleteDependent()
$association->junction($articleTag);
$this->article
->getAssociation($articleTag->alias())
->conditions(['click_count' => 3]);
->setConditions(['click_count' => 3]);

$articleTag->expects($this->never())
->method('deleteAll');
Expand Down
35 changes: 27 additions & 8 deletions tests/TestCase/ORM/Association/BelongsToTest.php
Expand Up @@ -84,19 +84,38 @@ public function tearDown()
}

/**
* Test that foreignKey generation ignores database names in target table.
* Test that foreignKey generation
*
* @return void
*/
public function testForeignKey()
public function testSetForeignKey()
{
$assoc = new BelongsTo('Companies', [
'sourceTable' => $this->client,
'targetTable' => $this->company,
]);
$this->assertEquals('company_id', $assoc->foreignKey());
$this->assertEquals('another_key', $assoc->foreignKey('another_key'));
$this->assertEquals('another_key', $assoc->foreignKey());
$this->assertEquals('company_id', $assoc->getForeignKey());
$this->assertSame($assoc, $assoc->setForeignKey('another_key'));
$this->assertEquals('another_key', $assoc->getForeignKey());
}

/**
* Test that foreignKey generation
*
* @group deprecated
* @return void
*/
public function testForeignKey()
{
$this->deprecated(function () {
$assoc = new BelongsTo('Companies', [
'sourceTable' => $this->client,
'targetTable' => $this->company,
]);
$this->assertEquals('company_id', $assoc->foreignKey());
$this->assertEquals('another_key', $assoc->foreignKey('another_key'));
$this->assertEquals('another_key', $assoc->foreignKey());
});
}

/**
Expand All @@ -112,7 +131,7 @@ public function testForeignKeyIgnoreDatabaseName()
'sourceTable' => $this->client,
'targetTable' => $this->company,
]);
$this->assertEquals('company_id', $assoc->foreignKey());
$this->assertEquals('company_id', $assoc->getForeignKey());
}

/**
Expand Down Expand Up @@ -336,7 +355,7 @@ public function testPropertyOption()
{
$config = ['propertyName' => 'thing_placeholder'];
$association = new BelongsTo('Thing', $config);
$this->assertEquals('thing_placeholder', $association->property());
$this->assertEquals('thing_placeholder', $association->getProperty());
}

/**
Expand All @@ -354,7 +373,7 @@ public function testPropertyNoPlugin()
'targetTable' => $mock,
];
$association = new BelongsTo('Contacts.Companies', $config);
$this->assertEquals('company', $association->property());
$this->assertEquals('company', $association->getProperty());
}

/**
Expand Down

0 comments on commit 1a98f56

Please sign in to comment.