Skip to content

Commit

Permalink
Store object original data in the object. Do not track _id changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Mar 20, 2017
1 parent fb84c72 commit bedae2f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 33 deletions.
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ version: '2'
services:
yadm:
image: formapro/nginx-php-fpm:latest-all-exts
working_dir: /yadm
volumes:
- ./:/yadm
environment:
- MONGODB_URI=mongodb://mongo/

mongo:
image: mongo:3
ports:
- "27017:27017"
# ports:
# - "27017:27017"
34 changes: 13 additions & 21 deletions src/ChangesCollector.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
<?php
namespace Makasim\Yadm;

use function Makasim\Values\get_values;
use mikemccabe\JsonPatch\JsonPatch;

class ChangesCollector
{
private $originalValues;

public function register($object)
{
if ($id = get_object_id($object)) {
$this->originalValues[$id] = get_values($object);
}
}

public function unregister($object)
{
if ($id = get_object_id($object)) {
unset($this->originalValues[$id]);
}
(function() {
$this->originalValues = $this->values;
})->call($object);
}

public function changes($object)
{
if (false == $id = get_object_id($object)) {
throw new \LogicException(sprintf('Object does not have an id set.'));
}
return (function() {
$values = $this->values;

if (property_exists($this, 'originalValues')) {
$originalValues = $this->originalValues;

if (false == array_key_exists($id, $this->originalValues)) {
throw new \LogicException(sprintf('Changes has not been collected. The object with id "%s" original data is missing.'));
}
$diff = JsonPatch::diff($originalValues, $values);

$diff = JsonPatch::diff($this->originalValues[$id], get_values($object));
return Converter::convertJsonPatchToMongoUpdate($diff);
}

return Converter::convertJsonPatchToMongoUpdate($diff);
return ['$set' => $values];
})->call($object);
}
}
4 changes: 4 additions & 0 deletions src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public static function convertJsonPatchToMongoUpdate(array $diff)
{
$update = ['$set' => [], '$unset' => []];
foreach ($diff as $op) {
if (isset($op['path']) && '/_id' == $op['path']) {
continue;
}

switch ($op['op']) {
case 'add':
if (is_array($op['value'])) {
Expand Down
4 changes: 0 additions & 4 deletions src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ public function delete($model, array $options = [])
throw new \LogicException('Operation is not acknowledged');
}

// TODO remove id???
$this->changesCollector->unregister($model);

return $result;
}

Expand Down
55 changes: 49 additions & 6 deletions tests/ChangesCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

class ChangesCollectorTest extends TestCase
{
/**
* @group d
*/
public function testShouldTrackSetValue()
{
$obj = $this->createPersistedObject();
Expand All @@ -23,15 +20,15 @@ public function testShouldTrackSetValue()
$collector->register($obj);

set_value($obj, 'aKey', 'aVal');
var_dump($collector->changes($obj));

self::assertEquals([
'$set' => [
'aKey' => 'aVal',
],
], $collector->changes($obj));
}

public function testShouldTrackAddedValue()
public function testShouldTrackAddedValueToEmptyCollection()
{
$obj = $this->createPersistedObject();

Expand All @@ -47,6 +44,52 @@ public function testShouldTrackAddedValue()
], $collector->changes($obj));
}

public function testShouldSkipMongoIdField()
{
$obj = $this->createPersistedObject();
set_value($obj, '_id',123);

$collector = new ChangesCollector();
$collector->register($obj);

set_value($obj, '_id',321);

self::assertEquals([], $collector->changes($obj));
}

public function testShouldUseWholeValuesIfNotRegistered()
{
$collector = new ChangesCollector();

$obj = new Object();
set_value($obj, 'foo','fooVal');
set_value($obj, 'bar.baz','barVal');

self::assertEquals([
'$set' => [
'foo' => 'fooVal',
'bar' => ['baz' => 'barVal'],
],
], $collector->changes($obj));
}

public function testShouldTrackAddedValue()
{
$obj = $this->createPersistedObject();
add_value($obj, 'aKey', 'anOldVal');

$collector = new ChangesCollector();
$collector->register($obj);

add_value($obj, 'aKey', 'aVal');

self::assertEquals([
'$set' => [
'aKey.1' => 'aVal',
],
], $collector->changes($obj));
}

public function testShouldNotTrackSetValueAndUnsetLater()
{
$obj = $this->createPersistedObject();
Expand Down Expand Up @@ -155,7 +198,7 @@ public function testShouldFoo()
}

/**
* @return Object
* @return object
*/
private function createPersistedObject(array $values = [])
{
Expand Down

0 comments on commit bedae2f

Please sign in to comment.