Skip to content

Commit 4feda13

Browse files
committed
Enhance test coverage
And simplify object comparison, as it is useless in its current state. todo : check how to do it when taking a snapshot ?
1 parent 6da0e45 commit 4feda13

8 files changed

Lines changed: 155 additions & 35 deletions

File tree

phpunit.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@
1818
<directory>./test</directory>
1919
</testsuite>
2020
</testsuites>
21+
22+
<filter>
23+
<whitelist>
24+
<directory suffix=".php">./src</directory>
25+
</whitelist>
26+
</filter>
2127
</phpunit>
28+

src/Totem/AbstractSnapshot.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ abstract class AbstractSnapshot
2727
/** @var array data stored in an array */
2828
protected $data = [];
2929

30-
/** @var float Time when this snapshot was taken */
31-
private $time;
32-
33-
public function __construct() {
34-
$this->time = microtime(true); // @todo use Datetime instead ?
35-
}
36-
3730
/**
3831
* Check if the two snapshots are comparable
3932
*
@@ -42,7 +35,11 @@ public function __construct() {
4235
*/
4336
abstract public function isComparable(self $snapshot);
4437

45-
/** Clone this object */
38+
/**
39+
* Clone this object
40+
*
41+
* @codeCoverageIgnore
42+
*/
4643
final private function __clone() {}
4744

4845
/**
@@ -59,12 +56,7 @@ public function diff(self $snapshot)
5956
throw new IncomparableDataException('this object is not comparable with the base');
6057
}
6158

62-
$data = [$this->data, $snapshot->data];
63-
64-
if ($snapshot->time < $this->time) {
65-
$data = [$snapshot->data, $this->data];
66-
}
67-
68-
return new Set($data[0], $data[1]);
59+
return new Set($this->data, $snapshot->data);
6960
}
7061
}
62+

src/Totem/Set.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
\IteratorAggregate,
1818

1919
\OutOfBoundsException,
20+
\BadMethodCallException,
2021
\InvalidArgumentException;
2122

2223
use Totem\Snapshot\ObjectSnapshot,
@@ -142,19 +143,11 @@ protected function compute(array $old, array $new)
142143

143144
// -- if it is an object, try to check the hashes and then the diff
144145
if (is_object($old[$key])) {
145-
try {
146-
$oldSnapshot = new ObjectSnapshot($old[$key]);
147-
$newSnapshot = new ObjectSnapshot($new[$key]);
148-
149-
$set = $oldSnapshot->diff($newSnapshot);
150-
151-
if (0 === count($set)) {
152-
continue;
153-
}
146+
$oldSnapshot = new ObjectSnapshot($old[$key]);
154147

148+
// @todo Check how to check the differences into this object if it is the same object
149+
if (!$oldSnapshot->isComparable(new ObjectSnapshot($new[$key]))) {
155150
$this->changes[$key] = $set;
156-
} catch (IncomparableDataException $e) {
157-
$this->changes[$key] = new Change($old[$key], $new[$key]);
158151
}
159152

160153
continue;

src/Totem/Snapshot/ObjectSnapshot.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ public function __construct($object)
4545

4646
foreach ($refl->getProperties() as $reflProperty) {
4747
$reflProperty->setAccessible(true);
48-
$this->data[$reflProperty->getName()] = $reflProperty->getValue();
48+
$this->data[$reflProperty->getName()] = $reflProperty->getValue($object);
4949
}
50-
51-
parent::__construct();
5250
}
5351

5452
/** {@inheritDoc} */
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* This file is part of the Totem package
4+
*
5+
* For the full copyright and license information, please view the LICENSE file
6+
* that was distributed with this source code.
7+
*
8+
* @copyright Baptiste Clavié <clavie.b@gmail.com>
9+
* @license http://www.opensource.org/licenses/MIT-License MIT License
10+
*/
11+
12+
namespace test\Totem\Snapshot;
13+
14+
use \stdClass,
15+
\ReflectionProperty;
16+
17+
use \PHPUnit_Framework_TestCase;
18+
19+
use Totem\Snapshot\AbstractSnapshot;
20+
21+
class AbstractSnapshotTest extends PHPUnit_Framework_TestCase
22+
{
23+
/**
24+
* @expectedException Totem\Exception\IncomparableDataException
25+
*/
26+
public function testDiffIncomparable()
27+
{
28+
$snapshot = $this->getMockBuilder('Totem\\AbstractSnapshot')
29+
->setMethods(['isComparable'])
30+
->getMock();
31+
32+
$snapshot->expects(self::once())
33+
->method('isComparable')
34+
->with(self::isInstanceOf('Totem\\AbstractSnapshot'))
35+
->will(self::returnValue(false));
36+
37+
$snapshot->diff($snapshot);
38+
}
39+
}
40+

