Skip to content

Commit

Permalink
Fixed #5 - Save now the correct value (initial value, instead of curr…
Browse files Browse the repository at this point in the history
…ent)
  • Loading branch information
marcj committed Dec 15, 2014
1 parent 61c9f5a commit 6748d89
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 24 deletions.
72 changes: 57 additions & 15 deletions src/ChangeLoggerBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public function objectAttributes($builder)
foreach ($this->getColumns() as $column) {
$name = lcfirst($column->getPhpName());
$script .= "
/**
* Is used as comment for ChangeLoggerBehavior.
*
* @var string
*/
protected \${$name}ChangeComment;
/**
* Is used as comment for ChangeLoggerBehavior.
*
* @var string
*/
protected \${$name}ChangeComment;
";
}
}
Expand All @@ -63,16 +63,24 @@ public function objectAttributes($builder)
$name = lcfirst($column->getPhpName());

$script .= "
/**
* Is used as createdBy for ChangeLoggerBehavior.
*
* @var string
*/
protected \${$name}ChangeBy;
/**
* Is used as createdBy for ChangeLoggerBehavior.
*
* @var string
*/
protected \${$name}ChangeBy;
";
}
}

$script .= "
/**
*
* @var mixed[]
*/
protected \$changeLoggerTracker = [];
";

return $script;
}

Expand Down Expand Up @@ -142,12 +150,18 @@ protected function appendAddVersionMethod(ObjectBuilder $builder, &$script, $col
$logARClassName = $builder->getClassNameFromBuilder($builder->getNewStubObjectBuilder($logTable));
$logARQueryName = $builder->getNewStubQueryBuilder($logTable)->getFullyQualifiedClassName();

$varName = lcfirst($column->getPhpName());

$script .= "
/**
* @return $logARClassName model instance of saved log ($logTableName)
* @return $logARClassName|false model instance of saved log ($logTableName) or false if nothing was changed.
*/
public function $methodName()
{
if (!isset(\$this->changeLoggerTracker['$varName'])) {
return false;
}
\$log = new {$logARClassName}();";

foreach ($this->getTable()->getPrimaryKey() as $col) {
Expand All @@ -156,7 +170,7 @@ public function $methodName()
}

$script .= "
\$log->set" . $column->getPhpName() . "(\$this->get" . $column->getPhpName() . "());";
\$log->set" . $column->getPhpName() . "(\$this->changeLoggerTracker['$varName']);";

if ('true' === $this->getParameter('created_at')) {
$createdAtColumn = $logTable->getColumn($this->getParameter('created_at_column'));
Expand Down Expand Up @@ -190,6 +204,7 @@ public function $methodName()
\$log->setVersion(\$lastVersion ? \$lastVersion->getVersion() + 1 : 1);
\$log->save();
\$this->changeLoggerTracker['$varName'] = \$this->get{$column->getPhpName()}();
return \$log;
}
";
Expand All @@ -215,7 +230,6 @@ public function postUpdate(ObjectBuilder $builder)
return $hooks;
}


/**
* @param ObjectBuilder $builder
*
Expand All @@ -234,6 +248,34 @@ public function preSave(ObjectBuilder $builder)
return $hooks;
}

public function postSave(ObjectBuilder $builder)
{
$hooks = '';

foreach ($this->getColumns() as $column) {
$getter = 'get' . $column->getPhpName();
$varName = lcfirst($column->getPhpName());
$hooks .= "
\$this->changeLoggerTracker['$varName'] = \$this->{$getter}();";
}

return $hooks;
}

public function postHydrate(ObjectBuilder $builder)
{
$hooks = '';

foreach ($this->getColumns() as $column) {
$getter = 'get' . $column->getPhpName();
$varName = lcfirst($column->getPhpName());
$hooks .= "
\$this->changeLoggerTracker['$varName'] = \$this->{$getter}();";
}

return $hooks;
}

/**
* @return Column[]
*/
Expand Down
52 changes: 43 additions & 9 deletions tests/ChangeLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,24 @@ public function testSingle()
$item = new \ChangeloggerBehaviorSingle();

$this->assertTrue(method_exists($item, 'addTitleVersion'));
$item->setTitle('Initial');
$item->save();

//initial save doesn't save a log entry
$this->assertEquals(0, \ChangeloggerBehaviorSingleTitleLogQuery::create()->count());

$item->setTitle('Teschd');
$item->setTitleChangeComment('Sohalt.');
$item->setTitleChangeBy('Me');
$item->save();

//initial save saves already a log entry
//second save saves a log entry
$this->assertEquals(1, \ChangeloggerBehaviorSingleTitleLogQuery::create()->count());
$changeLog = \ChangeloggerBehaviorSingleTitleLogQuery::create()->findOne();
$this->assertEquals(1, $changeLog->getVersion());
$this->assertEquals('Sohalt.', $changeLog->getLogComment());
$this->assertEquals('Me', $changeLog->getLogCreatedBy());
$this->assertEquals('Initial', $changeLog->getTitle());

$item->setAge(2);
$item->save();
Expand All @@ -69,7 +73,7 @@ public function testSingle()
$this->assertEquals(1, \ChangeloggerBehaviorSingleTitleLogQuery::create()->count());
$lastVersion = \ChangeloggerBehaviorSingleTitleLogQuery::create()->orderByVersion('desc')->findOne();
$this->assertEquals(1, $lastVersion->getVersion());
$this->assertEquals('Teschd', $lastVersion->getTitle());
$this->assertEquals('Initial', $lastVersion->getTitle());

$item->setTitle('Changed');
$item->save();
Expand All @@ -78,7 +82,31 @@ public function testSingle()
$this->assertEquals(2, \ChangeloggerBehaviorSingleTitleLogQuery::create()->count());
$lastVersion = \ChangeloggerBehaviorSingleTitleLogQuery::create()->orderByVersion('desc')->findOne();
$this->assertEquals(2, $lastVersion->getVersion());
$this->assertEquals('Changed', $lastVersion->getTitle());
$this->assertEquals('Teschd', $lastVersion->getTitle());
}

