From f5e4ef45b91275874bad68b28723e5ff07c2525b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20=22Talus=22=20Clavi=C3=A9?= Date: Wed, 6 Nov 2013 16:13:53 +0100 Subject: [PATCH] Add ArraySnapshot --- composer.json | 5 +++ src/Totem/AbstractSnapshot.php | 2 +- src/Totem/Set.php | 11 ++--- src/Totem/Snapshot/ArraySnapshot.php | 43 +++++++++++++++++++ .../{Object.php => ObjectSnapshot.php} | 4 +- test/Totem/Snapshot/ArraySnapshotTest.php | 39 +++++++++++++++++ ...{ObjectTest.php => ObjectSnapshotTest.php} | 10 ++--- 7 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 src/Totem/Snapshot/ArraySnapshot.php rename src/Totem/Snapshot/{Object.php => ObjectSnapshot.php} (95%) create mode 100644 test/Totem/Snapshot/ArraySnapshotTest.php rename test/Totem/Snapshot/{ObjectTest.php => ObjectSnapshotTest.php} (73%) diff --git a/composer.json b/composer.json index 9409505..3a38ca5 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,11 @@ "email": "clavie.b@gmail.com", "homepage": "http://baptiste.clavié.net", "role": "Lead Developper" + }, + + { + "name": "Rémy Gazelot", + "email": "rgazelot@gmail.com" } ], diff --git a/src/Totem/AbstractSnapshot.php b/src/Totem/AbstractSnapshot.php index dd69116..4e12023 100644 --- a/src/Totem/AbstractSnapshot.php +++ b/src/Totem/AbstractSnapshot.php @@ -53,7 +53,7 @@ final private function __clone() {} * @return Set Changeset between the two snapshots * @throws IncomparableDataException If the two snapshots are not comparable */ - final public function diff(self $snapshot) + public function diff(self $snapshot) { if (!$this->isComparable($snapshot)) { throw new IncomparableDataException('this object is not comparable with the base'); diff --git a/src/Totem/Set.php b/src/Totem/Set.php index 6eeae30..95c8c40 100644 --- a/src/Totem/Set.php +++ b/src/Totem/Set.php @@ -19,7 +19,7 @@ \OutOfBoundsException, \InvalidArgumentException; -use Totem\Snapshot\Object, +use Totem\Snapshot\ObjectSnapshot, Totem\Exception\IncomparableDataException; /** @@ -122,9 +122,10 @@ public function getNew() * @param array $old Old array * @param array $new New array * + * @internal * @throws InvalidArgumentException If the two arrays does not have the same keys */ - private function compute(array $old, array $new) + protected function compute(array $old, array $new) { if (array_keys($old) !== array_keys($new)) { throw new \InvalidArgumentException('You should compare two arrays with the same keys !'); @@ -142,8 +143,8 @@ private function compute(array $old, array $new) // -- if it is an object, try to check the hashes and then the diff if (is_object($old[$key])) { try { - $oldSnapshot = new Object($old[$key]); - $newSnapshot = new Object($new[$key]); + $oldSnapshot = new ObjectSnapshot($old[$key]); + $newSnapshot = new ObjectSnapshot($new[$key]); $set = $oldSnapshot->diff($newSnapshot); @@ -168,7 +169,7 @@ private function compute(array $old, array $new) } // -- same size / same keys ; return only what has changed - $set = new self($old[$key], $new[$key]); + $set = new static($old[$key], $new[$key]); if (0 !== count($set)) { $this->changes[$key] = $set; diff --git a/src/Totem/Snapshot/ArraySnapshot.php b/src/Totem/Snapshot/ArraySnapshot.php new file mode 100644 index 0000000..162a9ac --- /dev/null +++ b/src/Totem/Snapshot/ArraySnapshot.php @@ -0,0 +1,43 @@ + + * @license http://www.opensource.org/licenses/MIT-License MIT License + */ + +namespace Totem\Snapshot; + +use \ReflectionObject, + \ReflectionProperty, + + \InvalidArgumentException; + +use Totem\AbstractSnapshot; + +/** + * Represents a snapshot of an array at a given time + * + * @author Baptiste Clavié + */ +class ArraySnapshot extends AbstractSnapshot +{ + public function __construct(array $data) + { + $this->data = $data; + } + + /** {@inheritDoc} */ + public function isComparable(AbstractSnapshot $snapshot) + { + if (!$snapshot instanceof static) { + return false; + } + + return array_keys($snapshot->data) === array_keys($this->data); + } +} + diff --git a/src/Totem/Snapshot/Object.php b/src/Totem/Snapshot/ObjectSnapshot.php similarity index 95% rename from src/Totem/Snapshot/Object.php rename to src/Totem/Snapshot/ObjectSnapshot.php index 359c3c1..1d14518 100644 --- a/src/Totem/Snapshot/Object.php +++ b/src/Totem/Snapshot/ObjectSnapshot.php @@ -23,10 +23,10 @@ * * @author Baptiste Clavié */ -class Object extends AbstractSnapshot +class ObjectSnapshot extends AbstractSnapshot { /** @var string object's hash */ - private $oid; + protected $oid; /** * Build this snapshot diff --git a/test/Totem/Snapshot/ArraySnapshotTest.php b/test/Totem/Snapshot/ArraySnapshotTest.php new file mode 100644 index 0000000..bba03b2 --- /dev/null +++ b/test/Totem/Snapshot/ArraySnapshotTest.php @@ -0,0 +1,39 @@ + + * @license http://www.opensource.org/licenses/MIT-License MIT License + */ + +namespace test\Totem\Snapshot; + +use \stdClass; + +use \PHPUnit_Framework_TestCase; + +use Totem\Snapshot\ArraySnapshot; + +class ArraySnapshotTest extends PHPUnit_Framework_TestCase +{ + /** + * @expectedException Totem\Exception\IncomparableDataException + */ + public function testDiffWrongArray() + { + $snapshot = new ArraySnapshot(['foo', 'bar']); + $snapshot->diff(new ArraySnapshot(['foo' => 'bar'])); + } + + public function testDiff() + { + $snapshot = new ArraySnapshot(['foo' => 'bar']); + $set = $snapshot->diff($snapshot); + + $this->assertInstanceOf('Totem\\Set', $set); + } +} + diff --git a/test/Totem/Snapshot/ObjectTest.php b/test/Totem/Snapshot/ObjectSnapshotTest.php similarity index 73% rename from test/Totem/Snapshot/ObjectTest.php rename to test/Totem/Snapshot/ObjectSnapshotTest.php index 95c3a21..a70905d 100644 --- a/test/Totem/Snapshot/ObjectTest.php +++ b/test/Totem/Snapshot/ObjectSnapshotTest.php @@ -15,24 +15,24 @@ use \PHPUnit_Framework_TestCase; -use Totem\Snapshot\Object; +use Totem\Snapshot\ObjectSnapshot; -class ObjectTest extends PHPUnit_Framework_TestCase +class ObjectSnapshotTest extends PHPUnit_Framework_TestCase { /** * @expectedException Totem\Exception\IncomparableDataException */ public function testDiffWrongOid() { - $snapshot = new Object(new stdClass); - $snapshot->diff(new Object(new stdClass)); + $snapshot = new ObjectSnapshot(new stdClass); + $snapshot->diff(new ObjectSnapshot(new stdClass)); } public function testDiff() { $object = new stdClass; - $snapshot = new Object($object); + $snapshot = new ObjectSnapshot($object); $set = $snapshot->diff($snapshot); $this->assertInstanceOf('Totem\\Set', $set);