test/Totem/SetTest.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111

1212
namespace test\Totem;
1313

14+
use \stdClass;
15+
1416
use \PHPUnit_Framework_TestCase;
1517

16-
use Totem\Set;
18+
use Totem\Set,
19+
Totem\Snapshot\ObjectSnapshot;
1720

1821
class ChangeSetTest extends \PHPUnit_Framework_TestCase
1922
{
@@ -44,7 +47,9 @@ public function testHasChanged()
4447
$set = new Set($old, $new);
4548

4649
$this->assertTrue($set->hasChanged('foo'));
50+
$this->assertTrue(isset($set['foo']));
4751
$this->assertFalse($set->hasChanged('baz'));
52+
$this->assertFalse(isset($set['baz']));
4853
}
4954

5055
/**
@@ -59,18 +64,59 @@ public function testGetChangeWithInvalidProperty()
5964
$set->getChange('foo');
6065
}
6166

67+
// @todo to break up
6268
public function testGetChange()
6369
{
64-
$old = $new = ['foo' => 'foo',
65-
'bar' => ['foo', 'bar']];
66-
67-
$new['foo'] = 'bar';
68-
$new['bar'] = ['foo', 'baz'];
70+
$old = $new = ['foo' => 'foo',
71+
'bar' => ['foo', 'bar'],
72+
'baz' => new stdClass,
73+
'qux' => 'foo',
74+
'fubar' => (object) ['foo' => 'bar'],
75+
'fubaz' => ['foo', 'bar']];
76+
77+
$new['foo'] = 'bar';
78+
$new['bar'] = ['foo', 'baz'];
79+
$new['baz'] = clone $old['fubar'];
80+
$new['qux'] = 42;
81+
$new['fubaz'][] = 'baz';
6982

7083
$set = new Set($old, $new);
7184

7285
$this->assertInstanceOf('Totem\\Change', $set->getChange('foo'));
7386
$this->assertInstanceOf('Totem\\Set', $set->getChange('bar'));
87+
$this->assertInstanceOf('Totem\\Change', $set['foo']);
88+
}
89+
90+
public function testGetters()
91+
{
92+
$old = ['foo', 'bar'];
93+
$set = new Set($old, $old);
94+
95+
$this->assertSame($old, $set->getOld());
96+
$this->assertSame($old, $set->getNew());
97+
}
98+
99+
/**
100+
* @expectedException BadMethodCallException
101+
*/
102+
public function testForbidenSetter()
103+
{
104+
$old = ['foo'];
105+
$set = new Set($old, $old);
106+
107+
$set[] = 'baz';
74108
}
109+
110+
/**
111+
* @expectedException BadMethodCallException
112+
*/
113+
public function testForbidenUnsetter()
114+
{
115+
$old = ['foo'];
116+
$set = new Set($old, $old);
117+
118+
unset($set[0]);
119+
}
120+
75121
}
76122

test/Totem/Snapshot/ArraySnapshotTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,22 @@ public function testDiff()
3535

3636
$this->assertInstanceOf('Totem\\Set', $set);
3737
}
38+
39+
/**
40+
* @dataProvider providerCompare
41+
*/
42+
public function testCompare($compare, $expect)
43+
{
44+
$snapshot = new ArraySnapshot([]);
45+
46+
$this->assertSame($expect, $snapshot->isComparable($compare));
47+
}
48+
49+
public function providerCompare()
50+
{
51+
return [[new ArraySnapshot([]), true],
52+
[new ArraySnapshot(['foo']), false],
53+
[$this->getMock('Totem\\AbstractSnapshot'), false]];
54+
}
3855
}
3956

test/Totem/Snapshot/ObjectSnapshotTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,32 @@ public function testDiff()
3737

3838
$this->assertInstanceOf('Totem\\Set', $set);
3939
}
40+
41+
/**
42+
* @dataProvider providerCompare
43+
*/
44+
public function testCompare($object, $compare, $expect)
45+
{
46+
$snapshot = new ObjectSnapshot($object);
47+
48+
$this->assertSame($expect, $snapshot->isComparable($compare));
49+
}
50+
51+
public function providerCompare()
52+
{
53+
$object = new stdClass;
54+
55+
return [[$object, new ObjectSnapshot($object), true],
56+
[$object, new ObjectSnapshot(clone $object), false],
57+
[$object, $this->getMock('Totem\\AbstractSnapshot'), false]];
58+
}
59+
60+
/**
61+
* @expectedException InvalidArgumentException
62+
*/
63+
public function testConstructWithoutObject()
64+
{
65+
new ObjectSnapshot([]);
66+
}
4067
}
4168

0 commit comments

Comments
 (0)