Skip to content

Commit

Permalink
#25 : The changeset for a Collection is now numeric indexed based
Browse files Browse the repository at this point in the history
  • Loading branch information
Taluu committed Jul 23, 2014
1 parent c35f656 commit effabf0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
Totem\Change\Modification,

Totem\SetInterface,
Totem\AbstractSnapshot;
Totem\AbstractSnapshot,
Totem\Snapshot\CollectionSnapshot;

/**
* Represents a changeset
Expand Down Expand Up @@ -149,6 +150,14 @@ public function compute(AbstractSnapshot $old, AbstractSnapshot $new)
$this->changes[$key] = $result;
}
}

/*
* Collection tricky case : each changes should not be represented by
* its primary key, but by a numeric key. Because it is a collection, duh.
*/
if ($old instanceof CollectionSnapshot && $new instanceof CollectionSnapshot) {
$this->changes = array_values($this->changes);
}
}

/**
Expand Down
35 changes: 34 additions & 1 deletion test/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
use \PHPUnit_Framework_TestCase;

use Totem\Snapshot\ArraySnapshot,
Totem\Snapshot\ObjectSnapshot;
Totem\Snapshot\ObjectSnapshot,
Totem\Snapshot\CollectionSnapshot;

class SetTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -180,5 +181,37 @@ public function testAlreadyComputedSetShouldNotRecompute()

$set->compute($old, $new);
}

public function testComputeCollections()
{
$old = $new = [['foo' => 'bar', 'baz' => 'fubar'], ['foo' => 'baz', 'baz' => 'fubar']];
$new[0]['baz'] = 'fubaz';

$old = new CollectionSnapshot($old, 'foo');
$new = new CollectionSnapshot($new, 'foo');

$set = new Set;
$set->compute($old, $new);

$this->assertContainsOnly('integer',array_keys(iterator_to_array($set)));
}

/** @dataProvider unaffectedSnapshotComputerProvider */
public function testUnaffectedCollections(AbstractSnapshot $origin, AbstractSnapshot $upstream)
{
$set = new Set;
$set->compute($origin, $upstream);

$this->assertNotContainsOnly('integer',array_keys(iterator_to_array($set)));
}

public function unaffectedSnapshotComputerProvider()
{
$old = ['foo' => 'bar', 'baz' => 'fubar'];

return [[new CollectionSnapshot([$old], 'foo'), new ArraySnapshot($old)],
[new ArraySnapshot($old), new CollectionSnapshot([$old], 'foo')],
[new ArraySnapshot($old), new ArraySnapshot(array_merge($old, ['baz' => 'fubaz']))]];
}
}

0 comments on commit effabf0

Please sign in to comment.