From effabf0de8d5171ab6ccad285e19cfb0184e8d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Wed, 23 Jul 2014 17:40:53 +0200 Subject: [PATCH] #25 : The changeset for a Collection is now numeric indexed based --- src/Set.php | 11 ++++++++++- test/SetTest.php | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/Set.php b/src/Set.php index 37d445a..ce79e50 100644 --- a/src/Set.php +++ b/src/Set.php @@ -26,7 +26,8 @@ Totem\Change\Modification, Totem\SetInterface, - Totem\AbstractSnapshot; + Totem\AbstractSnapshot, + Totem\Snapshot\CollectionSnapshot; /** * Represents a changeset @@ -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); + } } /** diff --git a/test/SetTest.php b/test/SetTest.php index b1a62b7..a5132e2 100644 --- a/test/SetTest.php +++ b/test/SetTest.php @@ -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 { @@ -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']))]]; + } }