diff --git a/src/ORM/Association.php b/src/ORM/Association.php index b2beab5f551..9466a301721 100644 --- a/src/ORM/Association.php +++ b/src/ORM/Association.php @@ -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); } @@ -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); } @@ -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; } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } diff --git a/tests/TestCase/ORM/Association/BelongsToManyTest.php b/tests/TestCase/ORM/Association/BelongsToManyTest.php index b847ce9f51b..be82935fbcc 100644 --- a/tests/TestCase/ORM/Association/BelongsToManyTest.php +++ b/tests/TestCase/ORM/Association/BelongsToManyTest.php @@ -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; @@ -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()); + }); } /** @@ -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()); } @@ -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); } /** @@ -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'); @@ -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()); } /** @@ -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()); } /** @@ -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') @@ -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'); diff --git a/tests/TestCase/ORM/Association/BelongsToTest.php b/tests/TestCase/ORM/Association/BelongsToTest.php index b0200a26c45..aff67db6002 100644 --- a/tests/TestCase/ORM/Association/BelongsToTest.php +++ b/tests/TestCase/ORM/Association/BelongsToTest.php @@ -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()); + }); } /** @@ -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()); } /** @@ -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()); } /** @@ -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()); } /** diff --git a/tests/TestCase/ORM/Association/HasManyTest.php b/tests/TestCase/ORM/Association/HasManyTest.php index e47ae21e61d..cd865308ad0 100644 --- a/tests/TestCase/ORM/Association/HasManyTest.php +++ b/tests/TestCase/ORM/Association/HasManyTest.php @@ -99,14 +99,32 @@ public function tearDown() * * @return void */ - public function testForeignKey() + public function testSetForeignKey() { $assoc = new HasMany('Articles', [ 'sourceTable' => $this->author ]); - $this->assertEquals('author_id', $assoc->foreignKey()); - $this->assertEquals('another_key', $assoc->foreignKey('another_key')); - $this->assertEquals('another_key', $assoc->foreignKey()); + $this->assertEquals('author_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 HasMany('Articles', [ + 'sourceTable' => $this->author + ]); + $this->assertEquals('author_id', $assoc->foreignKey()); + $this->assertEquals('another_key', $assoc->foreignKey('another_key')); + $this->assertEquals('another_key', $assoc->foreignKey()); + }); } /** @@ -120,7 +138,7 @@ public function testForeignKeyIgnoreDatabaseName() $assoc = new HasMany('Articles', [ 'sourceTable' => $this->author ]); - $this->assertEquals('author_id', $assoc->foreignKey()); + $this->assertEquals('author_id', $assoc->getForeignKey()); } /** @@ -173,10 +191,10 @@ public function testRequiresKeys() $assoc = new HasMany('Test'); $this->assertTrue($assoc->requiresKeys()); - $assoc->strategy(HasMany::STRATEGY_SUBQUERY); + $assoc->setStrategy(HasMany::STRATEGY_SUBQUERY); $this->assertFalse($assoc->requiresKeys()); - $assoc->strategy(HasMany::STRATEGY_SELECT); + $assoc->setStrategy(HasMany::STRATEGY_SELECT); $this->assertTrue($assoc->requiresKeys()); } @@ -190,7 +208,7 @@ public function testStrategyFailure() $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Invalid strategy "join" was provided'); $assoc = new HasMany('Test'); - $assoc->strategy(HasMany::STRATEGY_JOIN); + $assoc->setStrategy(HasMany::STRATEGY_JOIN); } /** @@ -287,7 +305,7 @@ public function testEagerLoaderWithOverrides() $association = new HasMany('Articles', $config); $keys = [1, 2, 3, 4]; $query = $this->article->query(); - $query->addDefaultTypes($this->article->Comments->source()); + $query->addDefaultTypes($this->article->Comments->getSource()); $this->article->method('find') ->with('all') @@ -552,7 +570,7 @@ public function testPropertyOption() { $config = ['propertyName' => 'thing_placeholder']; $association = new hasMany('Thing', $config); - $this->assertEquals('thing_placeholder', $association->property()); + $this->assertEquals('thing_placeholder', $association->getProperty()); } /** @@ -570,7 +588,7 @@ public function testPropertyNoPlugin() 'targetTable' => $mock, ]; $association = new HasMany('Contacts.Addresses', $config); - $this->assertEquals('addresses', $association->property()); + $this->assertEquals('addresses', $association->getProperty()); } /** diff --git a/tests/TestCase/ORM/Association/HasOneTest.php b/tests/TestCase/ORM/Association/HasOneTest.php index 2ea6c8f66f1..fd28c339c94 100644 --- a/tests/TestCase/ORM/Association/HasOneTest.php +++ b/tests/TestCase/ORM/Association/HasOneTest.php @@ -66,16 +66,34 @@ public function tearDown() /** * Tests that foreignKey() returns the correct configured value * + * @group deprecated * @return void */ public function testForeignKey() + { + $this->deprecated(function () { + $assoc = new HasOne('Profiles', [ + 'sourceTable' => $this->user + ]); + $this->assertEquals('user_id', $assoc->foreignKey()); + $this->assertEquals('another_key', $assoc->foreignKey('another_key')); + $this->assertEquals('another_key', $assoc->foreignKey()); + }); + } + + /** + * Tests that setForeignKey() returns the correct configured value + * + * @return void + */ + public function testSetForeignKey() { $assoc = new HasOne('Profiles', [ 'sourceTable' => $this->user ]); - $this->assertEquals('user_id', $assoc->foreignKey()); - $this->assertEquals('another_key', $assoc->foreignKey('another_key')); - $this->assertEquals('another_key', $assoc->foreignKey()); + $this->assertEquals('user_id', $assoc->getForeignKey()); + $this->assertEquals($assoc, $assoc->setForeignKey('another_key')); + $this->assertEquals('another_key', $assoc->getForeignKey()); } /** @@ -250,7 +268,7 @@ public function testPropertyOption() { $config = ['propertyName' => 'thing_placeholder']; $association = new hasOne('Thing', $config); - $this->assertEquals('thing_placeholder', $association->property()); + $this->assertEquals('thing_placeholder', $association->getProperty()); } /** @@ -265,7 +283,7 @@ public function testPropertyNoPlugin() 'targetTable' => $this->profile, ]; $association = new HasOne('Contacts.Profiles', $config); - $this->assertEquals('profile', $association->property()); + $this->assertEquals('profile', $association->getProperty()); } /**