Skip to content

Commit

Permalink
Merge pull request #27 from Taluu/set-collection-keys
Browse files Browse the repository at this point in the history
Set changesets of collections to be more collection like
  • Loading branch information
Taluu committed Jul 23, 2014
2 parents 2d63a4e + effabf0 commit 4ae716d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 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
28 changes: 27 additions & 1 deletion src/Snapshot/CollectionSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
*/
class CollectionSnapshot extends AbstractSnapshot
{
/**
* Collection of the new keys (extracted from the primary key) => old keys
*
* @var integer[]
*/
private $link = [];

/**
* Construct the snapshot
*
Expand Down Expand Up @@ -97,7 +104,10 @@ public function __construct($data, $pkey, array $options = [])
throw new InvalidArgumentException(sprintf('The key "%s" is not defined or readable in one of the elements of the collection', $pkey));
}

$this->data[$accessor->getValue($value, $primary)] = $this->snapshot($value, $snapshot);
$primary = $accessor->getValue($value, $primary);

$this->link[$primary] = $key;
$this->data[$primary] = $this->snapshot($value, $snapshot);
}

parent::normalize();
Expand All @@ -122,5 +132,21 @@ private function snapshot($value, $class = null)

return new $class($value);
}

/**
* Returns the original key for the primary key $primary
*
* @param mixed $primary Primary key to search
*
* @return integer original key
* @throws InvalidArgumentException primary key not found
*/
public function getOriginalKey($primary) {
if (!isset($this->link[$primary])) {
throw new InvalidArgumentException(sprintf('The primary key "%s" is not in the computed dataset', $primary));
}

return $this->link[$primary];
}
}

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']))]];
}
}

17 changes: 17 additions & 0 deletions test/Snapshot/CollectionSnapshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,22 @@ public function allValidProvider()
return [[true],
[false]];
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The primary key "baz" is not in the computed dataset
*/
public function testOriginalKeyNotFound()
{
$snapshot = new CollectionSnapshot([['foo' => 'bar']], 'foo');
$snapshot->getOriginalKey('baz');
}

public function testOriginalKey()
{
$snapshot = new CollectionSnapshot([['foo' => 'bar']], 'foo');

$this->assertSame(0, $snapshot->getOriginalKey('bar'));
}
}

0 comments on commit 4ae716d

Please sign in to comment.