Browse Source

Implement enhancement #43 with tests

tags/1.7.0
Alban Jubert 6 years ago
parent
commit
6e8353302e
  1. 33
      src/SaveRelationsBehavior.php
  2. 27
      tests/SaveRelationsBehaviorTest.php

33
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;
}
}

27
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());
}
}

Loading…
Cancel
Save