From 6e8353302eab9e823bdd71050c204640ce906969 Mon Sep 17 00:00:00 2001 From: Alban Jubert Date: Tue, 11 Sep 2018 10:53:06 +0200 Subject: [PATCH] Implement enhancement #43 with tests --- src/SaveRelationsBehavior.php | 33 +++++++++++++++++++++++++++++++-- tests/SaveRelationsBehaviorTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/SaveRelationsBehavior.php b/src/SaveRelationsBehavior.php index 140534d..ce261fc 100644 --- a/src/SaveRelationsBehavior.php +++ b/src/SaveRelationsBehavior.php @@ -766,7 +766,7 @@ class SaveRelationsBehavior extends Behavior /** * Return the old relations values. - * @return array + * @return array The old relations (name-value pairs) */ public function getOldRelations() { @@ -779,11 +779,40 @@ class SaveRelationsBehavior extends Behavior /** * Returns the old value of the named relation. - * @param $relationName + * @param $relationName The relations name as defined in the behavior `relations` parameter * @return mixed */ public function getOldRelation($relationName) { return array_key_exists($relationName, $this->_oldRelationValue) ? $this->_oldRelationValue[$relationName] : $this->owner->{$relationName}; } + + /** + * Returns the relations that have been modified since they are loaded. + * @return array The changed relations (name-value pairs) + */ + public function getDirtyRelations() + { + $dirtyRelations = []; + foreach ($this->_relations as $relationName) { + if (array_key_exists($relationName, $this->_oldRelationValue)) { + $dirtyRelations[$relationName] = $this->owner->{$relationName}; + } + } + return $dirtyRelations; + } + + /** + * Mark a relation as dirty + * @param $relationName + * @return bool Whether the operation succeeded. + */ + public function markRelationDirty($relationName) + { + if (in_array($relationName, $this->_relations) && !array_key_exists($relationName, $this->_oldRelationValue)) { + $this->_oldRelationValue[$relationName] = $this->owner->{$relationName}; + return true; + } + return false; + } } diff --git a/tests/SaveRelationsBehaviorTest.php b/tests/SaveRelationsBehaviorTest.php index ddbad76..00ca011 100644 --- a/tests/SaveRelationsBehaviorTest.php +++ b/tests/SaveRelationsBehaviorTest.php @@ -950,4 +950,31 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oldCompany->name, 'Microsoft'); } + + public function testGetDirtyRelations() + { + $project = Project::findOne(1); + $project->company = Company::findOne(2); + $links = [ + ['language' => 'fr', 'name' => 'windows10', 'link' => 'https://www.microsoft.com/fr-fr/windows/features', 'link_type_id' => 2], + ['language' => 'en', 'name' => 'windows10', 'link' => 'https://www.microsoft.com/en-us/windows/features', 'link_type_id' => 2] + ]; + $project->links = $links; + $dirtyRelations = $project->getDirtyRelations(); + $this->assertCount(2, $dirtyRelations); + $this->assertArrayHasKey('company', $dirtyRelations); + $this->assertArrayHasKey('links', $dirtyRelations); + $this->assertArrayNotHasKey('tags', $dirtyRelations); + $this->assertEquals($dirtyRelations['company'], $project->company); + $this->assertEquals($dirtyRelations['links'], $project->links); + } + + public function testMarkRelationDirty() + { + $project = Project::findOne(1); + $this->assertArrayNotHasKey('company', $project->getDirtyRelations()); + $this->assertFalse($project->markRelationDirty('wrongRelationName')); + $this->assertTrue($project->markRelationDirty('company')); + $this->assertArrayHasKey('company', $project->getDirtyRelations()); + } }