public function testFromQuery()
{
\ChangeloggerBehaviorSingleQuery::create()->deleteAll();
\ChangeloggerBehaviorSingleTitleLogQuery::create()->deleteAll();

$item = new \ChangeloggerBehaviorSingle();

$item->setTitle('Initial');
$item->save();

\Map\ChangeloggerBehaviorSingleTableMap::clearInstancePool();

$itemRetrieved = \ChangeloggerBehaviorSingleQuery::create()->findOne();
$this->assertNotSame($itemRetrieved, $item);

$itemRetrieved->setTitle('New Title');
$itemRetrieved->save();

$this->assertEquals(1, \ChangeloggerBehaviorSingleTitleLogQuery::create()->count());
$lastVersion = \ChangeloggerBehaviorSingleTitleLogQuery::create()->orderByVersion('desc')->findOne();
$this->assertEquals(1, $lastVersion->getVersion());
$this->assertEquals('Initial', $lastVersion->getTitle());
}

public function testMultiple()
Expand All @@ -94,7 +122,9 @@ public function testMultiple()

$this->assertTrue(method_exists($item, 'addTitleVersion'));
$this->assertTrue(method_exists($item, 'addAgeVersion'));
$item->setTitle('Initial');
$item->save();

$this->assertEquals(0, \ChangeloggerBehaviorMultipleTitleLogQuery::create()->count());

$item->setTitle('Teschd');
Expand All @@ -105,7 +135,7 @@ public function testMultiple()
$this->assertEquals(1, \ChangeloggerBehaviorMultipleTitleLogQuery::create()->findOne()->getVersion());
$lastVersion = \ChangeloggerBehaviorMultipleTitleLogQuery::create()->orderByVersion('desc')->findOne();
$this->assertEquals(1, $lastVersion->getVersion());
$this->assertEquals('Teschd', $lastVersion->getTitle());
$this->assertEquals('Initial', $lastVersion->getTitle());
$this->assertEquals(0, \ChangeloggerBehaviorMultipleAgeLogQuery::create()->count());

$item->setAge(2);
Expand All @@ -115,22 +145,26 @@ public function testMultiple()
$this->assertEquals(1, \ChangeloggerBehaviorMultipleTitleLogQuery::create()->count());
$lastVersion = \ChangeloggerBehaviorMultipleTitleLogQuery::create()->orderByVersion('desc')->findOne();
$this->assertEquals(1, $lastVersion->getVersion());
$this->assertEquals('Teschd', $lastVersion->getTitle());
$this->assertEquals('Initial', $lastVersion->getTitle());

//`age` is empty, so we have no log record
$this->assertEquals(0, \ChangeloggerBehaviorMultipleAgeLogQuery::create()->count());

$item->setTitle('Changed');
$item->setAge(null);
$item->save();

//we have now additional a `age` log entry
$this->assertEquals(1, \ChangeloggerBehaviorMultipleAgeLogQuery::create()->count());
$lastVersion = \ChangeloggerBehaviorMultipleAgeLogQuery::create()->orderByVersion('desc')->findOne();
$this->assertEquals(1, $lastVersion->getVersion());
$this->assertEquals(2, $lastVersion->getAge());

$item->setTitle('Changed');
$item->save();

//title has been changed, we have now two versions
$this->assertEquals(2, \ChangeloggerBehaviorMultipleTitleLogQuery::create()->count());
$lastVersion = \ChangeloggerBehaviorMultipleTitleLogQuery::create()->orderByVersion('desc')->findOne();
$this->assertEquals(2, $lastVersion->getVersion());
$this->assertEquals('Changed', $lastVersion->getTitle());
$this->assertEquals('Teschd', $lastVersion->getTitle());

//age has not changed anything, so check as above
$this->assertEquals(1, \ChangeloggerBehaviorMultipleAgeLogQuery::create()->count());
Expand Down

0 comments on commit 6748d89

Please sign in to comment.