Skip to content

Commit

Permalink
Add ArraySnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Taluu committed Nov 6, 2013
1 parent 2ebf5c9 commit f5e4ef4
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 13 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"email": "[email protected]",
"homepage": "http://baptiste.clavié.net",
"role": "Lead Developper"
},

{
"name": "Rémy Gazelot",
"email": "[email protected]"
}
],

Expand Down
2 changes: 1 addition & 1 deletion src/Totem/AbstractSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
11 changes: 6 additions & 5 deletions src/Totem/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
\OutOfBoundsException,
\InvalidArgumentException;

use Totem\Snapshot\Object,
use Totem\Snapshot\ObjectSnapshot,
Totem\Exception\IncomparableDataException;

/**
Expand Down Expand Up @@ -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 !');
Expand All @@ -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);

Expand All @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions src/Totem/Snapshot/ArraySnapshot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* This file is part of the Totem package
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*
* @copyright Baptiste Clavié <[email protected]>
* @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é <[email protected]>
*/
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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*
* @author Baptiste Clavié <[email protected]>
*/
class Object extends AbstractSnapshot
class ObjectSnapshot extends AbstractSnapshot
{
/** @var string object's hash */
private $oid;
protected $oid;

/**
* Build this snapshot
Expand Down
39 changes: 39 additions & 0 deletions test/Totem/Snapshot/ArraySnapshotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* This file is part of the Totem package
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*
* @copyright Baptiste Clavié <[email protected]>
* @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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f5e4ef4

Please sign in to comment.