From be4eeef6de2426bc0e385b8228bafb24b6040828 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 28 Feb 2016 01:05:48 +0530 Subject: [PATCH] Reduce code duplication. --- src/ORM/Association.php | 14 ++++++++++++-- src/ORM/Association/BelongsTo.php | 17 ++++------------- src/ORM/Association/HasOne.php | 17 ++++------------- .../TestCase/ORM/AssociationCollectionTest.php | 12 +++++++++--- 4 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/ORM/Association.php b/src/ORM/Association.php index 6e4bd3b82e0..ed3ce99063d 100644 --- a/src/ORM/Association.php +++ b/src/ORM/Association.php @@ -436,8 +436,7 @@ public function property($name = null) $this->_propertyName = $name; } if ($name === null && !$this->_propertyName) { - list(, $name) = pluginSplit($this->_name); - $this->_propertyName = Inflector::underscore($name); + $this->_propertyName = $this->_propertyName(); if (in_array($this->_propertyName, $this->_sourceTable->schema()->columns())) { $msg = 'Association property name "%s" clashes with field of same name of table "%s".' . "\n" . 'You should explicitly specify the "propertyName" option.'; @@ -450,6 +449,17 @@ public function property($name = null) return $this->_propertyName; } + /** + * Returns default property name based on association name. + * + * @return string + */ + protected function _propertyName() + { + list(, $name) = pluginSplit($this->_name); + return Inflector::underscore($name); + } + /** * Sets the strategy name to be used to fetch associated records. Keep in mind * that some association types might not implement but a default strategy, diff --git a/src/ORM/Association/BelongsTo.php b/src/ORM/Association/BelongsTo.php index 6a15a88f666..a81b8c65dc0 100644 --- a/src/ORM/Association/BelongsTo.php +++ b/src/ORM/Association/BelongsTo.php @@ -72,23 +72,14 @@ public function cascadeDelete(EntityInterface $entity, array $options = []) } /** - * Sets the property name that should be filled with data from the target table - * in the source table record. - * If no arguments are passed, currently configured type is returned. + * Returns default property name based on association name. * - * @param string|null $name The property name, use null to read the current property. * @return string */ - public function property($name = null) + protected function _propertyName() { - if ($name !== null) { - return parent::property($name); - } - if ($name === null && !$this->_propertyName) { - list(, $name) = pluginSplit($this->_name); - $this->_propertyName = Inflector::underscore(Inflector::singularize($name)); - } - return $this->_propertyName; + list(, $name) = pluginSplit($this->_name); + return Inflector::underscore(Inflector::singularize($name)); } /** diff --git a/src/ORM/Association/HasOne.php b/src/ORM/Association/HasOne.php index a217c5ac8a6..c98243faece 100644 --- a/src/ORM/Association/HasOne.php +++ b/src/ORM/Association/HasOne.php @@ -57,23 +57,14 @@ public function foreignKey($key = null) } /** - * Sets the property name that should be filled with data from the target table - * in the source table record. - * If no arguments are passed, currently configured type is returned. + * Returns default property name based on association name. * - * @param string|null $name The name of the property. Pass null to read the current value. * @return string */ - public function property($name = null) + protected function _propertyName() { - if ($name !== null) { - return parent::property($name); - } - if ($name === null && !$this->_propertyName) { - list(, $name) = pluginSplit($this->_name); - $this->_propertyName = Inflector::underscore(Inflector::singularize($name)); - } - return $this->_propertyName; + list(, $name) = pluginSplit($this->_name); + return Inflector::underscore(Inflector::singularize($name)); } /** diff --git a/tests/TestCase/ORM/AssociationCollectionTest.php b/tests/TestCase/ORM/AssociationCollectionTest.php index c3402866d07..a5d73e22baf 100644 --- a/tests/TestCase/ORM/AssociationCollectionTest.php +++ b/tests/TestCase/ORM/AssociationCollectionTest.php @@ -91,7 +91,11 @@ public function testRemoveAll() */ public function testGetByProperty() { - $belongsTo = new BelongsTo('Users', []); + $table = $this->getMock('Cake\ORM\Table', ['table'], [[]]); + $table->schema([]); + $belongsTo = new BelongsTo('Users', [ + 'sourceTable' => $table + ]); $this->assertEquals('user', $belongsTo->property()); $this->associations->add('Users', $belongsTo); $this->assertNull($this->associations->get('user')); @@ -186,7 +190,8 @@ public function testCascadeDelete() */ public function testSaveParents() { - $table = $this->getMock('Cake\ORM\Table', [], [[]]); + $table = $this->getMock('Cake\ORM\Table', ['table'], [[]]); + $table->schema([]); $mockOne = $this->getMock( 'Cake\ORM\Association\BelongsTo', ['saveAssociated'], @@ -235,7 +240,8 @@ public function testSaveParents() */ public function testSaveParentsFiltered() { - $table = $this->getMock('Cake\ORM\Table', [], [[]]); + $table = $this->getMock('Cake\ORM\Table', ['table'], [[]]); + $table->schema([]); $mockOne = $this->getMock( 'Cake\ORM\Association\BelongsTo', ['saveAssociated'],