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é <[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/MIT-License MIT License |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Totem; |
13
|
|
|
|
14
|
|
|
use ReflectionMethod; |
15
|
|
|
use ReflectionProperty; |
16
|
|
|
|
17
|
|
|
use PHPUnit_Framework_TestCase; |
18
|
|
|
|
19
|
|
|
class AbstractSnapshotTest extends PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @expectedException Totem\Exception\IncomparableDataException |
23
|
|
|
* @expectedExceptionMessage This data is not comparable with the base |
24
|
|
|
*/ |
25
|
|
|
public function testDiffIncomparable() |
26
|
|
|
{ |
27
|
|
|
$snapshot = new Snapshot(['comparable' => false]); |
28
|
|
|
$snapshot->diff($snapshot); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @expectedException InvalidArgumentException |
33
|
|
|
* @expectedExceptionMessage The computed data is not an array, "string" given |
34
|
|
|
*/ |
35
|
|
|
public function testComparableDataFailure() |
36
|
|
|
{ |
37
|
|
|
$snapshot = new Snapshot(['data' => 'foo']); |
38
|
|
|
$snapshot->getComparableData(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @dataProvider existsProvider |
43
|
|
|
*/ |
44
|
|
|
public function testOffsetExists($key, $expect) |
45
|
|
|
{ |
46
|
|
|
$snapshot = new Snapshot(['data' => ['foo' => 'bar']]); |
47
|
|
|
$this->assertSame($expect, isset($snapshot[$key])); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function existsProvider() |
51
|
|
|
{ |
52
|
|
|
return [['foo', true], |
53
|
|
|
['bar', false]]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @expectedException BadMethodCallException |
58
|
|
|
* @expectedExceptionMessage A snapshot is frozen by nature |
59
|
|
|
*/ |
60
|
|
|
public function testOffsetUnset() |
61
|
|
|
{ |
62
|
|
|
$snapshot = new Snapshot(['data' => ['foo' => 'bar']]); |
63
|
|
|
unset($snapshot['foo']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @expectedException BadMethodCallException |
68
|
|
|
* @expectedExceptionMessage A snapshot is frozen by nature |
69
|
|
|
*/ |
70
|
|
|
public function testOffsetSet() |
71
|
|
|
{ |
72
|
|
|
$snapshot = new Snapshot(['data' => ['foo' => 'bar']]); |
73
|
|
|
$snapshot[] = 'foo'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @expectedException InvalidArgumentException |
78
|
|
|
* @expectedExceptionMessage The computed data is not an array, "string" given |
79
|
|
|
*/ |
80
|
|
|
public function testInvalidDataNormalizer() |
81
|
|
|
{ |
82
|
|
|
$snapshot = new Snapshot(['data' => 'foo']); |
83
|
|
|
|
84
|
|
|
$refl = new ReflectionMethod('Totem\\AbstractSnapshot', 'normalize'); |
85
|
|
|
$refl->setAccessible(true); |
86
|
|
|
$refl->invoke($snapshot); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** @dataProvider normalizerProvider */ |
90
|
|
|
public function testNormalizer($data, $snapshotClass, $setClass = null) |
91
|
|
|
{ |
92
|
|
|
$snapshot = new Snapshot; |
93
|
|
|
$setClass = $setClass ?: 'stdClass'; |
94
|
|
|
|
95
|
|
|
$dataProperty = new ReflectionProperty('Totem\\AbstractSnapshot', 'data'); |
96
|
|
|
$dataProperty->setAccessible(true); |
97
|
|
|
$dataProperty->setValue($snapshot, [$data]); |
98
|
|
|
|
99
|
|
|
$setClassProperty = new ReflectionProperty('Totem\\AbstractSnapshot', 'setClass'); |
100
|
|
|
$setClassProperty->setAccessible(true); |
101
|
|
|
$setClassProperty->setValue($snapshot, $setClass); |
102
|
|
|
|
103
|
|
|
$method = new ReflectionMethod('Totem\\AbstractSnapshot', 'normalize'); |
104
|
|
|
$method->setAccessible(true); |
105
|
|
|
$method->invoke($snapshot); |
106
|
|
|
|
107
|
|
|
$this->assertInstanceOf($snapshotClass, $dataProperty->getValue($snapshot)[0]); |
108
|
|
|
$this->assertEquals($setClass, $setClassProperty->getValue($dataProperty->getValue($snapshot)[0])); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function normalizerProvider() |
112
|
|
|
{ |
113
|
|
|
return [[new Snapshot, 'Totem\\Snapshot', 'Totem\\Set'], |
114
|
|
|
[['foo' => 'bar'], 'Totem\\Snapshot\\ArraySnapshot'], |
115
|
|
|
[(object) ['foo' => 'bar'], 'Totem\\Snapshot\\ObjectSnapshot']]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testDiff() |
119
|
|
|
{ |
120
|
|
|
$snapshot = new Snapshot(['data' => []]); |
121
|
|
|
|
122
|
|
|
$this->assertInstanceOf('Totem\\Set', $snapshot->diff($snapshot)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testCorrectSetClass() |
126
|
|
|
{ |
127
|
|
|
$snapshot = new Snapshot(['data' => []]); |
128
|
|
|
$snapshot->setSetClass('Totem\\Set'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @expectedException InvalidArgumentException |
133
|
|
|
* @expectedExceptionMessage A Set Class should be instantiable and implement Totem\SetInterface |
134
|
|
|
*/ |
135
|
|
|
public function testWrongSetClass() |
136
|
|
|
{ |
137
|
|
|
$snapshot = new Snapshot(['data' => []]); |
138
|
|
|
$snapshot->setSetClass('stdclass'); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|